des_s390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the DES Cipher Algorithm.
  5. *
  6. * Copyright IBM Corp. 2003,2007
  7. * Author(s): Thomas Spatzier
  8. * Jan Glauber (jan.glauber@de.ibm.com)
  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 <crypto/algapi.h>
  17. #include <linux/init.h>
  18. #include <linux/module.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_192_KEY_SIZE (3 * DES_KEY_SIZE)
  24. #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
  25. struct crypt_s390_des_ctx {
  26. u8 iv[DES_BLOCK_SIZE];
  27. u8 key[DES_KEY_SIZE];
  28. };
  29. struct crypt_s390_des3_192_ctx {
  30. u8 iv[DES_BLOCK_SIZE];
  31. u8 key[DES3_192_KEY_SIZE];
  32. };
  33. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  34. unsigned int keylen)
  35. {
  36. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  37. u32 *flags = &tfm->crt_flags;
  38. int ret;
  39. /* test if key is valid (not a weak key) */
  40. ret = crypto_des_check_key(key, keylen, flags);
  41. if (ret == 0)
  42. memcpy(dctx->key, key, keylen);
  43. return ret;
  44. }
  45. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  46. {
  47. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  48. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  49. }
  50. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  51. {
  52. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  53. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  54. }
  55. static struct crypto_alg des_alg = {
  56. .cra_name = "des",
  57. .cra_driver_name = "des-s390",
  58. .cra_priority = CRYPT_S390_PRIORITY,
  59. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  60. .cra_blocksize = DES_BLOCK_SIZE,
  61. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  62. .cra_module = THIS_MODULE,
  63. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  64. .cra_u = {
  65. .cipher = {
  66. .cia_min_keysize = DES_KEY_SIZE,
  67. .cia_max_keysize = DES_KEY_SIZE,
  68. .cia_setkey = des_setkey,
  69. .cia_encrypt = des_encrypt,
  70. .cia_decrypt = des_decrypt,
  71. }
  72. }
  73. };
  74. static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
  75. void *param, struct blkcipher_walk *walk)
  76. {
  77. int ret = blkcipher_walk_virt(desc, walk);
  78. unsigned int nbytes;
  79. while ((nbytes = walk->nbytes)) {
  80. /* only use complete blocks */
  81. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  82. u8 *out = walk->dst.virt.addr;
  83. u8 *in = walk->src.virt.addr;
  84. ret = crypt_s390_km(func, param, out, in, n);
  85. BUG_ON((ret < 0) || (ret != n));
  86. nbytes &= DES_BLOCK_SIZE - 1;
  87. ret = blkcipher_walk_done(desc, walk, nbytes);
  88. }
  89. return ret;
  90. }
  91. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  92. void *param, struct blkcipher_walk *walk)
  93. {
  94. int ret = blkcipher_walk_virt(desc, walk);
  95. unsigned int nbytes = walk->nbytes;
  96. if (!nbytes)
  97. goto out;
  98. memcpy(param, walk->iv, DES_BLOCK_SIZE);
  99. do {
  100. /* only use complete blocks */
  101. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  102. u8 *out = walk->dst.virt.addr;
  103. u8 *in = walk->src.virt.addr;
  104. ret = crypt_s390_kmc(func, param, out, in, n);
  105. BUG_ON((ret < 0) || (ret != n));
  106. nbytes &= DES_BLOCK_SIZE - 1;
  107. ret = blkcipher_walk_done(desc, walk, nbytes);
  108. } while ((nbytes = walk->nbytes));
  109. memcpy(walk->iv, param, DES_BLOCK_SIZE);
  110. out:
  111. return ret;
  112. }
  113. static int ecb_des_encrypt(struct blkcipher_desc *desc,
  114. struct scatterlist *dst, struct scatterlist *src,
  115. unsigned int nbytes)
  116. {
  117. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  118. struct blkcipher_walk walk;
  119. blkcipher_walk_init(&walk, dst, src, nbytes);
  120. return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
  121. }
  122. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  123. struct scatterlist *dst, struct scatterlist *src,
  124. unsigned int nbytes)
  125. {
  126. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  127. struct blkcipher_walk walk;
  128. blkcipher_walk_init(&walk, dst, src, nbytes);
  129. return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
  130. }
  131. static struct crypto_alg ecb_des_alg = {
  132. .cra_name = "ecb(des)",
  133. .cra_driver_name = "ecb-des-s390",
  134. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  135. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  136. .cra_blocksize = DES_BLOCK_SIZE,
  137. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  138. .cra_type = &crypto_blkcipher_type,
  139. .cra_module = THIS_MODULE,
  140. .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
  141. .cra_u = {
  142. .blkcipher = {
  143. .min_keysize = DES_KEY_SIZE,
  144. .max_keysize = DES_KEY_SIZE,
  145. .setkey = des_setkey,
  146. .encrypt = ecb_des_encrypt,
  147. .decrypt = ecb_des_decrypt,
  148. }
  149. }
  150. };
  151. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  152. struct scatterlist *dst, struct scatterlist *src,
  153. unsigned int nbytes)
  154. {
  155. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  156. struct blkcipher_walk walk;
  157. blkcipher_walk_init(&walk, dst, src, nbytes);
  158. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
  159. }
  160. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  161. struct scatterlist *dst, struct scatterlist *src,
  162. unsigned int nbytes)
  163. {
  164. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  165. struct blkcipher_walk walk;
  166. blkcipher_walk_init(&walk, dst, src, nbytes);
  167. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
  168. }
  169. static struct crypto_alg cbc_des_alg = {
  170. .cra_name = "cbc(des)",
  171. .cra_driver_name = "cbc-des-s390",
  172. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  173. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  174. .cra_blocksize = DES_BLOCK_SIZE,
  175. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  176. .cra_type = &crypto_blkcipher_type,
  177. .cra_module = THIS_MODULE,
  178. .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
  179. .cra_u = {
  180. .blkcipher = {
  181. .min_keysize = DES_KEY_SIZE,
  182. .max_keysize = DES_KEY_SIZE,
  183. .ivsize = DES_BLOCK_SIZE,
  184. .setkey = des_setkey,
  185. .encrypt = cbc_des_encrypt,
  186. .decrypt = cbc_des_decrypt,
  187. }
  188. }
  189. };
  190. /*
  191. * RFC2451:
  192. *
  193. * For DES-EDE3, there is no known need to reject weak or
  194. * complementation keys. Any weakness is obviated by the use of
  195. * multiple keys.
  196. *
  197. * However, if the first two or last two independent 64-bit keys are
  198. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  199. * same as DES. Implementers MUST reject keys that exhibit this
  200. * property.
  201. *
  202. */
  203. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  204. unsigned int keylen)
  205. {
  206. int i, ret;
  207. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  208. const u8 *temp_key = key;
  209. u32 *flags = &tfm->crt_flags;
  210. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  211. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  212. DES_KEY_SIZE)) &&
  213. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  214. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  215. return -EINVAL;
  216. }
  217. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  218. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. memcpy(dctx->key, key, keylen);
  223. return 0;
  224. }
  225. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  226. {
  227. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  228. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  229. DES3_192_BLOCK_SIZE);
  230. }
  231. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  232. {
  233. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  234. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  235. DES3_192_BLOCK_SIZE);
  236. }
  237. static struct crypto_alg des3_192_alg = {
  238. .cra_name = "des3_ede",
  239. .cra_driver_name = "des3_ede-s390",
  240. .cra_priority = CRYPT_S390_PRIORITY,
  241. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  242. .cra_blocksize = DES3_192_BLOCK_SIZE,
  243. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  244. .cra_module = THIS_MODULE,
  245. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  246. .cra_u = {
  247. .cipher = {
  248. .cia_min_keysize = DES3_192_KEY_SIZE,
  249. .cia_max_keysize = DES3_192_KEY_SIZE,
  250. .cia_setkey = des3_192_setkey,
  251. .cia_encrypt = des3_192_encrypt,
  252. .cia_decrypt = des3_192_decrypt,
  253. }
  254. }
  255. };
  256. static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
  257. struct scatterlist *dst,
  258. struct scatterlist *src, unsigned int nbytes)
  259. {
  260. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  261. struct blkcipher_walk walk;
  262. blkcipher_walk_init(&walk, dst, src, nbytes);
  263. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
  264. }
  265. static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
  266. struct scatterlist *dst,
  267. struct scatterlist *src, unsigned int nbytes)
  268. {
  269. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  270. struct blkcipher_walk walk;
  271. blkcipher_walk_init(&walk, dst, src, nbytes);
  272. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
  273. }
  274. static struct crypto_alg ecb_des3_192_alg = {
  275. .cra_name = "ecb(des3_ede)",
  276. .cra_driver_name = "ecb-des3_ede-s390",
  277. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  278. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  279. .cra_blocksize = DES3_192_BLOCK_SIZE,
  280. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  281. .cra_type = &crypto_blkcipher_type,
  282. .cra_module = THIS_MODULE,
  283. .cra_list = LIST_HEAD_INIT(
  284. ecb_des3_192_alg.cra_list),
  285. .cra_u = {
  286. .blkcipher = {
  287. .min_keysize = DES3_192_KEY_SIZE,
  288. .max_keysize = DES3_192_KEY_SIZE,
  289. .setkey = des3_192_setkey,
  290. .encrypt = ecb_des3_192_encrypt,
  291. .decrypt = ecb_des3_192_decrypt,
  292. }
  293. }
  294. };
  295. static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
  296. struct scatterlist *dst,
  297. struct scatterlist *src, unsigned int nbytes)
  298. {
  299. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  300. struct blkcipher_walk walk;
  301. blkcipher_walk_init(&walk, dst, src, nbytes);
  302. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
  303. }
  304. static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
  305. struct scatterlist *dst,
  306. struct scatterlist *src, unsigned int nbytes)
  307. {
  308. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  309. struct blkcipher_walk walk;
  310. blkcipher_walk_init(&walk, dst, src, nbytes);
  311. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
  312. }
  313. static struct crypto_alg cbc_des3_192_alg = {
  314. .cra_name = "cbc(des3_ede)",
  315. .cra_driver_name = "cbc-des3_ede-s390",
  316. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  317. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  318. .cra_blocksize = DES3_192_BLOCK_SIZE,
  319. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  320. .cra_type = &crypto_blkcipher_type,
  321. .cra_module = THIS_MODULE,
  322. .cra_list = LIST_HEAD_INIT(
  323. cbc_des3_192_alg.cra_list),
  324. .cra_u = {
  325. .blkcipher = {
  326. .min_keysize = DES3_192_KEY_SIZE,
  327. .max_keysize = DES3_192_KEY_SIZE,
  328. .ivsize = DES3_192_BLOCK_SIZE,
  329. .setkey = des3_192_setkey,
  330. .encrypt = cbc_des3_192_encrypt,
  331. .decrypt = cbc_des3_192_decrypt,
  332. }
  333. }
  334. };
  335. static int des_s390_init(void)
  336. {
  337. int ret;
  338. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  339. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  340. return -EOPNOTSUPP;
  341. ret = crypto_register_alg(&des_alg);
  342. if (ret)
  343. goto des_err;
  344. ret = crypto_register_alg(&ecb_des_alg);
  345. if (ret)
  346. goto ecb_des_err;
  347. ret = crypto_register_alg(&cbc_des_alg);
  348. if (ret)
  349. goto cbc_des_err;
  350. ret = crypto_register_alg(&des3_192_alg);
  351. if (ret)
  352. goto des3_192_err;
  353. ret = crypto_register_alg(&ecb_des3_192_alg);
  354. if (ret)
  355. goto ecb_des3_192_err;
  356. ret = crypto_register_alg(&cbc_des3_192_alg);
  357. if (ret)
  358. goto cbc_des3_192_err;
  359. out:
  360. return ret;
  361. cbc_des3_192_err:
  362. crypto_unregister_alg(&ecb_des3_192_alg);
  363. ecb_des3_192_err:
  364. crypto_unregister_alg(&des3_192_alg);
  365. des3_192_err:
  366. crypto_unregister_alg(&cbc_des_alg);
  367. cbc_des_err:
  368. crypto_unregister_alg(&ecb_des_alg);
  369. ecb_des_err:
  370. crypto_unregister_alg(&des_alg);
  371. des_err:
  372. goto out;
  373. }
  374. static void __exit des_s390_fini(void)
  375. {
  376. crypto_unregister_alg(&cbc_des3_192_alg);
  377. crypto_unregister_alg(&ecb_des3_192_alg);
  378. crypto_unregister_alg(&des3_192_alg);
  379. crypto_unregister_alg(&cbc_des_alg);
  380. crypto_unregister_alg(&ecb_des_alg);
  381. crypto_unregister_alg(&des_alg);
  382. }
  383. module_init(des_s390_init);
  384. module_exit(des_s390_fini);
  385. MODULE_ALIAS("des");
  386. MODULE_ALIAS("des3_ede");
  387. MODULE_LICENSE("GPL");
  388. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");