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 if (__paes_set_key(ctx)) {
155 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
161 static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)
163 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
164 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
165 struct skcipher_walk walk;
166 unsigned int nbytes, n, k;
169 ret = skcipher_walk_virt(&walk, req, false);
170 while ((nbytes = walk.nbytes) != 0) {
171 /* only use complete blocks */
172 n = nbytes & ~(AES_BLOCK_SIZE - 1);
173 k = cpacf_km(ctx->fc | modifier, ctx->pk.protkey,
174 walk.dst.virt.addr, walk.src.virt.addr, n);
176 ret = skcipher_walk_done(&walk, nbytes - k);
178 if (__paes_set_key(ctx) != 0)
179 return skcipher_walk_done(&walk, -EIO);
185 static int ecb_paes_encrypt(struct skcipher_request *req)
187 return ecb_paes_crypt(req, 0);
190 static int ecb_paes_decrypt(struct skcipher_request *req)
192 return ecb_paes_crypt(req, CPACF_DECRYPT);
195 static struct skcipher_alg ecb_paes_alg = {
196 .base.cra_name = "ecb(paes)",
197 .base.cra_driver_name = "ecb-paes-s390",
198 .base.cra_priority = 401, /* combo: aes + ecb + 1 */
199 .base.cra_blocksize = AES_BLOCK_SIZE,
200 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
201 .base.cra_module = THIS_MODULE,
202 .base.cra_list = LIST_HEAD_INIT(ecb_paes_alg.base.cra_list),
203 .init = ecb_paes_init,
204 .exit = ecb_paes_exit,
205 .min_keysize = PAES_MIN_KEYSIZE,
206 .max_keysize = PAES_MAX_KEYSIZE,
207 .setkey = ecb_paes_set_key,
208 .encrypt = ecb_paes_encrypt,
209 .decrypt = ecb_paes_decrypt,
212 static int cbc_paes_init(struct crypto_skcipher *tfm)
214 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
221 static void cbc_paes_exit(struct crypto_skcipher *tfm)
223 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
225 _free_kb_keybuf(&ctx->kb);
228 static int __cbc_paes_set_key(struct s390_paes_ctx *ctx)
232 if (__paes_convert_key(&ctx->kb, &ctx->pk))
235 /* Pick the correct function code based on the protected key type */
236 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMC_PAES_128 :
237 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMC_PAES_192 :
238 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KMC_PAES_256 : 0;
240 /* Check if the function code is available */
241 ctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
243 return ctx->fc ? 0 : -EINVAL;
246 static int cbc_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
247 unsigned int key_len)
250 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
252 _free_kb_keybuf(&ctx->kb);
253 rc = _copy_key_to_kb(&ctx->kb, in_key, key_len);
257 if (__cbc_paes_set_key(ctx)) {
258 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
264 static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)
266 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
267 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
268 struct skcipher_walk walk;
269 unsigned int nbytes, n, k;
272 u8 iv[AES_BLOCK_SIZE];
273 u8 key[MAXPROTKEYSIZE];
276 ret = skcipher_walk_virt(&walk, req, false);
279 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
280 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
281 while ((nbytes = walk.nbytes) != 0) {
282 /* only use complete blocks */
283 n = nbytes & ~(AES_BLOCK_SIZE - 1);
284 k = cpacf_kmc(ctx->fc | modifier, ¶m,
285 walk.dst.virt.addr, walk.src.virt.addr, n);
287 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
288 ret = skcipher_walk_done(&walk, nbytes - k);
291 if (__cbc_paes_set_key(ctx) != 0)
292 return skcipher_walk_done(&walk, -EIO);
293 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
299 static int cbc_paes_encrypt(struct skcipher_request *req)
301 return cbc_paes_crypt(req, 0);
304 static int cbc_paes_decrypt(struct skcipher_request *req)
306 return cbc_paes_crypt(req, CPACF_DECRYPT);
309 static struct skcipher_alg cbc_paes_alg = {
310 .base.cra_name = "cbc(paes)",
311 .base.cra_driver_name = "cbc-paes-s390",
312 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
313 .base.cra_blocksize = AES_BLOCK_SIZE,
314 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
315 .base.cra_module = THIS_MODULE,
316 .base.cra_list = LIST_HEAD_INIT(cbc_paes_alg.base.cra_list),
317 .init = cbc_paes_init,
318 .exit = cbc_paes_exit,
319 .min_keysize = PAES_MIN_KEYSIZE,
320 .max_keysize = PAES_MAX_KEYSIZE,
321 .ivsize = AES_BLOCK_SIZE,
322 .setkey = cbc_paes_set_key,
323 .encrypt = cbc_paes_encrypt,
324 .decrypt = cbc_paes_decrypt,
327 static int xts_paes_init(struct crypto_skcipher *tfm)
329 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
331 ctx->kb[0].key = NULL;
332 ctx->kb[1].key = NULL;
337 static void xts_paes_exit(struct crypto_skcipher *tfm)
339 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
341 _free_kb_keybuf(&ctx->kb[0]);
342 _free_kb_keybuf(&ctx->kb[1]);
345 static int __xts_paes_set_key(struct s390_pxts_ctx *ctx)
349 if (__paes_convert_key(&ctx->kb[0], &ctx->pk[0]) ||
350 __paes_convert_key(&ctx->kb[1], &ctx->pk[1]))
353 if (ctx->pk[0].type != ctx->pk[1].type)
356 /* Pick the correct function code based on the protected key type */
357 fc = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PXTS_128 :
358 (ctx->pk[0].type == PKEY_KEYTYPE_AES_256) ?
359 CPACF_KM_PXTS_256 : 0;
361 /* Check if the function code is available */
362 ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
364 return ctx->fc ? 0 : -EINVAL;
367 static int xts_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
368 unsigned int xts_key_len)
371 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
372 u8 ckey[2 * AES_MAX_KEY_SIZE];
373 unsigned int ckey_len, key_len;
378 key_len = xts_key_len / 2;
380 _free_kb_keybuf(&ctx->kb[0]);
381 _free_kb_keybuf(&ctx->kb[1]);
382 rc = _copy_key_to_kb(&ctx->kb[0], in_key, key_len);
385 rc = _copy_key_to_kb(&ctx->kb[1], in_key + key_len, key_len);
389 if (__xts_paes_set_key(ctx)) {
390 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
395 * xts_check_key verifies the key length is not odd and makes
396 * sure that the two keys are not the same. This can be done
397 * on the two protected keys as well
399 ckey_len = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ?
400 AES_KEYSIZE_128 : AES_KEYSIZE_256;
401 memcpy(ckey, ctx->pk[0].protkey, ckey_len);
402 memcpy(ckey + ckey_len, ctx->pk[1].protkey, ckey_len);
403 return xts_verify_key(tfm, ckey, 2*ckey_len);
406 static int xts_paes_crypt(struct skcipher_request *req, unsigned long modifier)
408 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
409 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
410 struct skcipher_walk walk;
411 unsigned int keylen, offset, nbytes, n, k;
414 u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
421 u8 key[MAXPROTKEYSIZE]; /* key + verification pattern */
425 ret = skcipher_walk_virt(&walk, req, false);
428 keylen = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 48 : 64;
429 offset = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 16 : 0;
431 memset(&pcc_param, 0, sizeof(pcc_param));
432 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
433 memcpy(pcc_param.key + offset, ctx->pk[1].protkey, keylen);
434 cpacf_pcc(ctx->fc, pcc_param.key + offset);
436 memcpy(xts_param.key + offset, ctx->pk[0].protkey, keylen);
437 memcpy(xts_param.init, pcc_param.xts, 16);
439 while ((nbytes = walk.nbytes) != 0) {
440 /* only use complete blocks */
441 n = nbytes & ~(AES_BLOCK_SIZE - 1);
442 k = cpacf_km(ctx->fc | modifier, xts_param.key + offset,
443 walk.dst.virt.addr, walk.src.virt.addr, n);
445 ret = skcipher_walk_done(&walk, nbytes - k);
447 if (__xts_paes_set_key(ctx) != 0)
448 return skcipher_walk_done(&walk, -EIO);
455 static int xts_paes_encrypt(struct skcipher_request *req)
457 return xts_paes_crypt(req, 0);
460 static int xts_paes_decrypt(struct skcipher_request *req)
462 return xts_paes_crypt(req, CPACF_DECRYPT);
465 static struct skcipher_alg xts_paes_alg = {
466 .base.cra_name = "xts(paes)",
467 .base.cra_driver_name = "xts-paes-s390",
468 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
469 .base.cra_blocksize = AES_BLOCK_SIZE,
470 .base.cra_ctxsize = sizeof(struct s390_pxts_ctx),
471 .base.cra_module = THIS_MODULE,
472 .base.cra_list = LIST_HEAD_INIT(xts_paes_alg.base.cra_list),
473 .init = xts_paes_init,
474 .exit = xts_paes_exit,
475 .min_keysize = 2 * PAES_MIN_KEYSIZE,
476 .max_keysize = 2 * PAES_MAX_KEYSIZE,
477 .ivsize = AES_BLOCK_SIZE,
478 .setkey = xts_paes_set_key,
479 .encrypt = xts_paes_encrypt,
480 .decrypt = xts_paes_decrypt,
483 static int ctr_paes_init(struct crypto_skcipher *tfm)
485 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
492 static void ctr_paes_exit(struct crypto_skcipher *tfm)
494 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
496 _free_kb_keybuf(&ctx->kb);
499 static int __ctr_paes_set_key(struct s390_paes_ctx *ctx)
503 if (__paes_convert_key(&ctx->kb, &ctx->pk))
506 /* Pick the correct function code based on the protected key type */
507 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMCTR_PAES_128 :
508 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMCTR_PAES_192 :
509 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ?
510 CPACF_KMCTR_PAES_256 : 0;
512 /* Check if the function code is available */
513 ctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
515 return ctx->fc ? 0 : -EINVAL;
518 static int ctr_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
519 unsigned int key_len)
522 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
524 _free_kb_keybuf(&ctx->kb);
525 rc = _copy_key_to_kb(&ctx->kb, in_key, key_len);
529 if (__ctr_paes_set_key(ctx)) {
530 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
536 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
540 /* only use complete blocks, max. PAGE_SIZE */
541 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
542 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
543 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
544 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
545 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
546 ctrptr += AES_BLOCK_SIZE;
551 static int ctr_paes_crypt(struct skcipher_request *req)
553 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
554 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
555 u8 buf[AES_BLOCK_SIZE], *ctrptr;
556 struct skcipher_walk walk;
557 unsigned int nbytes, n, k;
560 locked = spin_trylock(&ctrblk_lock);
562 ret = skcipher_walk_virt(&walk, req, false);
563 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
565 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
566 n = __ctrblk_init(ctrblk, walk.iv, nbytes);
567 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
568 k = cpacf_kmctr(ctx->fc, ctx->pk.protkey, walk.dst.virt.addr,
569 walk.src.virt.addr, n, ctrptr);
571 if (ctrptr == ctrblk)
572 memcpy(walk.iv, ctrptr + k - AES_BLOCK_SIZE,
574 crypto_inc(walk.iv, AES_BLOCK_SIZE);
575 ret = skcipher_walk_done(&walk, nbytes - n);
578 if (__ctr_paes_set_key(ctx) != 0) {
580 spin_unlock(&ctrblk_lock);
581 return skcipher_walk_done(&walk, -EIO);
586 spin_unlock(&ctrblk_lock);
588 * final block may be < AES_BLOCK_SIZE, copy only nbytes
592 if (cpacf_kmctr(ctx->fc, ctx->pk.protkey, buf,
593 walk.src.virt.addr, AES_BLOCK_SIZE,
594 walk.iv) == AES_BLOCK_SIZE)
596 if (__ctr_paes_set_key(ctx) != 0)
597 return skcipher_walk_done(&walk, -EIO);
599 memcpy(walk.dst.virt.addr, buf, nbytes);
600 crypto_inc(walk.iv, AES_BLOCK_SIZE);
601 ret = skcipher_walk_done(&walk, 0);
607 static struct skcipher_alg ctr_paes_alg = {
608 .base.cra_name = "ctr(paes)",
609 .base.cra_driver_name = "ctr-paes-s390",
610 .base.cra_priority = 402, /* ecb-paes-s390 + 1 */
611 .base.cra_blocksize = 1,
612 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
613 .base.cra_module = THIS_MODULE,
614 .base.cra_list = LIST_HEAD_INIT(ctr_paes_alg.base.cra_list),
615 .init = ctr_paes_init,
616 .exit = ctr_paes_exit,
617 .min_keysize = PAES_MIN_KEYSIZE,
618 .max_keysize = PAES_MAX_KEYSIZE,
619 .ivsize = AES_BLOCK_SIZE,
620 .setkey = ctr_paes_set_key,
621 .encrypt = ctr_paes_crypt,
622 .decrypt = ctr_paes_crypt,
623 .chunksize = AES_BLOCK_SIZE,
626 static inline void __crypto_unregister_skcipher(struct skcipher_alg *alg)
628 if (!list_empty(&alg->base.cra_list))
629 crypto_unregister_skcipher(alg);
632 static void paes_s390_fini(void)
635 free_page((unsigned long) ctrblk);
636 __crypto_unregister_skcipher(&ctr_paes_alg);
637 __crypto_unregister_skcipher(&xts_paes_alg);
638 __crypto_unregister_skcipher(&cbc_paes_alg);
639 __crypto_unregister_skcipher(&ecb_paes_alg);
642 static int __init paes_s390_init(void)
646 /* Query available functions for KM, KMC and KMCTR */
647 cpacf_query(CPACF_KM, &km_functions);
648 cpacf_query(CPACF_KMC, &kmc_functions);
649 cpacf_query(CPACF_KMCTR, &kmctr_functions);
651 if (cpacf_test_func(&km_functions, CPACF_KM_PAES_128) ||
652 cpacf_test_func(&km_functions, CPACF_KM_PAES_192) ||
653 cpacf_test_func(&km_functions, CPACF_KM_PAES_256)) {
654 ret = crypto_register_skcipher(&ecb_paes_alg);
659 if (cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
660 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
661 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256)) {
662 ret = crypto_register_skcipher(&cbc_paes_alg);
667 if (cpacf_test_func(&km_functions, CPACF_KM_PXTS_128) ||
668 cpacf_test_func(&km_functions, CPACF_KM_PXTS_256)) {
669 ret = crypto_register_skcipher(&xts_paes_alg);
674 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_128) ||
675 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_192) ||
676 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_256)) {
677 ret = crypto_register_skcipher(&ctr_paes_alg);
680 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
693 module_init(paes_s390_init);
694 module_exit(paes_s390_fini);
696 MODULE_ALIAS_CRYPTO("paes");
698 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm with protected keys");
699 MODULE_LICENSE("GPL");