des_s390.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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/cpufeature.h>
  19. #include <linux/crypto.h>
  20. #include <crypto/algapi.h>
  21. #include <crypto/des.h>
  22. #include "crypt_s390.h"
  23. #define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
  24. static u8 *ctrblk;
  25. static DEFINE_SPINLOCK(ctrblk_lock);
  26. struct s390_des_ctx {
  27. u8 iv[DES_BLOCK_SIZE];
  28. u8 key[DES3_KEY_SIZE];
  29. };
  30. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  31. unsigned int key_len)
  32. {
  33. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  34. u32 *flags = &tfm->crt_flags;
  35. u32 tmp[DES_EXPKEY_WORDS];
  36. /* check for weak keys */
  37. if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  38. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  39. return -EINVAL;
  40. }
  41. memcpy(ctx->key, key, key_len);
  42. return 0;
  43. }
  44. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  45. {
  46. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  47. crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
  48. }
  49. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  50. {
  51. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  52. crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
  53. }
  54. static struct crypto_alg des_alg = {
  55. .cra_name = "des",
  56. .cra_driver_name = "des-s390",
  57. .cra_priority = CRYPT_S390_PRIORITY,
  58. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  59. .cra_blocksize = DES_BLOCK_SIZE,
  60. .cra_ctxsize = sizeof(struct s390_des_ctx),
  61. .cra_module = THIS_MODULE,
  62. .cra_u = {
  63. .cipher = {
  64. .cia_min_keysize = DES_KEY_SIZE,
  65. .cia_max_keysize = DES_KEY_SIZE,
  66. .cia_setkey = des_setkey,
  67. .cia_encrypt = des_encrypt,
  68. .cia_decrypt = des_decrypt,
  69. }
  70. }
  71. };
  72. static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
  73. u8 *key, struct blkcipher_walk *walk)
  74. {
  75. int ret = blkcipher_walk_virt(desc, walk);
  76. unsigned int nbytes;
  77. while ((nbytes = walk->nbytes)) {
  78. /* only use complete blocks */
  79. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  80. u8 *out = walk->dst.virt.addr;
  81. u8 *in = walk->src.virt.addr;
  82. ret = crypt_s390_km(func, key, out, in, n);
  83. if (ret < 0 || ret != n)
  84. return -EIO;
  85. nbytes &= DES_BLOCK_SIZE - 1;
  86. ret = blkcipher_walk_done(desc, walk, nbytes);
  87. }
  88. return ret;
  89. }
  90. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  91. struct blkcipher_walk *walk)
  92. {
  93. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  94. int ret = blkcipher_walk_virt(desc, walk);
  95. unsigned int nbytes = walk->nbytes;
  96. struct {
  97. u8 iv[DES_BLOCK_SIZE];
  98. u8 key[DES3_KEY_SIZE];
  99. } param;
  100. if (!nbytes)
  101. goto out;
  102. memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
  103. memcpy(param.key, ctx->key, DES3_KEY_SIZE);
  104. do {
  105. /* only use complete blocks */
  106. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  107. u8 *out = walk->dst.virt.addr;
  108. u8 *in = walk->src.virt.addr;
  109. ret = crypt_s390_kmc(func, &param, out, in, n);
  110. if (ret < 0 || ret != n)
  111. return -EIO;
  112. nbytes &= DES_BLOCK_SIZE - 1;
  113. ret = blkcipher_walk_done(desc, walk, nbytes);
  114. } while ((nbytes = walk->nbytes));
  115. memcpy(walk->iv, param.iv, 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 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_ENCRYPT, ctx->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 s390_des_ctx *ctx = 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, ctx->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 s390_des_ctx),
  144. .cra_type = &crypto_blkcipher_type,
  145. .cra_module = THIS_MODULE,
  146. .cra_u = {
  147. .blkcipher = {
  148. .min_keysize = DES_KEY_SIZE,
  149. .max_keysize = DES_KEY_SIZE,
  150. .setkey = des_setkey,
  151. .encrypt = ecb_des_encrypt,
  152. .decrypt = ecb_des_decrypt,
  153. }
  154. }
  155. };
  156. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  157. struct scatterlist *dst, struct scatterlist *src,
  158. unsigned int nbytes)
  159. {
  160. struct blkcipher_walk walk;
  161. blkcipher_walk_init(&walk, dst, src, nbytes);
  162. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, &walk);
  163. }
  164. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  165. struct scatterlist *dst, struct scatterlist *src,
  166. unsigned int nbytes)
  167. {
  168. struct blkcipher_walk walk;
  169. blkcipher_walk_init(&walk, dst, src, nbytes);
  170. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, &walk);
  171. }
  172. static struct crypto_alg cbc_des_alg = {
  173. .cra_name = "cbc(des)",
  174. .cra_driver_name = "cbc-des-s390",
  175. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  176. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  177. .cra_blocksize = DES_BLOCK_SIZE,
  178. .cra_ctxsize = sizeof(struct s390_des_ctx),
  179. .cra_type = &crypto_blkcipher_type,
  180. .cra_module = THIS_MODULE,
  181. .cra_u = {
  182. .blkcipher = {
  183. .min_keysize = DES_KEY_SIZE,
  184. .max_keysize = DES_KEY_SIZE,
  185. .ivsize = DES_BLOCK_SIZE,
  186. .setkey = des_setkey,
  187. .encrypt = cbc_des_encrypt,
  188. .decrypt = cbc_des_decrypt,
  189. }
  190. }
  191. };
  192. /*
  193. * RFC2451:
  194. *
  195. * For DES-EDE3, there is no known need to reject weak or
  196. * complementation keys. Any weakness is obviated by the use of
  197. * multiple keys.
  198. *
  199. * However, if the first two or last two independent 64-bit keys are
  200. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  201. * same as DES. Implementers MUST reject keys that exhibit this
  202. * property.
  203. *
  204. */
  205. static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
  206. unsigned int key_len)
  207. {
  208. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  209. u32 *flags = &tfm->crt_flags;
  210. if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  211. crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  212. DES_KEY_SIZE)) &&
  213. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  214. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  215. return -EINVAL;
  216. }
  217. memcpy(ctx->key, key, key_len);
  218. return 0;
  219. }
  220. static void des3_encrypt(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_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
  224. }
  225. static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  226. {
  227. struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
  228. crypt_s390_km(KM_TDEA_192_DECRYPT, 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 = CRYPT_S390_PRIORITY,
  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 s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  253. struct blkcipher_walk walk;
  254. blkcipher_walk_init(&walk, dst, src, nbytes);
  255. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
  256. }
  257. static int ecb_des3_decrypt(struct blkcipher_desc *desc,
  258. struct scatterlist *dst, struct scatterlist *src,
  259. unsigned int nbytes)
  260. {
  261. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  262. struct blkcipher_walk walk;
  263. blkcipher_walk_init(&walk, dst, src, nbytes);
  264. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
  265. }
  266. static struct crypto_alg ecb_des3_alg = {
  267. .cra_name = "ecb(des3_ede)",
  268. .cra_driver_name = "ecb-des3_ede-s390",
  269. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  270. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  271. .cra_blocksize = DES_BLOCK_SIZE,
  272. .cra_ctxsize = sizeof(struct s390_des_ctx),
  273. .cra_type = &crypto_blkcipher_type,
  274. .cra_module = THIS_MODULE,
  275. .cra_u = {
  276. .blkcipher = {
  277. .min_keysize = DES3_KEY_SIZE,
  278. .max_keysize = DES3_KEY_SIZE,
  279. .setkey = des3_setkey,
  280. .encrypt = ecb_des3_encrypt,
  281. .decrypt = ecb_des3_decrypt,
  282. }
  283. }
  284. };
  285. static int cbc_des3_encrypt(struct blkcipher_desc *desc,
  286. struct scatterlist *dst, struct scatterlist *src,
  287. unsigned int nbytes)
  288. {
  289. struct blkcipher_walk walk;
  290. blkcipher_walk_init(&walk, dst, src, nbytes);
  291. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, &walk);
  292. }
  293. static int cbc_des3_decrypt(struct blkcipher_desc *desc,
  294. struct scatterlist *dst, struct scatterlist *src,
  295. unsigned int nbytes)
  296. {
  297. struct blkcipher_walk walk;
  298. blkcipher_walk_init(&walk, dst, src, nbytes);
  299. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, &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_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, 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. for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
  327. memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
  328. crypto_inc(ctrptr + i, DES_BLOCK_SIZE);
  329. }
  330. return n;
  331. }
  332. static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
  333. struct s390_des_ctx *ctx,
  334. struct blkcipher_walk *walk)
  335. {
  336. int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
  337. unsigned int n, nbytes;
  338. u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
  339. u8 *out, *in, *ctrptr = ctrbuf;
  340. if (!walk->nbytes)
  341. return ret;
  342. if (spin_trylock(&ctrblk_lock))
  343. ctrptr = ctrblk;
  344. memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE);
  345. while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
  346. out = walk->dst.virt.addr;
  347. in = walk->src.virt.addr;
  348. while (nbytes >= DES_BLOCK_SIZE) {
  349. if (ctrptr == ctrblk)
  350. n = __ctrblk_init(ctrptr, nbytes);
  351. else
  352. n = DES_BLOCK_SIZE;
  353. ret = crypt_s390_kmctr(func, ctx->key, out, in,
  354. n, ctrptr);
  355. if (ret < 0 || ret != n) {
  356. if (ctrptr == ctrblk)
  357. spin_unlock(&ctrblk_lock);
  358. return -EIO;
  359. }
  360. if (n > DES_BLOCK_SIZE)
  361. memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
  362. DES_BLOCK_SIZE);
  363. crypto_inc(ctrptr, DES_BLOCK_SIZE);
  364. out += n;
  365. in += n;
  366. nbytes -= n;
  367. }
  368. ret = blkcipher_walk_done(desc, walk, nbytes);
  369. }
  370. if (ctrptr == ctrblk) {
  371. if (nbytes)
  372. memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE);
  373. else
  374. memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
  375. spin_unlock(&ctrblk_lock);
  376. } else {
  377. if (!nbytes)
  378. memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
  379. }
  380. /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
  381. if (nbytes) {
  382. out = walk->dst.virt.addr;
  383. in = walk->src.virt.addr;
  384. ret = crypt_s390_kmctr(func, ctx->key, buf, in,
  385. DES_BLOCK_SIZE, ctrbuf);
  386. if (ret < 0 || ret != DES_BLOCK_SIZE)
  387. return -EIO;
  388. memcpy(out, buf, nbytes);
  389. crypto_inc(ctrbuf, DES_BLOCK_SIZE);
  390. ret = blkcipher_walk_done(desc, walk, 0);
  391. memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
  392. }
  393. return ret;
  394. }
  395. static int ctr_des_encrypt(struct blkcipher_desc *desc,
  396. struct scatterlist *dst, struct scatterlist *src,
  397. unsigned int nbytes)
  398. {
  399. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  400. struct blkcipher_walk walk;
  401. blkcipher_walk_init(&walk, dst, src, nbytes);
  402. return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
  403. }
  404. static int ctr_des_decrypt(struct blkcipher_desc *desc,
  405. struct scatterlist *dst, struct scatterlist *src,
  406. unsigned int nbytes)
  407. {
  408. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  409. struct blkcipher_walk walk;
  410. blkcipher_walk_init(&walk, dst, src, nbytes);
  411. return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
  412. }
  413. static struct crypto_alg ctr_des_alg = {
  414. .cra_name = "ctr(des)",
  415. .cra_driver_name = "ctr-des-s390",
  416. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  417. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  418. .cra_blocksize = 1,
  419. .cra_ctxsize = sizeof(struct s390_des_ctx),
  420. .cra_type = &crypto_blkcipher_type,
  421. .cra_module = THIS_MODULE,
  422. .cra_u = {
  423. .blkcipher = {
  424. .min_keysize = DES_KEY_SIZE,
  425. .max_keysize = DES_KEY_SIZE,
  426. .ivsize = DES_BLOCK_SIZE,
  427. .setkey = des_setkey,
  428. .encrypt = ctr_des_encrypt,
  429. .decrypt = ctr_des_decrypt,
  430. }
  431. }
  432. };
  433. static int ctr_des3_encrypt(struct blkcipher_desc *desc,
  434. struct scatterlist *dst, struct scatterlist *src,
  435. unsigned int nbytes)
  436. {
  437. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  438. struct blkcipher_walk walk;
  439. blkcipher_walk_init(&walk, dst, src, nbytes);
  440. return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
  441. }
  442. static int ctr_des3_decrypt(struct blkcipher_desc *desc,
  443. struct scatterlist *dst, struct scatterlist *src,
  444. unsigned int nbytes)
  445. {
  446. struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  447. struct blkcipher_walk walk;
  448. blkcipher_walk_init(&walk, dst, src, nbytes);
  449. return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
  450. }
  451. static struct crypto_alg ctr_des3_alg = {
  452. .cra_name = "ctr(des3_ede)",
  453. .cra_driver_name = "ctr-des3_ede-s390",
  454. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  455. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  456. .cra_blocksize = 1,
  457. .cra_ctxsize = sizeof(struct s390_des_ctx),
  458. .cra_type = &crypto_blkcipher_type,
  459. .cra_module = THIS_MODULE,
  460. .cra_u = {
  461. .blkcipher = {
  462. .min_keysize = DES3_KEY_SIZE,
  463. .max_keysize = DES3_KEY_SIZE,
  464. .ivsize = DES_BLOCK_SIZE,
  465. .setkey = des3_setkey,
  466. .encrypt = ctr_des3_encrypt,
  467. .decrypt = ctr_des3_decrypt,
  468. }
  469. }
  470. };
  471. static int __init des_s390_init(void)
  472. {
  473. int ret;
  474. if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
  475. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
  476. return -EOPNOTSUPP;
  477. ret = crypto_register_alg(&des_alg);
  478. if (ret)
  479. goto des_err;
  480. ret = crypto_register_alg(&ecb_des_alg);
  481. if (ret)
  482. goto ecb_des_err;
  483. ret = crypto_register_alg(&cbc_des_alg);
  484. if (ret)
  485. goto cbc_des_err;
  486. ret = crypto_register_alg(&des3_alg);
  487. if (ret)
  488. goto des3_err;
  489. ret = crypto_register_alg(&ecb_des3_alg);
  490. if (ret)
  491. goto ecb_des3_err;
  492. ret = crypto_register_alg(&cbc_des3_alg);
  493. if (ret)
  494. goto cbc_des3_err;
  495. if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT,
  496. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  497. crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT,
  498. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  499. ret = crypto_register_alg(&ctr_des_alg);
  500. if (ret)
  501. goto ctr_des_err;
  502. ret = crypto_register_alg(&ctr_des3_alg);
  503. if (ret)
  504. goto ctr_des3_err;
  505. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  506. if (!ctrblk) {
  507. ret = -ENOMEM;
  508. goto ctr_mem_err;
  509. }
  510. }
  511. out:
  512. return ret;
  513. ctr_mem_err:
  514. crypto_unregister_alg(&ctr_des3_alg);
  515. ctr_des3_err:
  516. crypto_unregister_alg(&ctr_des_alg);
  517. ctr_des_err:
  518. crypto_unregister_alg(&cbc_des3_alg);
  519. cbc_des3_err:
  520. crypto_unregister_alg(&ecb_des3_alg);
  521. ecb_des3_err:
  522. crypto_unregister_alg(&des3_alg);
  523. des3_err:
  524. crypto_unregister_alg(&cbc_des_alg);
  525. cbc_des_err:
  526. crypto_unregister_alg(&ecb_des_alg);
  527. ecb_des_err:
  528. crypto_unregister_alg(&des_alg);
  529. des_err:
  530. goto out;
  531. }
  532. static void __exit des_s390_exit(void)
  533. {
  534. if (ctrblk) {
  535. crypto_unregister_alg(&ctr_des_alg);
  536. crypto_unregister_alg(&ctr_des3_alg);
  537. free_page((unsigned long) ctrblk);
  538. }
  539. crypto_unregister_alg(&cbc_des3_alg);
  540. crypto_unregister_alg(&ecb_des3_alg);
  541. crypto_unregister_alg(&des3_alg);
  542. crypto_unregister_alg(&cbc_des_alg);
  543. crypto_unregister_alg(&ecb_des_alg);
  544. crypto_unregister_alg(&des_alg);
  545. }
  546. module_cpu_feature_match(MSA, des_s390_init);
  547. module_exit(des_s390_exit);
  548. MODULE_ALIAS_CRYPTO("des");
  549. MODULE_ALIAS_CRYPTO("des3_ede");
  550. MODULE_LICENSE("GPL");
  551. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");