des_s390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 *out, const u8 *in)
  51. {
  52. struct crypt_s390_des_ctx *dctx = ctx;
  53. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  54. }
  55. static void des_decrypt(void *ctx, u8 *out, const u8 *in)
  56. {
  57. struct crypt_s390_des_ctx *dctx = ctx;
  58. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  59. }
  60. static unsigned int des_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
  61. const u8 *in, unsigned int nbytes)
  62. {
  63. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  64. int ret;
  65. /* only use complete blocks */
  66. nbytes &= ~(DES_BLOCK_SIZE - 1);
  67. ret = crypt_s390_km(KM_DEA_ENCRYPT, sctx->key, out, in, nbytes);
  68. BUG_ON((ret < 0) || (ret != nbytes));
  69. return nbytes;
  70. }
  71. static unsigned int des_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
  72. const u8 *in, unsigned int nbytes)
  73. {
  74. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  75. int ret;
  76. /* only use complete blocks */
  77. nbytes &= ~(DES_BLOCK_SIZE - 1);
  78. ret = crypt_s390_km(KM_DEA_DECRYPT, sctx->key, out, in, nbytes);
  79. BUG_ON((ret < 0) || (ret != nbytes));
  80. return nbytes;
  81. }
  82. static unsigned int des_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
  83. const u8 *in, unsigned int nbytes)
  84. {
  85. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  86. int ret;
  87. /* only use complete blocks */
  88. nbytes &= ~(DES_BLOCK_SIZE - 1);
  89. memcpy(sctx->iv, desc->info, DES_BLOCK_SIZE);
  90. ret = crypt_s390_kmc(KMC_DEA_ENCRYPT, &sctx->iv, out, in, nbytes);
  91. BUG_ON((ret < 0) || (ret != nbytes));
  92. memcpy(desc->info, sctx->iv, DES_BLOCK_SIZE);
  93. return nbytes;
  94. }
  95. static unsigned int des_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
  96. const u8 *in, unsigned int nbytes)
  97. {
  98. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  99. int ret;
  100. /* only use complete blocks */
  101. nbytes &= ~(DES_BLOCK_SIZE - 1);
  102. memcpy(&sctx->iv, desc->info, DES_BLOCK_SIZE);
  103. ret = crypt_s390_kmc(KMC_DEA_DECRYPT, &sctx->iv, out, in, nbytes);
  104. BUG_ON((ret < 0) || (ret != nbytes));
  105. return nbytes;
  106. }
  107. static struct crypto_alg des_alg = {
  108. .cra_name = "des",
  109. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  110. .cra_blocksize = DES_BLOCK_SIZE,
  111. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  112. .cra_module = THIS_MODULE,
  113. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  114. .cra_u = {
  115. .cipher = {
  116. .cia_min_keysize = DES_KEY_SIZE,
  117. .cia_max_keysize = DES_KEY_SIZE,
  118. .cia_setkey = des_setkey,
  119. .cia_encrypt = des_encrypt,
  120. .cia_decrypt = des_decrypt,
  121. .cia_encrypt_ecb = des_encrypt_ecb,
  122. .cia_decrypt_ecb = des_decrypt_ecb,
  123. .cia_encrypt_cbc = des_encrypt_cbc,
  124. .cia_decrypt_cbc = des_decrypt_cbc,
  125. }
  126. }
  127. };
  128. /*
  129. * RFC2451:
  130. *
  131. * For DES-EDE3, there is no known need to reject weak or
  132. * complementation keys. Any weakness is obviated by the use of
  133. * multiple keys.
  134. *
  135. * However, if the two independent 64-bit keys are equal,
  136. * then the DES3 operation is simply the same as DES.
  137. * Implementers MUST reject keys that exhibit this property.
  138. *
  139. */
  140. static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen,
  141. u32 *flags)
  142. {
  143. int i, ret;
  144. struct crypt_s390_des3_128_ctx *dctx = ctx;
  145. const u8* temp_key = key;
  146. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
  147. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  148. return -EINVAL;
  149. }
  150. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  151. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  152. if (ret < 0)
  153. return ret;
  154. }
  155. memcpy(dctx->key, key, keylen);
  156. return 0;
  157. }
  158. static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
  159. {
  160. struct crypt_s390_des3_128_ctx *dctx = ctx;
  161. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  162. DES3_128_BLOCK_SIZE);
  163. }
  164. static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
  165. {
  166. struct crypt_s390_des3_128_ctx *dctx = ctx;
  167. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  168. DES3_128_BLOCK_SIZE);
  169. }
  170. static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc,
  171. u8 *out, const u8 *in,
  172. unsigned int nbytes)
  173. {
  174. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  175. int ret;
  176. /* only use complete blocks */
  177. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  178. ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes);
  179. BUG_ON((ret < 0) || (ret != nbytes));
  180. return nbytes;
  181. }
  182. static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc,
  183. u8 *out, const u8 *in,
  184. unsigned int nbytes)
  185. {
  186. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  187. int ret;
  188. /* only use complete blocks */
  189. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  190. ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes);
  191. BUG_ON((ret < 0) || (ret != nbytes));
  192. return nbytes;
  193. }
  194. static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc,
  195. u8 *out, const u8 *in,
  196. unsigned int nbytes)
  197. {
  198. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  199. int ret;
  200. /* only use complete blocks */
  201. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  202. memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  203. ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes);
  204. BUG_ON((ret < 0) || (ret != nbytes));
  205. memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE);
  206. return nbytes;
  207. }
  208. static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc,
  209. u8 *out, const u8 *in,
  210. unsigned int nbytes)
  211. {
  212. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  213. int ret;
  214. /* only use complete blocks */
  215. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  216. memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  217. ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes);
  218. BUG_ON((ret < 0) || (ret != nbytes));
  219. return nbytes;
  220. }
  221. static struct crypto_alg des3_128_alg = {
  222. .cra_name = "des3_ede128",
  223. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  224. .cra_blocksize = DES3_128_BLOCK_SIZE,
  225. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  226. .cra_module = THIS_MODULE,
  227. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  228. .cra_u = {
  229. .cipher = {
  230. .cia_min_keysize = DES3_128_KEY_SIZE,
  231. .cia_max_keysize = DES3_128_KEY_SIZE,
  232. .cia_setkey = des3_128_setkey,
  233. .cia_encrypt = des3_128_encrypt,
  234. .cia_decrypt = des3_128_decrypt,
  235. .cia_encrypt_ecb = des3_128_encrypt_ecb,
  236. .cia_decrypt_ecb = des3_128_decrypt_ecb,
  237. .cia_encrypt_cbc = des3_128_encrypt_cbc,
  238. .cia_decrypt_cbc = des3_128_decrypt_cbc,
  239. }
  240. }
  241. };
  242. /*
  243. * RFC2451:
  244. *
  245. * For DES-EDE3, there is no known need to reject weak or
  246. * complementation keys. Any weakness is obviated by the use of
  247. * multiple keys.
  248. *
  249. * However, if the first two or last two independent 64-bit keys are
  250. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  251. * same as DES. Implementers MUST reject keys that exhibit this
  252. * property.
  253. *
  254. */
  255. static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen,
  256. u32 *flags)
  257. {
  258. int i, ret;
  259. struct crypt_s390_des3_192_ctx *dctx = ctx;
  260. const u8* temp_key = key;
  261. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  262. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  263. DES_KEY_SIZE))) {
  264. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  265. return -EINVAL;
  266. }
  267. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  268. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. memcpy(dctx->key, key, keylen);
  273. return 0;
  274. }
  275. static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
  276. {
  277. struct crypt_s390_des3_192_ctx *dctx = ctx;
  278. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  279. DES3_192_BLOCK_SIZE);
  280. }
  281. static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
  282. {
  283. struct crypt_s390_des3_192_ctx *dctx = ctx;
  284. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  285. DES3_192_BLOCK_SIZE);
  286. }
  287. static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc,
  288. u8 *out, const u8 *in,
  289. unsigned int nbytes)
  290. {
  291. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  292. int ret;
  293. /* only use complete blocks */
  294. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  295. ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes);
  296. BUG_ON((ret < 0) || (ret != nbytes));
  297. return nbytes;
  298. }
  299. static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc,
  300. u8 *out, const u8 *in,
  301. unsigned int nbytes)
  302. {
  303. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  304. int ret;
  305. /* only use complete blocks */
  306. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  307. ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes);
  308. BUG_ON((ret < 0) || (ret != nbytes));
  309. return nbytes;
  310. }
  311. static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc,
  312. u8 *out, const u8 *in,
  313. unsigned int nbytes)
  314. {
  315. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  316. int ret;
  317. /* only use complete blocks */
  318. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  319. memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  320. ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes);
  321. BUG_ON((ret < 0) || (ret != nbytes));
  322. memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE);
  323. return nbytes;
  324. }
  325. static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc,
  326. u8 *out, const u8 *in,
  327. unsigned int nbytes)
  328. {
  329. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  330. int ret;
  331. /* only use complete blocks */
  332. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  333. memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  334. ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes);
  335. BUG_ON((ret < 0) || (ret != nbytes));
  336. return nbytes;
  337. }
  338. static struct crypto_alg des3_192_alg = {
  339. .cra_name = "des3_ede",
  340. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  341. .cra_blocksize = DES3_192_BLOCK_SIZE,
  342. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  343. .cra_module = THIS_MODULE,
  344. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  345. .cra_u = {
  346. .cipher = {
  347. .cia_min_keysize = DES3_192_KEY_SIZE,
  348. .cia_max_keysize = DES3_192_KEY_SIZE,
  349. .cia_setkey = des3_192_setkey,
  350. .cia_encrypt = des3_192_encrypt,
  351. .cia_decrypt = des3_192_decrypt,
  352. .cia_encrypt_ecb = des3_192_encrypt_ecb,
  353. .cia_decrypt_ecb = des3_192_decrypt_ecb,
  354. .cia_encrypt_cbc = des3_192_encrypt_cbc,
  355. .cia_decrypt_cbc = des3_192_decrypt_cbc,
  356. }
  357. }
  358. };
  359. static int init(void)
  360. {
  361. int ret = 0;
  362. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  363. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  364. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  365. return -ENOSYS;
  366. ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1;
  367. ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2;
  368. ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4;
  369. if (ret) {
  370. crypto_unregister_alg(&des3_192_alg);
  371. crypto_unregister_alg(&des3_128_alg);
  372. crypto_unregister_alg(&des_alg);
  373. return -EEXIST;
  374. }
  375. return 0;
  376. }
  377. static void __exit fini(void)
  378. {
  379. crypto_unregister_alg(&des3_192_alg);
  380. crypto_unregister_alg(&des3_128_alg);
  381. crypto_unregister_alg(&des_alg);
  382. }
  383. module_init(init);
  384. module_exit(fini);
  385. MODULE_ALIAS("des");
  386. MODULE_ALIAS("des3_ede");
  387. MODULE_LICENSE("GPL");
  388. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");