aes_s390.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. BUG_ON((ret < 0) || (ret != n));
  283. nbytes &= AES_BLOCK_SIZE - 1;
  284. ret = blkcipher_walk_done(desc, walk, nbytes);
  285. }
  286. return ret;
  287. }
  288. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  289. struct scatterlist *dst, struct scatterlist *src,
  290. unsigned int nbytes)
  291. {
  292. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  293. struct blkcipher_walk walk;
  294. if (unlikely(need_fallback(sctx->key_len)))
  295. return fallback_blk_enc(desc, dst, src, nbytes);
  296. blkcipher_walk_init(&walk, dst, src, nbytes);
  297. return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
  298. }
  299. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  300. struct scatterlist *dst, struct scatterlist *src,
  301. unsigned int nbytes)
  302. {
  303. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  304. struct blkcipher_walk walk;
  305. if (unlikely(need_fallback(sctx->key_len)))
  306. return fallback_blk_dec(desc, dst, src, nbytes);
  307. blkcipher_walk_init(&walk, dst, src, nbytes);
  308. return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
  309. }
  310. static int fallback_init_blk(struct crypto_tfm *tfm)
  311. {
  312. const char *name = tfm->__crt_alg->cra_name;
  313. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  314. sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
  315. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  316. if (IS_ERR(sctx->fallback.blk)) {
  317. pr_err("Allocating AES fallback algorithm %s failed\n",
  318. name);
  319. return PTR_ERR(sctx->fallback.blk);
  320. }
  321. return 0;
  322. }
  323. static void fallback_exit_blk(struct crypto_tfm *tfm)
  324. {
  325. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  326. crypto_free_blkcipher(sctx->fallback.blk);
  327. sctx->fallback.blk = NULL;
  328. }
  329. static struct crypto_alg ecb_aes_alg = {
  330. .cra_name = "ecb(aes)",
  331. .cra_driver_name = "ecb-aes-s390",
  332. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  333. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  334. CRYPTO_ALG_NEED_FALLBACK,
  335. .cra_blocksize = AES_BLOCK_SIZE,
  336. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  337. .cra_type = &crypto_blkcipher_type,
  338. .cra_module = THIS_MODULE,
  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, void *param,
  378. struct blkcipher_walk *walk)
  379. {
  380. int ret = blkcipher_walk_virt(desc, walk);
  381. unsigned int nbytes = walk->nbytes;
  382. if (!nbytes)
  383. goto out;
  384. memcpy(param, walk->iv, AES_BLOCK_SIZE);
  385. do {
  386. /* only use complete blocks */
  387. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  388. u8 *out = walk->dst.virt.addr;
  389. u8 *in = walk->src.virt.addr;
  390. ret = crypt_s390_kmc(func, param, out, in, n);
  391. BUG_ON((ret < 0) || (ret != n));
  392. nbytes &= AES_BLOCK_SIZE - 1;
  393. ret = blkcipher_walk_done(desc, walk, nbytes);
  394. } while ((nbytes = walk->nbytes));
  395. memcpy(walk->iv, param, AES_BLOCK_SIZE);
  396. out:
  397. return ret;
  398. }
  399. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  400. struct scatterlist *dst, struct scatterlist *src,
  401. unsigned int nbytes)
  402. {
  403. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  404. struct blkcipher_walk walk;
  405. if (unlikely(need_fallback(sctx->key_len)))
  406. return fallback_blk_enc(desc, dst, src, nbytes);
  407. blkcipher_walk_init(&walk, dst, src, nbytes);
  408. return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk);
  409. }
  410. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  411. struct scatterlist *dst, struct scatterlist *src,
  412. unsigned int nbytes)
  413. {
  414. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  415. struct blkcipher_walk walk;
  416. if (unlikely(need_fallback(sctx->key_len)))
  417. return fallback_blk_dec(desc, dst, src, nbytes);
  418. blkcipher_walk_init(&walk, dst, src, nbytes);
  419. return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk);
  420. }
  421. static struct crypto_alg cbc_aes_alg = {
  422. .cra_name = "cbc(aes)",
  423. .cra_driver_name = "cbc-aes-s390",
  424. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  425. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  426. CRYPTO_ALG_NEED_FALLBACK,
  427. .cra_blocksize = AES_BLOCK_SIZE,
  428. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  429. .cra_type = &crypto_blkcipher_type,
  430. .cra_module = THIS_MODULE,
  431. .cra_init = fallback_init_blk,
  432. .cra_exit = fallback_exit_blk,
  433. .cra_u = {
  434. .blkcipher = {
  435. .min_keysize = AES_MIN_KEY_SIZE,
  436. .max_keysize = AES_MAX_KEY_SIZE,
  437. .ivsize = AES_BLOCK_SIZE,
  438. .setkey = cbc_aes_set_key,
  439. .encrypt = cbc_aes_encrypt,
  440. .decrypt = cbc_aes_decrypt,
  441. }
  442. }
  443. };
  444. static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
  445. unsigned int len)
  446. {
  447. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  448. unsigned int ret;
  449. xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  450. xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags &
  451. CRYPTO_TFM_REQ_MASK);
  452. ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len);
  453. if (ret) {
  454. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  455. tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags &
  456. CRYPTO_TFM_RES_MASK);
  457. }
  458. return ret;
  459. }
  460. static int xts_fallback_decrypt(struct blkcipher_desc *desc,
  461. struct scatterlist *dst, struct scatterlist *src,
  462. unsigned int nbytes)
  463. {
  464. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  465. struct crypto_blkcipher *tfm;
  466. unsigned int ret;
  467. tfm = desc->tfm;
  468. desc->tfm = xts_ctx->fallback;
  469. ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
  470. desc->tfm = tfm;
  471. return ret;
  472. }
  473. static int xts_fallback_encrypt(struct blkcipher_desc *desc,
  474. struct scatterlist *dst, struct scatterlist *src,
  475. unsigned int nbytes)
  476. {
  477. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  478. struct crypto_blkcipher *tfm;
  479. unsigned int ret;
  480. tfm = desc->tfm;
  481. desc->tfm = xts_ctx->fallback;
  482. ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
  483. desc->tfm = tfm;
  484. return ret;
  485. }
  486. static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  487. unsigned int key_len)
  488. {
  489. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  490. u32 *flags = &tfm->crt_flags;
  491. switch (key_len) {
  492. case 32:
  493. xts_ctx->enc = KM_XTS_128_ENCRYPT;
  494. xts_ctx->dec = KM_XTS_128_DECRYPT;
  495. memcpy(xts_ctx->key + 16, in_key, 16);
  496. memcpy(xts_ctx->pcc.key + 16, in_key + 16, 16);
  497. break;
  498. case 48:
  499. xts_ctx->enc = 0;
  500. xts_ctx->dec = 0;
  501. xts_fallback_setkey(tfm, in_key, key_len);
  502. break;
  503. case 64:
  504. xts_ctx->enc = KM_XTS_256_ENCRYPT;
  505. xts_ctx->dec = KM_XTS_256_DECRYPT;
  506. memcpy(xts_ctx->key, in_key, 32);
  507. memcpy(xts_ctx->pcc.key, in_key + 32, 32);
  508. break;
  509. default:
  510. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  511. return -EINVAL;
  512. }
  513. xts_ctx->key_len = key_len;
  514. return 0;
  515. }
  516. static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
  517. struct s390_xts_ctx *xts_ctx,
  518. struct blkcipher_walk *walk)
  519. {
  520. unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
  521. int ret = blkcipher_walk_virt(desc, walk);
  522. unsigned int nbytes = walk->nbytes;
  523. unsigned int n;
  524. u8 *in, *out;
  525. void *param;
  526. if (!nbytes)
  527. goto out;
  528. memset(xts_ctx->pcc.block, 0, sizeof(xts_ctx->pcc.block));
  529. memset(xts_ctx->pcc.bit, 0, sizeof(xts_ctx->pcc.bit));
  530. memset(xts_ctx->pcc.xts, 0, sizeof(xts_ctx->pcc.xts));
  531. memcpy(xts_ctx->pcc.tweak, walk->iv, sizeof(xts_ctx->pcc.tweak));
  532. param = xts_ctx->pcc.key + offset;
  533. ret = crypt_s390_pcc(func, param);
  534. BUG_ON(ret < 0);
  535. memcpy(xts_ctx->xts_param, xts_ctx->pcc.xts, 16);
  536. param = xts_ctx->key + offset;
  537. do {
  538. /* only use complete blocks */
  539. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  540. out = walk->dst.virt.addr;
  541. in = walk->src.virt.addr;
  542. ret = crypt_s390_km(func, param, out, in, n);
  543. BUG_ON(ret < 0 || ret != n);
  544. nbytes &= AES_BLOCK_SIZE - 1;
  545. ret = blkcipher_walk_done(desc, walk, nbytes);
  546. } while ((nbytes = walk->nbytes));
  547. out:
  548. return ret;
  549. }
  550. static int xts_aes_encrypt(struct blkcipher_desc *desc,
  551. struct scatterlist *dst, struct scatterlist *src,
  552. unsigned int nbytes)
  553. {
  554. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  555. struct blkcipher_walk walk;
  556. if (unlikely(xts_ctx->key_len == 48))
  557. return xts_fallback_encrypt(desc, dst, src, nbytes);
  558. blkcipher_walk_init(&walk, dst, src, nbytes);
  559. return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
  560. }
  561. static int xts_aes_decrypt(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_decrypt(desc, dst, src, nbytes);
  569. blkcipher_walk_init(&walk, dst, src, nbytes);
  570. return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
  571. }
  572. static int xts_fallback_init(struct crypto_tfm *tfm)
  573. {
  574. const char *name = tfm->__crt_alg->cra_name;
  575. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  576. xts_ctx->fallback = crypto_alloc_blkcipher(name, 0,
  577. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  578. if (IS_ERR(xts_ctx->fallback)) {
  579. pr_err("Allocating XTS fallback algorithm %s failed\n",
  580. name);
  581. return PTR_ERR(xts_ctx->fallback);
  582. }
  583. return 0;
  584. }
  585. static void xts_fallback_exit(struct crypto_tfm *tfm)
  586. {
  587. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  588. crypto_free_blkcipher(xts_ctx->fallback);
  589. xts_ctx->fallback = NULL;
  590. }
  591. static struct crypto_alg xts_aes_alg = {
  592. .cra_name = "xts(aes)",
  593. .cra_driver_name = "xts-aes-s390",
  594. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  595. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  596. CRYPTO_ALG_NEED_FALLBACK,
  597. .cra_blocksize = AES_BLOCK_SIZE,
  598. .cra_ctxsize = sizeof(struct s390_xts_ctx),
  599. .cra_type = &crypto_blkcipher_type,
  600. .cra_module = THIS_MODULE,
  601. .cra_init = xts_fallback_init,
  602. .cra_exit = xts_fallback_exit,
  603. .cra_u = {
  604. .blkcipher = {
  605. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  606. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  607. .ivsize = AES_BLOCK_SIZE,
  608. .setkey = xts_aes_set_key,
  609. .encrypt = xts_aes_encrypt,
  610. .decrypt = xts_aes_decrypt,
  611. }
  612. }
  613. };
  614. static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  615. unsigned int key_len)
  616. {
  617. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  618. switch (key_len) {
  619. case 16:
  620. sctx->enc = KMCTR_AES_128_ENCRYPT;
  621. sctx->dec = KMCTR_AES_128_DECRYPT;
  622. break;
  623. case 24:
  624. sctx->enc = KMCTR_AES_192_ENCRYPT;
  625. sctx->dec = KMCTR_AES_192_DECRYPT;
  626. break;
  627. case 32:
  628. sctx->enc = KMCTR_AES_256_ENCRYPT;
  629. sctx->dec = KMCTR_AES_256_DECRYPT;
  630. break;
  631. }
  632. return aes_set_key(tfm, in_key, key_len);
  633. }
  634. static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
  635. struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
  636. {
  637. int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
  638. unsigned int i, n, nbytes;
  639. u8 buf[AES_BLOCK_SIZE];
  640. u8 *out, *in;
  641. if (!walk->nbytes)
  642. return ret;
  643. memcpy(ctrblk, walk->iv, AES_BLOCK_SIZE);
  644. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  645. out = walk->dst.virt.addr;
  646. in = walk->src.virt.addr;
  647. while (nbytes >= AES_BLOCK_SIZE) {
  648. /* only use complete blocks, max. PAGE_SIZE */
  649. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE :
  650. nbytes & ~(AES_BLOCK_SIZE - 1);
  651. for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
  652. memcpy(ctrblk + i, ctrblk + i - AES_BLOCK_SIZE,
  653. AES_BLOCK_SIZE);
  654. crypto_inc(ctrblk + i, AES_BLOCK_SIZE);
  655. }
  656. ret = crypt_s390_kmctr(func, sctx->key, out, in, n, ctrblk);
  657. BUG_ON(ret < 0 || ret != n);
  658. if (n > AES_BLOCK_SIZE)
  659. memcpy(ctrblk, ctrblk + n - AES_BLOCK_SIZE,
  660. AES_BLOCK_SIZE);
  661. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  662. out += n;
  663. in += n;
  664. nbytes -= n;
  665. }
  666. ret = blkcipher_walk_done(desc, walk, nbytes);
  667. }
  668. /*
  669. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  670. */
  671. if (nbytes) {
  672. out = walk->dst.virt.addr;
  673. in = walk->src.virt.addr;
  674. ret = crypt_s390_kmctr(func, sctx->key, buf, in,
  675. AES_BLOCK_SIZE, ctrblk);
  676. BUG_ON(ret < 0 || ret != AES_BLOCK_SIZE);
  677. memcpy(out, buf, nbytes);
  678. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  679. ret = blkcipher_walk_done(desc, walk, 0);
  680. }
  681. memcpy(walk->iv, ctrblk, AES_BLOCK_SIZE);
  682. return ret;
  683. }
  684. static int ctr_aes_encrypt(struct blkcipher_desc *desc,
  685. struct scatterlist *dst, struct scatterlist *src,
  686. unsigned int nbytes)
  687. {
  688. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  689. struct blkcipher_walk walk;
  690. blkcipher_walk_init(&walk, dst, src, nbytes);
  691. return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
  692. }
  693. static int ctr_aes_decrypt(struct blkcipher_desc *desc,
  694. struct scatterlist *dst, struct scatterlist *src,
  695. unsigned int nbytes)
  696. {
  697. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  698. struct blkcipher_walk walk;
  699. blkcipher_walk_init(&walk, dst, src, nbytes);
  700. return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
  701. }
  702. static struct crypto_alg ctr_aes_alg = {
  703. .cra_name = "ctr(aes)",
  704. .cra_driver_name = "ctr-aes-s390",
  705. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  706. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  707. .cra_blocksize = 1,
  708. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  709. .cra_type = &crypto_blkcipher_type,
  710. .cra_module = THIS_MODULE,
  711. .cra_u = {
  712. .blkcipher = {
  713. .min_keysize = AES_MIN_KEY_SIZE,
  714. .max_keysize = AES_MAX_KEY_SIZE,
  715. .ivsize = AES_BLOCK_SIZE,
  716. .setkey = ctr_aes_set_key,
  717. .encrypt = ctr_aes_encrypt,
  718. .decrypt = ctr_aes_decrypt,
  719. }
  720. }
  721. };
  722. static int __init aes_s390_init(void)
  723. {
  724. int ret;
  725. if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA))
  726. keylen_flag |= AES_KEYLEN_128;
  727. if (crypt_s390_func_available(KM_AES_192_ENCRYPT, CRYPT_S390_MSA))
  728. keylen_flag |= AES_KEYLEN_192;
  729. if (crypt_s390_func_available(KM_AES_256_ENCRYPT, CRYPT_S390_MSA))
  730. keylen_flag |= AES_KEYLEN_256;
  731. if (!keylen_flag)
  732. return -EOPNOTSUPP;
  733. /* z9 109 and z9 BC/EC only support 128 bit key length */
  734. if (keylen_flag == AES_KEYLEN_128)
  735. pr_info("AES hardware acceleration is only available for"
  736. " 128-bit keys\n");
  737. ret = crypto_register_alg(&aes_alg);
  738. if (ret)
  739. goto aes_err;
  740. ret = crypto_register_alg(&ecb_aes_alg);
  741. if (ret)
  742. goto ecb_aes_err;
  743. ret = crypto_register_alg(&cbc_aes_alg);
  744. if (ret)
  745. goto cbc_aes_err;
  746. if (crypt_s390_func_available(KM_XTS_128_ENCRYPT,
  747. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  748. crypt_s390_func_available(KM_XTS_256_ENCRYPT,
  749. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  750. ret = crypto_register_alg(&xts_aes_alg);
  751. if (ret)
  752. goto xts_aes_err;
  753. }
  754. if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT,
  755. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  756. crypt_s390_func_available(KMCTR_AES_192_ENCRYPT,
  757. CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
  758. crypt_s390_func_available(KMCTR_AES_256_ENCRYPT,
  759. CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
  760. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  761. if (!ctrblk) {
  762. ret = -ENOMEM;
  763. goto ctr_aes_err;
  764. }
  765. ret = crypto_register_alg(&ctr_aes_alg);
  766. if (ret) {
  767. free_page((unsigned long) ctrblk);
  768. goto ctr_aes_err;
  769. }
  770. }
  771. out:
  772. return ret;
  773. ctr_aes_err:
  774. crypto_unregister_alg(&xts_aes_alg);
  775. xts_aes_err:
  776. crypto_unregister_alg(&cbc_aes_alg);
  777. cbc_aes_err:
  778. crypto_unregister_alg(&ecb_aes_alg);
  779. ecb_aes_err:
  780. crypto_unregister_alg(&aes_alg);
  781. aes_err:
  782. goto out;
  783. }
  784. static void __exit aes_s390_fini(void)
  785. {
  786. crypto_unregister_alg(&ctr_aes_alg);
  787. free_page((unsigned long) ctrblk);
  788. crypto_unregister_alg(&xts_aes_alg);
  789. crypto_unregister_alg(&cbc_aes_alg);
  790. crypto_unregister_alg(&ecb_aes_alg);
  791. crypto_unregister_alg(&aes_alg);
  792. }
  793. module_init(aes_s390_init);
  794. module_exit(aes_s390_fini);
  795. MODULE_ALIAS("aes-all");
  796. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  797. MODULE_LICENSE("GPL");