paes_s390.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm with protected keys.
  5. *
  6. * s390 Version:
  7. * Copyright IBM Corp. 2017
  8. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. * Harald Freudenberger <freude@de.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. */
  16. #define KMSG_COMPONENT "paes_s390"
  17. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  18. #include <crypto/aes.h>
  19. #include <crypto/algapi.h>
  20. #include <linux/bug.h>
  21. #include <linux/err.h>
  22. #include <linux/module.h>
  23. #include <linux/cpufeature.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <crypto/xts.h>
  27. #include <asm/cpacf.h>
  28. #include <asm/pkey.h>
  29. static u8 *ctrblk;
  30. static DEFINE_SPINLOCK(ctrblk_lock);
  31. static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
  32. struct s390_paes_ctx {
  33. struct pkey_seckey sk;
  34. struct pkey_protkey pk;
  35. unsigned long fc;
  36. };
  37. struct s390_pxts_ctx {
  38. struct pkey_seckey sk[2];
  39. struct pkey_protkey pk[2];
  40. unsigned long fc;
  41. };
  42. static inline int __paes_convert_key(struct pkey_seckey *sk,
  43. struct pkey_protkey *pk)
  44. {
  45. int i, ret;
  46. /* try three times in case of failure */
  47. for (i = 0; i < 3; i++) {
  48. ret = pkey_skey2pkey(sk, pk);
  49. if (ret == 0)
  50. break;
  51. }
  52. return ret;
  53. }
  54. static int __paes_set_key(struct s390_paes_ctx *ctx)
  55. {
  56. unsigned long fc;
  57. if (__paes_convert_key(&ctx->sk, &ctx->pk))
  58. return -EINVAL;
  59. /* Pick the correct function code based on the protected key type */
  60. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PAES_128 :
  61. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KM_PAES_192 :
  62. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KM_PAES_256 : 0;
  63. /* Check if the function code is available */
  64. ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  65. return ctx->fc ? 0 : -EINVAL;
  66. }
  67. static int ecb_paes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  68. unsigned int key_len)
  69. {
  70. struct s390_paes_ctx *ctx = crypto_tfm_ctx(tfm);
  71. if (key_len != SECKEYBLOBSIZE)
  72. return -EINVAL;
  73. memcpy(ctx->sk.seckey, in_key, SECKEYBLOBSIZE);
  74. if (__paes_set_key(ctx)) {
  75. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. static int ecb_paes_crypt(struct blkcipher_desc *desc,
  81. unsigned long modifier,
  82. struct blkcipher_walk *walk)
  83. {
  84. struct s390_paes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  85. unsigned int nbytes, n, k;
  86. int ret;
  87. ret = blkcipher_walk_virt(desc, walk);
  88. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  89. /* only use complete blocks */
  90. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  91. k = cpacf_km(ctx->fc | modifier, ctx->pk.protkey,
  92. walk->dst.virt.addr, walk->src.virt.addr, n);
  93. if (k)
  94. ret = blkcipher_walk_done(desc, walk, nbytes - k);
  95. if (k < n) {
  96. if (__paes_set_key(ctx) != 0)
  97. return blkcipher_walk_done(desc, walk, -EIO);
  98. }
  99. }
  100. return ret;
  101. }
  102. static int ecb_paes_encrypt(struct blkcipher_desc *desc,
  103. struct scatterlist *dst, struct scatterlist *src,
  104. unsigned int nbytes)
  105. {
  106. struct blkcipher_walk walk;
  107. blkcipher_walk_init(&walk, dst, src, nbytes);
  108. return ecb_paes_crypt(desc, CPACF_ENCRYPT, &walk);
  109. }
  110. static int ecb_paes_decrypt(struct blkcipher_desc *desc,
  111. struct scatterlist *dst, struct scatterlist *src,
  112. unsigned int nbytes)
  113. {
  114. struct blkcipher_walk walk;
  115. blkcipher_walk_init(&walk, dst, src, nbytes);
  116. return ecb_paes_crypt(desc, CPACF_DECRYPT, &walk);
  117. }
  118. static struct crypto_alg ecb_paes_alg = {
  119. .cra_name = "ecb(paes)",
  120. .cra_driver_name = "ecb-paes-s390",
  121. .cra_priority = 400, /* combo: aes + ecb */
  122. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  123. .cra_blocksize = AES_BLOCK_SIZE,
  124. .cra_ctxsize = sizeof(struct s390_paes_ctx),
  125. .cra_type = &crypto_blkcipher_type,
  126. .cra_module = THIS_MODULE,
  127. .cra_list = LIST_HEAD_INIT(ecb_paes_alg.cra_list),
  128. .cra_u = {
  129. .blkcipher = {
  130. .min_keysize = SECKEYBLOBSIZE,
  131. .max_keysize = SECKEYBLOBSIZE,
  132. .setkey = ecb_paes_set_key,
  133. .encrypt = ecb_paes_encrypt,
  134. .decrypt = ecb_paes_decrypt,
  135. }
  136. }
  137. };
  138. static int __cbc_paes_set_key(struct s390_paes_ctx *ctx)
  139. {
  140. unsigned long fc;
  141. if (__paes_convert_key(&ctx->sk, &ctx->pk))
  142. return -EINVAL;
  143. /* Pick the correct function code based on the protected key type */
  144. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMC_PAES_128 :
  145. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMC_PAES_192 :
  146. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KMC_PAES_256 : 0;
  147. /* Check if the function code is available */
  148. ctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
  149. return ctx->fc ? 0 : -EINVAL;
  150. }
  151. static int cbc_paes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  152. unsigned int key_len)
  153. {
  154. struct s390_paes_ctx *ctx = crypto_tfm_ctx(tfm);
  155. memcpy(ctx->sk.seckey, in_key, SECKEYBLOBSIZE);
  156. if (__cbc_paes_set_key(ctx)) {
  157. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static int cbc_paes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  163. struct blkcipher_walk *walk)
  164. {
  165. struct s390_paes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  166. unsigned int nbytes, n, k;
  167. int ret;
  168. struct {
  169. u8 iv[AES_BLOCK_SIZE];
  170. u8 key[MAXPROTKEYSIZE];
  171. } param;
  172. ret = blkcipher_walk_virt(desc, walk);
  173. memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
  174. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  175. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  176. /* only use complete blocks */
  177. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  178. k = cpacf_kmc(ctx->fc | modifier, &param,
  179. walk->dst.virt.addr, walk->src.virt.addr, n);
  180. if (k)
  181. ret = blkcipher_walk_done(desc, walk, nbytes - k);
  182. if (n < k) {
  183. if (__cbc_paes_set_key(ctx) != 0)
  184. return blkcipher_walk_done(desc, walk, -EIO);
  185. memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
  186. }
  187. }
  188. memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
  189. return ret;
  190. }
  191. static int cbc_paes_encrypt(struct blkcipher_desc *desc,
  192. struct scatterlist *dst, struct scatterlist *src,
  193. unsigned int nbytes)
  194. {
  195. struct blkcipher_walk walk;
  196. blkcipher_walk_init(&walk, dst, src, nbytes);
  197. return cbc_paes_crypt(desc, 0, &walk);
  198. }
  199. static int cbc_paes_decrypt(struct blkcipher_desc *desc,
  200. struct scatterlist *dst, struct scatterlist *src,
  201. unsigned int nbytes)
  202. {
  203. struct blkcipher_walk walk;
  204. blkcipher_walk_init(&walk, dst, src, nbytes);
  205. return cbc_paes_crypt(desc, CPACF_DECRYPT, &walk);
  206. }
  207. static struct crypto_alg cbc_paes_alg = {
  208. .cra_name = "cbc(paes)",
  209. .cra_driver_name = "cbc-paes-s390",
  210. .cra_priority = 400, /* combo: aes + cbc */
  211. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  212. .cra_blocksize = AES_BLOCK_SIZE,
  213. .cra_ctxsize = sizeof(struct s390_paes_ctx),
  214. .cra_type = &crypto_blkcipher_type,
  215. .cra_module = THIS_MODULE,
  216. .cra_list = LIST_HEAD_INIT(cbc_paes_alg.cra_list),
  217. .cra_u = {
  218. .blkcipher = {
  219. .min_keysize = SECKEYBLOBSIZE,
  220. .max_keysize = SECKEYBLOBSIZE,
  221. .ivsize = AES_BLOCK_SIZE,
  222. .setkey = cbc_paes_set_key,
  223. .encrypt = cbc_paes_encrypt,
  224. .decrypt = cbc_paes_decrypt,
  225. }
  226. }
  227. };
  228. static int __xts_paes_set_key(struct s390_pxts_ctx *ctx)
  229. {
  230. unsigned long fc;
  231. if (__paes_convert_key(&ctx->sk[0], &ctx->pk[0]) ||
  232. __paes_convert_key(&ctx->sk[1], &ctx->pk[1]))
  233. return -EINVAL;
  234. if (ctx->pk[0].type != ctx->pk[1].type)
  235. return -EINVAL;
  236. /* Pick the correct function code based on the protected key type */
  237. fc = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PXTS_128 :
  238. (ctx->pk[0].type == PKEY_KEYTYPE_AES_256) ?
  239. CPACF_KM_PXTS_256 : 0;
  240. /* Check if the function code is available */
  241. ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
  242. return ctx->fc ? 0 : -EINVAL;
  243. }
  244. static int xts_paes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  245. unsigned int key_len)
  246. {
  247. struct s390_pxts_ctx *ctx = crypto_tfm_ctx(tfm);
  248. u8 ckey[2 * AES_MAX_KEY_SIZE];
  249. unsigned int ckey_len;
  250. memcpy(ctx->sk[0].seckey, in_key, SECKEYBLOBSIZE);
  251. memcpy(ctx->sk[1].seckey, in_key + SECKEYBLOBSIZE, SECKEYBLOBSIZE);
  252. if (__xts_paes_set_key(ctx)) {
  253. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  254. return -EINVAL;
  255. }
  256. /*
  257. * xts_check_key verifies the key length is not odd and makes
  258. * sure that the two keys are not the same. This can be done
  259. * on the two protected keys as well
  260. */
  261. ckey_len = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ?
  262. AES_KEYSIZE_128 : AES_KEYSIZE_256;
  263. memcpy(ckey, ctx->pk[0].protkey, ckey_len);
  264. memcpy(ckey + ckey_len, ctx->pk[1].protkey, ckey_len);
  265. return xts_check_key(tfm, ckey, 2*ckey_len);
  266. }
  267. static int xts_paes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  268. struct blkcipher_walk *walk)
  269. {
  270. struct s390_pxts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  271. unsigned int keylen, offset, nbytes, n, k;
  272. int ret;
  273. struct {
  274. u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
  275. u8 tweak[16];
  276. u8 block[16];
  277. u8 bit[16];
  278. u8 xts[16];
  279. } pcc_param;
  280. struct {
  281. u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
  282. u8 init[16];
  283. } xts_param;
  284. ret = blkcipher_walk_virt(desc, walk);
  285. keylen = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 48 : 64;
  286. offset = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 16 : 0;
  287. retry:
  288. memset(&pcc_param, 0, sizeof(pcc_param));
  289. memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
  290. memcpy(pcc_param.key + offset, ctx->pk[1].protkey, keylen);
  291. cpacf_pcc(ctx->fc, pcc_param.key + offset);
  292. memcpy(xts_param.key + offset, ctx->pk[0].protkey, keylen);
  293. memcpy(xts_param.init, pcc_param.xts, 16);
  294. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  295. /* only use complete blocks */
  296. n = nbytes & ~(AES_BLOCK_SIZE - 1);
  297. k = cpacf_km(ctx->fc | modifier, xts_param.key + offset,
  298. walk->dst.virt.addr, walk->src.virt.addr, n);
  299. if (k)
  300. ret = blkcipher_walk_done(desc, walk, nbytes - k);
  301. if (k < n) {
  302. if (__xts_paes_set_key(ctx) != 0)
  303. return blkcipher_walk_done(desc, walk, -EIO);
  304. goto retry;
  305. }
  306. }
  307. return ret;
  308. }
  309. static int xts_paes_encrypt(struct blkcipher_desc *desc,
  310. struct scatterlist *dst, struct scatterlist *src,
  311. unsigned int nbytes)
  312. {
  313. struct blkcipher_walk walk;
  314. blkcipher_walk_init(&walk, dst, src, nbytes);
  315. return xts_paes_crypt(desc, 0, &walk);
  316. }
  317. static int xts_paes_decrypt(struct blkcipher_desc *desc,
  318. struct scatterlist *dst, struct scatterlist *src,
  319. unsigned int nbytes)
  320. {
  321. struct blkcipher_walk walk;
  322. blkcipher_walk_init(&walk, dst, src, nbytes);
  323. return xts_paes_crypt(desc, CPACF_DECRYPT, &walk);
  324. }
  325. static struct crypto_alg xts_paes_alg = {
  326. .cra_name = "xts(paes)",
  327. .cra_driver_name = "xts-paes-s390",
  328. .cra_priority = 400, /* combo: aes + xts */
  329. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  330. .cra_blocksize = AES_BLOCK_SIZE,
  331. .cra_ctxsize = sizeof(struct s390_pxts_ctx),
  332. .cra_type = &crypto_blkcipher_type,
  333. .cra_module = THIS_MODULE,
  334. .cra_list = LIST_HEAD_INIT(xts_paes_alg.cra_list),
  335. .cra_u = {
  336. .blkcipher = {
  337. .min_keysize = 2 * SECKEYBLOBSIZE,
  338. .max_keysize = 2 * SECKEYBLOBSIZE,
  339. .ivsize = AES_BLOCK_SIZE,
  340. .setkey = xts_paes_set_key,
  341. .encrypt = xts_paes_encrypt,
  342. .decrypt = xts_paes_decrypt,
  343. }
  344. }
  345. };
  346. static int __ctr_paes_set_key(struct s390_paes_ctx *ctx)
  347. {
  348. unsigned long fc;
  349. if (__paes_convert_key(&ctx->sk, &ctx->pk))
  350. return -EINVAL;
  351. /* Pick the correct function code based on the protected key type */
  352. fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMCTR_PAES_128 :
  353. (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMCTR_PAES_192 :
  354. (ctx->pk.type == PKEY_KEYTYPE_AES_256) ?
  355. CPACF_KMCTR_PAES_256 : 0;
  356. /* Check if the function code is available */
  357. ctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
  358. return ctx->fc ? 0 : -EINVAL;
  359. }
  360. static int ctr_paes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  361. unsigned int key_len)
  362. {
  363. struct s390_paes_ctx *ctx = crypto_tfm_ctx(tfm);
  364. memcpy(ctx->sk.seckey, in_key, key_len);
  365. if (__ctr_paes_set_key(ctx)) {
  366. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  367. return -EINVAL;
  368. }
  369. return 0;
  370. }
  371. static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
  372. {
  373. unsigned int i, n;
  374. /* only use complete blocks, max. PAGE_SIZE */
  375. memcpy(ctrptr, iv, AES_BLOCK_SIZE);
  376. n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
  377. for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
  378. memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
  379. crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  380. ctrptr += AES_BLOCK_SIZE;
  381. }
  382. return n;
  383. }
  384. static int ctr_paes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
  385. struct blkcipher_walk *walk)
  386. {
  387. struct s390_paes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  388. u8 buf[AES_BLOCK_SIZE], *ctrptr;
  389. unsigned int nbytes, n, k;
  390. int ret, locked;
  391. locked = spin_trylock(&ctrblk_lock);
  392. ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
  393. while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
  394. n = AES_BLOCK_SIZE;
  395. if (nbytes >= 2*AES_BLOCK_SIZE && locked)
  396. n = __ctrblk_init(ctrblk, walk->iv, nbytes);
  397. ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
  398. k = cpacf_kmctr(ctx->fc | modifier, ctx->pk.protkey,
  399. walk->dst.virt.addr, walk->src.virt.addr,
  400. n, ctrptr);
  401. if (k) {
  402. if (ctrptr == ctrblk)
  403. memcpy(walk->iv, ctrptr + k - AES_BLOCK_SIZE,
  404. AES_BLOCK_SIZE);
  405. crypto_inc(walk->iv, AES_BLOCK_SIZE);
  406. ret = blkcipher_walk_done(desc, walk, nbytes - n);
  407. }
  408. if (k < n) {
  409. if (__ctr_paes_set_key(ctx) != 0) {
  410. if (locked)
  411. spin_unlock(&ctrblk_lock);
  412. return blkcipher_walk_done(desc, walk, -EIO);
  413. }
  414. }
  415. }
  416. if (locked)
  417. spin_unlock(&ctrblk_lock);
  418. /*
  419. * final block may be < AES_BLOCK_SIZE, copy only nbytes
  420. */
  421. if (nbytes) {
  422. while (1) {
  423. if (cpacf_kmctr(ctx->fc | modifier,
  424. ctx->pk.protkey, buf,
  425. walk->src.virt.addr, AES_BLOCK_SIZE,
  426. walk->iv) == AES_BLOCK_SIZE)
  427. break;
  428. if (__ctr_paes_set_key(ctx) != 0)
  429. return blkcipher_walk_done(desc, walk, -EIO);
  430. }
  431. memcpy(walk->dst.virt.addr, buf, nbytes);
  432. crypto_inc(walk->iv, AES_BLOCK_SIZE);
  433. ret = blkcipher_walk_done(desc, walk, 0);
  434. }
  435. return ret;
  436. }
  437. static int ctr_paes_encrypt(struct blkcipher_desc *desc,
  438. struct scatterlist *dst, struct scatterlist *src,
  439. unsigned int nbytes)
  440. {
  441. struct blkcipher_walk walk;
  442. blkcipher_walk_init(&walk, dst, src, nbytes);
  443. return ctr_paes_crypt(desc, 0, &walk);
  444. }
  445. static int ctr_paes_decrypt(struct blkcipher_desc *desc,
  446. struct scatterlist *dst, struct scatterlist *src,
  447. unsigned int nbytes)
  448. {
  449. struct blkcipher_walk walk;
  450. blkcipher_walk_init(&walk, dst, src, nbytes);
  451. return ctr_paes_crypt(desc, CPACF_DECRYPT, &walk);
  452. }
  453. static struct crypto_alg ctr_paes_alg = {
  454. .cra_name = "ctr(paes)",
  455. .cra_driver_name = "ctr-paes-s390",
  456. .cra_priority = 400, /* combo: aes + ctr */
  457. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  458. .cra_blocksize = 1,
  459. .cra_ctxsize = sizeof(struct s390_paes_ctx),
  460. .cra_type = &crypto_blkcipher_type,
  461. .cra_module = THIS_MODULE,
  462. .cra_list = LIST_HEAD_INIT(ctr_paes_alg.cra_list),
  463. .cra_u = {
  464. .blkcipher = {
  465. .min_keysize = SECKEYBLOBSIZE,
  466. .max_keysize = SECKEYBLOBSIZE,
  467. .ivsize = AES_BLOCK_SIZE,
  468. .setkey = ctr_paes_set_key,
  469. .encrypt = ctr_paes_encrypt,
  470. .decrypt = ctr_paes_decrypt,
  471. }
  472. }
  473. };
  474. static inline void __crypto_unregister_alg(struct crypto_alg *alg)
  475. {
  476. if (!list_empty(&alg->cra_list))
  477. crypto_unregister_alg(alg);
  478. }
  479. static void paes_s390_fini(void)
  480. {
  481. if (ctrblk)
  482. free_page((unsigned long) ctrblk);
  483. __crypto_unregister_alg(&ctr_paes_alg);
  484. __crypto_unregister_alg(&xts_paes_alg);
  485. __crypto_unregister_alg(&cbc_paes_alg);
  486. __crypto_unregister_alg(&ecb_paes_alg);
  487. }
  488. static int __init paes_s390_init(void)
  489. {
  490. int ret;
  491. /* Query available functions for KM, KMC and KMCTR */
  492. cpacf_query(CPACF_KM, &km_functions);
  493. cpacf_query(CPACF_KMC, &kmc_functions);
  494. cpacf_query(CPACF_KMCTR, &kmctr_functions);
  495. if (cpacf_test_func(&km_functions, CPACF_KM_PAES_128) ||
  496. cpacf_test_func(&km_functions, CPACF_KM_PAES_192) ||
  497. cpacf_test_func(&km_functions, CPACF_KM_PAES_256)) {
  498. ret = crypto_register_alg(&ecb_paes_alg);
  499. if (ret)
  500. goto out_err;
  501. }
  502. if (cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
  503. cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
  504. cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256)) {
  505. ret = crypto_register_alg(&cbc_paes_alg);
  506. if (ret)
  507. goto out_err;
  508. }
  509. if (cpacf_test_func(&km_functions, CPACF_KM_PXTS_128) ||
  510. cpacf_test_func(&km_functions, CPACF_KM_PXTS_256)) {
  511. ret = crypto_register_alg(&xts_paes_alg);
  512. if (ret)
  513. goto out_err;
  514. }
  515. if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_128) ||
  516. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_192) ||
  517. cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_256)) {
  518. ret = crypto_register_alg(&ctr_paes_alg);
  519. if (ret)
  520. goto out_err;
  521. ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
  522. if (!ctrblk) {
  523. ret = -ENOMEM;
  524. goto out_err;
  525. }
  526. }
  527. return 0;
  528. out_err:
  529. paes_s390_fini();
  530. return ret;
  531. }
  532. module_init(paes_s390_init);
  533. module_exit(paes_s390_fini);
  534. MODULE_ALIAS_CRYPTO("paes");
  535. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm with protected keys");
  536. MODULE_LICENSE("GPL");