des_z990.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Cryptographic API.
  3. *
  4. * z990 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/mm.h>
  19. #include <linux/errno.h>
  20. #include <asm/scatterlist.h>
  21. #include <linux/crypto.h>
  22. #include "crypt_z990.h"
  23. #include "crypto_des.h"
  24. #define DES_BLOCK_SIZE 8
  25. #define DES_KEY_SIZE 8
  26. #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
  27. #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
  28. #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
  29. #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
  30. struct crypt_z990_des_ctx {
  31. u8 iv[DES_BLOCK_SIZE];
  32. u8 key[DES_KEY_SIZE];
  33. };
  34. struct crypt_z990_des3_128_ctx {
  35. u8 iv[DES_BLOCK_SIZE];
  36. u8 key[DES3_128_KEY_SIZE];
  37. };
  38. struct crypt_z990_des3_192_ctx {
  39. u8 iv[DES_BLOCK_SIZE];
  40. u8 key[DES3_192_KEY_SIZE];
  41. };
  42. static int
  43. des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  44. {
  45. struct crypt_z990_des_ctx *dctx;
  46. int ret;
  47. dctx = ctx;
  48. //test if key is valid (not a weak key)
  49. ret = crypto_des_check_key(key, keylen, flags);
  50. if (ret == 0){
  51. memcpy(dctx->key, key, keylen);
  52. }
  53. return ret;
  54. }
  55. static void
  56. des_encrypt(void *ctx, u8 *dst, const u8 *src)
  57. {
  58. struct crypt_z990_des_ctx *dctx;
  59. dctx = ctx;
  60. crypt_z990_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
  61. }
  62. static void
  63. des_decrypt(void *ctx, u8 *dst, const u8 *src)
  64. {
  65. struct crypt_z990_des_ctx *dctx;
  66. dctx = ctx;
  67. crypt_z990_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
  68. }
  69. static struct crypto_alg des_alg = {
  70. .cra_name = "des",
  71. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  72. .cra_blocksize = DES_BLOCK_SIZE,
  73. .cra_ctxsize = sizeof(struct crypt_z990_des_ctx),
  74. .cra_module = THIS_MODULE,
  75. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  76. .cra_u = { .cipher = {
  77. .cia_min_keysize = DES_KEY_SIZE,
  78. .cia_max_keysize = DES_KEY_SIZE,
  79. .cia_setkey = des_setkey,
  80. .cia_encrypt = des_encrypt,
  81. .cia_decrypt = des_decrypt } }
  82. };
  83. /*
  84. * RFC2451:
  85. *
  86. * For DES-EDE3, there is no known need to reject weak or
  87. * complementation keys. Any weakness is obviated by the use of
  88. * multiple keys.
  89. *
  90. * However, if the two independent 64-bit keys are equal,
  91. * then the DES3 operation is simply the same as DES.
  92. * Implementers MUST reject keys that exhibit this property.
  93. *
  94. */
  95. static int
  96. des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  97. {
  98. int i, ret;
  99. struct crypt_z990_des3_128_ctx *dctx;
  100. const u8* temp_key = key;
  101. dctx = ctx;
  102. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
  103. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  104. return -EINVAL;
  105. }
  106. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  107. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  108. if (ret < 0)
  109. return ret;
  110. }
  111. memcpy(dctx->key, key, keylen);
  112. return 0;
  113. }
  114. static void
  115. des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
  116. {
  117. struct crypt_z990_des3_128_ctx *dctx;
  118. dctx = ctx;
  119. crypt_z990_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  120. DES3_128_BLOCK_SIZE);
  121. }
  122. static void
  123. des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
  124. {
  125. struct crypt_z990_des3_128_ctx *dctx;
  126. dctx = ctx;
  127. crypt_z990_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  128. DES3_128_BLOCK_SIZE);
  129. }
  130. static struct crypto_alg des3_128_alg = {
  131. .cra_name = "des3_ede128",
  132. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  133. .cra_blocksize = DES3_128_BLOCK_SIZE,
  134. .cra_ctxsize = sizeof(struct crypt_z990_des3_128_ctx),
  135. .cra_module = THIS_MODULE,
  136. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  137. .cra_u = { .cipher = {
  138. .cia_min_keysize = DES3_128_KEY_SIZE,
  139. .cia_max_keysize = DES3_128_KEY_SIZE,
  140. .cia_setkey = des3_128_setkey,
  141. .cia_encrypt = des3_128_encrypt,
  142. .cia_decrypt = des3_128_decrypt } }
  143. };
  144. /*
  145. * RFC2451:
  146. *
  147. * For DES-EDE3, there is no known need to reject weak or
  148. * complementation keys. Any weakness is obviated by the use of
  149. * multiple keys.
  150. *
  151. * However, if the first two or last two independent 64-bit keys are
  152. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  153. * same as DES. Implementers MUST reject keys that exhibit this
  154. * property.
  155. *
  156. */
  157. static int
  158. des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  159. {
  160. int i, ret;
  161. struct crypt_z990_des3_192_ctx *dctx;
  162. const u8* temp_key;
  163. dctx = ctx;
  164. temp_key = key;
  165. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  166. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  167. DES_KEY_SIZE))) {
  168. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  169. return -EINVAL;
  170. }
  171. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  172. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  173. if (ret < 0){
  174. return ret;
  175. }
  176. }
  177. memcpy(dctx->key, key, keylen);
  178. return 0;
  179. }
  180. static void
  181. des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
  182. {
  183. struct crypt_z990_des3_192_ctx *dctx;
  184. dctx = ctx;
  185. crypt_z990_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  186. DES3_192_BLOCK_SIZE);
  187. }
  188. static void
  189. des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
  190. {
  191. struct crypt_z990_des3_192_ctx *dctx;
  192. dctx = ctx;
  193. crypt_z990_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  194. DES3_192_BLOCK_SIZE);
  195. }
  196. static struct crypto_alg des3_192_alg = {
  197. .cra_name = "des3_ede",
  198. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  199. .cra_blocksize = DES3_192_BLOCK_SIZE,
  200. .cra_ctxsize = sizeof(struct crypt_z990_des3_192_ctx),
  201. .cra_module = THIS_MODULE,
  202. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  203. .cra_u = { .cipher = {
  204. .cia_min_keysize = DES3_192_KEY_SIZE,
  205. .cia_max_keysize = DES3_192_KEY_SIZE,
  206. .cia_setkey = des3_192_setkey,
  207. .cia_encrypt = des3_192_encrypt,
  208. .cia_decrypt = des3_192_decrypt } }
  209. };
  210. static int
  211. init(void)
  212. {
  213. int ret;
  214. if (!crypt_z990_func_available(KM_DEA_ENCRYPT) ||
  215. !crypt_z990_func_available(KM_TDEA_128_ENCRYPT) ||
  216. !crypt_z990_func_available(KM_TDEA_192_ENCRYPT)){
  217. return -ENOSYS;
  218. }
  219. ret = 0;
  220. ret |= (crypto_register_alg(&des_alg) == 0)? 0:1;
  221. ret |= (crypto_register_alg(&des3_128_alg) == 0)? 0:2;
  222. ret |= (crypto_register_alg(&des3_192_alg) == 0)? 0:4;
  223. if (ret){
  224. crypto_unregister_alg(&des3_192_alg);
  225. crypto_unregister_alg(&des3_128_alg);
  226. crypto_unregister_alg(&des_alg);
  227. return -EEXIST;
  228. }
  229. printk(KERN_INFO "crypt_z990: des_z990 loaded.\n");
  230. return 0;
  231. }
  232. static void __exit
  233. fini(void)
  234. {
  235. crypto_unregister_alg(&des3_192_alg);
  236. crypto_unregister_alg(&des3_128_alg);
  237. crypto_unregister_alg(&des_alg);
  238. }
  239. module_init(init);
  240. module_exit(fini);
  241. MODULE_ALIAS("des");
  242. MODULE_ALIAS("des3_ede");
  243. MODULE_LICENSE("GPL");
  244. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");