des_s390.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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_RES_BAD_KEY_SCHED;
  217. return -EINVAL;
  218. }
  219. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  220. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  221. if (ret < 0)
  222. return ret;
  223. }
  224. memcpy(dctx->key, key, keylen);
  225. return 0;
  226. }
  227. static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  228. {
  229. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  230. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  231. DES3_128_BLOCK_SIZE);
  232. }
  233. static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  234. {
  235. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  236. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  237. DES3_128_BLOCK_SIZE);
  238. }
  239. static struct crypto_alg des3_128_alg = {
  240. .cra_name = "des3_ede128",
  241. .cra_driver_name = "des3_ede128-s390",
  242. .cra_priority = CRYPT_S390_PRIORITY,
  243. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  244. .cra_blocksize = DES3_128_BLOCK_SIZE,
  245. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  246. .cra_module = THIS_MODULE,
  247. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  248. .cra_u = {
  249. .cipher = {
  250. .cia_min_keysize = DES3_128_KEY_SIZE,
  251. .cia_max_keysize = DES3_128_KEY_SIZE,
  252. .cia_setkey = des3_128_setkey,
  253. .cia_encrypt = des3_128_encrypt,
  254. .cia_decrypt = des3_128_decrypt,
  255. }
  256. }
  257. };
  258. static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
  259. struct scatterlist *dst,
  260. struct scatterlist *src, unsigned int nbytes)
  261. {
  262. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  263. struct blkcipher_walk walk;
  264. blkcipher_walk_init(&walk, dst, src, nbytes);
  265. return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
  266. }
  267. static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
  268. struct scatterlist *dst,
  269. struct scatterlist *src, unsigned int nbytes)
  270. {
  271. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  272. struct blkcipher_walk walk;
  273. blkcipher_walk_init(&walk, dst, src, nbytes);
  274. return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
  275. }
  276. static struct crypto_alg ecb_des3_128_alg = {
  277. .cra_name = "ecb(des3_ede128)",
  278. .cra_driver_name = "ecb-des3_ede128-s390",
  279. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  280. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  281. .cra_blocksize = DES3_128_BLOCK_SIZE,
  282. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  283. .cra_type = &crypto_blkcipher_type,
  284. .cra_module = THIS_MODULE,
  285. .cra_list = LIST_HEAD_INIT(
  286. ecb_des3_128_alg.cra_list),
  287. .cra_u = {
  288. .blkcipher = {
  289. .min_keysize = DES3_128_KEY_SIZE,
  290. .max_keysize = DES3_128_KEY_SIZE,
  291. .setkey = des3_128_setkey,
  292. .encrypt = ecb_des3_128_encrypt,
  293. .decrypt = ecb_des3_128_decrypt,
  294. }
  295. }
  296. };
  297. static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
  298. struct scatterlist *dst,
  299. struct scatterlist *src, unsigned int nbytes)
  300. {
  301. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  302. struct blkcipher_walk walk;
  303. blkcipher_walk_init(&walk, dst, src, nbytes);
  304. return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
  305. }
  306. static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
  307. struct scatterlist *dst,
  308. struct scatterlist *src, unsigned int nbytes)
  309. {
  310. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  311. struct blkcipher_walk walk;
  312. blkcipher_walk_init(&walk, dst, src, nbytes);
  313. return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
  314. }
  315. static struct crypto_alg cbc_des3_128_alg = {
  316. .cra_name = "cbc(des3_ede128)",
  317. .cra_driver_name = "cbc-des3_ede128-s390",
  318. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  319. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  320. .cra_blocksize = DES3_128_BLOCK_SIZE,
  321. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  322. .cra_type = &crypto_blkcipher_type,
  323. .cra_module = THIS_MODULE,
  324. .cra_list = LIST_HEAD_INIT(
  325. cbc_des3_128_alg.cra_list),
  326. .cra_u = {
  327. .blkcipher = {
  328. .min_keysize = DES3_128_KEY_SIZE,
  329. .max_keysize = DES3_128_KEY_SIZE,
  330. .ivsize = DES3_128_BLOCK_SIZE,
  331. .setkey = des3_128_setkey,
  332. .encrypt = cbc_des3_128_encrypt,
  333. .decrypt = cbc_des3_128_decrypt,
  334. }
  335. }
  336. };
  337. /*
  338. * RFC2451:
  339. *
  340. * For DES-EDE3, there is no known need to reject weak or
  341. * complementation keys. Any weakness is obviated by the use of
  342. * multiple keys.
  343. *
  344. * However, if the first two or last two independent 64-bit keys are
  345. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  346. * same as DES. Implementers MUST reject keys that exhibit this
  347. * property.
  348. *
  349. */
  350. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  351. unsigned int keylen)
  352. {
  353. int i, ret;
  354. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  355. const u8 *temp_key = key;
  356. u32 *flags = &tfm->crt_flags;
  357. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  358. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  359. DES_KEY_SIZE))) {
  360. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  361. return -EINVAL;
  362. }
  363. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  364. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  365. if (ret < 0)
  366. return ret;
  367. }
  368. memcpy(dctx->key, key, keylen);
  369. return 0;
  370. }
  371. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  372. {
  373. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  374. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  375. DES3_192_BLOCK_SIZE);
  376. }
  377. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  378. {
  379. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  380. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  381. DES3_192_BLOCK_SIZE);
  382. }
  383. static struct crypto_alg des3_192_alg = {
  384. .cra_name = "des3_ede",
  385. .cra_driver_name = "des3_ede-s390",
  386. .cra_priority = CRYPT_S390_PRIORITY,
  387. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  388. .cra_blocksize = DES3_192_BLOCK_SIZE,
  389. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  390. .cra_module = THIS_MODULE,
  391. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  392. .cra_u = {
  393. .cipher = {
  394. .cia_min_keysize = DES3_192_KEY_SIZE,
  395. .cia_max_keysize = DES3_192_KEY_SIZE,
  396. .cia_setkey = des3_192_setkey,
  397. .cia_encrypt = des3_192_encrypt,
  398. .cia_decrypt = des3_192_decrypt,
  399. }
  400. }
  401. };
  402. static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
  403. struct scatterlist *dst,
  404. struct scatterlist *src, unsigned int nbytes)
  405. {
  406. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  407. struct blkcipher_walk walk;
  408. blkcipher_walk_init(&walk, dst, src, nbytes);
  409. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
  410. }
  411. static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
  412. struct scatterlist *dst,
  413. struct scatterlist *src, unsigned int nbytes)
  414. {
  415. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  416. struct blkcipher_walk walk;
  417. blkcipher_walk_init(&walk, dst, src, nbytes);
  418. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
  419. }
  420. static struct crypto_alg ecb_des3_192_alg = {
  421. .cra_name = "ecb(des3_ede)",
  422. .cra_driver_name = "ecb-des3_ede-s390",
  423. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  424. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  425. .cra_blocksize = DES3_192_BLOCK_SIZE,
  426. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  427. .cra_type = &crypto_blkcipher_type,
  428. .cra_module = THIS_MODULE,
  429. .cra_list = LIST_HEAD_INIT(
  430. ecb_des3_192_alg.cra_list),
  431. .cra_u = {
  432. .blkcipher = {
  433. .min_keysize = DES3_192_KEY_SIZE,
  434. .max_keysize = DES3_192_KEY_SIZE,
  435. .setkey = des3_192_setkey,
  436. .encrypt = ecb_des3_192_encrypt,
  437. .decrypt = ecb_des3_192_decrypt,
  438. }
  439. }
  440. };
  441. static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
  442. struct scatterlist *dst,
  443. struct scatterlist *src, unsigned int nbytes)
  444. {
  445. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  446. struct blkcipher_walk walk;
  447. blkcipher_walk_init(&walk, dst, src, nbytes);
  448. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
  449. }
  450. static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
  451. struct scatterlist *dst,
  452. struct scatterlist *src, unsigned int nbytes)
  453. {
  454. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  455. struct blkcipher_walk walk;
  456. blkcipher_walk_init(&walk, dst, src, nbytes);
  457. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
  458. }
  459. static struct crypto_alg cbc_des3_192_alg = {
  460. .cra_name = "cbc(des3_ede)",
  461. .cra_driver_name = "cbc-des3_ede-s390",
  462. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  463. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  464. .cra_blocksize = DES3_192_BLOCK_SIZE,
  465. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  466. .cra_type = &crypto_blkcipher_type,
  467. .cra_module = THIS_MODULE,
  468. .cra_list = LIST_HEAD_INIT(
  469. cbc_des3_192_alg.cra_list),
  470. .cra_u = {
  471. .blkcipher = {
  472. .min_keysize = DES3_192_KEY_SIZE,
  473. .max_keysize = DES3_192_KEY_SIZE,
  474. .ivsize = DES3_192_BLOCK_SIZE,
  475. .setkey = des3_192_setkey,
  476. .encrypt = cbc_des3_192_encrypt,
  477. .decrypt = cbc_des3_192_decrypt,
  478. }
  479. }
  480. };
  481. static int init(void)
  482. {
  483. int ret = 0;
  484. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  485. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  486. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  487. return -EOPNOTSUPP;
  488. ret = crypto_register_alg(&des_alg);
  489. if (ret)
  490. goto des_err;
  491. ret = crypto_register_alg(&ecb_des_alg);
  492. if (ret)
  493. goto ecb_des_err;
  494. ret = crypto_register_alg(&cbc_des_alg);
  495. if (ret)
  496. goto cbc_des_err;
  497. ret = crypto_register_alg(&des3_128_alg);
  498. if (ret)
  499. goto des3_128_err;
  500. ret = crypto_register_alg(&ecb_des3_128_alg);
  501. if (ret)
  502. goto ecb_des3_128_err;
  503. ret = crypto_register_alg(&cbc_des3_128_alg);
  504. if (ret)
  505. goto cbc_des3_128_err;
  506. ret = crypto_register_alg(&des3_192_alg);
  507. if (ret)
  508. goto des3_192_err;
  509. ret = crypto_register_alg(&ecb_des3_192_alg);
  510. if (ret)
  511. goto ecb_des3_192_err;
  512. ret = crypto_register_alg(&cbc_des3_192_alg);
  513. if (ret)
  514. goto cbc_des3_192_err;
  515. out:
  516. return ret;
  517. cbc_des3_192_err:
  518. crypto_unregister_alg(&ecb_des3_192_alg);
  519. ecb_des3_192_err:
  520. crypto_unregister_alg(&des3_192_alg);
  521. des3_192_err:
  522. crypto_unregister_alg(&cbc_des3_128_alg);
  523. cbc_des3_128_err:
  524. crypto_unregister_alg(&ecb_des3_128_alg);
  525. ecb_des3_128_err:
  526. crypto_unregister_alg(&des3_128_alg);
  527. des3_128_err:
  528. crypto_unregister_alg(&cbc_des_alg);
  529. cbc_des_err:
  530. crypto_unregister_alg(&ecb_des_alg);
  531. ecb_des_err:
  532. crypto_unregister_alg(&des_alg);
  533. des_err:
  534. goto out;
  535. }
  536. static void __exit fini(void)
  537. {
  538. crypto_unregister_alg(&cbc_des3_192_alg);
  539. crypto_unregister_alg(&ecb_des3_192_alg);
  540. crypto_unregister_alg(&des3_192_alg);
  541. crypto_unregister_alg(&cbc_des3_128_alg);
  542. crypto_unregister_alg(&ecb_des3_128_alg);
  543. crypto_unregister_alg(&des3_128_alg);
  544. crypto_unregister_alg(&cbc_des_alg);
  545. crypto_unregister_alg(&ecb_des_alg);
  546. crypto_unregister_alg(&des_alg);
  547. }
  548. module_init(init);
  549. module_exit(fini);
  550. MODULE_ALIAS("des");
  551. MODULE_ALIAS("des3_ede");
  552. MODULE_LICENSE("GPL");
  553. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");