aes_s390.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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/cpufeature.h>
  26. #include <linux/init.h>
  27. #include <linux/spinlock.h>
  28. #include <crypto/xts.h>
  29. #include <asm/cpacf.h>
  30. #define AES_KEYLEN_128 1
  31. #define AES_KEYLEN_192 2
  32. #define AES_KEYLEN_256 4
  33. static u8 *ctrblk;
  34. static DEFINE_SPINLOCK(ctrblk_lock);
  35. static char keylen_flag;
  36. struct s390_aes_ctx {
  37. u8 key[AES_MAX_KEY_SIZE];
  38. long enc;
  39. long dec;
  40. int key_len;
  41. union {
  42. struct crypto_blkcipher *blk;
  43. struct crypto_cipher *cip;
  44. } fallback;
  45. };
  46. struct pcc_param {
  47. u8 key[32];
  48. u8 tweak[16];
  49. u8 block[16];
  50. u8 bit[16];
  51. u8 xts[16];
  52. };
  53. struct s390_xts_ctx {
  54. u8 key[32];
  55. u8 pcc_key[32];
  56. long enc;
  57. long dec;
  58. int key_len;
  59. struct crypto_blkcipher *fallback;
  60. };
  61. /*
  62. * Check if the key_len is supported by the HW.
  63. * Returns 0 if it is, a positive number if it is not and software fallback is
  64. * required or a negative number in case the key size is not valid
  65. */
  66. static int need_fallback(unsigned int key_len)
  67. {
  68. switch (key_len) {
  69. case 16:
  70. if (!(keylen_flag & AES_KEYLEN_128))
  71. return 1;
  72. break;
  73. case 24:
  74. if (!(keylen_flag & AES_KEYLEN_192))
  75. return 1;
  76. break;
  77. case 32:
  78. if (!(keylen_flag & AES_KEYLEN_256))
  79. return 1;
  80. break;
  81. default:
  82. return -1;
  83. break;
  84. }
  85. return 0;
  86. }
  87. static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
  88. unsigned int key_len)
  89. {
  90. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  91. int ret;
  92. sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  93. sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
  94. CRYPTO_TFM_REQ_MASK);
  95. ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
  96. if (ret) {
  97. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  98. tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
  99. CRYPTO_TFM_RES_MASK);
  100. }
  101. return ret;
  102. }
  103. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  104. unsigned int key_len)
  105. {
  106. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  107. u32 *flags = &tfm->crt_flags;
  108. int ret;
  109. ret = need_fallback(key_len);
  110. if (ret < 0) {
  111. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  112. return -EINVAL;
  113. }
  114. sctx->key_len = key_len;
  115. if (!ret) {
  116. memcpy(sctx->key, in_key, key_len);
  117. return 0;
  118. }
  119. return setkey_fallback_cip(tfm, in_key, key_len);
  120. }
  121. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  122. {
  123. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  124. if (unlikely(need_fallback(sctx->key_len))) {
  125. crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
  126. return;
  127. }
  128. switch (sctx->key_len) {
  129. case 16:
  130. cpacf_km(CPACF_KM_AES_128_ENC, &sctx->key, out, in,
  131. AES_BLOCK_SIZE);
  132. break;
  133. case 24:
  134. cpacf_km(CPACF_KM_AES_192_ENC, &sctx->key, out, in,
  135. AES_BLOCK_SIZE);
  136. break;
  137. case 32:
  138. cpacf_km(CPACF_KM_AES_256_ENC, &sctx->key, out, in,
  139. AES_BLOCK_SIZE);
  140. break;
  141. }
  142. }
  143. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  144. {
  145. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  146. if (unlikely(need_fallback(sctx->key_len))) {
  147. crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
  148. return;
  149. }
  150. switch (sctx->key_len) {
  151. case 16:
  152. cpacf_km(CPACF_KM_AES_128_DEC, &sctx->key, out, in,
  153. AES_BLOCK_SIZE);
  154. break;
  155. case 24:
  156. cpacf_km(CPACF_KM_AES_192_DEC, &sctx->key, out, in,
  157. AES_BLOCK_SIZE);
  158. break;
  159. case 32:
  160. cpacf_km(CPACF_KM_AES_256_DEC, &sctx->key, out, in,
  161. AES_BLOCK_SIZE);
  162. break;
  163. }
  164. }
  165. static int fallback_init_cip(struct crypto_tfm *tfm)
  166. {
  167. const char *name = tfm->__crt_alg->cra_name;
  168. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  169. sctx->fallback.cip = crypto_alloc_cipher(name, 0,
  170. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  171. if (IS_ERR(sctx->fallback.cip)) {
  172. pr_err("Allocating AES fallback algorithm %s failed\n",
  173. name);
  174. return PTR_ERR(sctx->fallback.cip);
  175. }
  176. return 0;
  177. }
  178. static void fallback_exit_cip(struct crypto_tfm *tfm)
  179. {
  180. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  181. crypto_free_cipher(sctx->fallback.cip);
  182. sctx->fallback.cip = NULL;
  183. }
  184. static struct crypto_alg aes_alg = {
  185. .cra_name = "aes",
  186. .cra_driver_name = "aes-s390",
  187. .cra_priority = 300,
  188. .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
  189. CRYPTO_ALG_NEED_FALLBACK,
  190. .cra_blocksize = AES_BLOCK_SIZE,
  191. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  192. .cra_module = THIS_MODULE,
  193. .cra_init = fallback_init_cip,
  194. .cra_exit = fallback_exit_cip,
  195. .cra_u = {
  196. .cipher = {
  197. .cia_min_keysize = AES_MIN_KEY_SIZE,
  198. .cia_max_keysize = AES_MAX_KEY_SIZE,
  199. .cia_setkey = aes_set_key,
  200. .cia_encrypt = aes_encrypt,
  201. .cia_decrypt = aes_decrypt,
  202. }
  203. }
  204. };
  205. static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
  206. unsigned int len)
  207. {
  208. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  209. unsigned int ret;
  210. sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  211. sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
  212. CRYPTO_TFM_REQ_MASK);
  213. ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
  214. if (ret) {
  215. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  216. tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
  217. CRYPTO_TFM_RES_MASK);
  218. }
  219. return ret;
  220. }
  221. static int fallback_blk_dec(struct blkcipher_desc *desc,
  222. struct scatterlist *dst, struct scatterlist *src,
  223. unsigned int nbytes)
  224. {
  225. unsigned int ret;
  226. struct crypto_blkcipher *tfm;
  227. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  228. tfm = desc->tfm;
  229. desc->tfm = sctx->fallback.blk;
  230. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  231. desc->tfm = tfm;
  232. return ret;
  233. }
  234. static int fallback_blk_enc(struct blkcipher_desc *desc,
  235. struct scatterlist *dst, struct scatterlist *src,
  236. unsigned int nbytes)
  237. {
  238. unsigned int ret;
  239. struct crypto_blkcipher *tfm;
  240. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  241. tfm = desc->tfm;
  242. desc->tfm = sctx->fallback.blk;
  243. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  244. desc->tfm = tfm;
  245. return ret;
  246. }
  247. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  248. unsigned int key_len)
  249. {
  250. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  251. int ret;
  252. ret = need_fallback(key_len);
  253. if (ret > 0) {
  254. sctx->key_len = key_len;
  255. return setkey_fallback_blk(tfm, in_key, key_len);
  256. }
  257. switch (key_len) {
  258. case 16:
  259. sctx->enc = CPACF_KM_AES_128_ENC;
  260. sctx->dec = CPACF_KM_AES_128_DEC;
  261. break;
  262. case 24:
  263. sctx->enc = CPACF_KM_AES_192_ENC;
  264. sctx->dec = CPACF_KM_AES_192_DEC;
  265. break;
  266. case 32:
  267. sctx->enc = CPACF_KM_AES_256_ENC;
  268. sctx->dec = CPACF_KM_AES_256_DEC;
  269. break;
  270. }
  271. return aes_set_key(tfm, in_key, key_len);
  272. }
  273. static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  274. struct blkcipher_walk *walk)
  275. {
  276. int ret = blkcipher_walk_virt(desc, walk);
  277. unsigned int nbytes;
  278. while ((nbytes = walk->nbytes)) {
  279. /* only use complete blocks */
  280. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  281. u8 *out = walk->dst.virt.addr;
  282. u8 *in = walk->src.virt.addr;
  283. ret = cpacf_km(func, param, out, in, n);
  284. if (ret < 0 || ret != n)
  285. return -EIO;
  286. nbytes &= AES_BLOCK_SIZE - 1;
  287. ret = blkcipher_walk_done(desc, walk, nbytes);
  288. }
  289. return ret;
  290. }
  291. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  292. struct scatterlist *dst, struct scatterlist *src,
  293. unsigned int nbytes)
  294. {
  295. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  296. struct blkcipher_walk walk;
  297. if (unlikely(need_fallback(sctx->key_len)))
  298. return fallback_blk_enc(desc, dst, src, nbytes);
  299. blkcipher_walk_init(&walk, dst, src, nbytes);
  300. return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
  301. }
  302. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  303. struct scatterlist *dst, struct scatterlist *src,
  304. unsigned int nbytes)
  305. {
  306. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  307. struct blkcipher_walk walk;
  308. if (unlikely(need_fallback(sctx->key_len)))
  309. return fallback_blk_dec(desc, dst, src, nbytes);
  310. blkcipher_walk_init(&walk, dst, src, nbytes);
  311. return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
  312. }
  313. static int fallback_init_blk(struct crypto_tfm *tfm)
  314. {
  315. const char *name = tfm->__crt_alg->cra_name;
  316. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  317. sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
  318. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  319. if (IS_ERR(sctx->fallback.blk)) {
  320. pr_err("Allocating AES fallback algorithm %s failed\n",
  321. name);
  322. return PTR_ERR(sctx->fallback.blk);
  323. }
  324. return 0;
  325. }
  326. static void fallback_exit_blk(struct crypto_tfm *tfm)
  327. {
  328. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  329. crypto_free_blkcipher(sctx->fallback.blk);
  330. sctx->fallback.blk = NULL;
  331. }
  332. static struct crypto_alg ecb_aes_alg = {
  333. .cra_name = "ecb(aes)",
  334. .cra_driver_name = "ecb-aes-s390",
  335. .cra_priority = 400, /* combo: aes + ecb */
  336. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  337. CRYPTO_ALG_NEED_FALLBACK,
  338. .cra_blocksize = AES_BLOCK_SIZE,
  339. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  340. .cra_type = &crypto_blkcipher_type,
  341. .cra_module = THIS_MODULE,
  342. .cra_init = fallback_init_blk,
  343. .cra_exit = fallback_exit_blk,
  344. .cra_u = {
  345. .blkcipher = {
  346. .min_keysize = AES_MIN_KEY_SIZE,
  347. .max_keysize = AES_MAX_KEY_SIZE,
  348. .setkey = ecb_aes_set_key,
  349. .encrypt = ecb_aes_encrypt,
  350. .decrypt = ecb_aes_decrypt,
  351. }
  352. }
  353. };
  354. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  355. unsigned int key_len)
  356. {
  357. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  358. int ret;
  359. ret = need_fallback(key_len);
  360. if (ret > 0) {
  361. sctx->key_len = key_len;
  362. return setkey_fallback_blk(tfm, in_key, key_len);
  363. }
  364. switch (key_len) {
  365. case 16:
  366. sctx->enc = CPACF_KMC_AES_128_ENC;
  367. sctx->dec = CPACF_KMC_AES_128_DEC;
  368. break;
  369. case 24:
  370. sctx->enc = CPACF_KMC_AES_192_ENC;
  371. sctx->dec = CPACF_KMC_AES_192_DEC;
  372. break;
  373. case 32:
  374. sctx->enc = CPACF_KMC_AES_256_ENC;
  375. sctx->dec = CPACF_KMC_AES_256_DEC;
  376. break;
  377. }
  378. return aes_set_key(tfm, in_key, key_len);
  379. }
  380. static int cbc_aes_crypt(struct blkcipher_desc *desc, long func,
  381. struct blkcipher_walk *walk)
  382. {
  383. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  384. int ret = blkcipher_walk_virt(desc, walk);
  385. unsigned int nbytes = walk->nbytes;
  386. struct {
  387. u8 iv[AES_BLOCK_SIZE];
  388. u8 key[AES_MAX_KEY_SIZE];
  389. } param;
  390. if (!nbytes)
  391. goto out;
  392. memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
  393. memcpy(param.key, sctx->key, sctx->key_len);
  394. do {
  395. /* only use complete blocks */
  396. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  397. u8 *out = walk->dst.virt.addr;
  398. u8 *in = walk->src.virt.addr;
  399. ret = cpacf_kmc(func, &param, out, in, n);
  400. if (ret < 0 || ret != n)
  401. return -EIO;
  402. nbytes &= AES_BLOCK_SIZE - 1;
  403. ret = blkcipher_walk_done(desc, walk, nbytes);
  404. } while ((nbytes = walk->nbytes));
  405. memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
  406. out:
  407. return ret;
  408. }
  409. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  410. struct scatterlist *dst, struct scatterlist *src,
  411. unsigned int nbytes)
  412. {
  413. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  414. struct blkcipher_walk walk;
  415. if (unlikely(need_fallback(sctx->key_len)))
  416. return fallback_blk_enc(desc, dst, src, nbytes);
  417. blkcipher_walk_init(&walk, dst, src, nbytes);
  418. return cbc_aes_crypt(desc, sctx->enc, &walk);
  419. }
  420. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  421. struct scatterlist *dst, struct scatterlist *src,
  422. unsigned int nbytes)
  423. {
  424. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  425. struct blkcipher_walk walk;
  426. if (unlikely(need_fallback(sctx->key_len)))
  427. return fallback_blk_dec(desc, dst, src, nbytes);
  428. blkcipher_walk_init(&walk, dst, src, nbytes);
  429. return cbc_aes_crypt(desc, sctx->dec, &walk);
  430. }
  431. static struct crypto_alg cbc_aes_alg = {
  432. .cra_name = "cbc(aes)",
  433. .cra_driver_name = "cbc-aes-s390",
  434. .cra_priority = 400, /* combo: aes + cbc */
  435. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  436. CRYPTO_ALG_NEED_FALLBACK,
  437. .cra_blocksize = AES_BLOCK_SIZE,
  438. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  439. .cra_type = &crypto_blkcipher_type,
  440. .cra_module = THIS_MODULE,
  441. .cra_init = fallback_init_blk,
  442. .cra_exit = fallback_exit_blk,
  443. .cra_u = {
  444. .blkcipher = {
  445. .min_keysize = AES_MIN_KEY_SIZE,
  446. .max_keysize = AES_MAX_KEY_SIZE,
  447. .ivsize = AES_BLOCK_SIZE,
  448. .setkey = cbc_aes_set_key,
  449. .encrypt = cbc_aes_encrypt,
  450. .decrypt = cbc_aes_decrypt,
  451. }
  452. }
  453. };
  454. static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
  455. unsigned int len)
  456. {
  457. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  458. unsigned int ret;
  459. xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  460. xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags &
  461. CRYPTO_TFM_REQ_MASK);
  462. ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len);
  463. if (ret) {
  464. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  465. tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags &
  466. CRYPTO_TFM_RES_MASK);
  467. }
  468. return ret;
  469. }
  470. static int xts_fallback_decrypt(struct blkcipher_desc *desc,
  471. struct scatterlist *dst, struct scatterlist *src,
  472. unsigned int nbytes)
  473. {
  474. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  475. struct crypto_blkcipher *tfm;
  476. unsigned int ret;
  477. tfm = desc->tfm;
  478. desc->tfm = xts_ctx->fallback;
  479. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  480. desc->tfm = tfm;
  481. return ret;
  482. }
  483. static int xts_fallback_encrypt(struct blkcipher_desc *desc,
  484. struct scatterlist *dst, struct scatterlist *src,
  485. unsigned int nbytes)
  486. {
  487. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  488. struct crypto_blkcipher *tfm;
  489. unsigned int ret;
  490. tfm = desc->tfm;
  491. desc->tfm = xts_ctx->fallback;
  492. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  493. desc->tfm = tfm;
  494. return ret;
  495. }
  496. static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  497. unsigned int key_len)
  498. {
  499. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  500. u32 *flags = &tfm->crt_flags;
  501. int err;
  502. err = xts_check_key(tfm, in_key, key_len);
  503. if (err)
  504. return err;
  505. switch (key_len) {
  506. case 32:
  507. xts_ctx->enc = CPACF_KM_XTS_128_ENC;
  508. xts_ctx->dec = CPACF_KM_XTS_128_DEC;
  509. memcpy(xts_ctx->key + 16, in_key, 16);
  510. memcpy(xts_ctx->pcc_key + 16, in_key + 16, 16);
  511. break;
  512. case 48:
  513. xts_ctx->enc = 0;
  514. xts_ctx->dec = 0;
  515. xts_fallback_setkey(tfm, in_key, key_len);
  516. break;
  517. case 64:
  518. xts_ctx->enc = CPACF_KM_XTS_256_ENC;
  519. xts_ctx->dec = CPACF_KM_XTS_256_DEC;
  520. memcpy(xts_ctx->key, in_key, 32);
  521. memcpy(xts_ctx->pcc_key, in_key + 32, 32);
  522. break;
  523. default:
  524. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  525. return -EINVAL;
  526. }
  527. xts_ctx->key_len = key_len;
  528. return 0;
  529. }
  530. static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
  531. struct s390_xts_ctx *xts_ctx,
  532. struct blkcipher_walk *walk)
  533. {
  534. unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
  535. int ret = blkcipher_walk_virt(desc, walk);
  536. unsigned int nbytes = walk->nbytes;
  537. unsigned int n;
  538. u8 *in, *out;
  539. struct pcc_param pcc_param;
  540. struct {
  541. u8 key[32];
  542. u8 init[16];
  543. } xts_param;
  544. if (!nbytes)
  545. goto out;
  546. memset(pcc_param.block, 0, sizeof(pcc_param.block));
  547. memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
  548. memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
  549. memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
  550. memcpy(pcc_param.key, xts_ctx->pcc_key, 32);
  551. /* remove decipher modifier bit from 'func' and call PCC */
  552. ret = cpacf_pcc(func & 0x7f, &pcc_param.key[offset]);
  553. if (ret < 0)
  554. return -EIO;
  555. memcpy(xts_param.key, xts_ctx->key, 32);
  556. memcpy(xts_param.init, pcc_param.xts, 16);
  557. do {
  558. /* only use complete blocks */
  559. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  560. out = walk->dst.virt.addr;
  561. in = walk->src.virt.addr;
  562. ret = cpacf_km(func, &xts_param.key[offset], out, in, n);
  563. if (ret < 0 || ret != n)
  564. return -EIO;
  565. nbytes &= AES_BLOCK_SIZE - 1;
  566. ret = blkcipher_walk_done(desc, walk, nbytes);
  567. } while ((nbytes = walk->nbytes));
  568. out:
  569. return ret;
  570. }
  571. static int xts_aes_encrypt(struct blkcipher_desc *desc,
  572. struct scatterlist *dst, struct scatterlist *src,
  573. unsigned int nbytes)
  574. {
  575. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  576. struct blkcipher_walk walk;
  577. if (unlikely(xts_ctx->key_len == 48))
  578. return xts_fallback_encrypt(desc, dst, src, nbytes);
  579. blkcipher_walk_init(&walk, dst, src, nbytes);
  580. return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
  581. }
  582. static int xts_aes_decrypt(struct blkcipher_desc *desc,
  583. struct scatterlist *dst, struct scatterlist *src,
  584. unsigned int nbytes)
  585. {
  586. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  587. struct blkcipher_walk walk;
  588. if (unlikely(xts_ctx->key_len == 48))
  589. return xts_fallback_decrypt(desc, dst, src, nbytes);
  590. blkcipher_walk_init(&walk, dst, src, nbytes);
  591. return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
  592. }
  593. static int xts_fallback_init(struct crypto_tfm *tfm)
  594. {
  595. const char *name = tfm->__crt_alg->cra_name;
  596. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  597. xts_ctx->fallback = crypto_alloc_blkcipher(name, 0,
  598. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  599. if (IS_ERR(xts_ctx->fallback)) {
  600. pr_err("Allocating XTS fallback algorithm %s failed\n",
  601. name);
  602. return PTR_ERR(xts_ctx->fallback);
  603. }
  604. return 0;
  605. }
  606. static void xts_fallback_exit(struct crypto_tfm *tfm)
  607. {
  608. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  609. crypto_free_blkcipher(xts_ctx->fallback);
  610. xts_ctx->fallback = NULL;
  611. }
  612. static struct crypto_alg xts_aes_alg = {
  613. .cra_name = "xts(aes)",
  614. .cra_driver_name = "xts-aes-s390",
  615. .cra_priority = 400, /* combo: aes + xts */
  616. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  617. CRYPTO_ALG_NEED_FALLBACK,
  618. .cra_blocksize = AES_BLOCK_SIZE,
  619. .cra_ctxsize = sizeof(struct s390_xts_ctx),
  620. .cra_type = &crypto_blkcipher_type,
  621. .cra_module = THIS_MODULE,
  622. .cra_init = xts_fallback_init,
  623. .cra_exit = xts_fallback_exit,
  624. .cra_u = {
  625. .blkcipher = {
  626. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  627. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  628. .ivsize = AES_BLOCK_SIZE,
  629. .setkey = xts_aes_set_key,
  630. .encrypt = xts_aes_encrypt,
  631. .decrypt = xts_aes_decrypt,
  632. }
  633. }
  634. };
  635. static int xts_aes_alg_reg;
  636. static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  637. unsigned int key_len)
  638. {
  639. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  640. switch (key_len) {
  641. case 16:
  642. sctx->enc = CPACF_KMCTR_AES_128_ENC;
  643. sctx->dec = CPACF_KMCTR_AES_128_DEC;
  644. break;
  645. case 24:
  646. sctx->enc = CPACF_KMCTR_AES_192_ENC;
  647. sctx->dec = CPACF_KMCTR_AES_192_DEC;
  648. break;
  649. case 32:
  650. sctx->enc = CPACF_KMCTR_AES_256_ENC;
  651. sctx->dec = CPACF_KMCTR_AES_256_DEC;
  652. break;
  653. }
  654. return aes_set_key(tfm, in_key, key_len);
  655. }
  656. static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
  657. {
  658. unsigned int i, n;
  659. /* only use complete blocks, max. PAGE_SIZE */
  660. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
  661. for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
  662. memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE,
  663. AES_BLOCK_SIZE);
  664. crypto_inc(ctrptr + i, AES_BLOCK_SIZE);
  665. }
  666. return n;
  667. }
  668. static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
  669. struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
  670. {
  671. int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
  672. unsigned int n, nbytes;
  673. u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE];
  674. u8 *out, *in, *ctrptr = ctrbuf;
  675. if (!walk->nbytes)
  676. return ret;
  677. if (spin_trylock(&ctrblk_lock))
  678. ctrptr = ctrblk;
  679. memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE);
  680. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  681. out = walk->dst.virt.addr;
  682. in = walk->src.virt.addr;
  683. while (nbytes >= AES_BLOCK_SIZE) {
  684. if (ctrptr == ctrblk)
  685. n = __ctrblk_init(ctrptr, nbytes);
  686. else
  687. n = AES_BLOCK_SIZE;
  688. ret = cpacf_kmctr(func, sctx->key, out, in, n, ctrptr);
  689. if (ret < 0 || ret != n) {
  690. if (ctrptr == ctrblk)
  691. spin_unlock(&ctrblk_lock);
  692. return -EIO;
  693. }
  694. if (n > AES_BLOCK_SIZE)
  695. memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE,
  696. AES_BLOCK_SIZE);
  697. crypto_inc(ctrptr, AES_BLOCK_SIZE);
  698. out += n;
  699. in += n;
  700. nbytes -= n;
  701. }
  702. ret = blkcipher_walk_done(desc, walk, nbytes);
  703. }
  704. if (ctrptr == ctrblk) {
  705. if (nbytes)
  706. memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE);
  707. else
  708. memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
  709. spin_unlock(&ctrblk_lock);
  710. } else {
  711. if (!nbytes)
  712. memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
  713. }
  714. /*
  715. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  716. */
  717. if (nbytes) {
  718. out = walk->dst.virt.addr;
  719. in = walk->src.virt.addr;
  720. ret = cpacf_kmctr(func, sctx->key, buf, in,
  721. AES_BLOCK_SIZE, ctrbuf);
  722. if (ret < 0 || ret != AES_BLOCK_SIZE)
  723. return -EIO;
  724. memcpy(out, buf, nbytes);
  725. crypto_inc(ctrbuf, AES_BLOCK_SIZE);
  726. ret = blkcipher_walk_done(desc, walk, 0);
  727. memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE);
  728. }
  729. return ret;
  730. }
  731. static int ctr_aes_encrypt(struct blkcipher_desc *desc,
  732. struct scatterlist *dst, struct scatterlist *src,
  733. unsigned int nbytes)
  734. {
  735. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  736. struct blkcipher_walk walk;
  737. blkcipher_walk_init(&walk, dst, src, nbytes);
  738. return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
  739. }
  740. static int ctr_aes_decrypt(struct blkcipher_desc *desc,
  741. struct scatterlist *dst, struct scatterlist *src,
  742. unsigned int nbytes)
  743. {
  744. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  745. struct blkcipher_walk walk;
  746. blkcipher_walk_init(&walk, dst, src, nbytes);
  747. return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
  748. }
  749. static struct crypto_alg ctr_aes_alg = {
  750. .cra_name = "ctr(aes)",
  751. .cra_driver_name = "ctr-aes-s390",
  752. .cra_priority = 400, /* combo: aes + ctr */
  753. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  754. .cra_blocksize = 1,
  755. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  756. .cra_type = &crypto_blkcipher_type,
  757. .cra_module = THIS_MODULE,
  758. .cra_u = {
  759. .blkcipher = {
  760. .min_keysize = AES_MIN_KEY_SIZE,
  761. .max_keysize = AES_MAX_KEY_SIZE,
  762. .ivsize = AES_BLOCK_SIZE,
  763. .setkey = ctr_aes_set_key,
  764. .encrypt = ctr_aes_encrypt,
  765. .decrypt = ctr_aes_decrypt,
  766. }
  767. }
  768. };
  769. static int ctr_aes_alg_reg;
  770. static int __init aes_s390_init(void)
  771. {
  772. int ret;
  773. if (cpacf_query(CPACF_KM, CPACF_KM_AES_128_ENC))
  774. keylen_flag |= AES_KEYLEN_128;
  775. if (cpacf_query(CPACF_KM, CPACF_KM_AES_192_ENC))
  776. keylen_flag |= AES_KEYLEN_192;
  777. if (cpacf_query(CPACF_KM, CPACF_KM_AES_256_ENC))
  778. keylen_flag |= AES_KEYLEN_256;
  779. if (!keylen_flag)
  780. return -EOPNOTSUPP;
  781. /* z9 109 and z9 BC/EC only support 128 bit key length */
  782. if (keylen_flag == AES_KEYLEN_128)
  783. pr_info("AES hardware acceleration is only available for"
  784. " 128-bit keys\n");
  785. ret = crypto_register_alg(&aes_alg);
  786. if (ret)
  787. goto aes_err;
  788. ret = crypto_register_alg(&ecb_aes_alg);
  789. if (ret)
  790. goto ecb_aes_err;
  791. ret = crypto_register_alg(&cbc_aes_alg);
  792. if (ret)
  793. goto cbc_aes_err;
  794. if (cpacf_query(CPACF_KM, CPACF_KM_XTS_128_ENC) &&
  795. cpacf_query(CPACF_KM, CPACF_KM_XTS_256_ENC)) {
  796. ret = crypto_register_alg(&xts_aes_alg);
  797. if (ret)
  798. goto xts_aes_err;
  799. xts_aes_alg_reg = 1;
  800. }
  801. if (cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_128_ENC) &&
  802. cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_192_ENC) &&
  803. cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_256_ENC)) {
  804. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  805. if (!ctrblk) {
  806. ret = -ENOMEM;
  807. goto ctr_aes_err;
  808. }
  809. ret = crypto_register_alg(&ctr_aes_alg);
  810. if (ret) {
  811. free_page((unsigned long) ctrblk);
  812. goto ctr_aes_err;
  813. }
  814. ctr_aes_alg_reg = 1;
  815. }
  816. out:
  817. return ret;
  818. ctr_aes_err:
  819. crypto_unregister_alg(&xts_aes_alg);
  820. xts_aes_err:
  821. crypto_unregister_alg(&cbc_aes_alg);
  822. cbc_aes_err:
  823. crypto_unregister_alg(&ecb_aes_alg);
  824. ecb_aes_err:
  825. crypto_unregister_alg(&aes_alg);
  826. aes_err:
  827. goto out;
  828. }
  829. static void __exit aes_s390_fini(void)
  830. {
  831. if (ctr_aes_alg_reg) {
  832. crypto_unregister_alg(&ctr_aes_alg);
  833. free_page((unsigned long) ctrblk);
  834. }
  835. if (xts_aes_alg_reg)
  836. crypto_unregister_alg(&xts_aes_alg);
  837. crypto_unregister_alg(&cbc_aes_alg);
  838. crypto_unregister_alg(&ecb_aes_alg);
  839. crypto_unregister_alg(&aes_alg);
  840. }
  841. module_cpu_feature_match(MSA, aes_s390_init);
  842. module_exit(aes_s390_fini);
  843. MODULE_ALIAS_CRYPTO("aes-all");
  844. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  845. MODULE_LICENSE("GPL");