1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2017
6 * Author(s): Harald Freudenberger
9 #define KMSG_COMPONENT "pkey"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/init.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/kallsyms.h>
18 #include <linux/debugfs.h>
19 #include <linux/random.h>
20 #include <linux/cpufeature.h>
21 #include <asm/zcrypt.h>
22 #include <asm/cpacf.h>
24 #include <crypto/aes.h>
26 #include "zcrypt_api.h"
27 #include "zcrypt_ccamisc.h"
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("IBM Corporation");
31 MODULE_DESCRIPTION("s390 protected key interface");
33 /* mask of available pckmo subfunctions, fetched once at module init */
34 static cpacf_mask_t pckmo_functions;
37 * debug feature data and functions
40 static debug_info_t *debug_info;
42 #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
43 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
44 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
45 #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
47 static void __init pkey_debug_init(void)
49 /* 5 arguments per dbf entry (including the format string ptr) */
50 debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
51 debug_register_view(debug_info, &debug_sprintf_view);
52 debug_set_level(debug_info, 3);
55 static void __exit pkey_debug_exit(void)
57 debug_unregister(debug_info);
60 /* inside view of a protected key token (only type 0x00 version 0x01) */
61 struct protaeskeytoken {
62 u8 type; /* 0x00 for PAES specific key tokens */
64 u8 version; /* should be 0x01 for protected AES key token */
66 u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
67 u32 len; /* bytes actually stored in protkey[] */
68 u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */
72 * Create a protected key from a clear key value.
74 int pkey_clr2protkey(u32 keytype,
75 const struct pkey_clrkey *clrkey,
76 struct pkey_protkey *protkey)
83 case PKEY_KEYTYPE_AES_128:
85 fc = CPACF_PCKMO_ENC_AES_128_KEY;
87 case PKEY_KEYTYPE_AES_192:
89 fc = CPACF_PCKMO_ENC_AES_192_KEY;
91 case PKEY_KEYTYPE_AES_256:
93 fc = CPACF_PCKMO_ENC_AES_256_KEY;
96 DEBUG_ERR("%s unknown/unsupported keytype %d\n",
102 * Check if the needed pckmo subfunction is available.
103 * These subfunctions can be enabled/disabled by customers
104 * in the LPAR profile or may even change on the fly.
106 if (!cpacf_test_func(&pckmo_functions, fc)) {
107 DEBUG_ERR("%s pckmo functions not available\n", __func__);
111 /* prepare param block */
112 memset(paramblock, 0, sizeof(paramblock));
113 memcpy(paramblock, clrkey->clrkey, keysize);
115 /* call the pckmo instruction */
116 cpacf_pckmo(fc, paramblock);
118 /* copy created protected key */
119 protkey->type = keytype;
120 protkey->len = keysize + 32;
121 memcpy(protkey->protkey, paramblock, keysize + 32);
125 EXPORT_SYMBOL(pkey_clr2protkey);
128 * Find card and transform secure key into protected key.
130 int pkey_skey2pkey(const struct pkey_seckey *seckey,
131 struct pkey_protkey *pkey)
137 * The cca_sec2protkey call may fail when a card has been
138 * addressed where the master key was changed after last fetch
139 * of the mkvp into the cache. Try 3 times: First witout verify
140 * then with verify and last round with verify and old master
141 * key verification pattern match not ignored.
143 for (verify = 0; verify < 3; verify++) {
144 rc = cca_findcard(seckey->seckey, &cardnr, &domain, verify);
147 if (rc > 0 && verify < 2)
149 rc = cca_sec2protkey(cardnr, domain, seckey->seckey,
150 pkey->protkey, &pkey->len, &pkey->type);
156 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
160 EXPORT_SYMBOL(pkey_skey2pkey);
163 * Verify key and give back some info about the key.
165 int pkey_verifykey(const struct pkey_seckey *seckey,
166 u16 *pcardnr, u16 *pdomain,
167 u16 *pkeysize, u32 *pattributes)
169 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
173 /* check the secure key for valid AES secure key */
174 rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
178 *pattributes = PKEY_VERIFY_ATTR_AES;
180 *pkeysize = t->bitsize;
182 /* try to find a card which can handle this key */
183 rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
188 /* key mkvp matches to old master key mkvp */
189 DEBUG_DBG("%s secure key has old mkvp\n", __func__);
191 *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
201 DEBUG_DBG("%s rc=%d\n", __func__, rc);
204 EXPORT_SYMBOL(pkey_verifykey);
207 * Generate a random protected key
209 int pkey_genprotkey(__u32 keytype, struct pkey_protkey *protkey)
211 struct pkey_clrkey clrkey;
216 case PKEY_KEYTYPE_AES_128:
219 case PKEY_KEYTYPE_AES_192:
222 case PKEY_KEYTYPE_AES_256:
226 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
231 /* generate a dummy random clear key */
232 get_random_bytes(clrkey.clrkey, keysize);
234 /* convert it to a dummy protected key */
235 rc = pkey_clr2protkey(keytype, &clrkey, protkey);
239 /* replace the key part of the protected key with random bytes */
240 get_random_bytes(protkey->protkey, keysize);
244 EXPORT_SYMBOL(pkey_genprotkey);
247 * Verify if a protected key is still valid
249 int pkey_verifyprotkey(const struct pkey_protkey *protkey)
253 u8 iv[AES_BLOCK_SIZE];
254 u8 key[MAXPROTKEYSIZE];
256 u8 null_msg[AES_BLOCK_SIZE];
257 u8 dest_buf[AES_BLOCK_SIZE];
260 switch (protkey->type) {
261 case PKEY_KEYTYPE_AES_128:
262 fc = CPACF_KMC_PAES_128;
264 case PKEY_KEYTYPE_AES_192:
265 fc = CPACF_KMC_PAES_192;
267 case PKEY_KEYTYPE_AES_256:
268 fc = CPACF_KMC_PAES_256;
271 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
276 memset(null_msg, 0, sizeof(null_msg));
278 memset(param.iv, 0, sizeof(param.iv));
279 memcpy(param.key, protkey->protkey, sizeof(param.key));
281 k = cpacf_kmc(fc | CPACF_ENCRYPT, ¶m, null_msg, dest_buf,
283 if (k != sizeof(null_msg)) {
284 DEBUG_ERR("%s protected key is not valid\n", __func__);
285 return -EKEYREJECTED;
290 EXPORT_SYMBOL(pkey_verifyprotkey);
293 * Transform a non-CCA key token into a protected key
295 static int pkey_nonccatok2pkey(const __u8 *key, __u32 keylen,
296 struct pkey_protkey *protkey)
298 struct keytoken_header *hdr = (struct keytoken_header *)key;
299 struct protaeskeytoken *t;
301 switch (hdr->version) {
302 case TOKVER_PROTECTED_KEY:
303 if (keylen != sizeof(struct protaeskeytoken))
306 t = (struct protaeskeytoken *)key;
307 protkey->len = t->len;
308 protkey->type = t->keytype;
309 memcpy(protkey->protkey, t->protkey,
310 sizeof(protkey->protkey));
312 return pkey_verifyprotkey(protkey);
314 DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
315 __func__, hdr->version);
321 * Transform a CCA internal key token into a protected key
323 static int pkey_ccainttok2pkey(const __u8 *key, __u32 keylen,
324 struct pkey_protkey *protkey)
326 struct keytoken_header *hdr = (struct keytoken_header *)key;
328 switch (hdr->version) {
330 if (keylen != sizeof(struct secaeskeytoken))
333 return pkey_skey2pkey((struct pkey_seckey *)key,
336 DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
337 __func__, hdr->version);
343 * Transform a key blob (of any type) into a protected key
345 int pkey_keyblob2pkey(const __u8 *key, __u32 keylen,
346 struct pkey_protkey *protkey)
348 struct keytoken_header *hdr = (struct keytoken_header *)key;
350 if (keylen < sizeof(struct keytoken_header))
354 case TOKTYPE_NON_CCA:
355 return pkey_nonccatok2pkey(key, keylen, protkey);
356 case TOKTYPE_CCA_INTERNAL:
357 return pkey_ccainttok2pkey(key, keylen, protkey);
359 DEBUG_ERR("%s unknown/unsupported blob type %d\n", __func__,
364 EXPORT_SYMBOL(pkey_keyblob2pkey);
370 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
377 struct pkey_genseck __user *ugs = (void __user *) arg;
378 struct pkey_genseck kgs;
380 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
382 rc = cca_genseckey(kgs.cardnr, kgs.domain,
383 kgs.keytype, kgs.seckey.seckey);
384 DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
387 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
391 case PKEY_CLR2SECK: {
392 struct pkey_clr2seck __user *ucs = (void __user *) arg;
393 struct pkey_clr2seck kcs;
395 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
397 rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
398 kcs.clrkey.clrkey, kcs.seckey.seckey);
399 DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
402 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
404 memzero_explicit(&kcs, sizeof(kcs));
407 case PKEY_SEC2PROTK: {
408 struct pkey_sec2protk __user *usp = (void __user *) arg;
409 struct pkey_sec2protk ksp;
411 if (copy_from_user(&ksp, usp, sizeof(ksp)))
413 rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
414 ksp.seckey.seckey, ksp.protkey.protkey,
415 NULL, &ksp.protkey.type);
416 DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
419 if (copy_to_user(usp, &ksp, sizeof(ksp)))
423 case PKEY_CLR2PROTK: {
424 struct pkey_clr2protk __user *ucp = (void __user *) arg;
425 struct pkey_clr2protk kcp;
427 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
429 rc = pkey_clr2protkey(kcp.keytype,
430 &kcp.clrkey, &kcp.protkey);
431 DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
434 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
436 memzero_explicit(&kcp, sizeof(kcp));
439 case PKEY_FINDCARD: {
440 struct pkey_findcard __user *ufc = (void __user *) arg;
441 struct pkey_findcard kfc;
443 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
445 rc = cca_findcard(kfc.seckey.seckey,
446 &kfc.cardnr, &kfc.domain, 1);
447 DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
450 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
454 case PKEY_SKEY2PKEY: {
455 struct pkey_skey2pkey __user *usp = (void __user *) arg;
456 struct pkey_skey2pkey ksp;
458 if (copy_from_user(&ksp, usp, sizeof(ksp)))
460 rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
461 DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
464 if (copy_to_user(usp, &ksp, sizeof(ksp)))
468 case PKEY_VERIFYKEY: {
469 struct pkey_verifykey __user *uvk = (void __user *) arg;
470 struct pkey_verifykey kvk;
472 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
474 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
475 &kvk.keysize, &kvk.attributes);
476 DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
479 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
483 case PKEY_GENPROTK: {
484 struct pkey_genprotk __user *ugp = (void __user *) arg;
485 struct pkey_genprotk kgp;
487 if (copy_from_user(&kgp, ugp, sizeof(kgp)))
489 rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
490 DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
493 if (copy_to_user(ugp, &kgp, sizeof(kgp)))
497 case PKEY_VERIFYPROTK: {
498 struct pkey_verifyprotk __user *uvp = (void __user *) arg;
499 struct pkey_verifyprotk kvp;
501 if (copy_from_user(&kvp, uvp, sizeof(kvp)))
503 rc = pkey_verifyprotkey(&kvp.protkey);
504 DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
507 case PKEY_KBLOB2PROTK: {
508 struct pkey_kblob2pkey __user *utp = (void __user *) arg;
509 struct pkey_kblob2pkey ktp;
513 if (copy_from_user(&ktp, utp, sizeof(ktp)))
515 if (ktp.keylen < MINKEYBLOBSIZE ||
516 ktp.keylen > MAXKEYBLOBSIZE)
519 kkey = kmalloc(ktp.keylen, GFP_KERNEL);
522 if (copy_from_user(kkey, ukey, ktp.keylen)) {
526 rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
527 DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
531 if (copy_to_user(utp, &ktp, sizeof(ktp)))
536 /* unknown/unsupported ioctl cmd */
544 * Sysfs and file io operations
548 * Sysfs attribute read function for all protected key binary attributes.
549 * The implementation can not deal with partial reads, because a new random
550 * protected key blob is generated with each read. In case of partial reads
551 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
553 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
554 loff_t off, size_t count)
556 struct protaeskeytoken protkeytoken;
557 struct pkey_protkey protkey;
560 if (off != 0 || count < sizeof(protkeytoken))
563 if (count < 2 * sizeof(protkeytoken))
566 memset(&protkeytoken, 0, sizeof(protkeytoken));
567 protkeytoken.type = TOKTYPE_NON_CCA;
568 protkeytoken.version = TOKVER_PROTECTED_KEY;
569 protkeytoken.keytype = keytype;
571 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
575 protkeytoken.len = protkey.len;
576 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
578 memcpy(buf, &protkeytoken, sizeof(protkeytoken));
581 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
585 protkeytoken.len = protkey.len;
586 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
588 memcpy(buf + sizeof(protkeytoken), &protkeytoken,
589 sizeof(protkeytoken));
591 return 2 * sizeof(protkeytoken);
594 return sizeof(protkeytoken);
597 static ssize_t protkey_aes_128_read(struct file *filp,
598 struct kobject *kobj,
599 struct bin_attribute *attr,
600 char *buf, loff_t off,
603 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
607 static ssize_t protkey_aes_192_read(struct file *filp,
608 struct kobject *kobj,
609 struct bin_attribute *attr,
610 char *buf, loff_t off,
613 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
617 static ssize_t protkey_aes_256_read(struct file *filp,
618 struct kobject *kobj,
619 struct bin_attribute *attr,
620 char *buf, loff_t off,
623 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
627 static ssize_t protkey_aes_128_xts_read(struct file *filp,
628 struct kobject *kobj,
629 struct bin_attribute *attr,
630 char *buf, loff_t off,
633 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
637 static ssize_t protkey_aes_256_xts_read(struct file *filp,
638 struct kobject *kobj,
639 struct bin_attribute *attr,
640 char *buf, loff_t off,
643 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
647 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
648 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
649 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
650 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
651 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
653 static struct bin_attribute *protkey_attrs[] = {
654 &bin_attr_protkey_aes_128,
655 &bin_attr_protkey_aes_192,
656 &bin_attr_protkey_aes_256,
657 &bin_attr_protkey_aes_128_xts,
658 &bin_attr_protkey_aes_256_xts,
662 static struct attribute_group protkey_attr_group = {
664 .bin_attrs = protkey_attrs,
668 * Sysfs attribute read function for all secure key ccadata binary attributes.
669 * The implementation can not deal with partial reads, because a new random
670 * protected key blob is generated with each read. In case of partial reads
671 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
673 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
674 loff_t off, size_t count)
677 struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
679 if (off != 0 || count < sizeof(struct secaeskeytoken))
682 if (count < 2 * sizeof(struct secaeskeytoken))
685 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
691 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
695 return 2 * sizeof(struct secaeskeytoken);
698 return sizeof(struct secaeskeytoken);
701 static ssize_t ccadata_aes_128_read(struct file *filp,
702 struct kobject *kobj,
703 struct bin_attribute *attr,
704 char *buf, loff_t off,
707 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
711 static ssize_t ccadata_aes_192_read(struct file *filp,
712 struct kobject *kobj,
713 struct bin_attribute *attr,
714 char *buf, loff_t off,
717 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
721 static ssize_t ccadata_aes_256_read(struct file *filp,
722 struct kobject *kobj,
723 struct bin_attribute *attr,
724 char *buf, loff_t off,
727 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
731 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
732 struct kobject *kobj,
733 struct bin_attribute *attr,
734 char *buf, loff_t off,
737 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
741 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
742 struct kobject *kobj,
743 struct bin_attribute *attr,
744 char *buf, loff_t off,
747 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
751 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
752 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
753 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
754 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
755 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
757 static struct bin_attribute *ccadata_attrs[] = {
758 &bin_attr_ccadata_aes_128,
759 &bin_attr_ccadata_aes_192,
760 &bin_attr_ccadata_aes_256,
761 &bin_attr_ccadata_aes_128_xts,
762 &bin_attr_ccadata_aes_256_xts,
766 static struct attribute_group ccadata_attr_group = {
768 .bin_attrs = ccadata_attrs,
771 static const struct attribute_group *pkey_attr_groups[] = {
777 static const struct file_operations pkey_fops = {
778 .owner = THIS_MODULE,
779 .open = nonseekable_open,
781 .unlocked_ioctl = pkey_unlocked_ioctl,
784 static struct miscdevice pkey_dev = {
786 .minor = MISC_DYNAMIC_MINOR,
789 .groups = pkey_attr_groups,
795 static int __init pkey_init(void)
797 cpacf_mask_t kmc_functions;
800 * The pckmo instruction should be available - even if we don't
801 * actually invoke it. This instruction comes with MSA 3 which
802 * is also the minimum level for the kmc instructions which
803 * are able to work with protected keys.
805 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
808 /* check for kmc instructions available */
809 if (!cpacf_query(CPACF_KMC, &kmc_functions))
811 if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
812 !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
813 !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
818 return misc_register(&pkey_dev);
824 static void __exit pkey_exit(void)
826 misc_deregister(&pkey_dev);
830 module_cpu_feature_match(MSA, pkey_init);
831 module_exit(pkey_exit);