des_s390.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the DES Cipher Algorithm.
  5. *
  6. * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Thomas Spatzier (tspat@de.ibm.com)
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/crypto.h>
  19. #include "crypt_s390.h"
  20. #include "crypto_des.h"
  21. #define DES_BLOCK_SIZE 8
  22. #define DES_KEY_SIZE 8
  23. #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
  24. #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
  25. #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
  26. #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
  27. struct crypt_s390_des_ctx {
  28. u8 iv[DES_BLOCK_SIZE];
  29. u8 key[DES_KEY_SIZE];
  30. };
  31. struct crypt_s390_des3_128_ctx {
  32. u8 iv[DES_BLOCK_SIZE];
  33. u8 key[DES3_128_KEY_SIZE];
  34. };
  35. struct crypt_s390_des3_192_ctx {
  36. u8 iv[DES_BLOCK_SIZE];
  37. u8 key[DES3_192_KEY_SIZE];
  38. };
  39. static int des_setkey(void *ctx, const u8 *key, unsigned int keylen,
  40. u32 *flags)
  41. {
  42. struct crypt_s390_des_ctx *dctx = ctx;
  43. int ret;
  44. /* test if key is valid (not a weak key) */
  45. ret = crypto_des_check_key(key, keylen, flags);
  46. if (ret == 0)
  47. memcpy(dctx->key, key, keylen);
  48. return ret;
  49. }
  50. static void des_encrypt(void *ctx, u8 *dst, const u8 *src)
  51. {
  52. struct crypt_s390_des_ctx *dctx = ctx;
  53. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
  54. }
  55. static void des_decrypt(void *ctx, u8 *dst, const u8 *src)
  56. {
  57. struct crypt_s390_des_ctx *dctx = ctx;
  58. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
  59. }
  60. static struct crypto_alg des_alg = {
  61. .cra_name = "des",
  62. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  63. .cra_blocksize = DES_BLOCK_SIZE,
  64. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  65. .cra_module = THIS_MODULE,
  66. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  67. .cra_u = {
  68. .cipher = {
  69. .cia_min_keysize = DES_KEY_SIZE,
  70. .cia_max_keysize = DES_KEY_SIZE,
  71. .cia_setkey = des_setkey,
  72. .cia_encrypt = des_encrypt,
  73. .cia_decrypt = des_decrypt
  74. }
  75. }
  76. };
  77. /*
  78. * RFC2451:
  79. *
  80. * For DES-EDE3, there is no known need to reject weak or
  81. * complementation keys. Any weakness is obviated by the use of
  82. * multiple keys.
  83. *
  84. * However, if the two independent 64-bit keys are equal,
  85. * then the DES3 operation is simply the same as DES.
  86. * Implementers MUST reject keys that exhibit this property.
  87. *
  88. */
  89. static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen,
  90. u32 *flags)
  91. {
  92. int i, ret;
  93. struct crypt_s390_des3_128_ctx *dctx = ctx;
  94. const u8* temp_key = key;
  95. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
  96. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  97. return -EINVAL;
  98. }
  99. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  100. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  101. if (ret < 0)
  102. return ret;
  103. }
  104. memcpy(dctx->key, key, keylen);
  105. return 0;
  106. }
  107. static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
  108. {
  109. struct crypt_s390_des3_128_ctx *dctx = ctx;
  110. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  111. DES3_128_BLOCK_SIZE);
  112. }
  113. static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
  114. {
  115. struct crypt_s390_des3_128_ctx *dctx = ctx;
  116. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  117. DES3_128_BLOCK_SIZE);
  118. }
  119. static struct crypto_alg des3_128_alg = {
  120. .cra_name = "des3_ede128",
  121. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  122. .cra_blocksize = DES3_128_BLOCK_SIZE,
  123. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  124. .cra_module = THIS_MODULE,
  125. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  126. .cra_u = {
  127. .cipher = {
  128. .cia_min_keysize = DES3_128_KEY_SIZE,
  129. .cia_max_keysize = DES3_128_KEY_SIZE,
  130. .cia_setkey = des3_128_setkey,
  131. .cia_encrypt = des3_128_encrypt,
  132. .cia_decrypt = des3_128_decrypt
  133. }
  134. }
  135. };
  136. /*
  137. * RFC2451:
  138. *
  139. * For DES-EDE3, there is no known need to reject weak or
  140. * complementation keys. Any weakness is obviated by the use of
  141. * multiple keys.
  142. *
  143. * However, if the first two or last two independent 64-bit keys are
  144. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  145. * same as DES. Implementers MUST reject keys that exhibit this
  146. * property.
  147. *
  148. */
  149. static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen,
  150. u32 *flags)
  151. {
  152. int i, ret;
  153. struct crypt_s390_des3_192_ctx *dctx = ctx;
  154. const u8* temp_key = key;
  155. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  156. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  157. DES_KEY_SIZE))) {
  158. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  159. return -EINVAL;
  160. }
  161. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  162. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  163. if (ret < 0)
  164. return ret;
  165. }
  166. memcpy(dctx->key, key, keylen);
  167. return 0;
  168. }
  169. static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
  170. {
  171. struct crypt_s390_des3_192_ctx *dctx = ctx;
  172. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  173. DES3_192_BLOCK_SIZE);
  174. }
  175. static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
  176. {
  177. struct crypt_s390_des3_192_ctx *dctx = ctx;
  178. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  179. DES3_192_BLOCK_SIZE);
  180. }
  181. static struct crypto_alg des3_192_alg = {
  182. .cra_name = "des3_ede",
  183. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  184. .cra_blocksize = DES3_192_BLOCK_SIZE,
  185. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  186. .cra_module = THIS_MODULE,
  187. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  188. .cra_u = {
  189. .cipher = {
  190. .cia_min_keysize = DES3_192_KEY_SIZE,
  191. .cia_max_keysize = DES3_192_KEY_SIZE,
  192. .cia_setkey = des3_192_setkey,
  193. .cia_encrypt = des3_192_encrypt,
  194. .cia_decrypt = des3_192_decrypt
  195. }
  196. }
  197. };
  198. static int init(void)
  199. {
  200. int ret = 0;
  201. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  202. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  203. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  204. return -ENOSYS;
  205. ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1;
  206. ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2;
  207. ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4;
  208. if (ret) {
  209. crypto_unregister_alg(&des3_192_alg);
  210. crypto_unregister_alg(&des3_128_alg);
  211. crypto_unregister_alg(&des_alg);
  212. return -EEXIST;
  213. }
  214. return 0;
  215. }
  216. static void __exit fini(void)
  217. {
  218. crypto_unregister_alg(&des3_192_alg);
  219. crypto_unregister_alg(&des3_128_alg);
  220. crypto_unregister_alg(&des_alg);
  221. }
  222. module_init(init);
  223. module_exit(fini);
  224. MODULE_ALIAS("des");
  225. MODULE_ALIAS("des3_ede");
  226. MODULE_LICENSE("GPL");
  227. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");