des_s390.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/crypto.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. /*
  132. * RFC2451:
  133. *
  134. * For DES-EDE3, there is no known need to reject weak or
  135. * complementation keys. Any weakness is obviated by the use of
  136. * multiple keys.
  137. *
  138. * However, if the two independent 64-bit keys are equal,
  139. * then the DES3 operation is simply the same as DES.
  140. * Implementers MUST reject keys that exhibit this property.
  141. *
  142. */
  143. static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
  144. unsigned int keylen)
  145. {
  146. int i, ret;
  147. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  148. const u8 *temp_key = key;
  149. u32 *flags = &tfm->crt_flags;
  150. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
  151. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  152. return -EINVAL;
  153. }
  154. for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
  155. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  156. if (ret < 0)
  157. return ret;
  158. }
  159. memcpy(dctx->key, key, keylen);
  160. return 0;
  161. }
  162. static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  163. {
  164. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  165. crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
  166. DES3_128_BLOCK_SIZE);
  167. }
  168. static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  169. {
  170. struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
  171. crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
  172. DES3_128_BLOCK_SIZE);
  173. }
  174. static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc,
  175. u8 *out, const u8 *in,
  176. unsigned int nbytes)
  177. {
  178. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  179. int ret;
  180. /* only use complete blocks */
  181. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  182. ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes);
  183. BUG_ON((ret < 0) || (ret != nbytes));
  184. return nbytes;
  185. }
  186. static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc,
  187. u8 *out, const u8 *in,
  188. unsigned int nbytes)
  189. {
  190. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  191. int ret;
  192. /* only use complete blocks */
  193. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  194. ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes);
  195. BUG_ON((ret < 0) || (ret != nbytes));
  196. return nbytes;
  197. }
  198. static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc,
  199. u8 *out, const u8 *in,
  200. unsigned int nbytes)
  201. {
  202. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  203. int ret;
  204. /* only use complete blocks */
  205. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  206. memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  207. ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes);
  208. BUG_ON((ret < 0) || (ret != nbytes));
  209. memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE);
  210. return nbytes;
  211. }
  212. static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc,
  213. u8 *out, const u8 *in,
  214. unsigned int nbytes)
  215. {
  216. struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  217. int ret;
  218. /* only use complete blocks */
  219. nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
  220. memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
  221. ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes);
  222. BUG_ON((ret < 0) || (ret != nbytes));
  223. return nbytes;
  224. }
  225. static struct crypto_alg des3_128_alg = {
  226. .cra_name = "des3_ede128",
  227. .cra_driver_name = "des3_ede128-s390",
  228. .cra_priority = CRYPT_S390_PRIORITY,
  229. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  230. .cra_blocksize = DES3_128_BLOCK_SIZE,
  231. .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
  232. .cra_module = THIS_MODULE,
  233. .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
  234. .cra_u = {
  235. .cipher = {
  236. .cia_min_keysize = DES3_128_KEY_SIZE,
  237. .cia_max_keysize = DES3_128_KEY_SIZE,
  238. .cia_setkey = des3_128_setkey,
  239. .cia_encrypt = des3_128_encrypt,
  240. .cia_decrypt = des3_128_decrypt,
  241. .cia_encrypt_ecb = des3_128_encrypt_ecb,
  242. .cia_decrypt_ecb = des3_128_decrypt_ecb,
  243. .cia_encrypt_cbc = des3_128_encrypt_cbc,
  244. .cia_decrypt_cbc = des3_128_decrypt_cbc,
  245. }
  246. }
  247. };
  248. /*
  249. * RFC2451:
  250. *
  251. * For DES-EDE3, there is no known need to reject weak or
  252. * complementation keys. Any weakness is obviated by the use of
  253. * multiple keys.
  254. *
  255. * However, if the first two or last two independent 64-bit keys are
  256. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  257. * same as DES. Implementers MUST reject keys that exhibit this
  258. * property.
  259. *
  260. */
  261. static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
  262. unsigned int keylen)
  263. {
  264. int i, ret;
  265. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  266. const u8 *temp_key = key;
  267. u32 *flags = &tfm->crt_flags;
  268. if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
  269. memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
  270. DES_KEY_SIZE))) {
  271. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  272. return -EINVAL;
  273. }
  274. for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
  275. ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
  276. if (ret < 0)
  277. return ret;
  278. }
  279. memcpy(dctx->key, key, keylen);
  280. return 0;
  281. }
  282. static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  283. {
  284. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  285. crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
  286. DES3_192_BLOCK_SIZE);
  287. }
  288. static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  289. {
  290. struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
  291. crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
  292. DES3_192_BLOCK_SIZE);
  293. }
  294. static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc,
  295. u8 *out, const u8 *in,
  296. unsigned int nbytes)
  297. {
  298. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  299. int ret;
  300. /* only use complete blocks */
  301. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  302. ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes);
  303. BUG_ON((ret < 0) || (ret != nbytes));
  304. return nbytes;
  305. }
  306. static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc,
  307. u8 *out, const u8 *in,
  308. unsigned int nbytes)
  309. {
  310. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  311. int ret;
  312. /* only use complete blocks */
  313. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  314. ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes);
  315. BUG_ON((ret < 0) || (ret != nbytes));
  316. return nbytes;
  317. }
  318. static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc,
  319. u8 *out, const u8 *in,
  320. unsigned int nbytes)
  321. {
  322. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  323. int ret;
  324. /* only use complete blocks */
  325. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  326. memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  327. ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes);
  328. BUG_ON((ret < 0) || (ret != nbytes));
  329. memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE);
  330. return nbytes;
  331. }
  332. static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc,
  333. u8 *out, const u8 *in,
  334. unsigned int nbytes)
  335. {
  336. struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  337. int ret;
  338. /* only use complete blocks */
  339. nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
  340. memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
  341. ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes);
  342. BUG_ON((ret < 0) || (ret != nbytes));
  343. return nbytes;
  344. }
  345. static struct crypto_alg des3_192_alg = {
  346. .cra_name = "des3_ede",
  347. .cra_driver_name = "des3_ede-s390",
  348. .cra_priority = CRYPT_S390_PRIORITY,
  349. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  350. .cra_blocksize = DES3_192_BLOCK_SIZE,
  351. .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
  352. .cra_module = THIS_MODULE,
  353. .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
  354. .cra_u = {
  355. .cipher = {
  356. .cia_min_keysize = DES3_192_KEY_SIZE,
  357. .cia_max_keysize = DES3_192_KEY_SIZE,
  358. .cia_setkey = des3_192_setkey,
  359. .cia_encrypt = des3_192_encrypt,
  360. .cia_decrypt = des3_192_decrypt,
  361. .cia_encrypt_ecb = des3_192_encrypt_ecb,
  362. .cia_decrypt_ecb = des3_192_decrypt_ecb,
  363. .cia_encrypt_cbc = des3_192_encrypt_cbc,
  364. .cia_decrypt_cbc = des3_192_decrypt_cbc,
  365. }
  366. }
  367. };
  368. static int init(void)
  369. {
  370. int ret = 0;
  371. if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
  372. !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
  373. !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
  374. return -ENOSYS;
  375. ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1;
  376. ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2;
  377. ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4;
  378. if (ret) {
  379. crypto_unregister_alg(&des3_192_alg);
  380. crypto_unregister_alg(&des3_128_alg);
  381. crypto_unregister_alg(&des_alg);
  382. return -EEXIST;
  383. }
  384. return 0;
  385. }
  386. static void __exit fini(void)
  387. {
  388. crypto_unregister_alg(&des3_192_alg);
  389. crypto_unregister_alg(&des3_128_alg);
  390. crypto_unregister_alg(&des_alg);
  391. }
  392. module_init(init);
  393. module_exit(fini);
  394. MODULE_ALIAS("des");
  395. MODULE_ALIAS("des3_ede");
  396. MODULE_LICENSE("GPL");
  397. MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");