paes_s390.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cryptographic API.
  4. *
  5. * s390 implementation of the AES Cipher Algorithm with protected keys.
  6. *
  7. * s390 Version:
  8. * Copyright IBM Corp. 2017,2020
  9. * Author(s): Martin Schwidefsky <[email protected]>
  10. * Harald Freudenberger <[email protected]>
  11. */
  12. #define KMSG_COMPONENT "paes_s390"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <crypto/aes.h>
  15. #include <crypto/algapi.h>
  16. #include <linux/bug.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/cpufeature.h>
  20. #include <linux/init.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/delay.h>
  24. #include <crypto/internal/skcipher.h>
  25. #include <crypto/xts.h>
  26. #include <asm/cpacf.h>
  27. #include <asm/pkey.h>
  28. /*
  29. * Key blobs smaller/bigger than these defines are rejected
  30. * by the common code even before the individual setkey function
  31. * is called. As paes can handle different kinds of key blobs
  32. * and padding is also possible, the limits need to be generous.
  33. */
  34. #define PAES_MIN_KEYSIZE 16
  35. #define PAES_MAX_KEYSIZE MAXEP11AESKEYBLOBSIZE
  36. static u8 *ctrblk;
  37. static DEFINE_MUTEX(ctrblk_lock);
  38. static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
  39. struct key_blob {
  40. /*
  41. * Small keys will be stored in the keybuf. Larger keys are
  42. * stored in extra allocated memory. In both cases does
  43. * key point to the memory where the key is stored.
  44. * The code distinguishes by checking keylen against
  45. * sizeof(keybuf). See the two following helper functions.
  46. */
  47. u8 *key;
  48. u8 keybuf[128];
  49. unsigned int keylen;
  50. };
  51. static inline int _key_to_kb(struct key_blob *kb,
  52. const u8 *key,
  53. unsigned int keylen)
  54. {
  55. struct clearkey_header {
  56. u8 type;
  57. u8 res0[3];
  58. u8 version;
  59. u8 res1[3];
  60. u32 keytype;
  61. u32 len;
  62. } __packed * h;
  63. switch (keylen) {
  64. case 16:
  65. case 24:
  66. case 32:
  67. /* clear key value, prepare pkey clear key token in keybuf */
  68. memset(kb->keybuf, 0, sizeof(kb->keybuf));
  69. h = (struct clearkey_header *) kb->keybuf;
  70. h->version = 0x02; /* TOKVER_CLEAR_KEY */
  71. h->keytype = (keylen - 8) >> 3;
  72. h->len = keylen;
  73. memcpy(kb->keybuf + sizeof(*h), key, keylen);
  74. kb->keylen = sizeof(*h) + keylen;
  75. kb->key = kb->keybuf;
  76. break;
  77. default:
  78. /* other key material, let pkey handle this */
  79. if (keylen <= sizeof(kb->keybuf))
  80. kb->key = kb->keybuf;
  81. else {
  82. kb->key = kmalloc(keylen, GFP_KERNEL);
  83. if (!kb->key)
  84. return -ENOMEM;
  85. }
  86. memcpy(kb->key, key, keylen);
  87. kb->keylen = keylen;
  88. break;
  89. }
  90. return 0;
  91. }
  92. static inline void _free_kb_keybuf(struct key_blob *kb)
  93. {
  94. if (kb->key && kb->key != kb->keybuf
  95. && kb->keylen > sizeof(kb->keybuf)) {
  96. kfree(kb->key);
  97. kb->key = NULL;
  98. }
  99. }
  100. struct s390_paes_ctx {
  101. struct key_blob kb;
  102. struct pkey_protkey pk;
  103. spinlock_t pk_lock;
  104. unsigned long fc;
  105. };
  106. struct s390_pxts_ctx {
  107. struct key_blob kb[2];
  108. struct pkey_protkey pk[2];
  109. spinlock_t pk_lock;
  110. unsigned long fc;
  111. };
  112. static inline int __paes_keyblob2pkey(struct key_blob *kb,
  113. struct pkey_protkey *pk)
  114. {
  115. int i, ret;
  116. /* try three times in case of failure */
  117. for (i = 0; i < 3; i++) {
  118. if (i > 0 && ret == -EAGAIN && in_task())
  119. if (msleep_interruptible(1000))
  120. return -EINTR;
  121. ret = pkey_keyblob2pkey(kb->key, kb->keylen, pk);
  122. if (ret == 0)
  123. break;
  124. }
  125. return ret;
  126. }
  127. static inline int __paes_convert_key(struct s390_paes_ctx *ctx)
  128. {
  129. int ret;
  130. struct pkey_protkey pkey;
  131. ret = __paes_keyblob2pkey(&ctx->kb, &pkey);
  132. if (ret)
  133. return ret;
  134. spin_lock_bh(&ctx->pk_lock);
  135. memcpy(&ctx->pk, &pkey, sizeof(pkey));
  136. spin_unlock_bh(&ctx->pk_lock);
  137. return 0;
  138. }
  139. static int ecb_paes_init(struct crypto_skcipher *tfm)
  140. {
  141. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  142. ctx->kb.key = NULL;
  143. spin_lock_init(&ctx->pk_lock);
  144. return 0;
  145. }
  146. static void ecb_paes_exit(struct crypto_skcipher *tfm)
  147. {
  148. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  149. _free_kb_keybuf(&ctx->kb);
  150. }
  151. static inline int __ecb_paes_set_key(struct s390_paes_ctx *ctx)
  152. {
  153. int rc;
  154. unsigned long fc;
  155. rc = __paes_convert_key(ctx);
  156. if (rc)
  157. return rc;
  158. /* Pick the correct function code based on the protected key type */
  159. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PAES_128 :
  160. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KM_PAES_192 :
  161. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KM_PAES_256 : 0;
  162. /* Check if the function code is available */
  163. ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  164. return ctx->fc ? 0 : -EINVAL;
  165. }
  166. static int ecb_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
  167. unsigned int key_len)
  168. {
  169. int rc;
  170. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  171. _free_kb_keybuf(&ctx->kb);
  172. rc = _key_to_kb(&ctx->kb, in_key, key_len);
  173. if (rc)
  174. return rc;
  175. return __ecb_paes_set_key(ctx);
  176. }
  177. static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)
  178. {
  179. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  180. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  181. struct skcipher_walk walk;
  182. unsigned int nbytes, n, k;
  183. int ret;
  184. struct {
  185. u8 key[MAXPROTKEYSIZE];
  186. } param;
  187. ret = skcipher_walk_virt(&walk, req, false);
  188. if (ret)
  189. return ret;
  190. spin_lock_bh(&ctx->pk_lock);
  191. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  192. spin_unlock_bh(&ctx->pk_lock);
  193. while ((nbytes = walk.nbytes) != 0) {
  194. /* only use complete blocks */
  195. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  196. k = cpacf_km(ctx->fc | modifier, &param,
  197. walk.dst.virt.addr, walk.src.virt.addr, n);
  198. if (k)
  199. ret = skcipher_walk_done(&walk, nbytes - k);
  200. if (k < n) {
  201. if (__paes_convert_key(ctx))
  202. return skcipher_walk_done(&walk, -EIO);
  203. spin_lock_bh(&ctx->pk_lock);
  204. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  205. spin_unlock_bh(&ctx->pk_lock);
  206. }
  207. }
  208. return ret;
  209. }
  210. static int ecb_paes_encrypt(struct skcipher_request *req)
  211. {
  212. return ecb_paes_crypt(req, 0);
  213. }
  214. static int ecb_paes_decrypt(struct skcipher_request *req)
  215. {
  216. return ecb_paes_crypt(req, CPACF_DECRYPT);
  217. }
  218. static struct skcipher_alg ecb_paes_alg = {
  219. .base.cra_name = "ecb(paes)",
  220. .base.cra_driver_name = "ecb-paes-s390",
  221. .base.cra_priority = 401, /* combo: aes + ecb + 1 */
  222. .base.cra_blocksize = AES_BLOCK_SIZE,
  223. .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
  224. .base.cra_module = THIS_MODULE,
  225. .base.cra_list = LIST_HEAD_INIT(ecb_paes_alg.base.cra_list),
  226. .init = ecb_paes_init,
  227. .exit = ecb_paes_exit,
  228. .min_keysize = PAES_MIN_KEYSIZE,
  229. .max_keysize = PAES_MAX_KEYSIZE,
  230. .setkey = ecb_paes_set_key,
  231. .encrypt = ecb_paes_encrypt,
  232. .decrypt = ecb_paes_decrypt,
  233. };
  234. static int cbc_paes_init(struct crypto_skcipher *tfm)
  235. {
  236. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  237. ctx->kb.key = NULL;
  238. spin_lock_init(&ctx->pk_lock);
  239. return 0;
  240. }
  241. static void cbc_paes_exit(struct crypto_skcipher *tfm)
  242. {
  243. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  244. _free_kb_keybuf(&ctx->kb);
  245. }
  246. static inline int __cbc_paes_set_key(struct s390_paes_ctx *ctx)
  247. {
  248. int rc;
  249. unsigned long fc;
  250. rc = __paes_convert_key(ctx);
  251. if (rc)
  252. return rc;
  253. /* Pick the correct function code based on the protected key type */
  254. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMC_PAES_128 :
  255. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMC_PAES_192 :
  256. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KMC_PAES_256 : 0;
  257. /* Check if the function code is available */
  258. ctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
  259. return ctx->fc ? 0 : -EINVAL;
  260. }
  261. static int cbc_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
  262. unsigned int key_len)
  263. {
  264. int rc;
  265. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  266. _free_kb_keybuf(&ctx->kb);
  267. rc = _key_to_kb(&ctx->kb, in_key, key_len);
  268. if (rc)
  269. return rc;
  270. return __cbc_paes_set_key(ctx);
  271. }
  272. static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)
  273. {
  274. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  275. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  276. struct skcipher_walk walk;
  277. unsigned int nbytes, n, k;
  278. int ret;
  279. struct {
  280. u8 iv[AES_BLOCK_SIZE];
  281. u8 key[MAXPROTKEYSIZE];
  282. } param;
  283. ret = skcipher_walk_virt(&walk, req, false);
  284. if (ret)
  285. return ret;
  286. memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
  287. spin_lock_bh(&ctx->pk_lock);
  288. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  289. spin_unlock_bh(&ctx->pk_lock);
  290. while ((nbytes = walk.nbytes) != 0) {
  291. /* only use complete blocks */
  292. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  293. k = cpacf_kmc(ctx->fc | modifier, &param,
  294. walk.dst.virt.addr, walk.src.virt.addr, n);
  295. if (k) {
  296. memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
  297. ret = skcipher_walk_done(&walk, nbytes - k);
  298. }
  299. if (k < n) {
  300. if (__paes_convert_key(ctx))
  301. return skcipher_walk_done(&walk, -EIO);
  302. spin_lock_bh(&ctx->pk_lock);
  303. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  304. spin_unlock_bh(&ctx->pk_lock);
  305. }
  306. }
  307. return ret;
  308. }
  309. static int cbc_paes_encrypt(struct skcipher_request *req)
  310. {
  311. return cbc_paes_crypt(req, 0);
  312. }
  313. static int cbc_paes_decrypt(struct skcipher_request *req)
  314. {
  315. return cbc_paes_crypt(req, CPACF_DECRYPT);
  316. }
  317. static struct skcipher_alg cbc_paes_alg = {
  318. .base.cra_name = "cbc(paes)",
  319. .base.cra_driver_name = "cbc-paes-s390",
  320. .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
  321. .base.cra_blocksize = AES_BLOCK_SIZE,
  322. .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
  323. .base.cra_module = THIS_MODULE,
  324. .base.cra_list = LIST_HEAD_INIT(cbc_paes_alg.base.cra_list),
  325. .init = cbc_paes_init,
  326. .exit = cbc_paes_exit,
  327. .min_keysize = PAES_MIN_KEYSIZE,
  328. .max_keysize = PAES_MAX_KEYSIZE,
  329. .ivsize = AES_BLOCK_SIZE,
  330. .setkey = cbc_paes_set_key,
  331. .encrypt = cbc_paes_encrypt,
  332. .decrypt = cbc_paes_decrypt,
  333. };
  334. static int xts_paes_init(struct crypto_skcipher *tfm)
  335. {
  336. struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
  337. ctx->kb[0].key = NULL;
  338. ctx->kb[1].key = NULL;
  339. spin_lock_init(&ctx->pk_lock);
  340. return 0;
  341. }
  342. static void xts_paes_exit(struct crypto_skcipher *tfm)
  343. {
  344. struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
  345. _free_kb_keybuf(&ctx->kb[0]);
  346. _free_kb_keybuf(&ctx->kb[1]);
  347. }
  348. static inline int __xts_paes_convert_key(struct s390_pxts_ctx *ctx)
  349. {
  350. struct pkey_protkey pkey0, pkey1;
  351. if (__paes_keyblob2pkey(&ctx->kb[0], &pkey0) ||
  352. __paes_keyblob2pkey(&ctx->kb[1], &pkey1))
  353. return -EINVAL;
  354. spin_lock_bh(&ctx->pk_lock);
  355. memcpy(&ctx->pk[0], &pkey0, sizeof(pkey0));
  356. memcpy(&ctx->pk[1], &pkey1, sizeof(pkey1));
  357. spin_unlock_bh(&ctx->pk_lock);
  358. return 0;
  359. }
  360. static inline int __xts_paes_set_key(struct s390_pxts_ctx *ctx)
  361. {
  362. unsigned long fc;
  363. if (__xts_paes_convert_key(ctx))
  364. return -EINVAL;
  365. if (ctx->pk[0].type != ctx->pk[1].type)
  366. return -EINVAL;
  367. /* Pick the correct function code based on the protected key type */
  368. fc = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PXTS_128 :
  369. (ctx->pk[0].type == PKEY_KEYTYPE_AES_256) ?
  370. CPACF_KM_PXTS_256 : 0;
  371. /* Check if the function code is available */
  372. ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  373. return ctx->fc ? 0 : -EINVAL;
  374. }
  375. static int xts_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
  376. unsigned int xts_key_len)
  377. {
  378. int rc;
  379. struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
  380. u8 ckey[2 * AES_MAX_KEY_SIZE];
  381. unsigned int ckey_len, key_len;
  382. if (xts_key_len % 2)
  383. return -EINVAL;
  384. key_len = xts_key_len / 2;
  385. _free_kb_keybuf(&ctx->kb[0]);
  386. _free_kb_keybuf(&ctx->kb[1]);
  387. rc = _key_to_kb(&ctx->kb[0], in_key, key_len);
  388. if (rc)
  389. return rc;
  390. rc = _key_to_kb(&ctx->kb[1], in_key + key_len, key_len);
  391. if (rc)
  392. return rc;
  393. rc = __xts_paes_set_key(ctx);
  394. if (rc)
  395. return rc;
  396. /*
  397. * xts_check_key verifies the key length is not odd and makes
  398. * sure that the two keys are not the same. This can be done
  399. * on the two protected keys as well
  400. */
  401. ckey_len = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ?
  402. AES_KEYSIZE_128 : AES_KEYSIZE_256;
  403. memcpy(ckey, ctx->pk[0].protkey, ckey_len);
  404. memcpy(ckey + ckey_len, ctx->pk[1].protkey, ckey_len);
  405. return xts_verify_key(tfm, ckey, 2*ckey_len);
  406. }
  407. static int xts_paes_crypt(struct skcipher_request *req, unsigned long modifier)
  408. {
  409. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  410. struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
  411. struct skcipher_walk walk;
  412. unsigned int keylen, offset, nbytes, n, k;
  413. int ret;
  414. struct {
  415. u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
  416. u8 tweak[16];
  417. u8 block[16];
  418. u8 bit[16];
  419. u8 xts[16];
  420. } pcc_param;
  421. struct {
  422. u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
  423. u8 init[16];
  424. } xts_param;
  425. ret = skcipher_walk_virt(&walk, req, false);
  426. if (ret)
  427. return ret;
  428. keylen = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 48 : 64;
  429. offset = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 16 : 0;
  430. memset(&pcc_param, 0, sizeof(pcc_param));
  431. memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
  432. spin_lock_bh(&ctx->pk_lock);
  433. memcpy(pcc_param.key + offset, ctx->pk[1].protkey, keylen);
  434. memcpy(xts_param.key + offset, ctx->pk[0].protkey, keylen);
  435. spin_unlock_bh(&ctx->pk_lock);
  436. cpacf_pcc(ctx->fc, pcc_param.key + offset);
  437. memcpy(xts_param.init, pcc_param.xts, 16);
  438. while ((nbytes = walk.nbytes) != 0) {
  439. /* only use complete blocks */
  440. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  441. k = cpacf_km(ctx->fc | modifier, xts_param.key + offset,
  442. walk.dst.virt.addr, walk.src.virt.addr, n);
  443. if (k)
  444. ret = skcipher_walk_done(&walk, nbytes - k);
  445. if (k < n) {
  446. if (__xts_paes_convert_key(ctx))
  447. return skcipher_walk_done(&walk, -EIO);
  448. spin_lock_bh(&ctx->pk_lock);
  449. memcpy(xts_param.key + offset,
  450. ctx->pk[0].protkey, keylen);
  451. spin_unlock_bh(&ctx->pk_lock);
  452. }
  453. }
  454. return ret;
  455. }
  456. static int xts_paes_encrypt(struct skcipher_request *req)
  457. {
  458. return xts_paes_crypt(req, 0);
  459. }
  460. static int xts_paes_decrypt(struct skcipher_request *req)
  461. {
  462. return xts_paes_crypt(req, CPACF_DECRYPT);
  463. }
  464. static struct skcipher_alg xts_paes_alg = {
  465. .base.cra_name = "xts(paes)",
  466. .base.cra_driver_name = "xts-paes-s390",
  467. .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
  468. .base.cra_blocksize = AES_BLOCK_SIZE,
  469. .base.cra_ctxsize = sizeof(struct s390_pxts_ctx),
  470. .base.cra_module = THIS_MODULE,
  471. .base.cra_list = LIST_HEAD_INIT(xts_paes_alg.base.cra_list),
  472. .init = xts_paes_init,
  473. .exit = xts_paes_exit,
  474. .min_keysize = 2 * PAES_MIN_KEYSIZE,
  475. .max_keysize = 2 * PAES_MAX_KEYSIZE,
  476. .ivsize = AES_BLOCK_SIZE,
  477. .setkey = xts_paes_set_key,
  478. .encrypt = xts_paes_encrypt,
  479. .decrypt = xts_paes_decrypt,
  480. };
  481. static int ctr_paes_init(struct crypto_skcipher *tfm)
  482. {
  483. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  484. ctx->kb.key = NULL;
  485. spin_lock_init(&ctx->pk_lock);
  486. return 0;
  487. }
  488. static void ctr_paes_exit(struct crypto_skcipher *tfm)
  489. {
  490. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  491. _free_kb_keybuf(&ctx->kb);
  492. }
  493. static inline int __ctr_paes_set_key(struct s390_paes_ctx *ctx)
  494. {
  495. int rc;
  496. unsigned long fc;
  497. rc = __paes_convert_key(ctx);
  498. if (rc)
  499. return rc;
  500. /* Pick the correct function code based on the protected key type */
  501. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMCTR_PAES_128 :
  502. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMCTR_PAES_192 :
  503. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ?
  504. CPACF_KMCTR_PAES_256 : 0;
  505. /* Check if the function code is available */
  506. ctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
  507. return ctx->fc ? 0 : -EINVAL;
  508. }
  509. static int ctr_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
  510. unsigned int key_len)
  511. {
  512. int rc;
  513. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  514. _free_kb_keybuf(&ctx->kb);
  515. rc = _key_to_kb(&ctx->kb, in_key, key_len);
  516. if (rc)
  517. return rc;
  518. return __ctr_paes_set_key(ctx);
  519. }
  520. static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
  521. {
  522. unsigned int i, n;
  523. /* only use complete blocks, max. PAGE_SIZE */
  524. memcpy(ctrptr, iv, AES_BLOCK_SIZE);
  525. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
  526. for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
  527. memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
  528. crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  529. ctrptr += AES_BLOCK_SIZE;
  530. }
  531. return n;
  532. }
  533. static int ctr_paes_crypt(struct skcipher_request *req)
  534. {
  535. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  536. struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
  537. u8 buf[AES_BLOCK_SIZE], *ctrptr;
  538. struct skcipher_walk walk;
  539. unsigned int nbytes, n, k;
  540. int ret, locked;
  541. struct {
  542. u8 key[MAXPROTKEYSIZE];
  543. } param;
  544. ret = skcipher_walk_virt(&walk, req, false);
  545. if (ret)
  546. return ret;
  547. spin_lock_bh(&ctx->pk_lock);
  548. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  549. spin_unlock_bh(&ctx->pk_lock);
  550. locked = mutex_trylock(&ctrblk_lock);
  551. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  552. n = AES_BLOCK_SIZE;
  553. if (nbytes >= 2*AES_BLOCK_SIZE && locked)
  554. n = __ctrblk_init(ctrblk, walk.iv, nbytes);
  555. ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
  556. k = cpacf_kmctr(ctx->fc, &param, walk.dst.virt.addr,
  557. walk.src.virt.addr, n, ctrptr);
  558. if (k) {
  559. if (ctrptr == ctrblk)
  560. memcpy(walk.iv, ctrptr + k - AES_BLOCK_SIZE,
  561. AES_BLOCK_SIZE);
  562. crypto_inc(walk.iv, AES_BLOCK_SIZE);
  563. ret = skcipher_walk_done(&walk, nbytes - k);
  564. }
  565. if (k < n) {
  566. if (__paes_convert_key(ctx)) {
  567. if (locked)
  568. mutex_unlock(&ctrblk_lock);
  569. return skcipher_walk_done(&walk, -EIO);
  570. }
  571. spin_lock_bh(&ctx->pk_lock);
  572. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  573. spin_unlock_bh(&ctx->pk_lock);
  574. }
  575. }
  576. if (locked)
  577. mutex_unlock(&ctrblk_lock);
  578. /*
  579. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  580. */
  581. if (nbytes) {
  582. while (1) {
  583. if (cpacf_kmctr(ctx->fc, &param, buf,
  584. walk.src.virt.addr, AES_BLOCK_SIZE,
  585. walk.iv) == AES_BLOCK_SIZE)
  586. break;
  587. if (__paes_convert_key(ctx))
  588. return skcipher_walk_done(&walk, -EIO);
  589. spin_lock_bh(&ctx->pk_lock);
  590. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  591. spin_unlock_bh(&ctx->pk_lock);
  592. }
  593. memcpy(walk.dst.virt.addr, buf, nbytes);
  594. crypto_inc(walk.iv, AES_BLOCK_SIZE);
  595. ret = skcipher_walk_done(&walk, nbytes);
  596. }
  597. return ret;
  598. }
  599. static struct skcipher_alg ctr_paes_alg = {
  600. .base.cra_name = "ctr(paes)",
  601. .base.cra_driver_name = "ctr-paes-s390",
  602. .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
  603. .base.cra_blocksize = 1,
  604. .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
  605. .base.cra_module = THIS_MODULE,
  606. .base.cra_list = LIST_HEAD_INIT(ctr_paes_alg.base.cra_list),
  607. .init = ctr_paes_init,
  608. .exit = ctr_paes_exit,
  609. .min_keysize = PAES_MIN_KEYSIZE,
  610. .max_keysize = PAES_MAX_KEYSIZE,
  611. .ivsize = AES_BLOCK_SIZE,
  612. .setkey = ctr_paes_set_key,
  613. .encrypt = ctr_paes_crypt,
  614. .decrypt = ctr_paes_crypt,
  615. .chunksize = AES_BLOCK_SIZE,
  616. };
  617. static inline void __crypto_unregister_skcipher(struct skcipher_alg *alg)
  618. {
  619. if (!list_empty(&alg->base.cra_list))
  620. crypto_unregister_skcipher(alg);
  621. }
  622. static void paes_s390_fini(void)
  623. {
  624. __crypto_unregister_skcipher(&ctr_paes_alg);
  625. __crypto_unregister_skcipher(&xts_paes_alg);
  626. __crypto_unregister_skcipher(&cbc_paes_alg);
  627. __crypto_unregister_skcipher(&ecb_paes_alg);
  628. if (ctrblk)
  629. free_page((unsigned long) ctrblk);
  630. }
  631. static int __init paes_s390_init(void)
  632. {
  633. int ret;
  634. /* Query available functions for KM, KMC and KMCTR */
  635. cpacf_query(CPACF_KM, &km_functions);
  636. cpacf_query(CPACF_KMC, &kmc_functions);
  637. cpacf_query(CPACF_KMCTR, &kmctr_functions);
  638. if (cpacf_test_func(&km_functions, CPACF_KM_PAES_128) ||
  639. cpacf_test_func(&km_functions, CPACF_KM_PAES_192) ||
  640. cpacf_test_func(&km_functions, CPACF_KM_PAES_256)) {
  641. ret = crypto_register_skcipher(&ecb_paes_alg);
  642. if (ret)
  643. goto out_err;
  644. }
  645. if (cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
  646. cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
  647. cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256)) {
  648. ret = crypto_register_skcipher(&cbc_paes_alg);
  649. if (ret)
  650. goto out_err;
  651. }
  652. if (cpacf_test_func(&km_functions, CPACF_KM_PXTS_128) ||
  653. cpacf_test_func(&km_functions, CPACF_KM_PXTS_256)) {
  654. ret = crypto_register_skcipher(&xts_paes_alg);
  655. if (ret)
  656. goto out_err;
  657. }
  658. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_128) ||
  659. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_192) ||
  660. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_256)) {
  661. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  662. if (!ctrblk) {
  663. ret = -ENOMEM;
  664. goto out_err;
  665. }
  666. ret = crypto_register_skcipher(&ctr_paes_alg);
  667. if (ret)
  668. goto out_err;
  669. }
  670. return 0;
  671. out_err:
  672. paes_s390_fini();
  673. return ret;
  674. }
  675. module_init(paes_s390_init);
  676. module_exit(paes_s390_fini);
  677. MODULE_ALIAS_CRYPTO("paes");
  678. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm with protected keys");
  679. MODULE_LICENSE("GPL");