des_s390.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cryptographic API.
  4. *
  5. * s390 implementation of the DES Cipher Algorithm.
  6. *
  7. * Copyright IBM Corp. 2003, 2011
  8. * Author(s): Thomas Spatzier
  9. * Jan Glauber (jan.glauber@de.ibm.com)
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/cpufeature.h>
  14. #include <linux/crypto.h>
  15. #include <linux/fips.h>
  16. #include <linux/mutex.h>
  17. #include <crypto/algapi.h>
  18. #include <crypto/des.h>
  19. #include <asm/cpacf.h>
  20. #define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
  21. static u8 *ctrblk;
  22. static DEFINE_MUTEX(ctrblk_lock);
  23. static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
  24. struct s390_des_ctx {
  25. u8 iv[DES_BLOCK_SIZE];
  26. u8 key[DES3_KEY_SIZE];
  27. };
  28. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  29. unsigned int key_len)
  30. {
  31. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  32. u32 tmp[DES_EXPKEY_WORDS];
  33. /* check for weak keys */
  34. if (!des_ekey(tmp, key) &&
  35. (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  36. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  37. return -EINVAL;
  38. }
  39. memcpy(ctx->key, key, key_len);
  40. return 0;
  41. }
  42. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  43. {
  44. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  45. cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE);
  46. }
  47. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  48. {
  49. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  50. cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
  51. ctx->key, out, in, DES_BLOCK_SIZE);
  52. }
  53. static struct crypto_alg des_alg = {
  54. .cra_name = "des",
  55. .cra_driver_name = "des-s390",
  56. .cra_priority = 300,
  57. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  58. .cra_blocksize = DES_BLOCK_SIZE,
  59. .cra_ctxsize = sizeof(struct s390_des_ctx),
  60. .cra_module = THIS_MODULE,
  61. .cra_u = {
  62. .cipher = {
  63. .cia_min_keysize = DES_KEY_SIZE,
  64. .cia_max_keysize = DES_KEY_SIZE,
  65. .cia_setkey = des_setkey,
  66. .cia_encrypt = des_encrypt,
  67. .cia_decrypt = des_decrypt,
  68. }
  69. }
  70. };
  71. static int ecb_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
  72. struct blkcipher_walk *walk)
  73. {
  74. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  75. unsigned int nbytes, n;
  76. int ret;
  77. ret = blkcipher_walk_virt(desc, walk);
  78. while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
  79. /* only use complete blocks */
  80. n = nbytes & ~(DES_BLOCK_SIZE - 1);
  81. cpacf_km(fc, ctx->key, walk->dst.virt.addr,
  82. walk->src.virt.addr, n);
  83. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  84. }
  85. return ret;
  86. }
  87. static int cbc_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
  88. struct blkcipher_walk *walk)
  89. {
  90. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  91. unsigned int nbytes, n;
  92. int ret;
  93. struct {
  94. u8 iv[DES_BLOCK_SIZE];
  95. u8 key[DES3_KEY_SIZE];
  96. } param;
  97. ret = blkcipher_walk_virt(desc, walk);
  98. memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
  99. memcpy(param.key, ctx->key, DES3_KEY_SIZE);
  100. while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
  101. /* only use complete blocks */
  102. n = nbytes & ~(DES_BLOCK_SIZE - 1);
  103. cpacf_kmc(fc, &param, walk->dst.virt.addr,
  104. walk->src.virt.addr, n);
  105. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  106. }
  107. memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
  108. return ret;
  109. }
  110. static int ecb_des_encrypt(struct blkcipher_desc *desc,
  111. struct scatterlist *dst, struct scatterlist *src,
  112. unsigned int nbytes)
  113. {
  114. struct blkcipher_walk walk;
  115. blkcipher_walk_init(&walk, dst, src, nbytes);
  116. return ecb_desall_crypt(desc, CPACF_KM_DEA, &walk);
  117. }
  118. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  119. struct scatterlist *dst, struct scatterlist *src,
  120. unsigned int nbytes)
  121. {
  122. struct blkcipher_walk walk;
  123. blkcipher_walk_init(&walk, dst, src, nbytes);
  124. return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, &walk);
  125. }
  126. static struct crypto_alg ecb_des_alg = {
  127. .cra_name = "ecb(des)",
  128. .cra_driver_name = "ecb-des-s390",
  129. .cra_priority = 400, /* combo: des + ecb */
  130. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  131. .cra_blocksize = DES_BLOCK_SIZE,
  132. .cra_ctxsize = sizeof(struct s390_des_ctx),
  133. .cra_type = &crypto_blkcipher_type,
  134. .cra_module = THIS_MODULE,
  135. .cra_u = {
  136. .blkcipher = {
  137. .min_keysize = DES_KEY_SIZE,
  138. .max_keysize = DES_KEY_SIZE,
  139. .setkey = des_setkey,
  140. .encrypt = ecb_des_encrypt,
  141. .decrypt = ecb_des_decrypt,
  142. }
  143. }
  144. };
  145. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  146. struct scatterlist *dst, struct scatterlist *src,
  147. unsigned int nbytes)
  148. {
  149. struct blkcipher_walk walk;
  150. blkcipher_walk_init(&walk, dst, src, nbytes);
  151. return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk);
  152. }
  153. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  154. struct scatterlist *dst, struct scatterlist *src,
  155. unsigned int nbytes)
  156. {
  157. struct blkcipher_walk walk;
  158. blkcipher_walk_init(&walk, dst, src, nbytes);
  159. return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk);
  160. }
  161. static struct crypto_alg cbc_des_alg = {
  162. .cra_name = "cbc(des)",
  163. .cra_driver_name = "cbc-des-s390",
  164. .cra_priority = 400, /* combo: des + cbc */
  165. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  166. .cra_blocksize = DES_BLOCK_SIZE,
  167. .cra_ctxsize = sizeof(struct s390_des_ctx),
  168. .cra_type = &crypto_blkcipher_type,
  169. .cra_module = THIS_MODULE,
  170. .cra_u = {
  171. .blkcipher = {
  172. .min_keysize = DES_KEY_SIZE,
  173. .max_keysize = DES_KEY_SIZE,
  174. .ivsize = DES_BLOCK_SIZE,
  175. .setkey = des_setkey,
  176. .encrypt = cbc_des_encrypt,
  177. .decrypt = cbc_des_decrypt,
  178. }
  179. }
  180. };
  181. /*
  182. * RFC2451:
  183. *
  184. * For DES-EDE3, there is no known need to reject weak or
  185. * complementation keys. Any weakness is obviated by the use of
  186. * multiple keys.
  187. *
  188. * However, if the first two or last two independent 64-bit keys are
  189. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  190. * same as DES. Implementers MUST reject keys that exhibit this
  191. * property.
  192. *
  193. * In fips mode additinally check for all 3 keys are unique.
  194. *
  195. */
  196. static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
  197. unsigned int key_len)
  198. {
  199. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  200. if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  201. crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  202. DES_KEY_SIZE)) &&
  203. (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  204. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  205. return -EINVAL;
  206. }
  207. /* in fips mode, ensure k1 != k2 and k2 != k3 and k1 != k3 */
  208. if (fips_enabled &&
  209. !(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  210. crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  211. DES_KEY_SIZE) &&
  212. crypto_memneq(key, &key[DES_KEY_SIZE * 2], DES_KEY_SIZE))) {
  213. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  214. return -EINVAL;
  215. }
  216. memcpy(ctx->key, key, key_len);
  217. return 0;
  218. }
  219. static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  220. {
  221. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  222. cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE);
  223. }
  224. static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  225. {
  226. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  227. cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT,
  228. ctx->key, dst, src, DES_BLOCK_SIZE);
  229. }
  230. static struct crypto_alg des3_alg = {
  231. .cra_name = "des3_ede",
  232. .cra_driver_name = "des3_ede-s390",
  233. .cra_priority = 300,
  234. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  235. .cra_blocksize = DES_BLOCK_SIZE,
  236. .cra_ctxsize = sizeof(struct s390_des_ctx),
  237. .cra_module = THIS_MODULE,
  238. .cra_u = {
  239. .cipher = {
  240. .cia_min_keysize = DES3_KEY_SIZE,
  241. .cia_max_keysize = DES3_KEY_SIZE,
  242. .cia_setkey = des3_setkey,
  243. .cia_encrypt = des3_encrypt,
  244. .cia_decrypt = des3_decrypt,
  245. }
  246. }
  247. };
  248. static int ecb_des3_encrypt(struct blkcipher_desc *desc,
  249. struct scatterlist *dst, struct scatterlist *src,
  250. unsigned int nbytes)
  251. {
  252. struct blkcipher_walk walk;
  253. blkcipher_walk_init(&walk, dst, src, nbytes);
  254. return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, &walk);
  255. }
  256. static int ecb_des3_decrypt(struct blkcipher_desc *desc,
  257. struct scatterlist *dst, struct scatterlist *src,
  258. unsigned int nbytes)
  259. {
  260. struct blkcipher_walk walk;
  261. blkcipher_walk_init(&walk, dst, src, nbytes);
  262. return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
  263. &walk);
  264. }
  265. static struct crypto_alg ecb_des3_alg = {
  266. .cra_name = "ecb(des3_ede)",
  267. .cra_driver_name = "ecb-des3_ede-s390",
  268. .cra_priority = 400, /* combo: des3 + ecb */
  269. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  270. .cra_blocksize = DES_BLOCK_SIZE,
  271. .cra_ctxsize = sizeof(struct s390_des_ctx),
  272. .cra_type = &crypto_blkcipher_type,
  273. .cra_module = THIS_MODULE,
  274. .cra_u = {
  275. .blkcipher = {
  276. .min_keysize = DES3_KEY_SIZE,
  277. .max_keysize = DES3_KEY_SIZE,
  278. .setkey = des3_setkey,
  279. .encrypt = ecb_des3_encrypt,
  280. .decrypt = ecb_des3_decrypt,
  281. }
  282. }
  283. };
  284. static int cbc_des3_encrypt(struct blkcipher_desc *desc,
  285. struct scatterlist *dst, struct scatterlist *src,
  286. unsigned int nbytes)
  287. {
  288. struct blkcipher_walk walk;
  289. blkcipher_walk_init(&walk, dst, src, nbytes);
  290. return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &walk);
  291. }
  292. static int cbc_des3_decrypt(struct blkcipher_desc *desc,
  293. struct scatterlist *dst, struct scatterlist *src,
  294. unsigned int nbytes)
  295. {
  296. struct blkcipher_walk walk;
  297. blkcipher_walk_init(&walk, dst, src, nbytes);
  298. return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT,
  299. &walk);
  300. }
  301. static struct crypto_alg cbc_des3_alg = {
  302. .cra_name = "cbc(des3_ede)",
  303. .cra_driver_name = "cbc-des3_ede-s390",
  304. .cra_priority = 400, /* combo: des3 + cbc */
  305. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  306. .cra_blocksize = DES_BLOCK_SIZE,
  307. .cra_ctxsize = sizeof(struct s390_des_ctx),
  308. .cra_type = &crypto_blkcipher_type,
  309. .cra_module = THIS_MODULE,
  310. .cra_u = {
  311. .blkcipher = {
  312. .min_keysize = DES3_KEY_SIZE,
  313. .max_keysize = DES3_KEY_SIZE,
  314. .ivsize = DES_BLOCK_SIZE,
  315. .setkey = des3_setkey,
  316. .encrypt = cbc_des3_encrypt,
  317. .decrypt = cbc_des3_decrypt,
  318. }
  319. }
  320. };
  321. static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
  322. {
  323. unsigned int i, n;
  324. /* align to block size, max. PAGE_SIZE */
  325. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
  326. memcpy(ctrptr, iv, DES_BLOCK_SIZE);
  327. for (i = (n / DES_BLOCK_SIZE) - 1; i > 0; i--) {
  328. memcpy(ctrptr + DES_BLOCK_SIZE, ctrptr, DES_BLOCK_SIZE);
  329. crypto_inc(ctrptr + DES_BLOCK_SIZE, DES_BLOCK_SIZE);
  330. ctrptr += DES_BLOCK_SIZE;
  331. }
  332. return n;
  333. }
  334. static int ctr_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
  335. struct blkcipher_walk *walk)
  336. {
  337. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  338. u8 buf[DES_BLOCK_SIZE], *ctrptr;
  339. unsigned int n, nbytes;
  340. int ret, locked;
  341. locked = mutex_trylock(&ctrblk_lock);
  342. ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
  343. while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
  344. n = DES_BLOCK_SIZE;
  345. if (nbytes >= 2*DES_BLOCK_SIZE && locked)
  346. n = __ctrblk_init(ctrblk, walk->iv, nbytes);
  347. ctrptr = (n > DES_BLOCK_SIZE) ? ctrblk : walk->iv;
  348. cpacf_kmctr(fc, ctx->key, walk->dst.virt.addr,
  349. walk->src.virt.addr, n, ctrptr);
  350. if (ctrptr == ctrblk)
  351. memcpy(walk->iv, ctrptr + n - DES_BLOCK_SIZE,
  352. DES_BLOCK_SIZE);
  353. crypto_inc(walk->iv, DES_BLOCK_SIZE);
  354. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  355. }
  356. if (locked)
  357. mutex_unlock(&ctrblk_lock);
  358. /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
  359. if (nbytes) {
  360. cpacf_kmctr(fc, ctx->key, buf, walk->src.virt.addr,
  361. DES_BLOCK_SIZE, walk->iv);
  362. memcpy(walk->dst.virt.addr, buf, nbytes);
  363. crypto_inc(walk->iv, DES_BLOCK_SIZE);
  364. ret = blkcipher_walk_done(desc, walk, 0);
  365. }
  366. return ret;
  367. }
  368. static int ctr_des_encrypt(struct blkcipher_desc *desc,
  369. struct scatterlist *dst, struct scatterlist *src,
  370. unsigned int nbytes)
  371. {
  372. struct blkcipher_walk walk;
  373. blkcipher_walk_init(&walk, dst, src, nbytes);
  374. return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, &walk);
  375. }
  376. static int ctr_des_decrypt(struct blkcipher_desc *desc,
  377. struct scatterlist *dst, struct scatterlist *src,
  378. unsigned int nbytes)
  379. {
  380. struct blkcipher_walk walk;
  381. blkcipher_walk_init(&walk, dst, src, nbytes);
  382. return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, &walk);
  383. }
  384. static struct crypto_alg ctr_des_alg = {
  385. .cra_name = "ctr(des)",
  386. .cra_driver_name = "ctr-des-s390",
  387. .cra_priority = 400, /* combo: des + ctr */
  388. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  389. .cra_blocksize = 1,
  390. .cra_ctxsize = sizeof(struct s390_des_ctx),
  391. .cra_type = &crypto_blkcipher_type,
  392. .cra_module = THIS_MODULE,
  393. .cra_u = {
  394. .blkcipher = {
  395. .min_keysize = DES_KEY_SIZE,
  396. .max_keysize = DES_KEY_SIZE,
  397. .ivsize = DES_BLOCK_SIZE,
  398. .setkey = des_setkey,
  399. .encrypt = ctr_des_encrypt,
  400. .decrypt = ctr_des_decrypt,
  401. }
  402. }
  403. };
  404. static int ctr_des3_encrypt(struct blkcipher_desc *desc,
  405. struct scatterlist *dst, struct scatterlist *src,
  406. unsigned int nbytes)
  407. {
  408. struct blkcipher_walk walk;
  409. blkcipher_walk_init(&walk, dst, src, nbytes);
  410. return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, &walk);
  411. }
  412. static int ctr_des3_decrypt(struct blkcipher_desc *desc,
  413. struct scatterlist *dst, struct scatterlist *src,
  414. unsigned int nbytes)
  415. {
  416. struct blkcipher_walk walk;
  417. blkcipher_walk_init(&walk, dst, src, nbytes);
  418. return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
  419. &walk);
  420. }
  421. static struct crypto_alg ctr_des3_alg = {
  422. .cra_name = "ctr(des3_ede)",
  423. .cra_driver_name = "ctr-des3_ede-s390",
  424. .cra_priority = 400, /* combo: des3 + ede */
  425. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  426. .cra_blocksize = 1,
  427. .cra_ctxsize = sizeof(struct s390_des_ctx),
  428. .cra_type = &crypto_blkcipher_type,
  429. .cra_module = THIS_MODULE,
  430. .cra_u = {
  431. .blkcipher = {
  432. .min_keysize = DES3_KEY_SIZE,
  433. .max_keysize = DES3_KEY_SIZE,
  434. .ivsize = DES_BLOCK_SIZE,
  435. .setkey = des3_setkey,
  436. .encrypt = ctr_des3_encrypt,
  437. .decrypt = ctr_des3_decrypt,
  438. }
  439. }
  440. };
  441. static struct crypto_alg *des_s390_algs_ptr[8];
  442. static int des_s390_algs_num;
  443. static int des_s390_register_alg(struct crypto_alg *alg)
  444. {
  445. int ret;
  446. ret = crypto_register_alg(alg);
  447. if (!ret)
  448. des_s390_algs_ptr[des_s390_algs_num++] = alg;
  449. return ret;
  450. }
  451. static void des_s390_exit(void)
  452. {
  453. while (des_s390_algs_num--)
  454. crypto_unregister_alg(des_s390_algs_ptr[des_s390_algs_num]);
  455. if (ctrblk)
  456. free_page((unsigned long) ctrblk);
  457. }
  458. static int __init des_s390_init(void)
  459. {
  460. int ret;
  461. /* Query available functions for KM, KMC and KMCTR */
  462. cpacf_query(CPACF_KM, &km_functions);
  463. cpacf_query(CPACF_KMC, &kmc_functions);
  464. cpacf_query(CPACF_KMCTR, &kmctr_functions);
  465. if (cpacf_test_func(&km_functions, CPACF_KM_DEA)) {
  466. ret = des_s390_register_alg(&des_alg);
  467. if (ret)
  468. goto out_err;
  469. ret = des_s390_register_alg(&ecb_des_alg);
  470. if (ret)
  471. goto out_err;
  472. }
  473. if (cpacf_test_func(&kmc_functions, CPACF_KMC_DEA)) {
  474. ret = des_s390_register_alg(&cbc_des_alg);
  475. if (ret)
  476. goto out_err;
  477. }
  478. if (cpacf_test_func(&km_functions, CPACF_KM_TDEA_192)) {
  479. ret = des_s390_register_alg(&des3_alg);
  480. if (ret)
  481. goto out_err;
  482. ret = des_s390_register_alg(&ecb_des3_alg);
  483. if (ret)
  484. goto out_err;
  485. }
  486. if (cpacf_test_func(&kmc_functions, CPACF_KMC_TDEA_192)) {
  487. ret = des_s390_register_alg(&cbc_des3_alg);
  488. if (ret)
  489. goto out_err;
  490. }
  491. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) ||
  492. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
  493. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  494. if (!ctrblk) {
  495. ret = -ENOMEM;
  496. goto out_err;
  497. }
  498. }
  499. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
  500. ret = des_s390_register_alg(&ctr_des_alg);
  501. if (ret)
  502. goto out_err;
  503. }
  504. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
  505. ret = des_s390_register_alg(&ctr_des3_alg);
  506. if (ret)
  507. goto out_err;
  508. }
  509. return 0;
  510. out_err:
  511. des_s390_exit();
  512. return ret;
  513. }
  514. module_cpu_feature_match(MSA, des_s390_init);
  515. module_exit(des_s390_exit);
  516. MODULE_ALIAS_CRYPTO("des");
  517. MODULE_ALIAS_CRYPTO("des3_ede");
  518. MODULE_LICENSE("GPL");
  519. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");