1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2017,2019
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"
28 #include "zcrypt_ep11misc.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("IBM Corporation");
32 MODULE_DESCRIPTION("s390 protected key interface");
34 #define KEYBLOBBUFSIZE 8192 /* key buffer size used for internal processing */
35 #define PROTKEYBLOBBUFSIZE 256 /* protected key buffer size used internal */
36 #define MAXAPQNSINLIST 64 /* max 64 apqns within a apqn list */
39 * debug feature data and functions
42 static debug_info_t *debug_info;
44 #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
45 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
46 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
47 #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
49 static void __init pkey_debug_init(void)
51 /* 5 arguments per dbf entry (including the format string ptr) */
52 debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
53 debug_register_view(debug_info, &debug_sprintf_view);
54 debug_set_level(debug_info, 3);
57 static void __exit pkey_debug_exit(void)
59 debug_unregister(debug_info);
62 /* inside view of a protected key token (only type 0x00 version 0x01) */
63 struct protaeskeytoken {
64 u8 type; /* 0x00 for PAES specific key tokens */
66 u8 version; /* should be 0x01 for protected AES key token */
68 u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
69 u32 len; /* bytes actually stored in protkey[] */
70 u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */
73 /* inside view of a clear key token (type 0x00 version 0x02) */
74 struct clearaeskeytoken {
75 u8 type; /* 0x00 for PAES specific key tokens */
77 u8 version; /* 0x02 for clear AES key token */
79 u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
80 u32 len; /* bytes actually stored in clearkey[] */
81 u8 clearkey[]; /* clear key value */
85 * Create a protected key from a clear key value.
87 static int pkey_clr2protkey(u32 keytype,
88 const struct pkey_clrkey *clrkey,
89 struct pkey_protkey *protkey)
91 /* mask of available pckmo subfunctions */
92 static cpacf_mask_t pckmo_functions;
99 case PKEY_KEYTYPE_AES_128:
101 fc = CPACF_PCKMO_ENC_AES_128_KEY;
103 case PKEY_KEYTYPE_AES_192:
105 fc = CPACF_PCKMO_ENC_AES_192_KEY;
107 case PKEY_KEYTYPE_AES_256:
109 fc = CPACF_PCKMO_ENC_AES_256_KEY;
112 DEBUG_ERR("%s unknown/unsupported keytype %d\n",
117 /* Did we already check for PCKMO ? */
118 if (!pckmo_functions.bytes[0]) {
119 /* no, so check now */
120 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
123 /* check for the pckmo subfunction we need now */
124 if (!cpacf_test_func(&pckmo_functions, fc)) {
125 DEBUG_ERR("%s pckmo functions not available\n", __func__);
129 /* prepare param block */
130 memset(paramblock, 0, sizeof(paramblock));
131 memcpy(paramblock, clrkey->clrkey, keysize);
133 /* call the pckmo instruction */
134 cpacf_pckmo(fc, paramblock);
136 /* copy created protected key */
137 protkey->type = keytype;
138 protkey->len = keysize + 32;
139 memcpy(protkey->protkey, paramblock, keysize + 32);
145 * Find card and transform secure key into protected key.
147 static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
151 struct keytoken_header *hdr = (struct keytoken_header *)key;
154 * The cca_xxx2protkey call may fail when a card has been
155 * addressed where the master key was changed after last fetch
156 * of the mkvp into the cache. Try 3 times: First witout verify
157 * then with verify and last round with verify and old master
158 * key verification pattern match not ignored.
160 for (verify = 0; verify < 3; verify++) {
161 rc = cca_findcard(key, &cardnr, &domain, verify);
164 if (rc > 0 && verify < 2)
166 switch (hdr->version) {
168 rc = cca_sec2protkey(cardnr, domain,
170 &pkey->len, &pkey->type);
172 case TOKVER_CCA_VLSC:
173 rc = cca_cipher2protkey(cardnr, domain,
175 &pkey->len, &pkey->type);
185 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
191 * Construct EP11 key with given clear key value.
193 static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
194 u8 *keybuf, size_t *keybuflen)
198 u32 nr_apqns, *apqns = NULL;
200 /* build a list of apqns suitable for ep11 keys with cpacf support */
201 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
202 ZCRYPT_CEX7, EP11_API_V, NULL);
206 /* go through the list of apqns and try to bild an ep11 key */
207 for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
208 card = apqns[i] >> 16;
209 dom = apqns[i] & 0xFFFF;
210 rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
211 0, clrkey, keybuf, keybuflen);
219 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
224 * Find card and transform EP11 secure key into protected key.
226 static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
230 u32 nr_apqns, *apqns = NULL;
231 struct ep11keyblob *kb = (struct ep11keyblob *) key;
233 /* build a list of apqns suitable for this key */
234 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
235 ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
239 /* go through the list of apqns and try to derive an pkey */
240 for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
241 card = apqns[i] >> 16;
242 dom = apqns[i] & 0xFFFF;
243 pkey->len = sizeof(pkey->protkey);
244 rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
245 pkey->protkey, &pkey->len, &pkey->type);
253 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
258 * Verify key and give back some info about the key.
260 static int pkey_verifykey(const struct pkey_seckey *seckey,
261 u16 *pcardnr, u16 *pdomain,
262 u16 *pkeysize, u32 *pattributes)
264 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
268 /* check the secure key for valid AES secure key */
269 rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
273 *pattributes = PKEY_VERIFY_ATTR_AES;
275 *pkeysize = t->bitsize;
277 /* try to find a card which can handle this key */
278 rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
283 /* key mkvp matches to old master key mkvp */
284 DEBUG_DBG("%s secure key has old mkvp\n", __func__);
286 *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
296 DEBUG_DBG("%s rc=%d\n", __func__, rc);
301 * Generate a random protected key
303 static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
305 struct pkey_clrkey clrkey;
310 case PKEY_KEYTYPE_AES_128:
313 case PKEY_KEYTYPE_AES_192:
316 case PKEY_KEYTYPE_AES_256:
320 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
325 /* generate a dummy random clear key */
326 get_random_bytes(clrkey.clrkey, keysize);
328 /* convert it to a dummy protected key */
329 rc = pkey_clr2protkey(keytype, &clrkey, protkey);
333 /* replace the key part of the protected key with random bytes */
334 get_random_bytes(protkey->protkey, keysize);
340 * Verify if a protected key is still valid
342 static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
346 u8 iv[AES_BLOCK_SIZE];
347 u8 key[MAXPROTKEYSIZE];
349 u8 null_msg[AES_BLOCK_SIZE];
350 u8 dest_buf[AES_BLOCK_SIZE];
353 switch (protkey->type) {
354 case PKEY_KEYTYPE_AES_128:
355 fc = CPACF_KMC_PAES_128;
357 case PKEY_KEYTYPE_AES_192:
358 fc = CPACF_KMC_PAES_192;
360 case PKEY_KEYTYPE_AES_256:
361 fc = CPACF_KMC_PAES_256;
364 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
369 memset(null_msg, 0, sizeof(null_msg));
371 memset(param.iv, 0, sizeof(param.iv));
372 memcpy(param.key, protkey->protkey, sizeof(param.key));
374 k = cpacf_kmc(fc | CPACF_ENCRYPT, ¶m, null_msg, dest_buf,
376 if (k != sizeof(null_msg)) {
377 DEBUG_ERR("%s protected key is not valid\n", __func__);
378 return -EKEYREJECTED;
385 * Transform a non-CCA key token into a protected key
387 static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
388 struct pkey_protkey *protkey)
392 struct keytoken_header *hdr = (struct keytoken_header *)key;
394 switch (hdr->version) {
395 case TOKVER_PROTECTED_KEY: {
396 struct protaeskeytoken *t;
398 if (keylen != sizeof(struct protaeskeytoken))
400 t = (struct protaeskeytoken *)key;
401 protkey->len = t->len;
402 protkey->type = t->keytype;
403 memcpy(protkey->protkey, t->protkey,
404 sizeof(protkey->protkey));
405 rc = pkey_verifyprotkey(protkey);
408 case TOKVER_CLEAR_KEY: {
409 struct clearaeskeytoken *t;
410 struct pkey_clrkey ckey;
412 u8 skey[SECKEYBLOBSIZE];
413 u8 ep11key[MAXEP11AESKEYBLOBSIZE];
415 size_t tmpbuflen = sizeof(union u_tmpbuf);
417 if (keylen < sizeof(struct clearaeskeytoken))
419 t = (struct clearaeskeytoken *)key;
420 if (keylen != sizeof(*t) + t->len)
422 if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16)
423 || (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24)
424 || (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
425 memcpy(ckey.clrkey, t->clearkey, t->len);
428 /* alloc temp key buffer space */
429 tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
434 /* try direct way with the PCKMO instruction */
435 rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
438 /* PCKMO failed, so try the CCA secure key way */
439 rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
440 ckey.clrkey, tmpbuf);
442 rc = pkey_skey2pkey(tmpbuf, protkey);
445 /* if the CCA way also failed, let's try via EP11 */
446 rc = pkey_clr2ep11key(ckey.clrkey, t->len,
449 rc = pkey_ep11key2pkey(tmpbuf, protkey);
450 /* now we should really have an protected key */
451 DEBUG_ERR("%s unable to build protected key from clear",
455 case TOKVER_EP11_AES: {
456 /* check ep11 key for exportable as protected key */
457 rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
460 rc = pkey_ep11key2pkey(key, protkey);
463 case TOKVER_EP11_AES_WITH_HEADER:
464 /* check ep11 key with header for exportable as protected key */
465 rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
468 rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
472 DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
473 __func__, hdr->version);
483 * Transform a CCA internal key token into a protected key
485 static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
486 struct pkey_protkey *protkey)
488 struct keytoken_header *hdr = (struct keytoken_header *)key;
490 switch (hdr->version) {
492 if (keylen != sizeof(struct secaeskeytoken))
495 case TOKVER_CCA_VLSC:
496 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
500 DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
501 __func__, hdr->version);
505 return pkey_skey2pkey(key, protkey);
509 * Transform a key blob (of any type) into a protected key
511 int pkey_keyblob2pkey(const u8 *key, u32 keylen,
512 struct pkey_protkey *protkey)
515 struct keytoken_header *hdr = (struct keytoken_header *)key;
517 if (keylen < sizeof(struct keytoken_header)) {
518 DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
523 case TOKTYPE_NON_CCA:
524 rc = pkey_nonccatok2pkey(key, keylen, protkey);
526 case TOKTYPE_CCA_INTERNAL:
527 rc = pkey_ccainttok2pkey(key, keylen, protkey);
530 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
531 __func__, hdr->type);
535 DEBUG_DBG("%s rc=%d\n", __func__, rc);
539 EXPORT_SYMBOL(pkey_keyblob2pkey);
541 static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
542 enum pkey_key_type ktype, enum pkey_key_size ksize,
543 u32 kflags, u8 *keybuf, size_t *keybufsize)
545 int i, card, dom, rc;
547 /* check for at least one apqn given */
548 if (!apqns || !nr_apqns)
551 /* check key type and size */
553 case PKEY_TYPE_CCA_DATA:
554 case PKEY_TYPE_CCA_CIPHER:
555 if (*keybufsize < SECKEYBLOBSIZE)
559 if (*keybufsize < MINEP11AESKEYBLOBSIZE)
566 case PKEY_SIZE_AES_128:
567 case PKEY_SIZE_AES_192:
568 case PKEY_SIZE_AES_256:
574 /* simple try all apqns from the list */
575 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
576 card = apqns[i].card;
577 dom = apqns[i].domain;
578 if (ktype == PKEY_TYPE_EP11) {
579 rc = ep11_genaeskey(card, dom, ksize, kflags,
581 } else if (ktype == PKEY_TYPE_CCA_DATA) {
582 rc = cca_genseckey(card, dom, ksize, keybuf);
583 *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
584 } else /* TOKVER_CCA_VLSC */
585 rc = cca_gencipherkey(card, dom, ksize, kflags,
594 static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
595 enum pkey_key_type ktype, enum pkey_key_size ksize,
596 u32 kflags, const u8 *clrkey,
597 u8 *keybuf, size_t *keybufsize)
599 int i, card, dom, rc;
601 /* check for at least one apqn given */
602 if (!apqns || !nr_apqns)
605 /* check key type and size */
607 case PKEY_TYPE_CCA_DATA:
608 case PKEY_TYPE_CCA_CIPHER:
609 if (*keybufsize < SECKEYBLOBSIZE)
613 if (*keybufsize < MINEP11AESKEYBLOBSIZE)
620 case PKEY_SIZE_AES_128:
621 case PKEY_SIZE_AES_192:
622 case PKEY_SIZE_AES_256:
628 /* simple try all apqns from the list */
629 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
630 card = apqns[i].card;
631 dom = apqns[i].domain;
632 if (ktype == PKEY_TYPE_EP11) {
633 rc = ep11_clr2keyblob(card, dom, ksize, kflags,
634 clrkey, keybuf, keybufsize);
635 } else if (ktype == PKEY_TYPE_CCA_DATA) {
636 rc = cca_clr2seckey(card, dom, ksize,
638 *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
639 } else /* TOKVER_CCA_VLSC */
640 rc = cca_clr2cipherkey(card, dom, ksize, kflags,
641 clrkey, keybuf, keybufsize);
649 static int pkey_verifykey2(const u8 *key, size_t keylen,
650 u16 *cardnr, u16 *domain,
651 enum pkey_key_type *ktype,
652 enum pkey_key_size *ksize, u32 *flags)
655 u32 _nr_apqns, *_apqns = NULL;
656 struct keytoken_header *hdr = (struct keytoken_header *)key;
658 if (keylen < sizeof(struct keytoken_header))
661 if (hdr->type == TOKTYPE_CCA_INTERNAL
662 && hdr->version == TOKVER_CCA_AES) {
663 struct secaeskeytoken *t = (struct secaeskeytoken *)key;
665 rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
669 *ktype = PKEY_TYPE_CCA_DATA;
671 *ksize = (enum pkey_key_size) t->bitsize;
673 rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
674 ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
675 if (rc == 0 && flags)
676 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
678 rc = cca_findcard2(&_apqns, &_nr_apqns,
680 ZCRYPT_CEX3C, AES_MK_SET,
682 if (rc == 0 && flags)
683 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
688 *cardnr = ((struct pkey_apqn *)_apqns)->card;
689 *domain = ((struct pkey_apqn *)_apqns)->domain;
691 } else if (hdr->type == TOKTYPE_CCA_INTERNAL
692 && hdr->version == TOKVER_CCA_VLSC) {
693 struct cipherkeytoken *t = (struct cipherkeytoken *)key;
695 rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
699 *ktype = PKEY_TYPE_CCA_CIPHER;
701 *ksize = PKEY_SIZE_UNKNOWN;
702 if (!t->plfver && t->wpllen == 512)
703 *ksize = PKEY_SIZE_AES_128;
704 else if (!t->plfver && t->wpllen == 576)
705 *ksize = PKEY_SIZE_AES_192;
706 else if (!t->plfver && t->wpllen == 640)
707 *ksize = PKEY_SIZE_AES_256;
710 rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
711 ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
712 if (rc == 0 && flags)
713 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
715 rc = cca_findcard2(&_apqns, &_nr_apqns,
717 ZCRYPT_CEX6, AES_MK_SET,
719 if (rc == 0 && flags)
720 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
725 *cardnr = ((struct pkey_apqn *)_apqns)->card;
726 *domain = ((struct pkey_apqn *)_apqns)->domain;
728 } else if (hdr->type == TOKTYPE_NON_CCA
729 && hdr->version == TOKVER_EP11_AES) {
730 struct ep11keyblob *kb = (struct ep11keyblob *)key;
732 rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
736 *ktype = PKEY_TYPE_EP11;
738 *ksize = kb->head.bitlen;
740 rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
741 ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
746 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
748 *cardnr = ((struct pkey_apqn *)_apqns)->card;
749 *domain = ((struct pkey_apqn *)_apqns)->domain;
759 static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
760 const u8 *key, size_t keylen,
761 struct pkey_protkey *pkey)
763 int i, card, dom, rc;
764 struct keytoken_header *hdr = (struct keytoken_header *)key;
766 /* check for at least one apqn given */
767 if (!apqns || !nr_apqns)
770 if (keylen < sizeof(struct keytoken_header))
773 if (hdr->type == TOKTYPE_CCA_INTERNAL) {
774 if (hdr->version == TOKVER_CCA_AES) {
775 if (keylen != sizeof(struct secaeskeytoken))
777 if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
779 } else if (hdr->version == TOKVER_CCA_VLSC) {
780 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
782 if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
785 DEBUG_ERR("%s unknown CCA internal token version %d\n",
786 __func__, hdr->version);
789 } else if (hdr->type == TOKTYPE_NON_CCA) {
790 if (hdr->version == TOKVER_EP11_AES) {
791 if (keylen < sizeof(struct ep11keyblob))
793 if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
796 return pkey_nonccatok2pkey(key, keylen, pkey);
799 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
800 __func__, hdr->type);
804 /* simple try all apqns from the list */
805 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
806 card = apqns[i].card;
807 dom = apqns[i].domain;
808 if (hdr->type == TOKTYPE_CCA_INTERNAL
809 && hdr->version == TOKVER_CCA_AES)
810 rc = cca_sec2protkey(card, dom, key, pkey->protkey,
811 &pkey->len, &pkey->type);
812 else if (hdr->type == TOKTYPE_CCA_INTERNAL
813 && hdr->version == TOKVER_CCA_VLSC)
814 rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
815 &pkey->len, &pkey->type);
816 else { /* EP11 AES secure key blob */
817 struct ep11keyblob *kb = (struct ep11keyblob *) key;
819 pkey->len = sizeof(pkey->protkey);
820 rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
821 pkey->protkey, &pkey->len,
831 static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
832 struct pkey_apqn *apqns, size_t *nr_apqns)
835 u32 _nr_apqns, *_apqns = NULL;
836 struct keytoken_header *hdr = (struct keytoken_header *)key;
838 if (keylen < sizeof(struct keytoken_header) || flags == 0)
841 if (hdr->type == TOKTYPE_NON_CCA
842 && (hdr->version == TOKVER_EP11_AES_WITH_HEADER
843 || hdr->version == TOKVER_EP11_ECC_WITH_HEADER)
844 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
845 int minhwtype = 0, api = 0;
846 struct ep11keyblob *kb = (struct ep11keyblob *)
847 (key + sizeof(struct ep11kblob_header));
849 if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
851 if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
852 minhwtype = ZCRYPT_CEX7;
855 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
856 minhwtype, api, kb->wkvp);
859 } else if (hdr->type == TOKTYPE_NON_CCA
860 && hdr->version == TOKVER_EP11_AES
861 && is_ep11_keyblob(key)) {
862 int minhwtype = 0, api = 0;
863 struct ep11keyblob *kb = (struct ep11keyblob *) key;
865 if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
867 if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
868 minhwtype = ZCRYPT_CEX7;
871 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
872 minhwtype, api, kb->wkvp);
875 } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
876 int minhwtype = ZCRYPT_CEX3C;
877 u64 cur_mkvp = 0, old_mkvp = 0;
879 if (hdr->version == TOKVER_CCA_AES) {
880 struct secaeskeytoken *t = (struct secaeskeytoken *)key;
882 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
884 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
886 } else if (hdr->version == TOKVER_CCA_VLSC) {
887 struct cipherkeytoken *t = (struct cipherkeytoken *)key;
889 minhwtype = ZCRYPT_CEX6;
890 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
892 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
895 /* unknown cca internal token type */
898 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
899 minhwtype, AES_MK_SET,
900 cur_mkvp, old_mkvp, 1);
903 } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
904 u64 cur_mkvp = 0, old_mkvp = 0;
905 struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
907 if (t->secid == 0x20) {
908 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
910 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
913 /* unknown cca internal 2 token type */
916 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
917 ZCRYPT_CEX7, APKA_MK_SET,
918 cur_mkvp, old_mkvp, 1);
925 if (*nr_apqns < _nr_apqns)
928 memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
930 *nr_apqns = _nr_apqns;
937 static int pkey_apqns4keytype(enum pkey_key_type ktype,
938 u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
939 struct pkey_apqn *apqns, size_t *nr_apqns)
942 u32 _nr_apqns, *_apqns = NULL;
944 if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
945 u64 cur_mkvp = 0, old_mkvp = 0;
946 int minhwtype = ZCRYPT_CEX3C;
948 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
949 cur_mkvp = *((u64 *) cur_mkvp);
950 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
951 old_mkvp = *((u64 *) alt_mkvp);
952 if (ktype == PKEY_TYPE_CCA_CIPHER)
953 minhwtype = ZCRYPT_CEX6;
954 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
955 minhwtype, AES_MK_SET,
956 cur_mkvp, old_mkvp, 1);
959 } else if (ktype == PKEY_TYPE_CCA_ECC) {
960 u64 cur_mkvp = 0, old_mkvp = 0;
962 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
963 cur_mkvp = *((u64 *) cur_mkvp);
964 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
965 old_mkvp = *((u64 *) alt_mkvp);
966 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
967 ZCRYPT_CEX7, APKA_MK_SET,
968 cur_mkvp, old_mkvp, 1);
972 } else if (ktype == PKEY_TYPE_EP11 ||
973 ktype == PKEY_TYPE_EP11_AES ||
974 ktype == PKEY_TYPE_EP11_ECC) {
977 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
979 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
980 ZCRYPT_CEX7, EP11_API_V, wkvp);
988 if (*nr_apqns < _nr_apqns)
991 memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
993 *nr_apqns = _nr_apqns;
1000 static int pkey_keyblob2pkey3(const struct pkey_apqn *apqns, size_t nr_apqns,
1001 const u8 *key, size_t keylen, u32 *protkeytype,
1002 u8 *protkey, u32 *protkeylen)
1004 int i, card, dom, rc;
1005 struct keytoken_header *hdr = (struct keytoken_header *)key;
1007 /* check for at least one apqn given */
1008 if (!apqns || !nr_apqns)
1011 if (keylen < sizeof(struct keytoken_header))
1014 if (hdr->type == TOKTYPE_NON_CCA
1015 && hdr->version == TOKVER_EP11_AES_WITH_HEADER
1016 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1017 /* EP11 AES key blob with header */
1018 if (ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1))
1020 } else if (hdr->type == TOKTYPE_NON_CCA
1021 && hdr->version == TOKVER_EP11_ECC_WITH_HEADER
1022 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1023 /* EP11 ECC key blob with header */
1024 if (ep11_check_ecc_key_with_hdr(debug_info, 3, key, keylen, 1))
1026 } else if (hdr->type == TOKTYPE_NON_CCA
1027 && hdr->version == TOKVER_EP11_AES
1028 && is_ep11_keyblob(key)) {
1029 /* EP11 AES key blob with header in session field */
1030 if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
1032 } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
1033 if (hdr->version == TOKVER_CCA_AES) {
1034 /* CCA AES data key */
1035 if (keylen != sizeof(struct secaeskeytoken))
1037 if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
1039 } else if (hdr->version == TOKVER_CCA_VLSC) {
1040 /* CCA AES cipher key */
1041 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
1043 if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
1046 DEBUG_ERR("%s unknown CCA internal token version %d\n",
1047 __func__, hdr->version);
1050 } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
1051 /* CCA ECC (private) key */
1052 if (keylen < sizeof(struct eccprivkeytoken))
1054 if (cca_check_sececckeytoken(debug_info, 3, key, keylen, 1))
1056 } else if (hdr->type == TOKTYPE_NON_CCA) {
1057 struct pkey_protkey pkey;
1059 rc = pkey_nonccatok2pkey(key, keylen, &pkey);
1062 memcpy(protkey, pkey.protkey, pkey.len);
1063 *protkeylen = pkey.len;
1064 *protkeytype = pkey.type;
1067 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
1068 __func__, hdr->type);
1072 /* simple try all apqns from the list */
1073 for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
1074 card = apqns[i].card;
1075 dom = apqns[i].domain;
1076 if (hdr->type == TOKTYPE_NON_CCA
1077 && (hdr->version == TOKVER_EP11_AES_WITH_HEADER
1078 || hdr->version == TOKVER_EP11_ECC_WITH_HEADER)
1079 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header)))
1080 rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1081 protkey, protkeylen, protkeytype);
1082 else if (hdr->type == TOKTYPE_NON_CCA
1083 && hdr->version == TOKVER_EP11_AES
1084 && is_ep11_keyblob(key))
1085 rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1086 protkey, protkeylen, protkeytype);
1087 else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1088 hdr->version == TOKVER_CCA_AES)
1089 rc = cca_sec2protkey(card, dom, key, protkey,
1090 protkeylen, protkeytype);
1091 else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1092 hdr->version == TOKVER_CCA_VLSC)
1093 rc = cca_cipher2protkey(card, dom, key, protkey,
1094 protkeylen, protkeytype);
1095 else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA)
1096 rc = cca_ecc2protkey(card, dom, key, protkey,
1097 protkeylen, protkeytype);
1109 static void *_copy_key_from_user(void __user *ukey, size_t keylen)
1111 if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
1112 return ERR_PTR(-EINVAL);
1114 return memdup_user(ukey, keylen);
1117 static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
1119 if (!uapqns || nr_apqns == 0)
1122 return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
1125 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1131 case PKEY_GENSECK: {
1132 struct pkey_genseck __user *ugs = (void __user *) arg;
1133 struct pkey_genseck kgs;
1135 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1137 rc = cca_genseckey(kgs.cardnr, kgs.domain,
1138 kgs.keytype, kgs.seckey.seckey);
1139 DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
1142 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1146 case PKEY_CLR2SECK: {
1147 struct pkey_clr2seck __user *ucs = (void __user *) arg;
1148 struct pkey_clr2seck kcs;
1150 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1152 rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1153 kcs.clrkey.clrkey, kcs.seckey.seckey);
1154 DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
1157 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1159 memzero_explicit(&kcs, sizeof(kcs));
1162 case PKEY_SEC2PROTK: {
1163 struct pkey_sec2protk __user *usp = (void __user *) arg;
1164 struct pkey_sec2protk ksp;
1166 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1168 rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
1169 ksp.seckey.seckey, ksp.protkey.protkey,
1170 &ksp.protkey.len, &ksp.protkey.type);
1171 DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
1174 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1178 case PKEY_CLR2PROTK: {
1179 struct pkey_clr2protk __user *ucp = (void __user *) arg;
1180 struct pkey_clr2protk kcp;
1182 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1184 rc = pkey_clr2protkey(kcp.keytype,
1185 &kcp.clrkey, &kcp.protkey);
1186 DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1189 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1191 memzero_explicit(&kcp, sizeof(kcp));
1194 case PKEY_FINDCARD: {
1195 struct pkey_findcard __user *ufc = (void __user *) arg;
1196 struct pkey_findcard kfc;
1198 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1200 rc = cca_findcard(kfc.seckey.seckey,
1201 &kfc.cardnr, &kfc.domain, 1);
1202 DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
1205 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1209 case PKEY_SKEY2PKEY: {
1210 struct pkey_skey2pkey __user *usp = (void __user *) arg;
1211 struct pkey_skey2pkey ksp;
1213 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1215 rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
1216 DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1219 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1223 case PKEY_VERIFYKEY: {
1224 struct pkey_verifykey __user *uvk = (void __user *) arg;
1225 struct pkey_verifykey kvk;
1227 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1229 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1230 &kvk.keysize, &kvk.attributes);
1231 DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1234 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1238 case PKEY_GENPROTK: {
1239 struct pkey_genprotk __user *ugp = (void __user *) arg;
1240 struct pkey_genprotk kgp;
1242 if (copy_from_user(&kgp, ugp, sizeof(kgp)))
1244 rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
1245 DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
1248 if (copy_to_user(ugp, &kgp, sizeof(kgp)))
1252 case PKEY_VERIFYPROTK: {
1253 struct pkey_verifyprotk __user *uvp = (void __user *) arg;
1254 struct pkey_verifyprotk kvp;
1256 if (copy_from_user(&kvp, uvp, sizeof(kvp)))
1258 rc = pkey_verifyprotkey(&kvp.protkey);
1259 DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
1262 case PKEY_KBLOB2PROTK: {
1263 struct pkey_kblob2pkey __user *utp = (void __user *) arg;
1264 struct pkey_kblob2pkey ktp;
1267 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1269 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1271 return PTR_ERR(kkey);
1272 rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
1273 DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
1274 memzero_explicit(kkey, ktp.keylen);
1278 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1282 case PKEY_GENSECK2: {
1283 struct pkey_genseck2 __user *ugs = (void __user *) arg;
1284 struct pkey_genseck2 kgs;
1285 struct pkey_apqn *apqns;
1286 size_t klen = KEYBLOBBUFSIZE;
1289 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1291 apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1293 return PTR_ERR(apqns);
1294 kkey = kmalloc(klen, GFP_KERNEL);
1299 rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1300 kgs.type, kgs.size, kgs.keygenflags,
1302 DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1309 if (kgs.keylen < klen) {
1313 if (copy_to_user(kgs.key, kkey, klen)) {
1319 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1324 case PKEY_CLR2SECK2: {
1325 struct pkey_clr2seck2 __user *ucs = (void __user *) arg;
1326 struct pkey_clr2seck2 kcs;
1327 struct pkey_apqn *apqns;
1328 size_t klen = KEYBLOBBUFSIZE;
1331 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1333 apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1335 return PTR_ERR(apqns);
1336 kkey = kmalloc(klen, GFP_KERNEL);
1341 rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1342 kcs.type, kcs.size, kcs.keygenflags,
1343 kcs.clrkey.clrkey, kkey, &klen);
1344 DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1351 if (kcs.keylen < klen) {
1355 if (copy_to_user(kcs.key, kkey, klen)) {
1361 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1363 memzero_explicit(&kcs, sizeof(kcs));
1367 case PKEY_VERIFYKEY2: {
1368 struct pkey_verifykey2 __user *uvk = (void __user *) arg;
1369 struct pkey_verifykey2 kvk;
1372 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1374 kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1376 return PTR_ERR(kkey);
1377 rc = pkey_verifykey2(kkey, kvk.keylen,
1378 &kvk.cardnr, &kvk.domain,
1379 &kvk.type, &kvk.size, &kvk.flags);
1380 DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1384 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1388 case PKEY_KBLOB2PROTK2: {
1389 struct pkey_kblob2pkey2 __user *utp = (void __user *) arg;
1390 struct pkey_kblob2pkey2 ktp;
1391 struct pkey_apqn *apqns = NULL;
1394 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1396 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1398 return PTR_ERR(apqns);
1399 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1402 return PTR_ERR(kkey);
1404 rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1405 kkey, ktp.keylen, &ktp.protkey);
1406 DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1408 memzero_explicit(kkey, ktp.keylen);
1412 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1416 case PKEY_APQNS4K: {
1417 struct pkey_apqns4key __user *uak = (void __user *) arg;
1418 struct pkey_apqns4key kak;
1419 struct pkey_apqn *apqns = NULL;
1420 size_t nr_apqns, len;
1423 if (copy_from_user(&kak, uak, sizeof(kak)))
1425 nr_apqns = kak.apqn_entries;
1427 apqns = kmalloc_array(nr_apqns,
1428 sizeof(struct pkey_apqn),
1433 kkey = _copy_key_from_user(kak.key, kak.keylen);
1436 return PTR_ERR(kkey);
1438 rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1440 DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1442 if (rc && rc != -ENOSPC) {
1446 if (!rc && kak.apqns) {
1447 if (nr_apqns > kak.apqn_entries) {
1451 len = nr_apqns * sizeof(struct pkey_apqn);
1453 if (copy_to_user(kak.apqns, apqns, len)) {
1459 kak.apqn_entries = nr_apqns;
1460 if (copy_to_user(uak, &kak, sizeof(kak)))
1465 case PKEY_APQNS4KT: {
1466 struct pkey_apqns4keytype __user *uat = (void __user *) arg;
1467 struct pkey_apqns4keytype kat;
1468 struct pkey_apqn *apqns = NULL;
1469 size_t nr_apqns, len;
1471 if (copy_from_user(&kat, uat, sizeof(kat)))
1473 nr_apqns = kat.apqn_entries;
1475 apqns = kmalloc_array(nr_apqns,
1476 sizeof(struct pkey_apqn),
1481 rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1482 kat.flags, apqns, &nr_apqns);
1483 DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1484 if (rc && rc != -ENOSPC) {
1488 if (!rc && kat.apqns) {
1489 if (nr_apqns > kat.apqn_entries) {
1493 len = nr_apqns * sizeof(struct pkey_apqn);
1495 if (copy_to_user(kat.apqns, apqns, len)) {
1501 kat.apqn_entries = nr_apqns;
1502 if (copy_to_user(uat, &kat, sizeof(kat)))
1507 case PKEY_KBLOB2PROTK3: {
1508 struct pkey_kblob2pkey3 __user *utp = (void __user *) arg;
1509 struct pkey_kblob2pkey3 ktp;
1510 struct pkey_apqn *apqns = NULL;
1511 u32 protkeylen = PROTKEYBLOBBUFSIZE;
1514 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1516 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1518 return PTR_ERR(apqns);
1519 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1522 return PTR_ERR(kkey);
1524 protkey = kmalloc(protkeylen, GFP_KERNEL);
1530 rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
1531 ktp.keylen, &ktp.pkeytype,
1532 protkey, &protkeylen);
1533 DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
1535 memzero_explicit(kkey, ktp.keylen);
1541 if (ktp.pkey && ktp.pkeylen) {
1542 if (protkeylen > ktp.pkeylen) {
1546 if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
1552 ktp.pkeylen = protkeylen;
1553 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1558 /* unknown/unsupported ioctl cmd */
1566 * Sysfs and file io operations
1570 * Sysfs attribute read function for all protected key binary attributes.
1571 * The implementation can not deal with partial reads, because a new random
1572 * protected key blob is generated with each read. In case of partial reads
1573 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1575 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1576 loff_t off, size_t count)
1578 struct protaeskeytoken protkeytoken;
1579 struct pkey_protkey protkey;
1582 if (off != 0 || count < sizeof(protkeytoken))
1585 if (count < 2 * sizeof(protkeytoken))
1588 memset(&protkeytoken, 0, sizeof(protkeytoken));
1589 protkeytoken.type = TOKTYPE_NON_CCA;
1590 protkeytoken.version = TOKVER_PROTECTED_KEY;
1591 protkeytoken.keytype = keytype;
1593 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1597 protkeytoken.len = protkey.len;
1598 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1600 memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1603 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1607 protkeytoken.len = protkey.len;
1608 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1610 memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1611 sizeof(protkeytoken));
1613 return 2 * sizeof(protkeytoken);
1616 return sizeof(protkeytoken);
1619 static ssize_t protkey_aes_128_read(struct file *filp,
1620 struct kobject *kobj,
1621 struct bin_attribute *attr,
1622 char *buf, loff_t off,
1625 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1629 static ssize_t protkey_aes_192_read(struct file *filp,
1630 struct kobject *kobj,
1631 struct bin_attribute *attr,
1632 char *buf, loff_t off,
1635 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1639 static ssize_t protkey_aes_256_read(struct file *filp,
1640 struct kobject *kobj,
1641 struct bin_attribute *attr,
1642 char *buf, loff_t off,
1645 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1649 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1650 struct kobject *kobj,
1651 struct bin_attribute *attr,
1652 char *buf, loff_t off,
1655 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1659 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1660 struct kobject *kobj,
1661 struct bin_attribute *attr,
1662 char *buf, loff_t off,
1665 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1669 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1670 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1671 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1672 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1673 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1675 static struct bin_attribute *protkey_attrs[] = {
1676 &bin_attr_protkey_aes_128,
1677 &bin_attr_protkey_aes_192,
1678 &bin_attr_protkey_aes_256,
1679 &bin_attr_protkey_aes_128_xts,
1680 &bin_attr_protkey_aes_256_xts,
1684 static struct attribute_group protkey_attr_group = {
1686 .bin_attrs = protkey_attrs,
1690 * Sysfs attribute read function for all secure key ccadata binary attributes.
1691 * The implementation can not deal with partial reads, because a new random
1692 * protected key blob is generated with each read. In case of partial reads
1693 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1695 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1696 loff_t off, size_t count)
1699 struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
1701 if (off != 0 || count < sizeof(struct secaeskeytoken))
1704 if (count < 2 * sizeof(struct secaeskeytoken))
1707 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1713 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1717 return 2 * sizeof(struct secaeskeytoken);
1720 return sizeof(struct secaeskeytoken);
1723 static ssize_t ccadata_aes_128_read(struct file *filp,
1724 struct kobject *kobj,
1725 struct bin_attribute *attr,
1726 char *buf, loff_t off,
1729 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1733 static ssize_t ccadata_aes_192_read(struct file *filp,
1734 struct kobject *kobj,
1735 struct bin_attribute *attr,
1736 char *buf, loff_t off,
1739 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1743 static ssize_t ccadata_aes_256_read(struct file *filp,
1744 struct kobject *kobj,
1745 struct bin_attribute *attr,
1746 char *buf, loff_t off,
1749 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1753 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1754 struct kobject *kobj,
1755 struct bin_attribute *attr,
1756 char *buf, loff_t off,
1759 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1763 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1764 struct kobject *kobj,
1765 struct bin_attribute *attr,
1766 char *buf, loff_t off,
1769 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1773 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1774 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1775 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1776 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1777 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1779 static struct bin_attribute *ccadata_attrs[] = {
1780 &bin_attr_ccadata_aes_128,
1781 &bin_attr_ccadata_aes_192,
1782 &bin_attr_ccadata_aes_256,
1783 &bin_attr_ccadata_aes_128_xts,
1784 &bin_attr_ccadata_aes_256_xts,
1788 static struct attribute_group ccadata_attr_group = {
1790 .bin_attrs = ccadata_attrs,
1793 #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80)
1796 * Sysfs attribute read function for all secure key ccacipher binary attributes.
1797 * The implementation can not deal with partial reads, because a new random
1798 * secure key blob is generated with each read. In case of partial reads
1799 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1801 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1802 bool is_xts, char *buf, loff_t off,
1805 int i, rc, card, dom;
1806 u32 nr_apqns, *apqns = NULL;
1807 size_t keysize = CCACIPHERTOKENSIZE;
1809 if (off != 0 || count < CCACIPHERTOKENSIZE)
1812 if (count < 2 * CCACIPHERTOKENSIZE)
1815 /* build a list of apqns able to generate an cipher key */
1816 rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1817 ZCRYPT_CEX6, 0, 0, 0, 0);
1821 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1823 /* simple try all apqns from the list */
1824 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1825 card = apqns[i] >> 16;
1826 dom = apqns[i] & 0xFFFF;
1827 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1835 keysize = CCACIPHERTOKENSIZE;
1836 buf += CCACIPHERTOKENSIZE;
1837 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1839 return 2 * CCACIPHERTOKENSIZE;
1842 return CCACIPHERTOKENSIZE;
1845 static ssize_t ccacipher_aes_128_read(struct file *filp,
1846 struct kobject *kobj,
1847 struct bin_attribute *attr,
1848 char *buf, loff_t off,
1851 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1855 static ssize_t ccacipher_aes_192_read(struct file *filp,
1856 struct kobject *kobj,
1857 struct bin_attribute *attr,
1858 char *buf, loff_t off,
1861 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1865 static ssize_t ccacipher_aes_256_read(struct file *filp,
1866 struct kobject *kobj,
1867 struct bin_attribute *attr,
1868 char *buf, loff_t off,
1871 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1875 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1876 struct kobject *kobj,
1877 struct bin_attribute *attr,
1878 char *buf, loff_t off,
1881 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1885 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1886 struct kobject *kobj,
1887 struct bin_attribute *attr,
1888 char *buf, loff_t off,
1891 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1895 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1896 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1897 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1898 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1899 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1901 static struct bin_attribute *ccacipher_attrs[] = {
1902 &bin_attr_ccacipher_aes_128,
1903 &bin_attr_ccacipher_aes_192,
1904 &bin_attr_ccacipher_aes_256,
1905 &bin_attr_ccacipher_aes_128_xts,
1906 &bin_attr_ccacipher_aes_256_xts,
1910 static struct attribute_group ccacipher_attr_group = {
1911 .name = "ccacipher",
1912 .bin_attrs = ccacipher_attrs,
1916 * Sysfs attribute read function for all ep11 aes key binary attributes.
1917 * The implementation can not deal with partial reads, because a new random
1918 * secure key blob is generated with each read. In case of partial reads
1919 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1920 * This function and the sysfs attributes using it provide EP11 key blobs
1921 * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1924 static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1925 bool is_xts, char *buf, loff_t off,
1928 int i, rc, card, dom;
1929 u32 nr_apqns, *apqns = NULL;
1930 size_t keysize = MAXEP11AESKEYBLOBSIZE;
1932 if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1935 if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1938 /* build a list of apqns able to generate an cipher key */
1939 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1940 ZCRYPT_CEX7, EP11_API_V, NULL);
1944 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1946 /* simple try all apqns from the list */
1947 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1948 card = apqns[i] >> 16;
1949 dom = apqns[i] & 0xFFFF;
1950 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1958 keysize = MAXEP11AESKEYBLOBSIZE;
1959 buf += MAXEP11AESKEYBLOBSIZE;
1960 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1962 return 2 * MAXEP11AESKEYBLOBSIZE;
1965 return MAXEP11AESKEYBLOBSIZE;
1968 static ssize_t ep11_aes_128_read(struct file *filp,
1969 struct kobject *kobj,
1970 struct bin_attribute *attr,
1971 char *buf, loff_t off,
1974 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1978 static ssize_t ep11_aes_192_read(struct file *filp,
1979 struct kobject *kobj,
1980 struct bin_attribute *attr,
1981 char *buf, loff_t off,
1984 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1988 static ssize_t ep11_aes_256_read(struct file *filp,
1989 struct kobject *kobj,
1990 struct bin_attribute *attr,
1991 char *buf, loff_t off,
1994 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1998 static ssize_t ep11_aes_128_xts_read(struct file *filp,
1999 struct kobject *kobj,
2000 struct bin_attribute *attr,
2001 char *buf, loff_t off,
2004 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
2008 static ssize_t ep11_aes_256_xts_read(struct file *filp,
2009 struct kobject *kobj,
2010 struct bin_attribute *attr,
2011 char *buf, loff_t off,
2014 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
2018 static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2019 static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2020 static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2021 static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2022 static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2024 static struct bin_attribute *ep11_attrs[] = {
2025 &bin_attr_ep11_aes_128,
2026 &bin_attr_ep11_aes_192,
2027 &bin_attr_ep11_aes_256,
2028 &bin_attr_ep11_aes_128_xts,
2029 &bin_attr_ep11_aes_256_xts,
2033 static struct attribute_group ep11_attr_group = {
2035 .bin_attrs = ep11_attrs,
2038 static const struct attribute_group *pkey_attr_groups[] = {
2039 &protkey_attr_group,
2040 &ccadata_attr_group,
2041 &ccacipher_attr_group,
2046 static const struct file_operations pkey_fops = {
2047 .owner = THIS_MODULE,
2048 .open = nonseekable_open,
2049 .llseek = no_llseek,
2050 .unlocked_ioctl = pkey_unlocked_ioctl,
2053 static struct miscdevice pkey_dev = {
2055 .minor = MISC_DYNAMIC_MINOR,
2058 .groups = pkey_attr_groups,
2064 static int __init pkey_init(void)
2066 cpacf_mask_t func_mask;
2069 * The pckmo instruction should be available - even if we don't
2070 * actually invoke it. This instruction comes with MSA 3 which
2071 * is also the minimum level for the kmc instructions which
2072 * are able to work with protected keys.
2074 if (!cpacf_query(CPACF_PCKMO, &func_mask))
2077 /* check for kmc instructions available */
2078 if (!cpacf_query(CPACF_KMC, &func_mask))
2080 if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) ||
2081 !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) ||
2082 !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256))
2087 return misc_register(&pkey_dev);
2093 static void __exit pkey_exit(void)
2095 misc_deregister(&pkey_dev);
2099 module_cpu_feature_match(MSA, pkey_init);
2100 module_exit(pkey_exit);