paes_s390.c 17 KB

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