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_init = fallback_init_cip,
  192. .cra_exit = fallback_exit_cip,
  193. .cra_u = {
  194. .cipher = {
  195. .cia_min_keysize = AES_MIN_KEY_SIZE,
  196. .cia_max_keysize = AES_MAX_KEY_SIZE,
  197. .cia_setkey = aes_set_key,
  198. .cia_encrypt = aes_encrypt,
  199. .cia_decrypt = aes_decrypt,
  200. }
  201. }
  202. };
  203. static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
  204. unsigned int len)
  205. {
  206. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  207. unsigned int ret;
  208. sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  209. sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
  210. CRYPTO_TFM_REQ_MASK);
  211. ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
  212. if (ret) {
  213. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  214. tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
  215. CRYPTO_TFM_RES_MASK);
  216. }
  217. return ret;
  218. }
  219. static int fallback_blk_dec(struct blkcipher_desc *desc,
  220. struct scatterlist *dst, struct scatterlist *src,
  221. unsigned int nbytes)
  222. {
  223. unsigned int ret;
  224. struct crypto_blkcipher *tfm;
  225. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  226. tfm = desc->tfm;
  227. desc->tfm = sctx->fallback.blk;
  228. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  229. desc->tfm = tfm;
  230. return ret;
  231. }
  232. static int fallback_blk_enc(struct blkcipher_desc *desc,
  233. struct scatterlist *dst, struct scatterlist *src,
  234. unsigned int nbytes)
  235. {
  236. unsigned int ret;
  237. struct crypto_blkcipher *tfm;
  238. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  239. tfm = desc->tfm;
  240. desc->tfm = sctx->fallback.blk;
  241. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  242. desc->tfm = tfm;
  243. return ret;
  244. }
  245. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  246. unsigned int key_len)
  247. {
  248. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  249. int ret;
  250. ret = need_fallback(key_len);
  251. if (ret > 0) {
  252. sctx->key_len = key_len;
  253. return setkey_fallback_blk(tfm, in_key, key_len);
  254. }
  255. switch (key_len) {
  256. case 16:
  257. sctx->enc = KM_AES_128_ENCRYPT;
  258. sctx->dec = KM_AES_128_DECRYPT;
  259. break;
  260. case 24:
  261. sctx->enc = KM_AES_192_ENCRYPT;
  262. sctx->dec = KM_AES_192_DECRYPT;
  263. break;
  264. case 32:
  265. sctx->enc = KM_AES_256_ENCRYPT;
  266. sctx->dec = KM_AES_256_DECRYPT;
  267. break;
  268. }
  269. return aes_set_key(tfm, in_key, key_len);
  270. }
  271. static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  272. struct blkcipher_walk *walk)
  273. {
  274. int ret = blkcipher_walk_virt(desc, walk);
  275. unsigned int nbytes;
  276. while ((nbytes = walk->nbytes)) {
  277. /* only use complete blocks */
  278. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  279. u8 *out = walk->dst.virt.addr;
  280. u8 *in = walk->src.virt.addr;
  281. ret = crypt_s390_km(func, param, out, in, n);
  282. if (ret < 0 || ret != n)
  283. return -EIO;
  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_init = fallback_init_blk,
  341. .cra_exit = fallback_exit_blk,
  342. .cra_u = {
  343. .blkcipher = {
  344. .min_keysize = AES_MIN_KEY_SIZE,
  345. .max_keysize = AES_MAX_KEY_SIZE,
  346. .setkey = ecb_aes_set_key,
  347. .encrypt = ecb_aes_encrypt,
  348. .decrypt = ecb_aes_decrypt,
  349. }
  350. }
  351. };
  352. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  353. unsigned int key_len)
  354. {
  355. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  356. int ret;
  357. ret = need_fallback(key_len);
  358. if (ret > 0) {
  359. sctx->key_len = key_len;
  360. return setkey_fallback_blk(tfm, in_key, key_len);
  361. }
  362. switch (key_len) {
  363. case 16:
  364. sctx->enc = KMC_AES_128_ENCRYPT;
  365. sctx->dec = KMC_AES_128_DECRYPT;
  366. break;
  367. case 24:
  368. sctx->enc = KMC_AES_192_ENCRYPT;
  369. sctx->dec = KMC_AES_192_DECRYPT;
  370. break;
  371. case 32:
  372. sctx->enc = KMC_AES_256_ENCRYPT;
  373. sctx->dec = KMC_AES_256_DECRYPT;
  374. break;
  375. }
  376. return aes_set_key(tfm, in_key, key_len);
  377. }
  378. static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  379. struct blkcipher_walk *walk)
  380. {
  381. int ret = blkcipher_walk_virt(desc, walk);
  382. unsigned int nbytes = walk->nbytes;
  383. if (!nbytes)
  384. goto out;
  385. memcpy(param, walk->iv, AES_BLOCK_SIZE);
  386. do {
  387. /* only use complete blocks */
  388. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  389. u8 *out = walk->dst.virt.addr;
  390. u8 *in = walk->src.virt.addr;
  391. ret = crypt_s390_kmc(func, param, out, in, n);
  392. if (ret < 0 || ret != n)
  393. return -EIO;
  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_init = fallback_init_blk,
  434. .cra_exit = fallback_exit_blk,
  435. .cra_u = {
  436. .blkcipher = {
  437. .min_keysize = AES_MIN_KEY_SIZE,
  438. .max_keysize = AES_MAX_KEY_SIZE,
  439. .ivsize = AES_BLOCK_SIZE,
  440. .setkey = cbc_aes_set_key,
  441. .encrypt = cbc_aes_encrypt,
  442. .decrypt = cbc_aes_decrypt,
  443. }
  444. }
  445. };
  446. static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
  447. unsigned int len)
  448. {
  449. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  450. unsigned int ret;
  451. xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  452. xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags &
  453. CRYPTO_TFM_REQ_MASK);
  454. ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len);
  455. if (ret) {
  456. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  457. tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags &
  458. CRYPTO_TFM_RES_MASK);
  459. }
  460. return ret;
  461. }
  462. static int xts_fallback_decrypt(struct blkcipher_desc *desc,
  463. struct scatterlist *dst, struct scatterlist *src,
  464. unsigned int nbytes)
  465. {
  466. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  467. struct crypto_blkcipher *tfm;
  468. unsigned int ret;
  469. tfm = desc->tfm;
  470. desc->tfm = xts_ctx->fallback;
  471. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  472. desc->tfm = tfm;
  473. return ret;
  474. }
  475. static int xts_fallback_encrypt(struct blkcipher_desc *desc,
  476. struct scatterlist *dst, struct scatterlist *src,
  477. unsigned int nbytes)
  478. {
  479. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  480. struct crypto_blkcipher *tfm;
  481. unsigned int ret;
  482. tfm = desc->tfm;
  483. desc->tfm = xts_ctx->fallback;
  484. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  485. desc->tfm = tfm;
  486. return ret;
  487. }
  488. static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  489. unsigned int key_len)
  490. {
  491. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  492. u32 *flags = &tfm->crt_flags;
  493. switch (key_len) {
  494. case 32:
  495. xts_ctx->enc = KM_XTS_128_ENCRYPT;
  496. xts_ctx->dec = KM_XTS_128_DECRYPT;
  497. memcpy(xts_ctx->key + 16, in_key, 16);
  498. memcpy(xts_ctx->pcc.key + 16, in_key + 16, 16);
  499. break;
  500. case 48:
  501. xts_ctx->enc = 0;
  502. xts_ctx->dec = 0;
  503. xts_fallback_setkey(tfm, in_key, key_len);
  504. break;
  505. case 64:
  506. xts_ctx->enc = KM_XTS_256_ENCRYPT;
  507. xts_ctx->dec = KM_XTS_256_DECRYPT;
  508. memcpy(xts_ctx->key, in_key, 32);
  509. memcpy(xts_ctx->pcc.key, in_key + 32, 32);
  510. break;
  511. default:
  512. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  513. return -EINVAL;
  514. }
  515. xts_ctx->key_len = key_len;
  516. return 0;
  517. }
  518. static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
  519. struct s390_xts_ctx *xts_ctx,
  520. struct blkcipher_walk *walk)
  521. {
  522. unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
  523. int ret = blkcipher_walk_virt(desc, walk);
  524. unsigned int nbytes = walk->nbytes;
  525. unsigned int n;
  526. u8 *in, *out;
  527. void *param;
  528. if (!nbytes)
  529. goto out;
  530. memset(xts_ctx->pcc.block, 0, sizeof(xts_ctx->pcc.block));
  531. memset(xts_ctx->pcc.bit, 0, sizeof(xts_ctx->pcc.bit));
  532. memset(xts_ctx->pcc.xts, 0, sizeof(xts_ctx->pcc.xts));
  533. memcpy(xts_ctx->pcc.tweak, walk->iv, sizeof(xts_ctx->pcc.tweak));
  534. param = xts_ctx->pcc.key + offset;
  535. ret = crypt_s390_pcc(func, param);
  536. if (ret < 0)
  537. return -EIO;
  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. if (ret < 0 || ret != n)
  547. return -EIO;
  548. nbytes &= AES_BLOCK_SIZE - 1;
  549. ret = blkcipher_walk_done(desc, walk, nbytes);
  550. } while ((nbytes = walk->nbytes));
  551. out:
  552. return ret;
  553. }
  554. static int xts_aes_encrypt(struct blkcipher_desc *desc,
  555. struct scatterlist *dst, struct scatterlist *src,
  556. unsigned int nbytes)
  557. {
  558. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  559. struct blkcipher_walk walk;
  560. if (unlikely(xts_ctx->key_len == 48))
  561. return xts_fallback_encrypt(desc, dst, src, nbytes);
  562. blkcipher_walk_init(&walk, dst, src, nbytes);
  563. return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
  564. }
  565. static int xts_aes_decrypt(struct blkcipher_desc *desc,
  566. struct scatterlist *dst, struct scatterlist *src,
  567. unsigned int nbytes)
  568. {
  569. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  570. struct blkcipher_walk walk;
  571. if (unlikely(xts_ctx->key_len == 48))
  572. return xts_fallback_decrypt(desc, dst, src, nbytes);
  573. blkcipher_walk_init(&walk, dst, src, nbytes);
  574. return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
  575. }
  576. static int xts_fallback_init(struct crypto_tfm *tfm)
  577. {
  578. const char *name = tfm->__crt_alg->cra_name;
  579. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  580. xts_ctx->fallback = crypto_alloc_blkcipher(name, 0,
  581. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  582. if (IS_ERR(xts_ctx->fallback)) {
  583. pr_err("Allocating XTS fallback algorithm %s failed\n",
  584. name);
  585. return PTR_ERR(xts_ctx->fallback);
  586. }
  587. return 0;
  588. }
  589. static void xts_fallback_exit(struct crypto_tfm *tfm)
  590. {
  591. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  592. crypto_free_blkcipher(xts_ctx->fallback);
  593. xts_ctx->fallback = NULL;
  594. }
  595. static struct crypto_alg xts_aes_alg = {
  596. .cra_name = "xts(aes)",
  597. .cra_driver_name = "xts-aes-s390",
  598. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  599. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  600. CRYPTO_ALG_NEED_FALLBACK,
  601. .cra_blocksize = AES_BLOCK_SIZE,
  602. .cra_ctxsize = sizeof(struct s390_xts_ctx),
  603. .cra_type = &crypto_blkcipher_type,
  604. .cra_module = THIS_MODULE,
  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. if (ret < 0 || ret != n)
  662. return -EIO;
  663. if (n > AES_BLOCK_SIZE)
  664. memcpy(ctrblk, ctrblk + n - AES_BLOCK_SIZE,
  665. AES_BLOCK_SIZE);
  666. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  667. out += n;
  668. in += n;
  669. nbytes -= n;
  670. }
  671. ret = blkcipher_walk_done(desc, walk, nbytes);
  672. }
  673. /*
  674. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  675. */
  676. if (nbytes) {
  677. out = walk->dst.virt.addr;
  678. in = walk->src.virt.addr;
  679. ret = crypt_s390_kmctr(func, sctx->key, buf, in,
  680. AES_BLOCK_SIZE, ctrblk);
  681. if (ret < 0 || ret != AES_BLOCK_SIZE)
  682. return -EIO;
  683. memcpy(out, buf, nbytes);
  684. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  685. ret = blkcipher_walk_done(desc, walk, 0);
  686. }
  687. memcpy(walk->iv, ctrblk, AES_BLOCK_SIZE);
  688. return ret;
  689. }
  690. static int ctr_aes_encrypt(struct blkcipher_desc *desc,
  691. struct scatterlist *dst, struct scatterlist *src,
  692. unsigned int nbytes)
  693. {
  694. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  695. struct blkcipher_walk walk;
  696. blkcipher_walk_init(&walk, dst, src, nbytes);
  697. return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
  698. }
  699. static int ctr_aes_decrypt(struct blkcipher_desc *desc,
  700. struct scatterlist *dst, struct scatterlist *src,
  701. unsigned int nbytes)
  702. {
  703. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  704. struct blkcipher_walk walk;
  705. blkcipher_walk_init(&walk, dst, src, nbytes);
  706. return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
  707. }
  708. static struct crypto_alg ctr_aes_alg = {
  709. .cra_name = "ctr(aes)",
  710. .cra_driver_name = "ctr-aes-s390",
  711. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  712. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  713. .cra_blocksize = 1,
  714. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  715. .cra_type = &crypto_blkcipher_type,
  716. .cra_module = THIS_MODULE,
  717. .cra_u = {
  718. .blkcipher = {
  719. .min_keysize = AES_MIN_KEY_SIZE,
  720. .max_keysize = AES_MAX_KEY_SIZE,
  721. .ivsize = AES_BLOCK_SIZE,
  722. .setkey = ctr_aes_set_key,
  723. .encrypt = ctr_aes_encrypt,
  724. .decrypt = ctr_aes_decrypt,
  725. }
  726. }
  727. };
  728. static int __init aes_s390_init(void)
  729. {
  730. int ret;
  731. if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA))
  732. keylen_flag |= AES_KEYLEN_128;
  733. if (crypt_s390_func_available(KM_AES_192_ENCRYPT, CRYPT_S390_MSA))
  734. keylen_flag |= AES_KEYLEN_192;
  735. if (crypt_s390_func_available(KM_AES_256_ENCRYPT, CRYPT_S390_MSA))
  736. keylen_flag |= AES_KEYLEN_256;
  737. if (!keylen_flag)
  738. return -EOPNOTSUPP;
  739. /* z9 109 and z9 BC/EC only support 128 bit key length */
  740. if (keylen_flag == AES_KEYLEN_128)
  741. pr_info("AES hardware acceleration is only available for"
  742. " 128-bit keys\n");
  743. ret = crypto_register_alg(&aes_alg);
  744. if (ret)
  745. goto aes_err;
  746. ret = crypto_register_alg(&ecb_aes_alg);
  747. if (ret)
  748. goto ecb_aes_err;
  749. ret = crypto_register_alg(&cbc_aes_alg);
  750. if (ret)
  751. goto cbc_aes_err;
  752. if (crypt_s390_func_available(KM_XTS_128_ENCRYPT,
  753. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  754. crypt_s390_func_available(KM_XTS_256_ENCRYPT,
  755. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  756. ret = crypto_register_alg(&xts_aes_alg);
  757. if (ret)
  758. goto xts_aes_err;
  759. }
  760. if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT,
  761. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  762. crypt_s390_func_available(KMCTR_AES_192_ENCRYPT,
  763. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  764. crypt_s390_func_available(KMCTR_AES_256_ENCRYPT,
  765. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  766. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  767. if (!ctrblk) {
  768. ret = -ENOMEM;
  769. goto ctr_aes_err;
  770. }
  771. ret = crypto_register_alg(&ctr_aes_alg);
  772. if (ret) {
  773. free_page((unsigned long) ctrblk);
  774. goto ctr_aes_err;
  775. }
  776. }
  777. out:
  778. return ret;
  779. ctr_aes_err:
  780. crypto_unregister_alg(&xts_aes_alg);
  781. xts_aes_err:
  782. crypto_unregister_alg(&cbc_aes_alg);
  783. cbc_aes_err:
  784. crypto_unregister_alg(&ecb_aes_alg);
  785. ecb_aes_err:
  786. crypto_unregister_alg(&aes_alg);
  787. aes_err:
  788. goto out;
  789. }
  790. static void __exit aes_s390_fini(void)
  791. {
  792. crypto_unregister_alg(&ctr_aes_alg);
  793. free_page((unsigned long) ctrblk);
  794. crypto_unregister_alg(&xts_aes_alg);
  795. crypto_unregister_alg(&cbc_aes_alg);
  796. crypto_unregister_alg(&ecb_aes_alg);
  797. crypto_unregister_alg(&aes_alg);
  798. }
  799. module_init(aes_s390_init);
  800. module_exit(aes_s390_fini);
  801. MODULE_ALIAS("aes-all");
  802. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  803. MODULE_LICENSE("GPL");