des_s390.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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_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(struct crypto_tfm *tfm, const u8 *key,
  40. unsigned int keylen)
  41. {
  42. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  43. u32 *flags = &tfm->crt_flags;
  44. int ret;
  45. /* test if key is valid (not a weak key) */
  46. ret = crypto_des_check_key(key, keylen, flags);
  47. if (ret == 0)
  48. memcpy(dctx->key, key, keylen);
  49. return ret;
  50. }
  51. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  52. {
  53. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  54. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  55. }
  56. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  57. {
  58. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  59. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  60. }
  61. static struct crypto_alg des_alg = {
  62. .cra_name = "des",
  63. .cra_driver_name = "des-s390",
  64. .cra_priority = CRYPT_S390_PRIORITY,
  65. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  66. .cra_blocksize = DES_BLOCK_SIZE,
  67. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  68. .cra_module = THIS_MODULE,
  69. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  70. .cra_u = {
  71. .cipher = {
  72. .cia_min_keysize = DES_KEY_SIZE,
  73. .cia_max_keysize = DES_KEY_SIZE,
  74. .cia_setkey = des_setkey,
  75. .cia_encrypt = des_encrypt,
  76. .cia_decrypt = des_decrypt,
  77. }
  78. }
  79. };
  80. static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
  81. void *param, struct blkcipher_walk *walk)
  82. {
  83. int ret = blkcipher_walk_virt(desc, walk);
  84. unsigned int nbytes;
  85. while ((nbytes = walk->nbytes)) {
  86. /* only use complete blocks */
  87. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  88. u8 *out = walk->dst.virt.addr;
  89. u8 *in = walk->src.virt.addr;
  90. ret = crypt_s390_km(func, param, out, in, n);
  91. BUG_ON((ret < 0) || (ret != n));
  92. nbytes &= DES_BLOCK_SIZE - 1;
  93. ret = blkcipher_walk_done(desc, walk, nbytes);
  94. }
  95. return ret;
  96. }
  97. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  98. void *param, struct blkcipher_walk *walk)
  99. {
  100. int ret = blkcipher_walk_virt(desc, walk);
  101. unsigned int nbytes = walk->nbytes;
  102. if (!nbytes)
  103. goto out;
  104. memcpy(param, walk->iv, DES_BLOCK_SIZE);
  105. do {
  106. /* only use complete blocks */
  107. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  108. u8 *out = walk->dst.virt.addr;
  109. u8 *in = walk->src.virt.addr;
  110. ret = crypt_s390_kmc(func, param, out, in, n);
  111. BUG_ON((ret < 0) || (ret != n));
  112. nbytes &= DES_BLOCK_SIZE - 1;
  113. ret = blkcipher_walk_done(desc, walk, nbytes);
  114. } while ((nbytes = walk->nbytes));
  115. memcpy(walk->iv, param, DES_BLOCK_SIZE);
  116. out:
  117. return ret;
  118. }
  119. static int ecb_des_encrypt(struct blkcipher_desc *desc,
  120. struct scatterlist *dst, struct scatterlist *src,
  121. unsigned int nbytes)
  122. {
  123. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  124. struct blkcipher_walk walk;
  125. blkcipher_walk_init(&walk, dst, src, nbytes);
  126. return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
  127. }
  128. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  129. struct scatterlist *dst, struct scatterlist *src,
  130. unsigned int nbytes)
  131. {
  132. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  133. struct blkcipher_walk walk;
  134. blkcipher_walk_init(&walk, dst, src, nbytes);
  135. return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
  136. }
  137. static struct crypto_alg ecb_des_alg = {
  138. .cra_name = "ecb(des)",
  139. .cra_driver_name = "ecb-des-s390",
  140. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  141. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  142. .cra_blocksize = DES_BLOCK_SIZE,
  143. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  144. .cra_type = &crypto_blkcipher_type,
  145. .cra_module = THIS_MODULE,
  146. .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
  147. .cra_u = {
  148. .blkcipher = {
  149. .min_keysize = DES_KEY_SIZE,
  150. .max_keysize = DES_KEY_SIZE,
  151. .setkey = des_setkey,
  152. .encrypt = ecb_des_encrypt,
  153. .decrypt = ecb_des_decrypt,
  154. }
  155. }
  156. };
  157. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  158. struct scatterlist *dst, struct scatterlist *src,
  159. unsigned int nbytes)
  160. {
  161. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  162. struct blkcipher_walk walk;
  163. blkcipher_walk_init(&walk, dst, src, nbytes);
  164. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
  165. }
  166. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  167. struct scatterlist *dst, struct scatterlist *src,
  168. unsigned int nbytes)
  169. {
  170. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  171. struct blkcipher_walk walk;
  172. blkcipher_walk_init(&walk, dst, src, nbytes);
  173. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
  174. }
  175. static struct crypto_alg cbc_des_alg = {
  176. .cra_name = "cbc(des)",
  177. .cra_driver_name = "cbc-des-s390",
  178. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  179. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  180. .cra_blocksize = DES_BLOCK_SIZE,
  181. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  182. .cra_type = &crypto_blkcipher_type,
  183. .cra_module = THIS_MODULE,
  184. .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
  185. .cra_u = {
  186. .blkcipher = {
  187. .min_keysize = DES_KEY_SIZE,
  188. .max_keysize = DES_KEY_SIZE,
  189. .ivsize = DES_BLOCK_SIZE,
  190. .setkey = des_setkey,
  191. .encrypt = cbc_des_encrypt,
  192. .decrypt = cbc_des_decrypt,
  193. }
  194. }
  195. };
  196. /*
  197. * RFC2451:
  198. *
  199. * For DES-EDE3, there is no known need to reject weak or
  200. * complementation keys. Any weakness is obviated by the use of
  201. * multiple keys.
  202. *
  203. * However, if the two independent 64-bit keys are equal,
  204. * then the DES3 operation is simply the same as DES.
  205. * Implementers MUST reject keys that exhibit this property.
  206. *
  207. */
  208. static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
  209. unsigned int keylen)
  210. {
  211. int i, ret;
  212. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  213. const u8 *temp_key = key;
  214. u32 *flags = &tfm->crt_flags;
  215. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE)) &&
  216. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  217. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  218. return -EINVAL;
  219. }
  220. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  221. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  222. if (ret < 0)
  223. return ret;
  224. }
  225. memcpy(dctx->key, key, keylen);
  226. return 0;
  227. }
  228. static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  229. {
  230. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  231. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  232. DES3_128_BLOCK_SIZE);
  233. }
  234. static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  235. {
  236. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  237. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  238. DES3_128_BLOCK_SIZE);
  239. }
  240. static struct crypto_alg des3_128_alg = {
  241. .cra_name = "des3_ede128",
  242. .cra_driver_name = "des3_ede128-s390",
  243. .cra_priority = CRYPT_S390_PRIORITY,
  244. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  245. .cra_blocksize = DES3_128_BLOCK_SIZE,
  246. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  247. .cra_module = THIS_MODULE,
  248. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  249. .cra_u = {
  250. .cipher = {
  251. .cia_min_keysize = DES3_128_KEY_SIZE,
  252. .cia_max_keysize = DES3_128_KEY_SIZE,
  253. .cia_setkey = des3_128_setkey,
  254. .cia_encrypt = des3_128_encrypt,
  255. .cia_decrypt = des3_128_decrypt,
  256. }
  257. }
  258. };
  259. static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
  260. struct scatterlist *dst,
  261. struct scatterlist *src, unsigned int nbytes)
  262. {
  263. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  264. struct blkcipher_walk walk;
  265. blkcipher_walk_init(&walk, dst, src, nbytes);
  266. return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
  267. }
  268. static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
  269. struct scatterlist *dst,
  270. struct scatterlist *src, unsigned int nbytes)
  271. {
  272. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  273. struct blkcipher_walk walk;
  274. blkcipher_walk_init(&walk, dst, src, nbytes);
  275. return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
  276. }
  277. static struct crypto_alg ecb_des3_128_alg = {
  278. .cra_name = "ecb(des3_ede128)",
  279. .cra_driver_name = "ecb-des3_ede128-s390",
  280. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  281. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  282. .cra_blocksize = DES3_128_BLOCK_SIZE,
  283. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  284. .cra_type = &crypto_blkcipher_type,
  285. .cra_module = THIS_MODULE,
  286. .cra_list = LIST_HEAD_INIT(
  287. ecb_des3_128_alg.cra_list),
  288. .cra_u = {
  289. .blkcipher = {
  290. .min_keysize = DES3_128_KEY_SIZE,
  291. .max_keysize = DES3_128_KEY_SIZE,
  292. .setkey = des3_128_setkey,
  293. .encrypt = ecb_des3_128_encrypt,
  294. .decrypt = ecb_des3_128_decrypt,
  295. }
  296. }
  297. };
  298. static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
  299. struct scatterlist *dst,
  300. struct scatterlist *src, unsigned int nbytes)
  301. {
  302. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  303. struct blkcipher_walk walk;
  304. blkcipher_walk_init(&walk, dst, src, nbytes);
  305. return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
  306. }
  307. static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
  308. struct scatterlist *dst,
  309. struct scatterlist *src, unsigned int nbytes)
  310. {
  311. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  312. struct blkcipher_walk walk;
  313. blkcipher_walk_init(&walk, dst, src, nbytes);
  314. return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
  315. }
  316. static struct crypto_alg cbc_des3_128_alg = {
  317. .cra_name = "cbc(des3_ede128)",
  318. .cra_driver_name = "cbc-des3_ede128-s390",
  319. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  320. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  321. .cra_blocksize = DES3_128_BLOCK_SIZE,
  322. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  323. .cra_type = &crypto_blkcipher_type,
  324. .cra_module = THIS_MODULE,
  325. .cra_list = LIST_HEAD_INIT(
  326. cbc_des3_128_alg.cra_list),
  327. .cra_u = {
  328. .blkcipher = {
  329. .min_keysize = DES3_128_KEY_SIZE,
  330. .max_keysize = DES3_128_KEY_SIZE,
  331. .ivsize = DES3_128_BLOCK_SIZE,
  332. .setkey = des3_128_setkey,
  333. .encrypt = cbc_des3_128_encrypt,
  334. .decrypt = cbc_des3_128_decrypt,
  335. }
  336. }
  337. };
  338. /*
  339. * RFC2451:
  340. *
  341. * For DES-EDE3, there is no known need to reject weak or
  342. * complementation keys. Any weakness is obviated by the use of
  343. * multiple keys.
  344. *
  345. * However, if the first two or last two independent 64-bit keys are
  346. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  347. * same as DES. Implementers MUST reject keys that exhibit this
  348. * property.
  349. *
  350. */
  351. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  352. unsigned int keylen)
  353. {
  354. int i, ret;
  355. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  356. const u8 *temp_key = key;
  357. u32 *flags = &tfm->crt_flags;
  358. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  359. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  360. DES_KEY_SIZE)) &&
  361. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  362. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  363. return -EINVAL;
  364. }
  365. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  366. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. memcpy(dctx->key, key, keylen);
  371. return 0;
  372. }
  373. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  374. {
  375. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  376. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  377. DES3_192_BLOCK_SIZE);
  378. }
  379. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  380. {
  381. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  382. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  383. DES3_192_BLOCK_SIZE);
  384. }
  385. static struct crypto_alg des3_192_alg = {
  386. .cra_name = "des3_ede",
  387. .cra_driver_name = "des3_ede-s390",
  388. .cra_priority = CRYPT_S390_PRIORITY,
  389. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  390. .cra_blocksize = DES3_192_BLOCK_SIZE,
  391. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  392. .cra_module = THIS_MODULE,
  393. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  394. .cra_u = {
  395. .cipher = {
  396. .cia_min_keysize = DES3_192_KEY_SIZE,
  397. .cia_max_keysize = DES3_192_KEY_SIZE,
  398. .cia_setkey = des3_192_setkey,
  399. .cia_encrypt = des3_192_encrypt,
  400. .cia_decrypt = des3_192_decrypt,
  401. }
  402. }
  403. };
  404. static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
  405. struct scatterlist *dst,
  406. struct scatterlist *src, unsigned int nbytes)
  407. {
  408. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  409. struct blkcipher_walk walk;
  410. blkcipher_walk_init(&walk, dst, src, nbytes);
  411. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
  412. }
  413. static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
  414. struct scatterlist *dst,
  415. struct scatterlist *src, unsigned int nbytes)
  416. {
  417. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  418. struct blkcipher_walk walk;
  419. blkcipher_walk_init(&walk, dst, src, nbytes);
  420. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
  421. }
  422. static struct crypto_alg ecb_des3_192_alg = {
  423. .cra_name = "ecb(des3_ede)",
  424. .cra_driver_name = "ecb-des3_ede-s390",
  425. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  426. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  427. .cra_blocksize = DES3_192_BLOCK_SIZE,
  428. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  429. .cra_type = &crypto_blkcipher_type,
  430. .cra_module = THIS_MODULE,
  431. .cra_list = LIST_HEAD_INIT(
  432. ecb_des3_192_alg.cra_list),
  433. .cra_u = {
  434. .blkcipher = {
  435. .min_keysize = DES3_192_KEY_SIZE,
  436. .max_keysize = DES3_192_KEY_SIZE,
  437. .setkey = des3_192_setkey,
  438. .encrypt = ecb_des3_192_encrypt,
  439. .decrypt = ecb_des3_192_decrypt,
  440. }
  441. }
  442. };
  443. static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
  444. struct scatterlist *dst,
  445. struct scatterlist *src, unsigned int nbytes)
  446. {
  447. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  448. struct blkcipher_walk walk;
  449. blkcipher_walk_init(&walk, dst, src, nbytes);
  450. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
  451. }
  452. static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
  453. struct scatterlist *dst,
  454. struct scatterlist *src, unsigned int nbytes)
  455. {
  456. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  457. struct blkcipher_walk walk;
  458. blkcipher_walk_init(&walk, dst, src, nbytes);
  459. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
  460. }
  461. static struct crypto_alg cbc_des3_192_alg = {
  462. .cra_name = "cbc(des3_ede)",
  463. .cra_driver_name = "cbc-des3_ede-s390",
  464. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  465. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  466. .cra_blocksize = DES3_192_BLOCK_SIZE,
  467. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  468. .cra_type = &crypto_blkcipher_type,
  469. .cra_module = THIS_MODULE,
  470. .cra_list = LIST_HEAD_INIT(
  471. cbc_des3_192_alg.cra_list),
  472. .cra_u = {
  473. .blkcipher = {
  474. .min_keysize = DES3_192_KEY_SIZE,
  475. .max_keysize = DES3_192_KEY_SIZE,
  476. .ivsize = DES3_192_BLOCK_SIZE,
  477. .setkey = des3_192_setkey,
  478. .encrypt = cbc_des3_192_encrypt,
  479. .decrypt = cbc_des3_192_decrypt,
  480. }
  481. }
  482. };
  483. static int des_s390_init(void)
  484. {
  485. int ret = 0;
  486. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  487. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  488. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  489. return -EOPNOTSUPP;
  490. ret = crypto_register_alg(&des_alg);
  491. if (ret)
  492. goto des_err;
  493. ret = crypto_register_alg(&ecb_des_alg);
  494. if (ret)
  495. goto ecb_des_err;
  496. ret = crypto_register_alg(&cbc_des_alg);
  497. if (ret)
  498. goto cbc_des_err;
  499. ret = crypto_register_alg(&des3_128_alg);
  500. if (ret)
  501. goto des3_128_err;
  502. ret = crypto_register_alg(&ecb_des3_128_alg);
  503. if (ret)
  504. goto ecb_des3_128_err;
  505. ret = crypto_register_alg(&cbc_des3_128_alg);
  506. if (ret)
  507. goto cbc_des3_128_err;
  508. ret = crypto_register_alg(&des3_192_alg);
  509. if (ret)
  510. goto des3_192_err;
  511. ret = crypto_register_alg(&ecb_des3_192_alg);
  512. if (ret)
  513. goto ecb_des3_192_err;
  514. ret = crypto_register_alg(&cbc_des3_192_alg);
  515. if (ret)
  516. goto cbc_des3_192_err;
  517. out:
  518. return ret;
  519. cbc_des3_192_err:
  520. crypto_unregister_alg(&ecb_des3_192_alg);
  521. ecb_des3_192_err:
  522. crypto_unregister_alg(&des3_192_alg);
  523. des3_192_err:
  524. crypto_unregister_alg(&cbc_des3_128_alg);
  525. cbc_des3_128_err:
  526. crypto_unregister_alg(&ecb_des3_128_alg);
  527. ecb_des3_128_err:
  528. crypto_unregister_alg(&des3_128_alg);
  529. des3_128_err:
  530. crypto_unregister_alg(&cbc_des_alg);
  531. cbc_des_err:
  532. crypto_unregister_alg(&ecb_des_alg);
  533. ecb_des_err:
  534. crypto_unregister_alg(&des_alg);
  535. des_err:
  536. goto out;
  537. }
  538. static void __exit des_s390_fini(void)
  539. {
  540. crypto_unregister_alg(&cbc_des3_192_alg);
  541. crypto_unregister_alg(&ecb_des3_192_alg);
  542. crypto_unregister_alg(&des3_192_alg);
  543. crypto_unregister_alg(&cbc_des3_128_alg);
  544. crypto_unregister_alg(&ecb_des3_128_alg);
  545. crypto_unregister_alg(&des3_128_alg);
  546. crypto_unregister_alg(&cbc_des_alg);
  547. crypto_unregister_alg(&ecb_des_alg);
  548. crypto_unregister_alg(&des_alg);
  549. }
  550. module_init(des_s390_init);
  551. module_exit(des_s390_fini);
  552. MODULE_ALIAS("des");
  553. MODULE_ALIAS("des3_ede");
  554. MODULE_LICENSE("GPL");
  555. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");