des_s390.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the DES Cipher Algorithm.
  5. *
  6. * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Thomas Spatzier (tspat@de.ibm.com)
  8. *
  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 <crypto/algapi.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include "crypt_s390.h"
  20. #include "crypto_des.h"
  21. #define DES_BLOCK_SIZE 8
  22. #define DES_KEY_SIZE 8
  23. #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
  24. #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
  25. #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
  26. #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
  27. struct crypt_s390_des_ctx {
  28. u8 iv[DES_BLOCK_SIZE];
  29. u8 key[DES_KEY_SIZE];
  30. };
  31. struct crypt_s390_des3_128_ctx {
  32. u8 iv[DES_BLOCK_SIZE];
  33. u8 key[DES3_128_KEY_SIZE];
  34. };
  35. struct crypt_s390_des3_192_ctx {
  36. u8 iv[DES_BLOCK_SIZE];
  37. u8 key[DES3_192_KEY_SIZE];
  38. };
  39. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  40. unsigned int keylen)
  41. {
  42. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  43. u32 *flags = &tfm->crt_flags;
  44. int ret;
  45. /* test if key is valid (not a weak key) */
  46. ret = crypto_des_check_key(key, keylen, flags);
  47. if (ret == 0)
  48. memcpy(dctx->key, key, keylen);
  49. return ret;
  50. }
  51. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  52. {
  53. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  54. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  55. }
  56. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  57. {
  58. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  59. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  60. }
  61. static unsigned int des_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
  62. const u8 *in, unsigned int nbytes)
  63. {
  64. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  65. int ret;
  66. /* only use complete blocks */
  67. nbytes &= ~(DES_BLOCK_SIZE - 1);
  68. ret = crypt_s390_km(KM_DEA_ENCRYPT, sctx->key, out, in, nbytes);
  69. BUG_ON((ret < 0) || (ret != nbytes));
  70. return nbytes;
  71. }
  72. static unsigned int des_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
  73. const u8 *in, unsigned int nbytes)
  74. {
  75. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  76. int ret;
  77. /* only use complete blocks */
  78. nbytes &= ~(DES_BLOCK_SIZE - 1);
  79. ret = crypt_s390_km(KM_DEA_DECRYPT, sctx->key, out, in, nbytes);
  80. BUG_ON((ret < 0) || (ret != nbytes));
  81. return nbytes;
  82. }
  83. static unsigned int des_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
  84. const u8 *in, unsigned int nbytes)
  85. {
  86. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  87. int ret;
  88. /* only use complete blocks */
  89. nbytes &= ~(DES_BLOCK_SIZE - 1);
  90. memcpy(sctx->iv, desc->info, DES_BLOCK_SIZE);
  91. ret = crypt_s390_kmc(KMC_DEA_ENCRYPT, &sctx->iv, out, in, nbytes);
  92. BUG_ON((ret < 0) || (ret != nbytes));
  93. memcpy(desc->info, sctx->iv, DES_BLOCK_SIZE);
  94. return nbytes;
  95. }
  96. static unsigned int des_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
  97. const u8 *in, unsigned int nbytes)
  98. {
  99. struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  100. int ret;
  101. /* only use complete blocks */
  102. nbytes &= ~(DES_BLOCK_SIZE - 1);
  103. memcpy(&sctx->iv, desc->info, DES_BLOCK_SIZE);
  104. ret = crypt_s390_kmc(KMC_DEA_DECRYPT, &sctx->iv, out, in, nbytes);
  105. BUG_ON((ret < 0) || (ret != nbytes));
  106. return nbytes;
  107. }
  108. static struct crypto_alg des_alg = {
  109. .cra_name = "des",
  110. .cra_driver_name = "des-s390",
  111. .cra_priority = CRYPT_S390_PRIORITY,
  112. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  113. .cra_blocksize = DES_BLOCK_SIZE,
  114. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  115. .cra_module = THIS_MODULE,
  116. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  117. .cra_u = {
  118. .cipher = {
  119. .cia_min_keysize = DES_KEY_SIZE,
  120. .cia_max_keysize = DES_KEY_SIZE,
  121. .cia_setkey = des_setkey,
  122. .cia_encrypt = des_encrypt,
  123. .cia_decrypt = des_decrypt,
  124. .cia_encrypt_ecb = des_encrypt_ecb,
  125. .cia_decrypt_ecb = des_decrypt_ecb,
  126. .cia_encrypt_cbc = des_encrypt_cbc,
  127. .cia_decrypt_cbc = des_decrypt_cbc,
  128. }
  129. }
  130. };
  131. static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
  132. void *param, struct blkcipher_walk *walk)
  133. {
  134. int ret = blkcipher_walk_virt(desc, walk);
  135. unsigned int nbytes;
  136. while ((nbytes = walk->nbytes)) {
  137. /* only use complete blocks */
  138. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  139. u8 *out = walk->dst.virt.addr;
  140. u8 *in = walk->src.virt.addr;
  141. ret = crypt_s390_km(func, param, out, in, n);
  142. BUG_ON((ret < 0) || (ret != n));
  143. nbytes &= DES_BLOCK_SIZE - 1;
  144. ret = blkcipher_walk_done(desc, walk, nbytes);
  145. }
  146. return ret;
  147. }
  148. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  149. void *param, struct blkcipher_walk *walk)
  150. {
  151. int ret = blkcipher_walk_virt(desc, walk);
  152. unsigned int nbytes = walk->nbytes;
  153. if (!nbytes)
  154. goto out;
  155. memcpy(param, walk->iv, DES_BLOCK_SIZE);
  156. do {
  157. /* only use complete blocks */
  158. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  159. u8 *out = walk->dst.virt.addr;
  160. u8 *in = walk->src.virt.addr;
  161. ret = crypt_s390_kmc(func, param, out, in, n);
  162. BUG_ON((ret < 0) || (ret != n));
  163. nbytes &= DES_BLOCK_SIZE - 1;
  164. ret = blkcipher_walk_done(desc, walk, nbytes);
  165. } while ((nbytes = walk->nbytes));
  166. memcpy(walk->iv, param, DES_BLOCK_SIZE);
  167. out:
  168. return ret;
  169. }
  170. static int ecb_des_encrypt(struct blkcipher_desc *desc,
  171. struct scatterlist *dst, struct scatterlist *src,
  172. unsigned int nbytes)
  173. {
  174. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  175. struct blkcipher_walk walk;
  176. blkcipher_walk_init(&walk, dst, src, nbytes);
  177. return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
  178. }
  179. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  180. struct scatterlist *dst, struct scatterlist *src,
  181. unsigned int nbytes)
  182. {
  183. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  184. struct blkcipher_walk walk;
  185. blkcipher_walk_init(&walk, dst, src, nbytes);
  186. return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
  187. }
  188. static struct crypto_alg ecb_des_alg = {
  189. .cra_name = "ecb(des)",
  190. .cra_driver_name = "ecb-des-s390",
  191. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  192. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  193. .cra_blocksize = DES_BLOCK_SIZE,
  194. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  195. .cra_type = &crypto_blkcipher_type,
  196. .cra_module = THIS_MODULE,
  197. .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
  198. .cra_u = {
  199. .blkcipher = {
  200. .min_keysize = DES_KEY_SIZE,
  201. .max_keysize = DES_KEY_SIZE,
  202. .setkey = des_setkey,
  203. .encrypt = ecb_des_encrypt,
  204. .decrypt = ecb_des_decrypt,
  205. }
  206. }
  207. };
  208. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  209. struct scatterlist *dst, struct scatterlist *src,
  210. unsigned int nbytes)
  211. {
  212. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  213. struct blkcipher_walk walk;
  214. blkcipher_walk_init(&walk, dst, src, nbytes);
  215. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
  216. }
  217. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  218. struct scatterlist *dst, struct scatterlist *src,
  219. unsigned int nbytes)
  220. {
  221. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  222. struct blkcipher_walk walk;
  223. blkcipher_walk_init(&walk, dst, src, nbytes);
  224. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
  225. }
  226. static struct crypto_alg cbc_des_alg = {
  227. .cra_name = "cbc(des)",
  228. .cra_driver_name = "cbc-des-s390",
  229. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  230. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  231. .cra_blocksize = DES_BLOCK_SIZE,
  232. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  233. .cra_type = &crypto_blkcipher_type,
  234. .cra_module = THIS_MODULE,
  235. .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
  236. .cra_u = {
  237. .blkcipher = {
  238. .min_keysize = DES_KEY_SIZE,
  239. .max_keysize = DES_KEY_SIZE,
  240. .ivsize = DES_BLOCK_SIZE,
  241. .setkey = des_setkey,
  242. .encrypt = cbc_des_encrypt,
  243. .decrypt = cbc_des_decrypt,
  244. }
  245. }
  246. };
  247. /*
  248. * RFC2451:
  249. *
  250. * For DES-EDE3, there is no known need to reject weak or
  251. * complementation keys. Any weakness is obviated by the use of
  252. * multiple keys.
  253. *
  254. * However, if the two independent 64-bit keys are equal,
  255. * then the DES3 operation is simply the same as DES.
  256. * Implementers MUST reject keys that exhibit this property.
  257. *
  258. */
  259. static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
  260. unsigned int keylen)
  261. {
  262. int i, ret;
  263. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  264. const u8 *temp_key = key;
  265. u32 *flags = &tfm->crt_flags;
  266. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
  267. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  268. return -EINVAL;
  269. }
  270. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  271. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  272. if (ret < 0)
  273. return ret;
  274. }
  275. memcpy(dctx->key, key, keylen);
  276. return 0;
  277. }
  278. static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  279. {
  280. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  281. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  282. DES3_128_BLOCK_SIZE);
  283. }
  284. static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  285. {
  286. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  287. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  288. DES3_128_BLOCK_SIZE);
  289. }
  290. static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc,
  291. u8 *out, const u8 *in,
  292. unsigned int nbytes)
  293. {
  294. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  295. int ret;
  296. /* only use complete blocks */
  297. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  298. ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes);
  299. BUG_ON((ret < 0) || (ret != nbytes));
  300. return nbytes;
  301. }
  302. static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc,
  303. u8 *out, const u8 *in,
  304. unsigned int nbytes)
  305. {
  306. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  307. int ret;
  308. /* only use complete blocks */
  309. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  310. ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes);
  311. BUG_ON((ret < 0) || (ret != nbytes));
  312. return nbytes;
  313. }
  314. static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc,
  315. u8 *out, const u8 *in,
  316. unsigned int nbytes)
  317. {
  318. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  319. int ret;
  320. /* only use complete blocks */
  321. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  322. memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  323. ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes);
  324. BUG_ON((ret < 0) || (ret != nbytes));
  325. memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE);
  326. return nbytes;
  327. }
  328. static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc,
  329. u8 *out, const u8 *in,
  330. unsigned int nbytes)
  331. {
  332. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  333. int ret;
  334. /* only use complete blocks */
  335. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  336. memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  337. ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes);
  338. BUG_ON((ret < 0) || (ret != nbytes));
  339. return nbytes;
  340. }
  341. static struct crypto_alg des3_128_alg = {
  342. .cra_name = "des3_ede128",
  343. .cra_driver_name = "des3_ede128-s390",
  344. .cra_priority = CRYPT_S390_PRIORITY,
  345. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  346. .cra_blocksize = DES3_128_BLOCK_SIZE,
  347. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  348. .cra_module = THIS_MODULE,
  349. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  350. .cra_u = {
  351. .cipher = {
  352. .cia_min_keysize = DES3_128_KEY_SIZE,
  353. .cia_max_keysize = DES3_128_KEY_SIZE,
  354. .cia_setkey = des3_128_setkey,
  355. .cia_encrypt = des3_128_encrypt,
  356. .cia_decrypt = des3_128_decrypt,
  357. .cia_encrypt_ecb = des3_128_encrypt_ecb,
  358. .cia_decrypt_ecb = des3_128_decrypt_ecb,
  359. .cia_encrypt_cbc = des3_128_encrypt_cbc,
  360. .cia_decrypt_cbc = des3_128_decrypt_cbc,
  361. }
  362. }
  363. };
  364. static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
  365. struct scatterlist *dst,
  366. struct scatterlist *src, unsigned int nbytes)
  367. {
  368. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  369. struct blkcipher_walk walk;
  370. blkcipher_walk_init(&walk, dst, src, nbytes);
  371. return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
  372. }
  373. static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
  374. struct scatterlist *dst,
  375. struct scatterlist *src, unsigned int nbytes)
  376. {
  377. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  378. struct blkcipher_walk walk;
  379. blkcipher_walk_init(&walk, dst, src, nbytes);
  380. return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
  381. }
  382. static struct crypto_alg ecb_des3_128_alg = {
  383. .cra_name = "ecb(des3_ede128)",
  384. .cra_driver_name = "ecb-des3_ede128-s390",
  385. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  386. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  387. .cra_blocksize = DES3_128_BLOCK_SIZE,
  388. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  389. .cra_type = &crypto_blkcipher_type,
  390. .cra_module = THIS_MODULE,
  391. .cra_list = LIST_HEAD_INIT(
  392. ecb_des3_128_alg.cra_list),
  393. .cra_u = {
  394. .blkcipher = {
  395. .min_keysize = DES3_128_KEY_SIZE,
  396. .max_keysize = DES3_128_KEY_SIZE,
  397. .setkey = des3_128_setkey,
  398. .encrypt = ecb_des3_128_encrypt,
  399. .decrypt = ecb_des3_128_decrypt,
  400. }
  401. }
  402. };
  403. static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
  404. struct scatterlist *dst,
  405. struct scatterlist *src, unsigned int nbytes)
  406. {
  407. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  408. struct blkcipher_walk walk;
  409. blkcipher_walk_init(&walk, dst, src, nbytes);
  410. return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
  411. }
  412. static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
  413. struct scatterlist *dst,
  414. struct scatterlist *src, unsigned int nbytes)
  415. {
  416. struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  417. struct blkcipher_walk walk;
  418. blkcipher_walk_init(&walk, dst, src, nbytes);
  419. return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
  420. }
  421. static struct crypto_alg cbc_des3_128_alg = {
  422. .cra_name = "cbc(des3_ede128)",
  423. .cra_driver_name = "cbc-des3_ede128-s390",
  424. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  425. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  426. .cra_blocksize = DES3_128_BLOCK_SIZE,
  427. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  428. .cra_type = &crypto_blkcipher_type,
  429. .cra_module = THIS_MODULE,
  430. .cra_list = LIST_HEAD_INIT(
  431. cbc_des3_128_alg.cra_list),
  432. .cra_u = {
  433. .blkcipher = {
  434. .min_keysize = DES3_128_KEY_SIZE,
  435. .max_keysize = DES3_128_KEY_SIZE,
  436. .ivsize = DES3_128_BLOCK_SIZE,
  437. .setkey = des3_128_setkey,
  438. .encrypt = cbc_des3_128_encrypt,
  439. .decrypt = cbc_des3_128_decrypt,
  440. }
  441. }
  442. };
  443. /*
  444. * RFC2451:
  445. *
  446. * For DES-EDE3, there is no known need to reject weak or
  447. * complementation keys. Any weakness is obviated by the use of
  448. * multiple keys.
  449. *
  450. * However, if the first two or last two independent 64-bit keys are
  451. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  452. * same as DES. Implementers MUST reject keys that exhibit this
  453. * property.
  454. *
  455. */
  456. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  457. unsigned int keylen)
  458. {
  459. int i, ret;
  460. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  461. const u8 *temp_key = key;
  462. u32 *flags = &tfm->crt_flags;
  463. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  464. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  465. DES_KEY_SIZE))) {
  466. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  467. return -EINVAL;
  468. }
  469. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  470. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  471. if (ret < 0)
  472. return ret;
  473. }
  474. memcpy(dctx->key, key, keylen);
  475. return 0;
  476. }
  477. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  478. {
  479. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  480. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  481. DES3_192_BLOCK_SIZE);
  482. }
  483. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  484. {
  485. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  486. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  487. DES3_192_BLOCK_SIZE);
  488. }
  489. static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc,
  490. u8 *out, const u8 *in,
  491. unsigned int nbytes)
  492. {
  493. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  494. int ret;
  495. /* only use complete blocks */
  496. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  497. ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes);
  498. BUG_ON((ret < 0) || (ret != nbytes));
  499. return nbytes;
  500. }
  501. static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc,
  502. u8 *out, const u8 *in,
  503. unsigned int nbytes)
  504. {
  505. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  506. int ret;
  507. /* only use complete blocks */
  508. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  509. ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes);
  510. BUG_ON((ret < 0) || (ret != nbytes));
  511. return nbytes;
  512. }
  513. static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc,
  514. u8 *out, const u8 *in,
  515. unsigned int nbytes)
  516. {
  517. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  518. int ret;
  519. /* only use complete blocks */
  520. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  521. memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  522. ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes);
  523. BUG_ON((ret < 0) || (ret != nbytes));
  524. memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE);
  525. return nbytes;
  526. }
  527. static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc,
  528. u8 *out, const u8 *in,
  529. unsigned int nbytes)
  530. {
  531. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  532. int ret;
  533. /* only use complete blocks */
  534. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  535. memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  536. ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes);
  537. BUG_ON((ret < 0) || (ret != nbytes));
  538. return nbytes;
  539. }
  540. static struct crypto_alg des3_192_alg = {
  541. .cra_name = "des3_ede",
  542. .cra_driver_name = "des3_ede-s390",
  543. .cra_priority = CRYPT_S390_PRIORITY,
  544. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  545. .cra_blocksize = DES3_192_BLOCK_SIZE,
  546. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  547. .cra_module = THIS_MODULE,
  548. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  549. .cra_u = {
  550. .cipher = {
  551. .cia_min_keysize = DES3_192_KEY_SIZE,
  552. .cia_max_keysize = DES3_192_KEY_SIZE,
  553. .cia_setkey = des3_192_setkey,
  554. .cia_encrypt = des3_192_encrypt,
  555. .cia_decrypt = des3_192_decrypt,
  556. .cia_encrypt_ecb = des3_192_encrypt_ecb,
  557. .cia_decrypt_ecb = des3_192_decrypt_ecb,
  558. .cia_encrypt_cbc = des3_192_encrypt_cbc,
  559. .cia_decrypt_cbc = des3_192_decrypt_cbc,
  560. }
  561. }
  562. };
  563. static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
  564. struct scatterlist *dst,
  565. struct scatterlist *src, unsigned int nbytes)
  566. {
  567. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  568. struct blkcipher_walk walk;
  569. blkcipher_walk_init(&walk, dst, src, nbytes);
  570. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
  571. }
  572. static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
  573. struct scatterlist *dst,
  574. struct scatterlist *src, unsigned int nbytes)
  575. {
  576. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  577. struct blkcipher_walk walk;
  578. blkcipher_walk_init(&walk, dst, src, nbytes);
  579. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
  580. }
  581. static struct crypto_alg ecb_des3_192_alg = {
  582. .cra_name = "ecb(des3_ede)",
  583. .cra_driver_name = "ecb-des3_ede-s390",
  584. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  585. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  586. .cra_blocksize = DES3_192_BLOCK_SIZE,
  587. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  588. .cra_type = &crypto_blkcipher_type,
  589. .cra_module = THIS_MODULE,
  590. .cra_list = LIST_HEAD_INIT(
  591. ecb_des3_192_alg.cra_list),
  592. .cra_u = {
  593. .blkcipher = {
  594. .min_keysize = DES3_192_KEY_SIZE,
  595. .max_keysize = DES3_192_KEY_SIZE,
  596. .setkey = des3_192_setkey,
  597. .encrypt = ecb_des3_192_encrypt,
  598. .decrypt = ecb_des3_192_decrypt,
  599. }
  600. }
  601. };
  602. static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
  603. struct scatterlist *dst,
  604. struct scatterlist *src, unsigned int nbytes)
  605. {
  606. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  607. struct blkcipher_walk walk;
  608. blkcipher_walk_init(&walk, dst, src, nbytes);
  609. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
  610. }
  611. static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
  612. struct scatterlist *dst,
  613. struct scatterlist *src, unsigned int nbytes)
  614. {
  615. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  616. struct blkcipher_walk walk;
  617. blkcipher_walk_init(&walk, dst, src, nbytes);
  618. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
  619. }
  620. static struct crypto_alg cbc_des3_192_alg = {
  621. .cra_name = "cbc(des3_ede)",
  622. .cra_driver_name = "cbc-des3_ede-s390",
  623. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  624. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  625. .cra_blocksize = DES3_192_BLOCK_SIZE,
  626. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  627. .cra_type = &crypto_blkcipher_type,
  628. .cra_module = THIS_MODULE,
  629. .cra_list = LIST_HEAD_INIT(
  630. cbc_des3_192_alg.cra_list),
  631. .cra_u = {
  632. .blkcipher = {
  633. .min_keysize = DES3_192_KEY_SIZE,
  634. .max_keysize = DES3_192_KEY_SIZE,
  635. .ivsize = DES3_192_BLOCK_SIZE,
  636. .setkey = des3_192_setkey,
  637. .encrypt = cbc_des3_192_encrypt,
  638. .decrypt = cbc_des3_192_decrypt,
  639. }
  640. }
  641. };
  642. static int init(void)
  643. {
  644. int ret = 0;
  645. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  646. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  647. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  648. return -ENOSYS;
  649. ret = crypto_register_alg(&des_alg);
  650. if (ret)
  651. goto des_err;
  652. ret = crypto_register_alg(&ecb_des_alg);
  653. if (ret)
  654. goto ecb_des_err;
  655. ret = crypto_register_alg(&cbc_des_alg);
  656. if (ret)
  657. goto cbc_des_err;
  658. ret = crypto_register_alg(&des3_128_alg);
  659. if (ret)
  660. goto des3_128_err;
  661. ret = crypto_register_alg(&ecb_des3_128_alg);
  662. if (ret)
  663. goto ecb_des3_128_err;
  664. ret = crypto_register_alg(&cbc_des3_128_alg);
  665. if (ret)
  666. goto cbc_des3_128_err;
  667. ret = crypto_register_alg(&des3_192_alg);
  668. if (ret)
  669. goto des3_192_err;
  670. ret = crypto_register_alg(&ecb_des3_192_alg);
  671. if (ret)
  672. goto ecb_des3_192_err;
  673. ret = crypto_register_alg(&cbc_des3_192_alg);
  674. if (ret)
  675. goto cbc_des3_192_err;
  676. out:
  677. return ret;
  678. cbc_des3_192_err:
  679. crypto_unregister_alg(&ecb_des3_192_alg);
  680. ecb_des3_192_err:
  681. crypto_unregister_alg(&des3_192_alg);
  682. des3_192_err:
  683. crypto_unregister_alg(&cbc_des3_128_alg);
  684. cbc_des3_128_err:
  685. crypto_unregister_alg(&ecb_des3_128_alg);
  686. ecb_des3_128_err:
  687. crypto_unregister_alg(&des3_128_alg);
  688. des3_128_err:
  689. crypto_unregister_alg(&cbc_des_alg);
  690. cbc_des_err:
  691. crypto_unregister_alg(&ecb_des_alg);
  692. ecb_des_err:
  693. crypto_unregister_alg(&des_alg);
  694. des_err:
  695. goto out;
  696. }
  697. static void __exit fini(void)
  698. {
  699. crypto_unregister_alg(&cbc_des3_192_alg);
  700. crypto_unregister_alg(&ecb_des3_192_alg);
  701. crypto_unregister_alg(&des3_192_alg);
  702. crypto_unregister_alg(&cbc_des3_128_alg);
  703. crypto_unregister_alg(&ecb_des3_128_alg);
  704. crypto_unregister_alg(&des3_128_alg);
  705. crypto_unregister_alg(&cbc_des_alg);
  706. crypto_unregister_alg(&ecb_des_alg);
  707. crypto_unregister_alg(&des_alg);
  708. }
  709. module_init(init);
  710. module_exit(fini);
  711. MODULE_ALIAS("des");
  712. MODULE_ALIAS("des3_ede");
  713. MODULE_LICENSE("GPL");
  714. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");