aes_s390.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright IBM Corp. 2005,2007
  8. * Author(s): Jan Glauber (jang@de.ibm.com)
  9. * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
  10. *
  11. * Derived from "crypto/aes_generic.c"
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. */
  19. #define KMSG_COMPONENT "aes_s390"
  20. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  21. #include <crypto/aes.h>
  22. #include <crypto/algapi.h>
  23. #include <linux/err.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include "crypt_s390.h"
  27. #define AES_KEYLEN_128 1
  28. #define AES_KEYLEN_192 2
  29. #define AES_KEYLEN_256 4
  30. static char keylen_flag = 0;
  31. struct s390_aes_ctx {
  32. u8 iv[AES_BLOCK_SIZE];
  33. u8 key[AES_MAX_KEY_SIZE];
  34. long enc;
  35. long dec;
  36. int key_len;
  37. union {
  38. struct crypto_blkcipher *blk;
  39. struct crypto_cipher *cip;
  40. } fallback;
  41. };
  42. /*
  43. * Check if the key_len is supported by the HW.
  44. * Returns 0 if it is, a positive number if it is not and software fallback is
  45. * required or a negative number in case the key size is not valid
  46. */
  47. static int need_fallback(unsigned int key_len)
  48. {
  49. switch (key_len) {
  50. case 16:
  51. if (!(keylen_flag & AES_KEYLEN_128))
  52. return 1;
  53. break;
  54. case 24:
  55. if (!(keylen_flag & AES_KEYLEN_192))
  56. return 1;
  57. break;
  58. case 32:
  59. if (!(keylen_flag & AES_KEYLEN_256))
  60. return 1;
  61. break;
  62. default:
  63. return -1;
  64. break;
  65. }
  66. return 0;
  67. }
  68. static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
  69. unsigned int key_len)
  70. {
  71. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  72. int ret;
  73. sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  74. sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
  75. CRYPTO_TFM_REQ_MASK);
  76. ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
  77. if (ret) {
  78. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  79. tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
  80. CRYPTO_TFM_RES_MASK);
  81. }
  82. return ret;
  83. }
  84. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  85. unsigned int key_len)
  86. {
  87. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  88. u32 *flags = &tfm->crt_flags;
  89. int ret;
  90. ret = need_fallback(key_len);
  91. if (ret < 0) {
  92. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  93. return -EINVAL;
  94. }
  95. sctx->key_len = key_len;
  96. if (!ret) {
  97. memcpy(sctx->key, in_key, key_len);
  98. return 0;
  99. }
  100. return setkey_fallback_cip(tfm, in_key, key_len);
  101. }
  102. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  103. {
  104. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  105. if (unlikely(need_fallback(sctx->key_len))) {
  106. crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
  107. return;
  108. }
  109. switch (sctx->key_len) {
  110. case 16:
  111. crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
  112. AES_BLOCK_SIZE);
  113. break;
  114. case 24:
  115. crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
  116. AES_BLOCK_SIZE);
  117. break;
  118. case 32:
  119. crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
  120. AES_BLOCK_SIZE);
  121. break;
  122. }
  123. }
  124. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  125. {
  126. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  127. if (unlikely(need_fallback(sctx->key_len))) {
  128. crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
  129. return;
  130. }
  131. switch (sctx->key_len) {
  132. case 16:
  133. crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
  134. AES_BLOCK_SIZE);
  135. break;
  136. case 24:
  137. crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
  138. AES_BLOCK_SIZE);
  139. break;
  140. case 32:
  141. crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
  142. AES_BLOCK_SIZE);
  143. break;
  144. }
  145. }
  146. static int fallback_init_cip(struct crypto_tfm *tfm)
  147. {
  148. const char *name = tfm->__crt_alg->cra_name;
  149. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  150. sctx->fallback.cip = crypto_alloc_cipher(name, 0,
  151. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  152. if (IS_ERR(sctx->fallback.cip)) {
  153. pr_err("Allocating AES fallback algorithm %s failed\n",
  154. name);
  155. return PTR_ERR(sctx->fallback.blk);
  156. }
  157. return 0;
  158. }
  159. static void fallback_exit_cip(struct crypto_tfm *tfm)
  160. {
  161. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  162. crypto_free_cipher(sctx->fallback.cip);
  163. sctx->fallback.cip = NULL;
  164. }
  165. static struct crypto_alg aes_alg = {
  166. .cra_name = "aes",
  167. .cra_driver_name = "aes-s390",
  168. .cra_priority = CRYPT_S390_PRIORITY,
  169. .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
  170. CRYPTO_ALG_NEED_FALLBACK,
  171. .cra_blocksize = AES_BLOCK_SIZE,
  172. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  173. .cra_module = THIS_MODULE,
  174. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  175. .cra_init = fallback_init_cip,
  176. .cra_exit = fallback_exit_cip,
  177. .cra_u = {
  178. .cipher = {
  179. .cia_min_keysize = AES_MIN_KEY_SIZE,
  180. .cia_max_keysize = AES_MAX_KEY_SIZE,
  181. .cia_setkey = aes_set_key,
  182. .cia_encrypt = aes_encrypt,
  183. .cia_decrypt = aes_decrypt,
  184. }
  185. }
  186. };
  187. static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
  188. unsigned int len)
  189. {
  190. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  191. unsigned int ret;
  192. sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  193. sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
  194. CRYPTO_TFM_REQ_MASK);
  195. ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
  196. if (ret) {
  197. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  198. tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
  199. CRYPTO_TFM_RES_MASK);
  200. }
  201. return ret;
  202. }
  203. static int fallback_blk_dec(struct blkcipher_desc *desc,
  204. struct scatterlist *dst, struct scatterlist *src,
  205. unsigned int nbytes)
  206. {
  207. unsigned int ret;
  208. struct crypto_blkcipher *tfm;
  209. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  210. tfm = desc->tfm;
  211. desc->tfm = sctx->fallback.blk;
  212. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  213. desc->tfm = tfm;
  214. return ret;
  215. }
  216. static int fallback_blk_enc(struct blkcipher_desc *desc,
  217. struct scatterlist *dst, struct scatterlist *src,
  218. unsigned int nbytes)
  219. {
  220. unsigned int ret;
  221. struct crypto_blkcipher *tfm;
  222. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  223. tfm = desc->tfm;
  224. desc->tfm = sctx->fallback.blk;
  225. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  226. desc->tfm = tfm;
  227. return ret;
  228. }
  229. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  230. unsigned int key_len)
  231. {
  232. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  233. int ret;
  234. ret = need_fallback(key_len);
  235. if (ret > 0) {
  236. sctx->key_len = key_len;
  237. return setkey_fallback_blk(tfm, in_key, key_len);
  238. }
  239. switch (key_len) {
  240. case 16:
  241. sctx->enc = KM_AES_128_ENCRYPT;
  242. sctx->dec = KM_AES_128_DECRYPT;
  243. break;
  244. case 24:
  245. sctx->enc = KM_AES_192_ENCRYPT;
  246. sctx->dec = KM_AES_192_DECRYPT;
  247. break;
  248. case 32:
  249. sctx->enc = KM_AES_256_ENCRYPT;
  250. sctx->dec = KM_AES_256_DECRYPT;
  251. break;
  252. }
  253. return aes_set_key(tfm, in_key, key_len);
  254. }
  255. static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  256. struct blkcipher_walk *walk)
  257. {
  258. int ret = blkcipher_walk_virt(desc, walk);
  259. unsigned int nbytes;
  260. while ((nbytes = walk->nbytes)) {
  261. /* only use complete blocks */
  262. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  263. u8 *out = walk->dst.virt.addr;
  264. u8 *in = walk->src.virt.addr;
  265. ret = crypt_s390_km(func, param, out, in, n);
  266. BUG_ON((ret < 0) || (ret != n));
  267. nbytes &= AES_BLOCK_SIZE - 1;
  268. ret = blkcipher_walk_done(desc, walk, nbytes);
  269. }
  270. return ret;
  271. }
  272. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  273. struct scatterlist *dst, struct scatterlist *src,
  274. unsigned int nbytes)
  275. {
  276. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  277. struct blkcipher_walk walk;
  278. if (unlikely(need_fallback(sctx->key_len)))
  279. return fallback_blk_enc(desc, dst, src, nbytes);
  280. blkcipher_walk_init(&walk, dst, src, nbytes);
  281. return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
  282. }
  283. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  284. struct scatterlist *dst, struct scatterlist *src,
  285. unsigned int nbytes)
  286. {
  287. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  288. struct blkcipher_walk walk;
  289. if (unlikely(need_fallback(sctx->key_len)))
  290. return fallback_blk_dec(desc, dst, src, nbytes);
  291. blkcipher_walk_init(&walk, dst, src, nbytes);
  292. return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
  293. }
  294. static int fallback_init_blk(struct crypto_tfm *tfm)
  295. {
  296. const char *name = tfm->__crt_alg->cra_name;
  297. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  298. sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
  299. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  300. if (IS_ERR(sctx->fallback.blk)) {
  301. pr_err("Allocating AES fallback algorithm %s failed\n",
  302. name);
  303. return PTR_ERR(sctx->fallback.blk);
  304. }
  305. return 0;
  306. }
  307. static void fallback_exit_blk(struct crypto_tfm *tfm)
  308. {
  309. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  310. crypto_free_blkcipher(sctx->fallback.blk);
  311. sctx->fallback.blk = NULL;
  312. }
  313. static struct crypto_alg ecb_aes_alg = {
  314. .cra_name = "ecb(aes)",
  315. .cra_driver_name = "ecb-aes-s390",
  316. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  317. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  318. CRYPTO_ALG_NEED_FALLBACK,
  319. .cra_blocksize = AES_BLOCK_SIZE,
  320. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  321. .cra_type = &crypto_blkcipher_type,
  322. .cra_module = THIS_MODULE,
  323. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  324. .cra_init = fallback_init_blk,
  325. .cra_exit = fallback_exit_blk,
  326. .cra_u = {
  327. .blkcipher = {
  328. .min_keysize = AES_MIN_KEY_SIZE,
  329. .max_keysize = AES_MAX_KEY_SIZE,
  330. .setkey = ecb_aes_set_key,
  331. .encrypt = ecb_aes_encrypt,
  332. .decrypt = ecb_aes_decrypt,
  333. }
  334. }
  335. };
  336. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  337. unsigned int key_len)
  338. {
  339. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  340. int ret;
  341. ret = need_fallback(key_len);
  342. if (ret > 0) {
  343. sctx->key_len = key_len;
  344. return setkey_fallback_blk(tfm, in_key, key_len);
  345. }
  346. switch (key_len) {
  347. case 16:
  348. sctx->enc = KMC_AES_128_ENCRYPT;
  349. sctx->dec = KMC_AES_128_DECRYPT;
  350. break;
  351. case 24:
  352. sctx->enc = KMC_AES_192_ENCRYPT;
  353. sctx->dec = KMC_AES_192_DECRYPT;
  354. break;
  355. case 32:
  356. sctx->enc = KMC_AES_256_ENCRYPT;
  357. sctx->dec = KMC_AES_256_DECRYPT;
  358. break;
  359. }
  360. return aes_set_key(tfm, in_key, key_len);
  361. }
  362. static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  363. struct blkcipher_walk *walk)
  364. {
  365. int ret = blkcipher_walk_virt(desc, walk);
  366. unsigned int nbytes = walk->nbytes;
  367. if (!nbytes)
  368. goto out;
  369. memcpy(param, walk->iv, AES_BLOCK_SIZE);
  370. do {
  371. /* only use complete blocks */
  372. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  373. u8 *out = walk->dst.virt.addr;
  374. u8 *in = walk->src.virt.addr;
  375. ret = crypt_s390_kmc(func, param, out, in, n);
  376. BUG_ON((ret < 0) || (ret != n));
  377. nbytes &= AES_BLOCK_SIZE - 1;
  378. ret = blkcipher_walk_done(desc, walk, nbytes);
  379. } while ((nbytes = walk->nbytes));
  380. memcpy(walk->iv, param, AES_BLOCK_SIZE);
  381. out:
  382. return ret;
  383. }
  384. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  385. struct scatterlist *dst, struct scatterlist *src,
  386. unsigned int nbytes)
  387. {
  388. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  389. struct blkcipher_walk walk;
  390. if (unlikely(need_fallback(sctx->key_len)))
  391. return fallback_blk_enc(desc, dst, src, nbytes);
  392. blkcipher_walk_init(&walk, dst, src, nbytes);
  393. return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk);
  394. }
  395. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  396. struct scatterlist *dst, struct scatterlist *src,
  397. unsigned int nbytes)
  398. {
  399. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  400. struct blkcipher_walk walk;
  401. if (unlikely(need_fallback(sctx->key_len)))
  402. return fallback_blk_dec(desc, dst, src, nbytes);
  403. blkcipher_walk_init(&walk, dst, src, nbytes);
  404. return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk);
  405. }
  406. static struct crypto_alg cbc_aes_alg = {
  407. .cra_name = "cbc(aes)",
  408. .cra_driver_name = "cbc-aes-s390",
  409. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  410. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  411. CRYPTO_ALG_NEED_FALLBACK,
  412. .cra_blocksize = AES_BLOCK_SIZE,
  413. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  414. .cra_type = &crypto_blkcipher_type,
  415. .cra_module = THIS_MODULE,
  416. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  417. .cra_init = fallback_init_blk,
  418. .cra_exit = fallback_exit_blk,
  419. .cra_u = {
  420. .blkcipher = {
  421. .min_keysize = AES_MIN_KEY_SIZE,
  422. .max_keysize = AES_MAX_KEY_SIZE,
  423. .ivsize = AES_BLOCK_SIZE,
  424. .setkey = cbc_aes_set_key,
  425. .encrypt = cbc_aes_encrypt,
  426. .decrypt = cbc_aes_decrypt,
  427. }
  428. }
  429. };
  430. static int __init aes_s390_init(void)
  431. {
  432. int ret;
  433. if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
  434. keylen_flag |= AES_KEYLEN_128;
  435. if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
  436. keylen_flag |= AES_KEYLEN_192;
  437. if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
  438. keylen_flag |= AES_KEYLEN_256;
  439. if (!keylen_flag)
  440. return -EOPNOTSUPP;
  441. /* z9 109 and z9 BC/EC only support 128 bit key length */
  442. if (keylen_flag == AES_KEYLEN_128)
  443. pr_info("AES hardware acceleration is only available for"
  444. " 128-bit keys\n");
  445. ret = crypto_register_alg(&aes_alg);
  446. if (ret)
  447. goto aes_err;
  448. ret = crypto_register_alg(&ecb_aes_alg);
  449. if (ret)
  450. goto ecb_aes_err;
  451. ret = crypto_register_alg(&cbc_aes_alg);
  452. if (ret)
  453. goto cbc_aes_err;
  454. out:
  455. return ret;
  456. cbc_aes_err:
  457. crypto_unregister_alg(&ecb_aes_alg);
  458. ecb_aes_err:
  459. crypto_unregister_alg(&aes_alg);
  460. aes_err:
  461. goto out;
  462. }
  463. static void __exit aes_s390_fini(void)
  464. {
  465. crypto_unregister_alg(&cbc_aes_alg);
  466. crypto_unregister_alg(&ecb_aes_alg);
  467. crypto_unregister_alg(&aes_alg);
  468. }
  469. module_init(aes_s390_init);
  470. module_exit(aes_s390_fini);
  471. MODULE_ALIAS("aes-all");
  472. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  473. MODULE_LICENSE("GPL");