aes_s390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright (C) 2005 IBM Deutschland GmbH, IBM Corporation
  8. * Author(s): Jan Glauber (jang@de.ibm.com)
  9. *
  10. * Derived from "crypto/aes.c"
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. #include <crypto/algapi.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include "crypt_s390.h"
  22. #define AES_MIN_KEY_SIZE 16
  23. #define AES_MAX_KEY_SIZE 32
  24. /* data block size for all key lengths */
  25. #define AES_BLOCK_SIZE 16
  26. int has_aes_128 = 0;
  27. int has_aes_192 = 0;
  28. int has_aes_256 = 0;
  29. struct s390_aes_ctx {
  30. u8 iv[AES_BLOCK_SIZE];
  31. u8 key[AES_MAX_KEY_SIZE];
  32. long enc;
  33. long dec;
  34. int key_len;
  35. };
  36. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  37. unsigned int key_len)
  38. {
  39. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  40. u32 *flags = &tfm->crt_flags;
  41. switch (key_len) {
  42. case 16:
  43. if (!has_aes_128)
  44. goto fail;
  45. break;
  46. case 24:
  47. if (!has_aes_192)
  48. goto fail;
  49. break;
  50. case 32:
  51. if (!has_aes_256)
  52. goto fail;
  53. break;
  54. default:
  55. /* invalid key length */
  56. goto fail;
  57. break;
  58. }
  59. sctx->key_len = key_len;
  60. memcpy(sctx->key, in_key, key_len);
  61. return 0;
  62. fail:
  63. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  64. return -EINVAL;
  65. }
  66. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  67. {
  68. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  69. switch (sctx->key_len) {
  70. case 16:
  71. crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
  72. AES_BLOCK_SIZE);
  73. break;
  74. case 24:
  75. crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
  76. AES_BLOCK_SIZE);
  77. break;
  78. case 32:
  79. crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
  80. AES_BLOCK_SIZE);
  81. break;
  82. }
  83. }
  84. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  85. {
  86. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  87. switch (sctx->key_len) {
  88. case 16:
  89. crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
  90. AES_BLOCK_SIZE);
  91. break;
  92. case 24:
  93. crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
  94. AES_BLOCK_SIZE);
  95. break;
  96. case 32:
  97. crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
  98. AES_BLOCK_SIZE);
  99. break;
  100. }
  101. }
  102. static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
  103. const u8 *in, unsigned int nbytes)
  104. {
  105. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  106. int ret;
  107. /* only use complete blocks */
  108. nbytes &= ~(AES_BLOCK_SIZE - 1);
  109. switch (sctx->key_len) {
  110. case 16:
  111. ret = crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes);
  112. BUG_ON((ret < 0) || (ret != nbytes));
  113. break;
  114. case 24:
  115. ret = crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes);
  116. BUG_ON((ret < 0) || (ret != nbytes));
  117. break;
  118. case 32:
  119. ret = crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes);
  120. BUG_ON((ret < 0) || (ret != nbytes));
  121. break;
  122. }
  123. return nbytes;
  124. }
  125. static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
  126. const u8 *in, unsigned int nbytes)
  127. {
  128. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  129. int ret;
  130. /* only use complete blocks */
  131. nbytes &= ~(AES_BLOCK_SIZE - 1);
  132. switch (sctx->key_len) {
  133. case 16:
  134. ret = crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes);
  135. BUG_ON((ret < 0) || (ret != nbytes));
  136. break;
  137. case 24:
  138. ret = crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes);
  139. BUG_ON((ret < 0) || (ret != nbytes));
  140. break;
  141. case 32:
  142. ret = crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes);
  143. BUG_ON((ret < 0) || (ret != nbytes));
  144. break;
  145. }
  146. return nbytes;
  147. }
  148. static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
  149. const u8 *in, unsigned int nbytes)
  150. {
  151. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  152. int ret;
  153. /* only use complete blocks */
  154. nbytes &= ~(AES_BLOCK_SIZE - 1);
  155. memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
  156. switch (sctx->key_len) {
  157. case 16:
  158. ret = crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes);
  159. BUG_ON((ret < 0) || (ret != nbytes));
  160. break;
  161. case 24:
  162. ret = crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes);
  163. BUG_ON((ret < 0) || (ret != nbytes));
  164. break;
  165. case 32:
  166. ret = crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes);
  167. BUG_ON((ret < 0) || (ret != nbytes));
  168. break;
  169. }
  170. memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE);
  171. return nbytes;
  172. }
  173. static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
  174. const u8 *in, unsigned int nbytes)
  175. {
  176. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  177. int ret;
  178. /* only use complete blocks */
  179. nbytes &= ~(AES_BLOCK_SIZE - 1);
  180. memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
  181. switch (sctx->key_len) {
  182. case 16:
  183. ret = crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes);
  184. BUG_ON((ret < 0) || (ret != nbytes));
  185. break;
  186. case 24:
  187. ret = crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes);
  188. BUG_ON((ret < 0) || (ret != nbytes));
  189. break;
  190. case 32:
  191. ret = crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes);
  192. BUG_ON((ret < 0) || (ret != nbytes));
  193. break;
  194. }
  195. return nbytes;
  196. }
  197. static struct crypto_alg aes_alg = {
  198. .cra_name = "aes",
  199. .cra_driver_name = "aes-s390",
  200. .cra_priority = CRYPT_S390_PRIORITY,
  201. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  202. .cra_blocksize = AES_BLOCK_SIZE,
  203. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  204. .cra_module = THIS_MODULE,
  205. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  206. .cra_u = {
  207. .cipher = {
  208. .cia_min_keysize = AES_MIN_KEY_SIZE,
  209. .cia_max_keysize = AES_MAX_KEY_SIZE,
  210. .cia_setkey = aes_set_key,
  211. .cia_encrypt = aes_encrypt,
  212. .cia_decrypt = aes_decrypt,
  213. .cia_encrypt_ecb = aes_encrypt_ecb,
  214. .cia_decrypt_ecb = aes_decrypt_ecb,
  215. .cia_encrypt_cbc = aes_encrypt_cbc,
  216. .cia_decrypt_cbc = aes_decrypt_cbc,
  217. }
  218. }
  219. };
  220. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  221. unsigned int key_len)
  222. {
  223. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  224. switch (key_len) {
  225. case 16:
  226. sctx->enc = KM_AES_128_ENCRYPT;
  227. sctx->dec = KM_AES_128_DECRYPT;
  228. break;
  229. case 24:
  230. sctx->enc = KM_AES_192_ENCRYPT;
  231. sctx->dec = KM_AES_192_DECRYPT;
  232. break;
  233. case 32:
  234. sctx->enc = KM_AES_256_ENCRYPT;
  235. sctx->dec = KM_AES_256_DECRYPT;
  236. break;
  237. }
  238. return aes_set_key(tfm, in_key, key_len);
  239. }
  240. static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  241. struct blkcipher_walk *walk)
  242. {
  243. int ret = blkcipher_walk_virt(desc, walk);
  244. unsigned int nbytes;
  245. while ((nbytes = walk->nbytes)) {
  246. /* only use complete blocks */
  247. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  248. u8 *out = walk->dst.virt.addr;
  249. u8 *in = walk->src.virt.addr;
  250. ret = crypt_s390_km(func, param, out, in, n);
  251. BUG_ON((ret < 0) || (ret != n));
  252. nbytes &= AES_BLOCK_SIZE - 1;
  253. ret = blkcipher_walk_done(desc, walk, nbytes);
  254. }
  255. return ret;
  256. }
  257. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  258. struct scatterlist *dst, struct scatterlist *src,
  259. unsigned int nbytes)
  260. {
  261. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  262. struct blkcipher_walk walk;
  263. blkcipher_walk_init(&walk, dst, src, nbytes);
  264. return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
  265. }
  266. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  267. struct scatterlist *dst, struct scatterlist *src,
  268. unsigned int nbytes)
  269. {
  270. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  271. struct blkcipher_walk walk;
  272. blkcipher_walk_init(&walk, dst, src, nbytes);
  273. return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
  274. }
  275. static struct crypto_alg ecb_aes_alg = {
  276. .cra_name = "ecb(aes)",
  277. .cra_driver_name = "ecb-aes-s390",
  278. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  279. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  280. .cra_blocksize = AES_BLOCK_SIZE,
  281. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  282. .cra_type = &crypto_blkcipher_type,
  283. .cra_module = THIS_MODULE,
  284. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  285. .cra_u = {
  286. .blkcipher = {
  287. .min_keysize = AES_MIN_KEY_SIZE,
  288. .max_keysize = AES_MAX_KEY_SIZE,
  289. .setkey = ecb_aes_set_key,
  290. .encrypt = ecb_aes_encrypt,
  291. .decrypt = ecb_aes_decrypt,
  292. }
  293. }
  294. };
  295. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  296. unsigned int key_len)
  297. {
  298. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  299. switch (key_len) {
  300. case 16:
  301. sctx->enc = KMC_AES_128_ENCRYPT;
  302. sctx->dec = KMC_AES_128_DECRYPT;
  303. break;
  304. case 24:
  305. sctx->enc = KMC_AES_192_ENCRYPT;
  306. sctx->dec = KMC_AES_192_DECRYPT;
  307. break;
  308. case 32:
  309. sctx->enc = KMC_AES_256_ENCRYPT;
  310. sctx->dec = KMC_AES_256_DECRYPT;
  311. break;
  312. }
  313. return aes_set_key(tfm, in_key, key_len);
  314. }
  315. static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  316. struct blkcipher_walk *walk)
  317. {
  318. int ret = blkcipher_walk_virt(desc, walk);
  319. unsigned int nbytes = walk->nbytes;
  320. if (!nbytes)
  321. goto out;
  322. memcpy(param, walk->iv, AES_BLOCK_SIZE);
  323. do {
  324. /* only use complete blocks */
  325. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  326. u8 *out = walk->dst.virt.addr;
  327. u8 *in = walk->src.virt.addr;
  328. ret = crypt_s390_kmc(func, param, out, in, n);
  329. BUG_ON((ret < 0) || (ret != n));
  330. nbytes &= AES_BLOCK_SIZE - 1;
  331. ret = blkcipher_walk_done(desc, walk, nbytes);
  332. } while ((nbytes = walk->nbytes));
  333. memcpy(walk->iv, param, AES_BLOCK_SIZE);
  334. out:
  335. return ret;
  336. }
  337. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  338. struct scatterlist *dst, struct scatterlist *src,
  339. unsigned int nbytes)
  340. {
  341. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  342. struct blkcipher_walk walk;
  343. blkcipher_walk_init(&walk, dst, src, nbytes);
  344. return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk);
  345. }
  346. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  347. struct scatterlist *dst, struct scatterlist *src,
  348. unsigned int nbytes)
  349. {
  350. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  351. struct blkcipher_walk walk;
  352. blkcipher_walk_init(&walk, dst, src, nbytes);
  353. return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk);
  354. }
  355. static struct crypto_alg cbc_aes_alg = {
  356. .cra_name = "cbc(aes)",
  357. .cra_driver_name = "cbc-aes-s390",
  358. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  359. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  360. .cra_blocksize = AES_BLOCK_SIZE,
  361. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  362. .cra_type = &crypto_blkcipher_type,
  363. .cra_module = THIS_MODULE,
  364. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  365. .cra_u = {
  366. .blkcipher = {
  367. .min_keysize = AES_MIN_KEY_SIZE,
  368. .max_keysize = AES_MAX_KEY_SIZE,
  369. .ivsize = AES_BLOCK_SIZE,
  370. .setkey = cbc_aes_set_key,
  371. .encrypt = cbc_aes_encrypt,
  372. .decrypt = cbc_aes_decrypt,
  373. }
  374. }
  375. };
  376. static int __init aes_init(void)
  377. {
  378. int ret;
  379. if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
  380. has_aes_128 = 1;
  381. if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
  382. has_aes_192 = 1;
  383. if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
  384. has_aes_256 = 1;
  385. if (!has_aes_128 && !has_aes_192 && !has_aes_256)
  386. return -ENOSYS;
  387. ret = crypto_register_alg(&aes_alg);
  388. if (ret != 0) {
  389. printk(KERN_INFO "crypt_s390: aes-s390 couldn't be loaded.\n");
  390. goto aes_err;
  391. }
  392. ret = crypto_register_alg(&ecb_aes_alg);
  393. if (ret != 0) {
  394. printk(KERN_INFO
  395. "crypt_s390: ecb-aes-s390 couldn't be loaded.\n");
  396. goto ecb_aes_err;
  397. }
  398. ret = crypto_register_alg(&cbc_aes_alg);
  399. if (ret != 0) {
  400. printk(KERN_INFO
  401. "crypt_s390: cbc-aes-s390 couldn't be loaded.\n");
  402. goto cbc_aes_err;
  403. }
  404. out:
  405. return ret;
  406. cbc_aes_err:
  407. crypto_unregister_alg(&ecb_aes_alg);
  408. ecb_aes_err:
  409. crypto_unregister_alg(&aes_alg);
  410. aes_err:
  411. goto out;
  412. }
  413. static void __exit aes_fini(void)
  414. {
  415. crypto_unregister_alg(&cbc_aes_alg);
  416. crypto_unregister_alg(&ecb_aes_alg);
  417. crypto_unregister_alg(&aes_alg);
  418. }
  419. module_init(aes_init);
  420. module_exit(aes_fini);
  421. MODULE_ALIAS("aes");
  422. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  423. MODULE_LICENSE("GPL");