aes_s390.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright IBM Corp. 2005, 2007
  8. * Author(s): Jan Glauber (jang@de.ibm.com)
  9. * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
  10. *
  11. * Derived from "crypto/aes_generic.c"
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. */
  19. #define KMSG_COMPONENT "aes_s390"
  20. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  21. #include <crypto/aes.h>
  22. #include <crypto/algapi.h>
  23. #include <crypto/internal/skcipher.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/cpufeature.h>
  27. #include <linux/init.h>
  28. #include <linux/spinlock.h>
  29. #include <crypto/xts.h>
  30. #include <asm/cpacf.h>
  31. static u8 *ctrblk;
  32. static DEFINE_SPINLOCK(ctrblk_lock);
  33. static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
  34. struct s390_aes_ctx {
  35. u8 key[AES_MAX_KEY_SIZE];
  36. int key_len;
  37. unsigned long fc;
  38. union {
  39. struct crypto_skcipher *blk;
  40. struct crypto_cipher *cip;
  41. } fallback;
  42. };
  43. struct s390_xts_ctx {
  44. u8 key[32];
  45. u8 pcc_key[32];
  46. int key_len;
  47. unsigned long fc;
  48. struct crypto_skcipher *fallback;
  49. };
  50. static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
  51. unsigned int key_len)
  52. {
  53. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  54. int ret;
  55. sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  56. sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
  57. CRYPTO_TFM_REQ_MASK);
  58. ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
  59. if (ret) {
  60. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  61. tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
  62. CRYPTO_TFM_RES_MASK);
  63. }
  64. return ret;
  65. }
  66. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  67. unsigned int key_len)
  68. {
  69. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  70. unsigned long fc;
  71. /* Pick the correct function code based on the key length */
  72. fc = (key_len == 16) ? CPACF_KM_AES_128 :
  73. (key_len == 24) ? CPACF_KM_AES_192 :
  74. (key_len == 32) ? CPACF_KM_AES_256 : 0;
  75. /* Check if the function code is available */
  76. sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  77. if (!sctx->fc)
  78. return setkey_fallback_cip(tfm, in_key, key_len);
  79. sctx->key_len = key_len;
  80. memcpy(sctx->key, in_key, key_len);
  81. return 0;
  82. }
  83. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  84. {
  85. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  86. if (unlikely(!sctx->fc)) {
  87. crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
  88. return;
  89. }
  90. cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
  91. }
  92. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  93. {
  94. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  95. if (unlikely(!sctx->fc)) {
  96. crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
  97. return;
  98. }
  99. cpacf_km(sctx->fc | CPACF_DECRYPT,
  100. &sctx->key, out, in, AES_BLOCK_SIZE);
  101. }
  102. static int fallback_init_cip(struct crypto_tfm *tfm)
  103. {
  104. const char *name = tfm->__crt_alg->cra_name;
  105. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  106. sctx->fallback.cip = crypto_alloc_cipher(name, 0,
  107. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  108. if (IS_ERR(sctx->fallback.cip)) {
  109. pr_err("Allocating AES fallback algorithm %s failed\n",
  110. name);
  111. return PTR_ERR(sctx->fallback.cip);
  112. }
  113. return 0;
  114. }
  115. static void fallback_exit_cip(struct crypto_tfm *tfm)
  116. {
  117. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  118. crypto_free_cipher(sctx->fallback.cip);
  119. sctx->fallback.cip = NULL;
  120. }
  121. static struct crypto_alg aes_alg = {
  122. .cra_name = "aes",
  123. .cra_driver_name = "aes-s390",
  124. .cra_priority = 300,
  125. .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
  126. CRYPTO_ALG_NEED_FALLBACK,
  127. .cra_blocksize = AES_BLOCK_SIZE,
  128. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  129. .cra_module = THIS_MODULE,
  130. .cra_init = fallback_init_cip,
  131. .cra_exit = fallback_exit_cip,
  132. .cra_u = {
  133. .cipher = {
  134. .cia_min_keysize = AES_MIN_KEY_SIZE,
  135. .cia_max_keysize = AES_MAX_KEY_SIZE,
  136. .cia_setkey = aes_set_key,
  137. .cia_encrypt = aes_encrypt,
  138. .cia_decrypt = aes_decrypt,
  139. }
  140. }
  141. };
  142. static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
  143. unsigned int len)
  144. {
  145. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  146. unsigned int ret;
  147. crypto_skcipher_clear_flags(sctx->fallback.blk, CRYPTO_TFM_REQ_MASK);
  148. crypto_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
  149. CRYPTO_TFM_REQ_MASK);
  150. ret = crypto_skcipher_setkey(sctx->fallback.blk, key, len);
  151. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  152. tfm->crt_flags |= crypto_skcipher_get_flags(sctx->fallback.blk) &
  153. CRYPTO_TFM_RES_MASK;
  154. return ret;
  155. }
  156. static int fallback_blk_dec(struct blkcipher_desc *desc,
  157. struct scatterlist *dst, struct scatterlist *src,
  158. unsigned int nbytes)
  159. {
  160. unsigned int ret;
  161. struct crypto_blkcipher *tfm = desc->tfm;
  162. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
  163. SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
  164. skcipher_request_set_tfm(req, sctx->fallback.blk);
  165. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  166. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  167. ret = crypto_skcipher_decrypt(req);
  168. skcipher_request_zero(req);
  169. return ret;
  170. }
  171. static int fallback_blk_enc(struct blkcipher_desc *desc,
  172. struct scatterlist *dst, struct scatterlist *src,
  173. unsigned int nbytes)
  174. {
  175. unsigned int ret;
  176. struct crypto_blkcipher *tfm = desc->tfm;
  177. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
  178. SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
  179. skcipher_request_set_tfm(req, sctx->fallback.blk);
  180. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  181. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  182. ret = crypto_skcipher_encrypt(req);
  183. return ret;
  184. }
  185. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  186. unsigned int key_len)
  187. {
  188. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  189. unsigned long fc;
  190. /* Pick the correct function code based on the key length */
  191. fc = (key_len == 16) ? CPACF_KM_AES_128 :
  192. (key_len == 24) ? CPACF_KM_AES_192 :
  193. (key_len == 32) ? CPACF_KM_AES_256 : 0;
  194. /* Check if the function code is available */
  195. sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  196. if (!sctx->fc)
  197. return setkey_fallback_blk(tfm, in_key, key_len);
  198. sctx->key_len = key_len;
  199. memcpy(sctx->key, in_key, key_len);
  200. return 0;
  201. }
  202. static int ecb_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  203. struct blkcipher_walk *walk)
  204. {
  205. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  206. unsigned int nbytes, n;
  207. int ret;
  208. ret = blkcipher_walk_virt(desc, walk);
  209. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  210. /* only use complete blocks */
  211. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  212. cpacf_km(sctx->fc | modifier, sctx->key,
  213. walk->dst.virt.addr, walk->src.virt.addr, n);
  214. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  215. }
  216. return ret;
  217. }
  218. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  219. struct scatterlist *dst, struct scatterlist *src,
  220. unsigned int nbytes)
  221. {
  222. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  223. struct blkcipher_walk walk;
  224. if (unlikely(!sctx->fc))
  225. return fallback_blk_enc(desc, dst, src, nbytes);
  226. blkcipher_walk_init(&walk, dst, src, nbytes);
  227. return ecb_aes_crypt(desc, 0, &walk);
  228. }
  229. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  230. struct scatterlist *dst, struct scatterlist *src,
  231. unsigned int nbytes)
  232. {
  233. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  234. struct blkcipher_walk walk;
  235. if (unlikely(!sctx->fc))
  236. return fallback_blk_dec(desc, dst, src, nbytes);
  237. blkcipher_walk_init(&walk, dst, src, nbytes);
  238. return ecb_aes_crypt(desc, CPACF_DECRYPT, &walk);
  239. }
  240. static int fallback_init_blk(struct crypto_tfm *tfm)
  241. {
  242. const char *name = tfm->__crt_alg->cra_name;
  243. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  244. sctx->fallback.blk = crypto_alloc_skcipher(name, 0,
  245. CRYPTO_ALG_ASYNC |
  246. CRYPTO_ALG_NEED_FALLBACK);
  247. if (IS_ERR(sctx->fallback.blk)) {
  248. pr_err("Allocating AES fallback algorithm %s failed\n",
  249. name);
  250. return PTR_ERR(sctx->fallback.blk);
  251. }
  252. return 0;
  253. }
  254. static void fallback_exit_blk(struct crypto_tfm *tfm)
  255. {
  256. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  257. crypto_free_skcipher(sctx->fallback.blk);
  258. }
  259. static struct crypto_alg ecb_aes_alg = {
  260. .cra_name = "ecb(aes)",
  261. .cra_driver_name = "ecb-aes-s390",
  262. .cra_priority = 400, /* combo: aes + ecb */
  263. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  264. CRYPTO_ALG_NEED_FALLBACK,
  265. .cra_blocksize = AES_BLOCK_SIZE,
  266. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  267. .cra_type = &crypto_blkcipher_type,
  268. .cra_module = THIS_MODULE,
  269. .cra_init = fallback_init_blk,
  270. .cra_exit = fallback_exit_blk,
  271. .cra_u = {
  272. .blkcipher = {
  273. .min_keysize = AES_MIN_KEY_SIZE,
  274. .max_keysize = AES_MAX_KEY_SIZE,
  275. .setkey = ecb_aes_set_key,
  276. .encrypt = ecb_aes_encrypt,
  277. .decrypt = ecb_aes_decrypt,
  278. }
  279. }
  280. };
  281. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  282. unsigned int key_len)
  283. {
  284. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  285. unsigned long fc;
  286. /* Pick the correct function code based on the key length */
  287. fc = (key_len == 16) ? CPACF_KMC_AES_128 :
  288. (key_len == 24) ? CPACF_KMC_AES_192 :
  289. (key_len == 32) ? CPACF_KMC_AES_256 : 0;
  290. /* Check if the function code is available */
  291. sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
  292. if (!sctx->fc)
  293. return setkey_fallback_blk(tfm, in_key, key_len);
  294. sctx->key_len = key_len;
  295. memcpy(sctx->key, in_key, key_len);
  296. return 0;
  297. }
  298. static int cbc_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  299. struct blkcipher_walk *walk)
  300. {
  301. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  302. unsigned int nbytes, n;
  303. int ret;
  304. struct {
  305. u8 iv[AES_BLOCK_SIZE];
  306. u8 key[AES_MAX_KEY_SIZE];
  307. } param;
  308. ret = blkcipher_walk_virt(desc, walk);
  309. memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
  310. memcpy(param.key, sctx->key, sctx->key_len);
  311. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  312. /* only use complete blocks */
  313. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  314. cpacf_kmc(sctx->fc | modifier, &param,
  315. walk->dst.virt.addr, walk->src.virt.addr, n);
  316. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  317. }
  318. memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
  319. return ret;
  320. }
  321. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  322. struct scatterlist *dst, struct scatterlist *src,
  323. unsigned int nbytes)
  324. {
  325. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  326. struct blkcipher_walk walk;
  327. if (unlikely(!sctx->fc))
  328. return fallback_blk_enc(desc, dst, src, nbytes);
  329. blkcipher_walk_init(&walk, dst, src, nbytes);
  330. return cbc_aes_crypt(desc, 0, &walk);
  331. }
  332. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  333. struct scatterlist *dst, struct scatterlist *src,
  334. unsigned int nbytes)
  335. {
  336. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  337. struct blkcipher_walk walk;
  338. if (unlikely(!sctx->fc))
  339. return fallback_blk_dec(desc, dst, src, nbytes);
  340. blkcipher_walk_init(&walk, dst, src, nbytes);
  341. return cbc_aes_crypt(desc, CPACF_DECRYPT, &walk);
  342. }
  343. static struct crypto_alg cbc_aes_alg = {
  344. .cra_name = "cbc(aes)",
  345. .cra_driver_name = "cbc-aes-s390",
  346. .cra_priority = 400, /* combo: aes + cbc */
  347. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  348. CRYPTO_ALG_NEED_FALLBACK,
  349. .cra_blocksize = AES_BLOCK_SIZE,
  350. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  351. .cra_type = &crypto_blkcipher_type,
  352. .cra_module = THIS_MODULE,
  353. .cra_init = fallback_init_blk,
  354. .cra_exit = fallback_exit_blk,
  355. .cra_u = {
  356. .blkcipher = {
  357. .min_keysize = AES_MIN_KEY_SIZE,
  358. .max_keysize = AES_MAX_KEY_SIZE,
  359. .ivsize = AES_BLOCK_SIZE,
  360. .setkey = cbc_aes_set_key,
  361. .encrypt = cbc_aes_encrypt,
  362. .decrypt = cbc_aes_decrypt,
  363. }
  364. }
  365. };
  366. static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
  367. unsigned int len)
  368. {
  369. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  370. unsigned int ret;
  371. crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
  372. crypto_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
  373. CRYPTO_TFM_REQ_MASK);
  374. ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
  375. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  376. tfm->crt_flags |= crypto_skcipher_get_flags(xts_ctx->fallback) &
  377. CRYPTO_TFM_RES_MASK;
  378. return ret;
  379. }
  380. static int xts_fallback_decrypt(struct blkcipher_desc *desc,
  381. struct scatterlist *dst, struct scatterlist *src,
  382. unsigned int nbytes)
  383. {
  384. struct crypto_blkcipher *tfm = desc->tfm;
  385. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
  386. SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
  387. unsigned int ret;
  388. skcipher_request_set_tfm(req, xts_ctx->fallback);
  389. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  390. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  391. ret = crypto_skcipher_decrypt(req);
  392. skcipher_request_zero(req);
  393. return ret;
  394. }
  395. static int xts_fallback_encrypt(struct blkcipher_desc *desc,
  396. struct scatterlist *dst, struct scatterlist *src,
  397. unsigned int nbytes)
  398. {
  399. struct crypto_blkcipher *tfm = desc->tfm;
  400. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
  401. SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
  402. unsigned int ret;
  403. skcipher_request_set_tfm(req, xts_ctx->fallback);
  404. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  405. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  406. ret = crypto_skcipher_encrypt(req);
  407. skcipher_request_zero(req);
  408. return ret;
  409. }
  410. static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  411. unsigned int key_len)
  412. {
  413. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  414. unsigned long fc;
  415. int err;
  416. err = xts_check_key(tfm, in_key, key_len);
  417. if (err)
  418. return err;
  419. /* Pick the correct function code based on the key length */
  420. fc = (key_len == 32) ? CPACF_KM_XTS_128 :
  421. (key_len == 64) ? CPACF_KM_XTS_256 : 0;
  422. /* Check if the function code is available */
  423. xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  424. if (!xts_ctx->fc)
  425. return xts_fallback_setkey(tfm, in_key, key_len);
  426. /* Split the XTS key into the two subkeys */
  427. key_len = key_len / 2;
  428. xts_ctx->key_len = key_len;
  429. memcpy(xts_ctx->key, in_key, key_len);
  430. memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
  431. return 0;
  432. }
  433. static int xts_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  434. struct blkcipher_walk *walk)
  435. {
  436. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  437. unsigned int offset, nbytes, n;
  438. int ret;
  439. struct {
  440. u8 key[32];
  441. u8 tweak[16];
  442. u8 block[16];
  443. u8 bit[16];
  444. u8 xts[16];
  445. } pcc_param;
  446. struct {
  447. u8 key[32];
  448. u8 init[16];
  449. } xts_param;
  450. ret = blkcipher_walk_virt(desc, walk);
  451. offset = xts_ctx->key_len & 0x10;
  452. memset(pcc_param.block, 0, sizeof(pcc_param.block));
  453. memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
  454. memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
  455. memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
  456. memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
  457. cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
  458. memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
  459. memcpy(xts_param.init, pcc_param.xts, 16);
  460. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  461. /* only use complete blocks */
  462. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  463. cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
  464. walk->dst.virt.addr, walk->src.virt.addr, n);
  465. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  466. }
  467. return ret;
  468. }
  469. static int xts_aes_encrypt(struct blkcipher_desc *desc,
  470. struct scatterlist *dst, struct scatterlist *src,
  471. unsigned int nbytes)
  472. {
  473. struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
  474. struct blkcipher_walk walk;
  475. if (unlikely(!xts_ctx->fc))
  476. return xts_fallback_encrypt(desc, dst, src, nbytes);
  477. blkcipher_walk_init(&walk, dst, src, nbytes);
  478. return xts_aes_crypt(desc, 0, &walk);
  479. }
  480. static int xts_aes_decrypt(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 blkcipher_walk walk;
  486. if (unlikely(!xts_ctx->fc))
  487. return xts_fallback_decrypt(desc, dst, src, nbytes);
  488. blkcipher_walk_init(&walk, dst, src, nbytes);
  489. return xts_aes_crypt(desc, CPACF_DECRYPT, &walk);
  490. }
  491. static int xts_fallback_init(struct crypto_tfm *tfm)
  492. {
  493. const char *name = tfm->__crt_alg->cra_name;
  494. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  495. xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
  496. CRYPTO_ALG_ASYNC |
  497. CRYPTO_ALG_NEED_FALLBACK);
  498. if (IS_ERR(xts_ctx->fallback)) {
  499. pr_err("Allocating XTS fallback algorithm %s failed\n",
  500. name);
  501. return PTR_ERR(xts_ctx->fallback);
  502. }
  503. return 0;
  504. }
  505. static void xts_fallback_exit(struct crypto_tfm *tfm)
  506. {
  507. struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
  508. crypto_free_skcipher(xts_ctx->fallback);
  509. }
  510. static struct crypto_alg xts_aes_alg = {
  511. .cra_name = "xts(aes)",
  512. .cra_driver_name = "xts-aes-s390",
  513. .cra_priority = 400, /* combo: aes + xts */
  514. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  515. CRYPTO_ALG_NEED_FALLBACK,
  516. .cra_blocksize = AES_BLOCK_SIZE,
  517. .cra_ctxsize = sizeof(struct s390_xts_ctx),
  518. .cra_type = &crypto_blkcipher_type,
  519. .cra_module = THIS_MODULE,
  520. .cra_init = xts_fallback_init,
  521. .cra_exit = xts_fallback_exit,
  522. .cra_u = {
  523. .blkcipher = {
  524. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  525. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  526. .ivsize = AES_BLOCK_SIZE,
  527. .setkey = xts_aes_set_key,
  528. .encrypt = xts_aes_encrypt,
  529. .decrypt = xts_aes_decrypt,
  530. }
  531. }
  532. };
  533. static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  534. unsigned int key_len)
  535. {
  536. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  537. unsigned long fc;
  538. /* Pick the correct function code based on the key length */
  539. fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
  540. (key_len == 24) ? CPACF_KMCTR_AES_192 :
  541. (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
  542. /* Check if the function code is available */
  543. sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
  544. if (!sctx->fc)
  545. return setkey_fallback_blk(tfm, in_key, key_len);
  546. sctx->key_len = key_len;
  547. memcpy(sctx->key, in_key, key_len);
  548. return 0;
  549. }
  550. static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
  551. {
  552. unsigned int i, n;
  553. /* only use complete blocks, max. PAGE_SIZE */
  554. memcpy(ctrptr, iv, AES_BLOCK_SIZE);
  555. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
  556. for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
  557. memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
  558. crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  559. ctrptr += AES_BLOCK_SIZE;
  560. }
  561. return n;
  562. }
  563. static int ctr_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  564. struct blkcipher_walk *walk)
  565. {
  566. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  567. u8 buf[AES_BLOCK_SIZE], *ctrptr;
  568. unsigned int n, nbytes;
  569. int ret, locked;
  570. locked = spin_trylock(&ctrblk_lock);
  571. ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
  572. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  573. n = AES_BLOCK_SIZE;
  574. if (nbytes >= 2*AES_BLOCK_SIZE && locked)
  575. n = __ctrblk_init(ctrblk, walk->iv, nbytes);
  576. ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
  577. cpacf_kmctr(sctx->fc | modifier, sctx->key,
  578. walk->dst.virt.addr, walk->src.virt.addr,
  579. n, ctrptr);
  580. if (ctrptr == ctrblk)
  581. memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
  582. AES_BLOCK_SIZE);
  583. crypto_inc(walk->iv, AES_BLOCK_SIZE);
  584. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  585. }
  586. if (locked)
  587. spin_unlock(&ctrblk_lock);
  588. /*
  589. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  590. */
  591. if (nbytes) {
  592. cpacf_kmctr(sctx->fc | modifier, sctx->key,
  593. buf, walk->src.virt.addr,
  594. AES_BLOCK_SIZE, walk->iv);
  595. memcpy(walk->dst.virt.addr, buf, nbytes);
  596. crypto_inc(walk->iv, AES_BLOCK_SIZE);
  597. ret = blkcipher_walk_done(desc, walk, 0);
  598. }
  599. return ret;
  600. }
  601. static int ctr_aes_encrypt(struct blkcipher_desc *desc,
  602. struct scatterlist *dst, struct scatterlist *src,
  603. unsigned int nbytes)
  604. {
  605. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  606. struct blkcipher_walk walk;
  607. if (unlikely(!sctx->fc))
  608. return fallback_blk_enc(desc, dst, src, nbytes);
  609. blkcipher_walk_init(&walk, dst, src, nbytes);
  610. return ctr_aes_crypt(desc, 0, &walk);
  611. }
  612. static int ctr_aes_decrypt(struct blkcipher_desc *desc,
  613. struct scatterlist *dst, struct scatterlist *src,
  614. unsigned int nbytes)
  615. {
  616. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  617. struct blkcipher_walk walk;
  618. if (unlikely(!sctx->fc))
  619. return fallback_blk_dec(desc, dst, src, nbytes);
  620. blkcipher_walk_init(&walk, dst, src, nbytes);
  621. return ctr_aes_crypt(desc, CPACF_DECRYPT, &walk);
  622. }
  623. static struct crypto_alg ctr_aes_alg = {
  624. .cra_name = "ctr(aes)",
  625. .cra_driver_name = "ctr-aes-s390",
  626. .cra_priority = 400, /* combo: aes + ctr */
  627. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  628. CRYPTO_ALG_NEED_FALLBACK,
  629. .cra_blocksize = 1,
  630. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  631. .cra_type = &crypto_blkcipher_type,
  632. .cra_module = THIS_MODULE,
  633. .cra_init = fallback_init_blk,
  634. .cra_exit = fallback_exit_blk,
  635. .cra_u = {
  636. .blkcipher = {
  637. .min_keysize = AES_MIN_KEY_SIZE,
  638. .max_keysize = AES_MAX_KEY_SIZE,
  639. .ivsize = AES_BLOCK_SIZE,
  640. .setkey = ctr_aes_set_key,
  641. .encrypt = ctr_aes_encrypt,
  642. .decrypt = ctr_aes_decrypt,
  643. }
  644. }
  645. };
  646. static struct crypto_alg *aes_s390_algs_ptr[5];
  647. static int aes_s390_algs_num;
  648. static int aes_s390_register_alg(struct crypto_alg *alg)
  649. {
  650. int ret;
  651. ret = crypto_register_alg(alg);
  652. if (!ret)
  653. aes_s390_algs_ptr[aes_s390_algs_num++] = alg;
  654. return ret;
  655. }
  656. static void aes_s390_fini(void)
  657. {
  658. while (aes_s390_algs_num--)
  659. crypto_unregister_alg(aes_s390_algs_ptr[aes_s390_algs_num]);
  660. if (ctrblk)
  661. free_page((unsigned long) ctrblk);
  662. }
  663. static int __init aes_s390_init(void)
  664. {
  665. int ret;
  666. /* Query available functions for KM, KMC and KMCTR */
  667. cpacf_query(CPACF_KM, &km_functions);
  668. cpacf_query(CPACF_KMC, &kmc_functions);
  669. cpacf_query(CPACF_KMCTR, &kmctr_functions);
  670. if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
  671. cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
  672. cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
  673. ret = aes_s390_register_alg(&aes_alg);
  674. if (ret)
  675. goto out_err;
  676. ret = aes_s390_register_alg(&ecb_aes_alg);
  677. if (ret)
  678. goto out_err;
  679. }
  680. if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
  681. cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
  682. cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
  683. ret = aes_s390_register_alg(&cbc_aes_alg);
  684. if (ret)
  685. goto out_err;
  686. }
  687. if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
  688. cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
  689. ret = aes_s390_register_alg(&xts_aes_alg);
  690. if (ret)
  691. goto out_err;
  692. }
  693. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
  694. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
  695. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
  696. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  697. if (!ctrblk) {
  698. ret = -ENOMEM;
  699. goto out_err;
  700. }
  701. ret = aes_s390_register_alg(&ctr_aes_alg);
  702. if (ret)
  703. goto out_err;
  704. }
  705. return 0;
  706. out_err:
  707. aes_s390_fini();
  708. return ret;
  709. }
  710. module_cpu_feature_match(MSA, aes_s390_init);
  711. module_exit(aes_s390_fini);
  712. MODULE_ALIAS_CRYPTO("aes-all");
  713. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  714. MODULE_LICENSE("GPL");