1 // SPDX-License-Identifier: GPL-2.0
5 * s390 implementation of the AES Cipher Algorithm with protected keys.
8 * Copyright IBM Corp. 2017,2019
9 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Harald Freudenberger <freude@de.ibm.com>
13 #define KMSG_COMPONENT "paes_s390"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <crypto/aes.h>
17 #include <crypto/algapi.h>
18 #include <linux/bug.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/cpufeature.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <crypto/internal/skcipher.h>
25 #include <crypto/xts.h>
26 #include <asm/cpacf.h>
30 * Key blobs smaller/bigger than these defines are rejected
31 * by the common code even before the individual setkey function
32 * is called. As paes can handle different kinds of key blobs
33 * and padding is also possible, the limits need to be generous.
35 #define PAES_MIN_KEYSIZE 64
36 #define PAES_MAX_KEYSIZE 256
39 static DEFINE_SPINLOCK(ctrblk_lock);
41 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
45 * Small keys will be stored in the keybuf. Larger keys are
46 * stored in extra allocated memory. In both cases does
47 * key point to the memory where the key is stored.
48 * The code distinguishes by checking keylen against
49 * sizeof(keybuf). See the two following helper functions.
56 static inline int _copy_key_to_kb(struct key_blob *kb,
60 if (keylen <= sizeof(kb->keybuf))
63 kb->key = kmalloc(keylen, GFP_KERNEL);
67 memcpy(kb->key, key, keylen);
73 static inline void _free_kb_keybuf(struct key_blob *kb)
75 if (kb->key && kb->key != kb->keybuf
76 && kb->keylen > sizeof(kb->keybuf)) {
82 struct s390_paes_ctx {
84 struct pkey_protkey pk;
88 struct s390_pxts_ctx {
89 struct key_blob kb[2];
90 struct pkey_protkey pk[2];
94 static inline int __paes_convert_key(struct key_blob *kb,
95 struct pkey_protkey *pk)
99 /* try three times in case of failure */
100 for (i = 0; i < 3; i++) {
101 ret = pkey_keyblob2pkey(kb->key, kb->keylen, pk);
109 static int __paes_set_key(struct s390_paes_ctx *ctx)
113 if (__paes_convert_key(&ctx->kb, &ctx->pk))
116 /* Pick the correct function code based on the protected key type */
117 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PAES_128 :
118 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KM_PAES_192 :
119 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KM_PAES_256 : 0;
121 /* Check if the function code is available */
122 ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
124 return ctx->fc ? 0 : -EINVAL;
127 static int ecb_paes_init(struct crypto_skcipher *tfm)
129 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
136 static void ecb_paes_exit(struct crypto_skcipher *tfm)
138 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
140 _free_kb_keybuf(&ctx->kb);
143 static int ecb_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
144 unsigned int key_len)
147 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
149 _free_kb_keybuf(&ctx->kb);
150 rc = _copy_key_to_kb(&ctx->kb, in_key, key_len);
154 return __paes_set_key(ctx);
157 static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)
159 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
160 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
161 struct skcipher_walk walk;
162 unsigned int nbytes, n, k;
165 ret = skcipher_walk_virt(&walk, req, false);
166 while ((nbytes = walk.nbytes) != 0) {
167 /* only use complete blocks */
168 n = nbytes & ~(AES_BLOCK_SIZE - 1);
169 k = cpacf_km(ctx->fc | modifier, ctx->pk.protkey,
170 walk.dst.virt.addr, walk.src.virt.addr, n);
172 ret = skcipher_walk_done(&walk, nbytes - k);
174 if (__paes_set_key(ctx) != 0)
175 return skcipher_walk_done(&walk, -EIO);
181 static int ecb_paes_encrypt(struct skcipher_request *req)
183 return ecb_paes_crypt(req, 0);
186 static int ecb_paes_decrypt(struct skcipher_request *req)
188 return ecb_paes_crypt(req, CPACF_DECRYPT);
191 static struct skcipher_alg ecb_paes_alg = {
192 .base.cra_name = "ecb(paes)",
193 .base.cra_driver_name = "ecb-paes-s390",
194 .base.cra_priority = 401, /* combo: aes + ecb + 1 */
195 .base.cra_blocksize = AES_BLOCK_SIZE,
196 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
197 .base.cra_module = THIS_MODULE,
198 .base.cra_list = LIST_HEAD_INIT(ecb_paes_alg.base.cra_list),
199 .init = ecb_paes_init,
200 .exit = ecb_paes_exit,
201 .min_keysize = PAES_MIN_KEYSIZE,
202 .max_keysize = PAES_MAX_KEYSIZE,
203 .setkey = ecb_paes_set_key,
204 .encrypt = ecb_paes_encrypt,
205 .decrypt = ecb_paes_decrypt,
208 static int cbc_paes_init(struct crypto_skcipher *tfm)
210 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
217 static void cbc_paes_exit(struct crypto_skcipher *tfm)
219 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
221 _free_kb_keybuf(&ctx->kb);
224 static int __cbc_paes_set_key(struct s390_paes_ctx *ctx)
228 if (__paes_convert_key(&ctx->kb, &ctx->pk))
231 /* Pick the correct function code based on the protected key type */
232 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMC_PAES_128 :
233 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMC_PAES_192 :
234 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KMC_PAES_256 : 0;
236 /* Check if the function code is available */
237 ctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
239 return ctx->fc ? 0 : -EINVAL;
242 static int cbc_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
243 unsigned int key_len)
246 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
248 _free_kb_keybuf(&ctx->kb);
249 rc = _copy_key_to_kb(&ctx->kb, in_key, key_len);
253 return __cbc_paes_set_key(ctx);
256 static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)
258 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
259 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
260 struct skcipher_walk walk;
261 unsigned int nbytes, n, k;
264 u8 iv[AES_BLOCK_SIZE];
265 u8 key[MAXPROTKEYSIZE];
268 ret = skcipher_walk_virt(&walk, req, false);
271 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
272 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
273 while ((nbytes = walk.nbytes) != 0) {
274 /* only use complete blocks */
275 n = nbytes & ~(AES_BLOCK_SIZE - 1);
276 k = cpacf_kmc(ctx->fc | modifier, ¶m,
277 walk.dst.virt.addr, walk.src.virt.addr, n);
279 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
280 ret = skcipher_walk_done(&walk, nbytes - k);
283 if (__cbc_paes_set_key(ctx) != 0)
284 return skcipher_walk_done(&walk, -EIO);
285 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
291 static int cbc_paes_encrypt(struct skcipher_request *req)
293 return cbc_paes_crypt(req, 0);
296 static int cbc_paes_decrypt(struct skcipher_request *req)
298 return cbc_paes_crypt(req, CPACF_DECRYPT);
301 static struct skcipher_alg cbc_paes_alg = {
302 .base.cra_name = "cbc(paes)",
303 .base.cra_driver_name = "cbc-paes-s390",
304 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
305 .base.cra_blocksize = AES_BLOCK_SIZE,
306 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
307 .base.cra_module = THIS_MODULE,
308 .base.cra_list = LIST_HEAD_INIT(cbc_paes_alg.base.cra_list),
309 .init = cbc_paes_init,
310 .exit = cbc_paes_exit,
311 .min_keysize = PAES_MIN_KEYSIZE,
312 .max_keysize = PAES_MAX_KEYSIZE,
313 .ivsize = AES_BLOCK_SIZE,
314 .setkey = cbc_paes_set_key,
315 .encrypt = cbc_paes_encrypt,
316 .decrypt = cbc_paes_decrypt,
319 static int xts_paes_init(struct crypto_skcipher *tfm)
321 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
323 ctx->kb[0].key = NULL;
324 ctx->kb[1].key = NULL;
329 static void xts_paes_exit(struct crypto_skcipher *tfm)
331 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
333 _free_kb_keybuf(&ctx->kb[0]);
334 _free_kb_keybuf(&ctx->kb[1]);
337 static int __xts_paes_set_key(struct s390_pxts_ctx *ctx)
341 if (__paes_convert_key(&ctx->kb[0], &ctx->pk[0]) ||
342 __paes_convert_key(&ctx->kb[1], &ctx->pk[1]))
345 if (ctx->pk[0].type != ctx->pk[1].type)
348 /* Pick the correct function code based on the protected key type */
349 fc = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PXTS_128 :
350 (ctx->pk[0].type == PKEY_KEYTYPE_AES_256) ?
351 CPACF_KM_PXTS_256 : 0;
353 /* Check if the function code is available */
354 ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
356 return ctx->fc ? 0 : -EINVAL;
359 static int xts_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
360 unsigned int xts_key_len)
363 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
364 u8 ckey[2 * AES_MAX_KEY_SIZE];
365 unsigned int ckey_len, key_len;
370 key_len = xts_key_len / 2;
372 _free_kb_keybuf(&ctx->kb[0]);
373 _free_kb_keybuf(&ctx->kb[1]);
374 rc = _copy_key_to_kb(&ctx->kb[0], in_key, key_len);
377 rc = _copy_key_to_kb(&ctx->kb[1], in_key + key_len, key_len);
381 rc = __xts_paes_set_key(ctx);
386 * xts_check_key verifies the key length is not odd and makes
387 * sure that the two keys are not the same. This can be done
388 * on the two protected keys as well
390 ckey_len = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ?
391 AES_KEYSIZE_128 : AES_KEYSIZE_256;
392 memcpy(ckey, ctx->pk[0].protkey, ckey_len);
393 memcpy(ckey + ckey_len, ctx->pk[1].protkey, ckey_len);
394 return xts_verify_key(tfm, ckey, 2*ckey_len);
397 static int xts_paes_crypt(struct skcipher_request *req, unsigned long modifier)
399 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
400 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
401 struct skcipher_walk walk;
402 unsigned int keylen, offset, nbytes, n, k;
405 u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
412 u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
416 ret = skcipher_walk_virt(&walk, req, false);
419 keylen = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 48 : 64;
420 offset = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 16 : 0;
422 memset(&pcc_param, 0, sizeof(pcc_param));
423 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
424 memcpy(pcc_param.key + offset, ctx->pk[1].protkey, keylen);
425 cpacf_pcc(ctx->fc, pcc_param.key + offset);
427 memcpy(xts_param.key + offset, ctx->pk[0].protkey, keylen);
428 memcpy(xts_param.init, pcc_param.xts, 16);
430 while ((nbytes = walk.nbytes) != 0) {
431 /* only use complete blocks */
432 n = nbytes & ~(AES_BLOCK_SIZE - 1);
433 k = cpacf_km(ctx->fc | modifier, xts_param.key + offset,
434 walk.dst.virt.addr, walk.src.virt.addr, n);
436 ret = skcipher_walk_done(&walk, nbytes - k);
438 if (__xts_paes_set_key(ctx) != 0)
439 return skcipher_walk_done(&walk, -EIO);
446 static int xts_paes_encrypt(struct skcipher_request *req)
448 return xts_paes_crypt(req, 0);
451 static int xts_paes_decrypt(struct skcipher_request *req)
453 return xts_paes_crypt(req, CPACF_DECRYPT);
456 static struct skcipher_alg xts_paes_alg = {
457 .base.cra_name = "xts(paes)",
458 .base.cra_driver_name = "xts-paes-s390",
459 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
460 .base.cra_blocksize = AES_BLOCK_SIZE,
461 .base.cra_ctxsize = sizeof(struct s390_pxts_ctx),
462 .base.cra_module = THIS_MODULE,
463 .base.cra_list = LIST_HEAD_INIT(xts_paes_alg.base.cra_list),
464 .init = xts_paes_init,
465 .exit = xts_paes_exit,
466 .min_keysize = 2 * PAES_MIN_KEYSIZE,
467 .max_keysize = 2 * PAES_MAX_KEYSIZE,
468 .ivsize = AES_BLOCK_SIZE,
469 .setkey = xts_paes_set_key,
470 .encrypt = xts_paes_encrypt,
471 .decrypt = xts_paes_decrypt,
474 static int ctr_paes_init(struct crypto_skcipher *tfm)
476 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
483 static void ctr_paes_exit(struct crypto_skcipher *tfm)
485 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
487 _free_kb_keybuf(&ctx->kb);
490 static int __ctr_paes_set_key(struct s390_paes_ctx *ctx)
494 if (__paes_convert_key(&ctx->kb, &ctx->pk))
497 /* Pick the correct function code based on the protected key type */
498 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMCTR_PAES_128 :
499 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMCTR_PAES_192 :
500 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ?
501 CPACF_KMCTR_PAES_256 : 0;
503 /* Check if the function code is available */
504 ctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
506 return ctx->fc ? 0 : -EINVAL;
509 static int ctr_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
510 unsigned int key_len)
513 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
515 _free_kb_keybuf(&ctx->kb);
516 rc = _copy_key_to_kb(&ctx->kb, in_key, key_len);
520 return __ctr_paes_set_key(ctx);
523 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
527 /* only use complete blocks, max. PAGE_SIZE */
528 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
529 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
530 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
531 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
532 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
533 ctrptr += AES_BLOCK_SIZE;
538 static int ctr_paes_crypt(struct skcipher_request *req)
540 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
541 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
542 u8 buf[AES_BLOCK_SIZE], *ctrptr;
543 struct skcipher_walk walk;
544 unsigned int nbytes, n, k;
547 locked = spin_trylock(&ctrblk_lock);
549 ret = skcipher_walk_virt(&walk, req, false);
550 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
552 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
553 n = __ctrblk_init(ctrblk, walk.iv, nbytes);
554 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
555 k = cpacf_kmctr(ctx->fc, ctx->pk.protkey, walk.dst.virt.addr,
556 walk.src.virt.addr, n, ctrptr);
558 if (ctrptr == ctrblk)
559 memcpy(walk.iv, ctrptr + k - AES_BLOCK_SIZE,
561 crypto_inc(walk.iv, AES_BLOCK_SIZE);
562 ret = skcipher_walk_done(&walk, nbytes - n);
565 if (__ctr_paes_set_key(ctx) != 0) {
567 spin_unlock(&ctrblk_lock);
568 return skcipher_walk_done(&walk, -EIO);
573 spin_unlock(&ctrblk_lock);
575 * final block may be < AES_BLOCK_SIZE, copy only nbytes
579 if (cpacf_kmctr(ctx->fc, ctx->pk.protkey, buf,
580 walk.src.virt.addr, AES_BLOCK_SIZE,
581 walk.iv) == AES_BLOCK_SIZE)
583 if (__ctr_paes_set_key(ctx) != 0)
584 return skcipher_walk_done(&walk, -EIO);
586 memcpy(walk.dst.virt.addr, buf, nbytes);
587 crypto_inc(walk.iv, AES_BLOCK_SIZE);
588 ret = skcipher_walk_done(&walk, 0);
594 static struct skcipher_alg ctr_paes_alg = {
595 .base.cra_name = "ctr(paes)",
596 .base.cra_driver_name = "ctr-paes-s390",
597 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
598 .base.cra_blocksize = 1,
599 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
600 .base.cra_module = THIS_MODULE,
601 .base.cra_list = LIST_HEAD_INIT(ctr_paes_alg.base.cra_list),
602 .init = ctr_paes_init,
603 .exit = ctr_paes_exit,
604 .min_keysize = PAES_MIN_KEYSIZE,
605 .max_keysize = PAES_MAX_KEYSIZE,
606 .ivsize = AES_BLOCK_SIZE,
607 .setkey = ctr_paes_set_key,
608 .encrypt = ctr_paes_crypt,
609 .decrypt = ctr_paes_crypt,
610 .chunksize = AES_BLOCK_SIZE,
613 static inline void __crypto_unregister_skcipher(struct skcipher_alg *alg)
615 if (!list_empty(&alg->base.cra_list))
616 crypto_unregister_skcipher(alg);
619 static void paes_s390_fini(void)
622 free_page((unsigned long) ctrblk);
623 __crypto_unregister_skcipher(&ctr_paes_alg);
624 __crypto_unregister_skcipher(&xts_paes_alg);
625 __crypto_unregister_skcipher(&cbc_paes_alg);
626 __crypto_unregister_skcipher(&ecb_paes_alg);
629 static int __init paes_s390_init(void)
633 /* Query available functions for KM, KMC and KMCTR */
634 cpacf_query(CPACF_KM, &km_functions);
635 cpacf_query(CPACF_KMC, &kmc_functions);
636 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);
646 if (cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
647 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
648 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256)) {
649 ret = crypto_register_skcipher(&cbc_paes_alg);
654 if (cpacf_test_func(&km_functions, CPACF_KM_PXTS_128) ||
655 cpacf_test_func(&km_functions, CPACF_KM_PXTS_256)) {
656 ret = crypto_register_skcipher(&xts_paes_alg);
661 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_128) ||
662 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_192) ||
663 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_256)) {
664 ret = crypto_register_skcipher(&ctr_paes_alg);
667 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
680 module_init(paes_s390_init);
681 module_exit(paes_s390_fini);
683 MODULE_ALIAS_CRYPTO("paes");
685 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm with protected keys");
686 MODULE_LICENSE("GPL");