des_s390.c 16 KB

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