aes_s390.c 24 KB

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