aes_s390.c 25 KB

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