des_s390.c 16 KB

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