aes_s390.c 13 KB

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