des_s390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the DES Cipher Algorithm.
  5. *
  6. * Copyright IBM Corp. 2003,2007
  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_192_KEY_SIZE (3 * DES_KEY_SIZE)
  23. struct crypt_s390_des_ctx {
  24. u8 iv[DES_BLOCK_SIZE];
  25. u8 key[DES_KEY_SIZE];
  26. };
  27. struct crypt_s390_des3_192_ctx {
  28. u8 iv[DES_BLOCK_SIZE];
  29. u8 key[DES3_192_KEY_SIZE];
  30. };
  31. static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
  32. unsigned int keylen)
  33. {
  34. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  35. u32 *flags = &tfm->crt_flags;
  36. u32 tmp[DES_EXPKEY_WORDS];
  37. /* check for weak keys */
  38. if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  39. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  40. return -EINVAL;
  41. }
  42. memcpy(dctx->key, key, keylen);
  43. return 0;
  44. }
  45. static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  46. {
  47. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  48. crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  49. }
  50. static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  51. {
  52. struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
  53. crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
  54. }
  55. static struct crypto_alg des_alg = {
  56. .cra_name = "des",
  57. .cra_driver_name = "des-s390",
  58. .cra_priority = CRYPT_S390_PRIORITY,
  59. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  60. .cra_blocksize = DES_BLOCK_SIZE,
  61. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  62. .cra_module = THIS_MODULE,
  63. .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
  64. .cra_u = {
  65. .cipher = {
  66. .cia_min_keysize = DES_KEY_SIZE,
  67. .cia_max_keysize = DES_KEY_SIZE,
  68. .cia_setkey = des_setkey,
  69. .cia_encrypt = des_encrypt,
  70. .cia_decrypt = des_decrypt,
  71. }
  72. }
  73. };
  74. static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
  75. void *param, struct blkcipher_walk *walk)
  76. {
  77. int ret = blkcipher_walk_virt(desc, walk);
  78. unsigned int nbytes;
  79. while ((nbytes = walk->nbytes)) {
  80. /* only use complete blocks */
  81. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  82. u8 *out = walk->dst.virt.addr;
  83. u8 *in = walk->src.virt.addr;
  84. ret = crypt_s390_km(func, param, out, in, n);
  85. BUG_ON((ret < 0) || (ret != n));
  86. nbytes &= DES_BLOCK_SIZE - 1;
  87. ret = blkcipher_walk_done(desc, walk, nbytes);
  88. }
  89. return ret;
  90. }
  91. static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
  92. void *param, struct blkcipher_walk *walk)
  93. {
  94. int ret = blkcipher_walk_virt(desc, walk);
  95. unsigned int nbytes = walk->nbytes;
  96. if (!nbytes)
  97. goto out;
  98. memcpy(param, walk->iv, DES_BLOCK_SIZE);
  99. do {
  100. /* only use complete blocks */
  101. unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
  102. u8 *out = walk->dst.virt.addr;
  103. u8 *in = walk->src.virt.addr;
  104. ret = crypt_s390_kmc(func, param, out, in, n);
  105. BUG_ON((ret < 0) || (ret != n));
  106. nbytes &= DES_BLOCK_SIZE - 1;
  107. ret = blkcipher_walk_done(desc, walk, nbytes);
  108. } while ((nbytes = walk->nbytes));
  109. memcpy(walk->iv, param, DES_BLOCK_SIZE);
  110. out:
  111. return ret;
  112. }
  113. static int ecb_des_encrypt(struct blkcipher_desc *desc,
  114. struct scatterlist *dst, struct scatterlist *src,
  115. unsigned int nbytes)
  116. {
  117. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  118. struct blkcipher_walk walk;
  119. blkcipher_walk_init(&walk, dst, src, nbytes);
  120. return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk);
  121. }
  122. static int ecb_des_decrypt(struct blkcipher_desc *desc,
  123. struct scatterlist *dst, struct scatterlist *src,
  124. unsigned int nbytes)
  125. {
  126. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  127. struct blkcipher_walk walk;
  128. blkcipher_walk_init(&walk, dst, src, nbytes);
  129. return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk);
  130. }
  131. static struct crypto_alg ecb_des_alg = {
  132. .cra_name = "ecb(des)",
  133. .cra_driver_name = "ecb-des-s390",
  134. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  135. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  136. .cra_blocksize = DES_BLOCK_SIZE,
  137. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  138. .cra_type = &crypto_blkcipher_type,
  139. .cra_module = THIS_MODULE,
  140. .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
  141. .cra_u = {
  142. .blkcipher = {
  143. .min_keysize = DES_KEY_SIZE,
  144. .max_keysize = DES_KEY_SIZE,
  145. .setkey = des_setkey,
  146. .encrypt = ecb_des_encrypt,
  147. .decrypt = ecb_des_decrypt,
  148. }
  149. }
  150. };
  151. static int cbc_des_encrypt(struct blkcipher_desc *desc,
  152. struct scatterlist *dst, struct scatterlist *src,
  153. unsigned int nbytes)
  154. {
  155. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  156. struct blkcipher_walk walk;
  157. blkcipher_walk_init(&walk, dst, src, nbytes);
  158. return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk);
  159. }
  160. static int cbc_des_decrypt(struct blkcipher_desc *desc,
  161. struct scatterlist *dst, struct scatterlist *src,
  162. unsigned int nbytes)
  163. {
  164. struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  165. struct blkcipher_walk walk;
  166. blkcipher_walk_init(&walk, dst, src, nbytes);
  167. return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk);
  168. }
  169. static struct crypto_alg cbc_des_alg = {
  170. .cra_name = "cbc(des)",
  171. .cra_driver_name = "cbc-des-s390",
  172. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  173. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  174. .cra_blocksize = DES_BLOCK_SIZE,
  175. .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
  176. .cra_type = &crypto_blkcipher_type,
  177. .cra_module = THIS_MODULE,
  178. .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
  179. .cra_u = {
  180. .blkcipher = {
  181. .min_keysize = DES_KEY_SIZE,
  182. .max_keysize = DES_KEY_SIZE,
  183. .ivsize = DES_BLOCK_SIZE,
  184. .setkey = des_setkey,
  185. .encrypt = cbc_des_encrypt,
  186. .decrypt = cbc_des_decrypt,
  187. }
  188. }
  189. };
  190. /*
  191. * RFC2451:
  192. *
  193. * For DES-EDE3, there is no known need to reject weak or
  194. * complementation keys. Any weakness is obviated by the use of
  195. * multiple keys.
  196. *
  197. * However, if the first two or last two independent 64-bit keys are
  198. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  199. * same as DES. Implementers MUST reject keys that exhibit this
  200. * property.
  201. *
  202. */
  203. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  204. unsigned int keylen)
  205. {
  206. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  207. u32 *flags = &tfm->crt_flags;
  208. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  209. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  210. DES_KEY_SIZE)) &&
  211. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  212. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  213. return -EINVAL;
  214. }
  215. memcpy(dctx->key, key, keylen);
  216. return 0;
  217. }
  218. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  219. {
  220. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  221. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  222. DES_BLOCK_SIZE);
  223. }
  224. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  225. {
  226. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  227. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  228. DES_BLOCK_SIZE);
  229. }
  230. static struct crypto_alg des3_192_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 crypt_s390_des3_192_ctx),
  237. .cra_module = THIS_MODULE,
  238. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  239. .cra_u = {
  240. .cipher = {
  241. .cia_min_keysize = DES3_192_KEY_SIZE,
  242. .cia_max_keysize = DES3_192_KEY_SIZE,
  243. .cia_setkey = des3_192_setkey,
  244. .cia_encrypt = des3_192_encrypt,
  245. .cia_decrypt = des3_192_decrypt,
  246. }
  247. }
  248. };
  249. static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
  250. struct scatterlist *dst,
  251. struct scatterlist *src, unsigned int nbytes)
  252. {
  253. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  254. struct blkcipher_walk walk;
  255. blkcipher_walk_init(&walk, dst, src, nbytes);
  256. return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
  257. }
  258. static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
  259. struct scatterlist *dst,
  260. struct scatterlist *src, unsigned int nbytes)
  261. {
  262. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  263. struct blkcipher_walk walk;
  264. blkcipher_walk_init(&walk, dst, src, nbytes);
  265. return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
  266. }
  267. static struct crypto_alg ecb_des3_192_alg = {
  268. .cra_name = "ecb(des3_ede)",
  269. .cra_driver_name = "ecb-des3_ede-s390",
  270. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  271. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  272. .cra_blocksize = DES_BLOCK_SIZE,
  273. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  274. .cra_type = &crypto_blkcipher_type,
  275. .cra_module = THIS_MODULE,
  276. .cra_list = LIST_HEAD_INIT(
  277. ecb_des3_192_alg.cra_list),
  278. .cra_u = {
  279. .blkcipher = {
  280. .min_keysize = DES3_192_KEY_SIZE,
  281. .max_keysize = DES3_192_KEY_SIZE,
  282. .setkey = des3_192_setkey,
  283. .encrypt = ecb_des3_192_encrypt,
  284. .decrypt = ecb_des3_192_decrypt,
  285. }
  286. }
  287. };
  288. static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
  289. struct scatterlist *dst,
  290. struct scatterlist *src, unsigned int nbytes)
  291. {
  292. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  293. struct blkcipher_walk walk;
  294. blkcipher_walk_init(&walk, dst, src, nbytes);
  295. return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
  296. }
  297. static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
  298. struct scatterlist *dst,
  299. struct scatterlist *src, unsigned int nbytes)
  300. {
  301. struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  302. struct blkcipher_walk walk;
  303. blkcipher_walk_init(&walk, dst, src, nbytes);
  304. return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
  305. }
  306. static struct crypto_alg cbc_des3_192_alg = {
  307. .cra_name = "cbc(des3_ede)",
  308. .cra_driver_name = "cbc-des3_ede-s390",
  309. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  310. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  311. .cra_blocksize = DES_BLOCK_SIZE,
  312. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  313. .cra_type = &crypto_blkcipher_type,
  314. .cra_module = THIS_MODULE,
  315. .cra_list = LIST_HEAD_INIT(
  316. cbc_des3_192_alg.cra_list),
  317. .cra_u = {
  318. .blkcipher = {
  319. .min_keysize = DES3_192_KEY_SIZE,
  320. .max_keysize = DES3_192_KEY_SIZE,
  321. .ivsize = DES_BLOCK_SIZE,
  322. .setkey = des3_192_setkey,
  323. .encrypt = cbc_des3_192_encrypt,
  324. .decrypt = cbc_des3_192_decrypt,
  325. }
  326. }
  327. };
  328. static int des_s390_init(void)
  329. {
  330. int ret;
  331. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  332. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  333. return -EOPNOTSUPP;
  334. ret = crypto_register_alg(&des_alg);
  335. if (ret)
  336. goto des_err;
  337. ret = crypto_register_alg(&ecb_des_alg);
  338. if (ret)
  339. goto ecb_des_err;
  340. ret = crypto_register_alg(&cbc_des_alg);
  341. if (ret)
  342. goto cbc_des_err;
  343. ret = crypto_register_alg(&des3_192_alg);
  344. if (ret)
  345. goto des3_192_err;
  346. ret = crypto_register_alg(&ecb_des3_192_alg);
  347. if (ret)
  348. goto ecb_des3_192_err;
  349. ret = crypto_register_alg(&cbc_des3_192_alg);
  350. if (ret)
  351. goto cbc_des3_192_err;
  352. out:
  353. return ret;
  354. cbc_des3_192_err:
  355. crypto_unregister_alg(&ecb_des3_192_alg);
  356. ecb_des3_192_err:
  357. crypto_unregister_alg(&des3_192_alg);
  358. des3_192_err:
  359. crypto_unregister_alg(&cbc_des_alg);
  360. cbc_des_err:
  361. crypto_unregister_alg(&ecb_des_alg);
  362. ecb_des_err:
  363. crypto_unregister_alg(&des_alg);
  364. des_err:
  365. goto out;
  366. }
  367. static void __exit des_s390_exit(void)
  368. {
  369. crypto_unregister_alg(&cbc_des3_192_alg);
  370. crypto_unregister_alg(&ecb_des3_192_alg);
  371. crypto_unregister_alg(&des3_192_alg);
  372. crypto_unregister_alg(&cbc_des_alg);
  373. crypto_unregister_alg(&ecb_des_alg);
  374. crypto_unregister_alg(&des_alg);
  375. }
  376. module_init(des_s390_init);
  377. module_exit(des_s390_exit);
  378. MODULE_ALIAS("des");
  379. MODULE_ALIAS("des3_ede");
  380. MODULE_LICENSE("GPL");
  381. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");