aes_s390.c 23 KB

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