des_s390.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. static DEFINE_SPINLOCK(ctrblk_lock);
  25. struct s390_des_ctx {
  26. u8 iv[DES_BLOCK_SIZE];
  27. u8 key[DES3_KEY_SIZE];
  28. };
  29. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  30. unsigned int key_len)
  31. {
  32. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  33. u32 *flags = &tfm->crt_flags;
  34. u32 tmp[DES_EXPKEY_WORDS];
  35. /* check for weak keys */
  36. if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  37. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  38. return -EINVAL;
  39. }
  40. memcpy(ctx->key, key, key_len);
  41. return 0;
  42. }
  43. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  44. {
  45. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  46. crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
  47. }
  48. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  49. {
  50. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  51. crypt_s390_km(KM_DEA_DECRYPT, 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 = CRYPT_S390_PRIORITY,
  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, 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. if (ret < 0 || ret != n)
  83. return -EIO;
  84. nbytes &= DES_BLOCK_SIZE - 1;
  85. ret = blkcipher_walk_done(desc, walk, nbytes);
  86. }
  87. return ret;
  88. }
  89. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  90. struct blkcipher_walk *walk)
  91. {
  92. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  93. int ret = blkcipher_walk_virt(desc, walk);
  94. unsigned int nbytes = walk->nbytes;
  95. struct {
  96. u8 iv[DES_BLOCK_SIZE];
  97. u8 key[DES3_KEY_SIZE];
  98. } param;
  99. if (!nbytes)
  100. goto out;
  101. memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
  102. memcpy(param.key, ctx->key, DES3_KEY_SIZE);
  103. do {
  104. /* only use complete blocks */
  105. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  106. u8 *out = walk->dst.virt.addr;
  107. u8 *in = walk->src.virt.addr;
  108. ret = crypt_s390_kmc(func, &param, out, in, n);
  109. if (ret < 0 || ret != n)
  110. return -EIO;
  111. nbytes &= DES_BLOCK_SIZE - 1;
  112. ret = blkcipher_walk_done(desc, walk, nbytes);
  113. } while ((nbytes = walk->nbytes));
  114. memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
  115. out:
  116. return ret;
  117. }
  118. static int ecb_des_encrypt(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_ENCRYPT, ctx->key, &walk);
  126. }
  127. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  128. struct scatterlist *dst, struct scatterlist *src,
  129. unsigned int nbytes)
  130. {
  131. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  132. struct blkcipher_walk walk;
  133. blkcipher_walk_init(&walk, dst, src, nbytes);
  134. return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
  135. }
  136. static struct crypto_alg ecb_des_alg = {
  137. .cra_name = "ecb(des)",
  138. .cra_driver_name = "ecb-des-s390",
  139. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  140. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  141. .cra_blocksize = DES_BLOCK_SIZE,
  142. .cra_ctxsize = sizeof(struct s390_des_ctx),
  143. .cra_type = &crypto_blkcipher_type,
  144. .cra_module = THIS_MODULE,
  145. .cra_u = {
  146. .blkcipher = {
  147. .min_keysize = DES_KEY_SIZE,
  148. .max_keysize = DES_KEY_SIZE,
  149. .setkey = des_setkey,
  150. .encrypt = ecb_des_encrypt,
  151. .decrypt = ecb_des_decrypt,
  152. }
  153. }
  154. };
  155. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  156. struct scatterlist *dst, struct scatterlist *src,
  157. unsigned int nbytes)
  158. {
  159. struct blkcipher_walk walk;
  160. blkcipher_walk_init(&walk, dst, src, nbytes);
  161. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, &walk);
  162. }
  163. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  164. struct scatterlist *dst, struct scatterlist *src,
  165. unsigned int nbytes)
  166. {
  167. struct blkcipher_walk walk;
  168. blkcipher_walk_init(&walk, dst, src, nbytes);
  169. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, &walk);
  170. }
  171. static struct crypto_alg cbc_des_alg = {
  172. .cra_name = "cbc(des)",
  173. .cra_driver_name = "cbc-des-s390",
  174. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  175. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  176. .cra_blocksize = DES_BLOCK_SIZE,
  177. .cra_ctxsize = sizeof(struct s390_des_ctx),
  178. .cra_type = &crypto_blkcipher_type,
  179. .cra_module = THIS_MODULE,
  180. .cra_u = {
  181. .blkcipher = {
  182. .min_keysize = DES_KEY_SIZE,
  183. .max_keysize = DES_KEY_SIZE,
  184. .ivsize = DES_BLOCK_SIZE,
  185. .setkey = des_setkey,
  186. .encrypt = cbc_des_encrypt,
  187. .decrypt = cbc_des_decrypt,
  188. }
  189. }
  190. };
  191. /*
  192. * RFC2451:
  193. *
  194. * For DES-EDE3, there is no known need to reject weak or
  195. * complementation keys. Any weakness is obviated by the use of
  196. * multiple keys.
  197. *
  198. * However, if the first two or last two independent 64-bit keys are
  199. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  200. * same as DES. Implementers MUST reject keys that exhibit this
  201. * property.
  202. *
  203. */
  204. static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
  205. unsigned int key_len)
  206. {
  207. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  208. u32 *flags = &tfm->crt_flags;
  209. if (!(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. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  213. *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. crypt_s390_km(KM_TDEA_192_ENCRYPT, 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. crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
  228. }
  229. static struct crypto_alg des3_alg = {
  230. .cra_name = "des3_ede",
  231. .cra_driver_name = "des3_ede-s390",
  232. .cra_priority = CRYPT_S390_PRIORITY,
  233. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  234. .cra_blocksize = DES_BLOCK_SIZE,
  235. .cra_ctxsize = sizeof(struct s390_des_ctx),
  236. .cra_module = THIS_MODULE,
  237. .cra_u = {
  238. .cipher = {
  239. .cia_min_keysize = DES3_KEY_SIZE,
  240. .cia_max_keysize = DES3_KEY_SIZE,
  241. .cia_setkey = des3_setkey,
  242. .cia_encrypt = des3_encrypt,
  243. .cia_decrypt = des3_decrypt,
  244. }
  245. }
  246. };
  247. static int ecb_des3_encrypt(struct blkcipher_desc *desc,
  248. struct scatterlist *dst, struct scatterlist *src,
  249. unsigned int nbytes)
  250. {
  251. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  252. struct blkcipher_walk walk;
  253. blkcipher_walk_init(&walk, dst, src, nbytes);
  254. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &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 s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  261. struct blkcipher_walk walk;
  262. blkcipher_walk_init(&walk, dst, src, nbytes);
  263. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &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 = CRYPT_S390_COMPOSITE_PRIORITY,
  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, KMC_TDEA_192_ENCRYPT, &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, KMC_TDEA_192_DECRYPT, &walk);
  299. }
  300. static struct crypto_alg cbc_des3_alg = {
  301. .cra_name = "cbc(des3_ede)",
  302. .cra_driver_name = "cbc-des3_ede-s390",
  303. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  304. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  305. .cra_blocksize = DES_BLOCK_SIZE,
  306. .cra_ctxsize = sizeof(struct s390_des_ctx),
  307. .cra_type = &crypto_blkcipher_type,
  308. .cra_module = THIS_MODULE,
  309. .cra_u = {
  310. .blkcipher = {
  311. .min_keysize = DES3_KEY_SIZE,
  312. .max_keysize = DES3_KEY_SIZE,
  313. .ivsize = DES_BLOCK_SIZE,
  314. .setkey = des3_setkey,
  315. .encrypt = cbc_des3_encrypt,
  316. .decrypt = cbc_des3_decrypt,
  317. }
  318. }
  319. };
  320. static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
  321. {
  322. unsigned int i, n;
  323. /* align to block size, max. PAGE_SIZE */
  324. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
  325. for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
  326. memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
  327. crypto_inc(ctrptr + i, DES_BLOCK_SIZE);
  328. }
  329. return n;
  330. }
  331. static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
  332. struct s390_des_ctx *ctx,
  333. struct blkcipher_walk *walk)
  334. {
  335. int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
  336. unsigned int n, nbytes;
  337. u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
  338. u8 *out, *in, *ctrptr = ctrbuf;
  339. if (!walk->nbytes)
  340. return ret;
  341. if (spin_trylock(&ctrblk_lock))
  342. ctrptr = ctrblk;
  343. memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
  344. while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
  345. out = walk->dst.virt.addr;
  346. in = walk->src.virt.addr;
  347. while (nbytes >= DES_BLOCK_SIZE) {
  348. if (ctrptr == ctrblk)
  349. n = __ctrblk_init(ctrptr, nbytes);
  350. else
  351. n = DES_BLOCK_SIZE;
  352. ret = crypt_s390_kmctr(func, ctx->key, out, in,
  353. n, ctrptr);
  354. if (ret < 0 || ret != n) {
  355. if (ctrptr == ctrblk)
  356. spin_unlock(&ctrblk_lock);
  357. return -EIO;
  358. }
  359. if (n > DES_BLOCK_SIZE)
  360. memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
  361. DES_BLOCK_SIZE);
  362. crypto_inc(ctrptr, DES_BLOCK_SIZE);
  363. out += n;
  364. in += n;
  365. nbytes -= n;
  366. }
  367. ret = blkcipher_walk_done(desc, walk, nbytes);
  368. }
  369. if (ctrptr == ctrblk) {
  370. if (nbytes)
  371. memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
  372. else
  373. memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
  374. spin_unlock(&ctrblk_lock);
  375. } else {
  376. if (!nbytes)
  377. memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
  378. }
  379. /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
  380. if (nbytes) {
  381. out = walk->dst.virt.addr;
  382. in = walk->src.virt.addr;
  383. ret = crypt_s390_kmctr(func, ctx->key, buf, in,
  384. DES_BLOCK_SIZE, ctrbuf);
  385. if (ret < 0 || ret != DES_BLOCK_SIZE)
  386. return -EIO;
  387. memcpy(out, buf, nbytes);
  388. crypto_inc(ctrbuf, DES_BLOCK_SIZE);
  389. ret = blkcipher_walk_done(desc, walk, 0);
  390. memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
  391. }
  392. return ret;
  393. }
  394. static int ctr_des_encrypt(struct blkcipher_desc *desc,
  395. struct scatterlist *dst, struct scatterlist *src,
  396. unsigned int nbytes)
  397. {
  398. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  399. struct blkcipher_walk walk;
  400. blkcipher_walk_init(&walk, dst, src, nbytes);
  401. return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
  402. }
  403. static int ctr_des_decrypt(struct blkcipher_desc *desc,
  404. struct scatterlist *dst, struct scatterlist *src,
  405. unsigned int nbytes)
  406. {
  407. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  408. struct blkcipher_walk walk;
  409. blkcipher_walk_init(&walk, dst, src, nbytes);
  410. return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
  411. }
  412. static struct crypto_alg ctr_des_alg = {
  413. .cra_name = "ctr(des)",
  414. .cra_driver_name = "ctr-des-s390",
  415. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  416. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  417. .cra_blocksize = 1,
  418. .cra_ctxsize = sizeof(struct s390_des_ctx),
  419. .cra_type = &crypto_blkcipher_type,
  420. .cra_module = THIS_MODULE,
  421. .cra_u = {
  422. .blkcipher = {
  423. .min_keysize = DES_KEY_SIZE,
  424. .max_keysize = DES_KEY_SIZE,
  425. .ivsize = DES_BLOCK_SIZE,
  426. .setkey = des_setkey,
  427. .encrypt = ctr_des_encrypt,
  428. .decrypt = ctr_des_decrypt,
  429. }
  430. }
  431. };
  432. static int ctr_des3_encrypt(struct blkcipher_desc *desc,
  433. struct scatterlist *dst, struct scatterlist *src,
  434. unsigned int nbytes)
  435. {
  436. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  437. struct blkcipher_walk walk;
  438. blkcipher_walk_init(&walk, dst, src, nbytes);
  439. return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
  440. }
  441. static int ctr_des3_decrypt(struct blkcipher_desc *desc,
  442. struct scatterlist *dst, struct scatterlist *src,
  443. unsigned int nbytes)
  444. {
  445. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  446. struct blkcipher_walk walk;
  447. blkcipher_walk_init(&walk, dst, src, nbytes);
  448. return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
  449. }
  450. static struct crypto_alg ctr_des3_alg = {
  451. .cra_name = "ctr(des3_ede)",
  452. .cra_driver_name = "ctr-des3_ede-s390",
  453. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  454. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  455. .cra_blocksize = 1,
  456. .cra_ctxsize = sizeof(struct s390_des_ctx),
  457. .cra_type = &crypto_blkcipher_type,
  458. .cra_module = THIS_MODULE,
  459. .cra_u = {
  460. .blkcipher = {
  461. .min_keysize = DES3_KEY_SIZE,
  462. .max_keysize = DES3_KEY_SIZE,
  463. .ivsize = DES_BLOCK_SIZE,
  464. .setkey = des3_setkey,
  465. .encrypt = ctr_des3_encrypt,
  466. .decrypt = ctr_des3_decrypt,
  467. }
  468. }
  469. };
  470. static int __init des_s390_init(void)
  471. {
  472. int ret;
  473. if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
  474. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
  475. return -EOPNOTSUPP;
  476. ret = crypto_register_alg(&des_alg);
  477. if (ret)
  478. goto des_err;
  479. ret = crypto_register_alg(&ecb_des_alg);
  480. if (ret)
  481. goto ecb_des_err;
  482. ret = crypto_register_alg(&cbc_des_alg);
  483. if (ret)
  484. goto cbc_des_err;
  485. ret = crypto_register_alg(&des3_alg);
  486. if (ret)
  487. goto des3_err;
  488. ret = crypto_register_alg(&ecb_des3_alg);
  489. if (ret)
  490. goto ecb_des3_err;
  491. ret = crypto_register_alg(&cbc_des3_alg);
  492. if (ret)
  493. goto cbc_des3_err;
  494. if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT,
  495. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  496. crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT,
  497. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  498. ret = crypto_register_alg(&ctr_des_alg);
  499. if (ret)
  500. goto ctr_des_err;
  501. ret = crypto_register_alg(&ctr_des3_alg);
  502. if (ret)
  503. goto ctr_des3_err;
  504. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  505. if (!ctrblk) {
  506. ret = -ENOMEM;
  507. goto ctr_mem_err;
  508. }
  509. }
  510. out:
  511. return ret;
  512. ctr_mem_err:
  513. crypto_unregister_alg(&ctr_des3_alg);
  514. ctr_des3_err:
  515. crypto_unregister_alg(&ctr_des_alg);
  516. ctr_des_err:
  517. crypto_unregister_alg(&cbc_des3_alg);
  518. cbc_des3_err:
  519. crypto_unregister_alg(&ecb_des3_alg);
  520. ecb_des3_err:
  521. crypto_unregister_alg(&des3_alg);
  522. des3_err:
  523. crypto_unregister_alg(&cbc_des_alg);
  524. cbc_des_err:
  525. crypto_unregister_alg(&ecb_des_alg);
  526. ecb_des_err:
  527. crypto_unregister_alg(&des_alg);
  528. des_err:
  529. goto out;
  530. }
  531. static void __exit des_s390_exit(void)
  532. {
  533. if (ctrblk) {
  534. crypto_unregister_alg(&ctr_des_alg);
  535. crypto_unregister_alg(&ctr_des3_alg);
  536. free_page((unsigned long) ctrblk);
  537. }
  538. crypto_unregister_alg(&cbc_des3_alg);
  539. crypto_unregister_alg(&ecb_des3_alg);
  540. crypto_unregister_alg(&des3_alg);
  541. crypto_unregister_alg(&cbc_des_alg);
  542. crypto_unregister_alg(&ecb_des_alg);
  543. crypto_unregister_alg(&des_alg);
  544. }
  545. module_init(des_s390_init);
  546. module_exit(des_s390_exit);
  547. MODULE_ALIAS("des");
  548. MODULE_ALIAS("des3_ede");
  549. MODULE_LICENSE("GPL");
  550. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");