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.keybitlen;
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);
1277 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1281 case PKEY_GENSECK2: {
1282 struct pkey_genseck2 __user *ugs = (void __user *) arg;
1283 struct pkey_genseck2 kgs;
1284 struct pkey_apqn *apqns;
1285 size_t klen = KEYBLOBBUFSIZE;
1288 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1290 apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1292 return PTR_ERR(apqns);
1293 kkey = kmalloc(klen, GFP_KERNEL);
1298 rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1299 kgs.type, kgs.size, kgs.keygenflags,
1301 DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1308 if (kgs.keylen < klen) {
1312 if (copy_to_user(kgs.key, kkey, klen)) {
1318 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1323 case PKEY_CLR2SECK2: {
1324 struct pkey_clr2seck2 __user *ucs = (void __user *) arg;
1325 struct pkey_clr2seck2 kcs;
1326 struct pkey_apqn *apqns;
1327 size_t klen = KEYBLOBBUFSIZE;
1330 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1332 apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1334 return PTR_ERR(apqns);
1335 kkey = kmalloc(klen, GFP_KERNEL);
1340 rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1341 kcs.type, kcs.size, kcs.keygenflags,
1342 kcs.clrkey.clrkey, kkey, &klen);
1343 DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1350 if (kcs.keylen < klen) {
1354 if (copy_to_user(kcs.key, kkey, klen)) {
1360 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1362 memzero_explicit(&kcs, sizeof(kcs));
1366 case PKEY_VERIFYKEY2: {
1367 struct pkey_verifykey2 __user *uvk = (void __user *) arg;
1368 struct pkey_verifykey2 kvk;
1371 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1373 kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1375 return PTR_ERR(kkey);
1376 rc = pkey_verifykey2(kkey, kvk.keylen,
1377 &kvk.cardnr, &kvk.domain,
1378 &kvk.type, &kvk.size, &kvk.flags);
1379 DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1383 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1387 case PKEY_KBLOB2PROTK2: {
1388 struct pkey_kblob2pkey2 __user *utp = (void __user *) arg;
1389 struct pkey_kblob2pkey2 ktp;
1390 struct pkey_apqn *apqns = NULL;
1393 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1395 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1397 return PTR_ERR(apqns);
1398 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1401 return PTR_ERR(kkey);
1403 rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1404 kkey, ktp.keylen, &ktp.protkey);
1405 DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1410 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1414 case PKEY_APQNS4K: {
1415 struct pkey_apqns4key __user *uak = (void __user *) arg;
1416 struct pkey_apqns4key kak;
1417 struct pkey_apqn *apqns = NULL;
1418 size_t nr_apqns, len;
1421 if (copy_from_user(&kak, uak, sizeof(kak)))
1423 nr_apqns = kak.apqn_entries;
1425 apqns = kmalloc_array(nr_apqns,
1426 sizeof(struct pkey_apqn),
1431 kkey = _copy_key_from_user(kak.key, kak.keylen);
1434 return PTR_ERR(kkey);
1436 rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1438 DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1440 if (rc && rc != -ENOSPC) {
1444 if (!rc && kak.apqns) {
1445 if (nr_apqns > kak.apqn_entries) {
1449 len = nr_apqns * sizeof(struct pkey_apqn);
1451 if (copy_to_user(kak.apqns, apqns, len)) {
1457 kak.apqn_entries = nr_apqns;
1458 if (copy_to_user(uak, &kak, sizeof(kak)))
1463 case PKEY_APQNS4KT: {
1464 struct pkey_apqns4keytype __user *uat = (void __user *) arg;
1465 struct pkey_apqns4keytype kat;
1466 struct pkey_apqn *apqns = NULL;
1467 size_t nr_apqns, len;
1469 if (copy_from_user(&kat, uat, sizeof(kat)))
1471 nr_apqns = kat.apqn_entries;
1473 apqns = kmalloc_array(nr_apqns,
1474 sizeof(struct pkey_apqn),
1479 rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1480 kat.flags, apqns, &nr_apqns);
1481 DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1482 if (rc && rc != -ENOSPC) {
1486 if (!rc && kat.apqns) {
1487 if (nr_apqns > kat.apqn_entries) {
1491 len = nr_apqns * sizeof(struct pkey_apqn);
1493 if (copy_to_user(kat.apqns, apqns, len)) {
1499 kat.apqn_entries = nr_apqns;
1500 if (copy_to_user(uat, &kat, sizeof(kat)))
1505 case PKEY_KBLOB2PROTK3: {
1506 struct pkey_kblob2pkey3 __user *utp = (void __user *) arg;
1507 struct pkey_kblob2pkey3 ktp;
1508 struct pkey_apqn *apqns = NULL;
1509 u32 protkeylen = PROTKEYBLOBBUFSIZE;
1512 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1514 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1516 return PTR_ERR(apqns);
1517 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1520 return PTR_ERR(kkey);
1522 protkey = kmalloc(protkeylen, GFP_KERNEL);
1528 rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
1529 ktp.keylen, &ktp.pkeytype,
1530 protkey, &protkeylen);
1531 DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
1538 if (ktp.pkey && ktp.pkeylen) {
1539 if (protkeylen > ktp.pkeylen) {
1543 if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
1549 ktp.pkeylen = protkeylen;
1550 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1555 /* unknown/unsupported ioctl cmd */
1563 * Sysfs and file io operations
1567 * Sysfs attribute read function for all protected key binary attributes.
1568 * The implementation can not deal with partial reads, because a new random
1569 * protected key blob is generated with each read. In case of partial reads
1570 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1572 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1573 loff_t off, size_t count)
1575 struct protaeskeytoken protkeytoken;
1576 struct pkey_protkey protkey;
1579 if (off != 0 || count < sizeof(protkeytoken))
1582 if (count < 2 * sizeof(protkeytoken))
1585 memset(&protkeytoken, 0, sizeof(protkeytoken));
1586 protkeytoken.type = TOKTYPE_NON_CCA;
1587 protkeytoken.version = TOKVER_PROTECTED_KEY;
1588 protkeytoken.keytype = keytype;
1590 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1594 protkeytoken.len = protkey.len;
1595 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1597 memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1600 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1604 protkeytoken.len = protkey.len;
1605 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1607 memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1608 sizeof(protkeytoken));
1610 return 2 * sizeof(protkeytoken);
1613 return sizeof(protkeytoken);
1616 static ssize_t protkey_aes_128_read(struct file *filp,
1617 struct kobject *kobj,
1618 struct bin_attribute *attr,
1619 char *buf, loff_t off,
1622 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1626 static ssize_t protkey_aes_192_read(struct file *filp,
1627 struct kobject *kobj,
1628 struct bin_attribute *attr,
1629 char *buf, loff_t off,
1632 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1636 static ssize_t protkey_aes_256_read(struct file *filp,
1637 struct kobject *kobj,
1638 struct bin_attribute *attr,
1639 char *buf, loff_t off,
1642 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1646 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1647 struct kobject *kobj,
1648 struct bin_attribute *attr,
1649 char *buf, loff_t off,
1652 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1656 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1657 struct kobject *kobj,
1658 struct bin_attribute *attr,
1659 char *buf, loff_t off,
1662 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1666 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1667 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1668 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1669 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1670 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1672 static struct bin_attribute *protkey_attrs[] = {
1673 &bin_attr_protkey_aes_128,
1674 &bin_attr_protkey_aes_192,
1675 &bin_attr_protkey_aes_256,
1676 &bin_attr_protkey_aes_128_xts,
1677 &bin_attr_protkey_aes_256_xts,
1681 static struct attribute_group protkey_attr_group = {
1683 .bin_attrs = protkey_attrs,
1687 * Sysfs attribute read function for all secure key ccadata binary attributes.
1688 * The implementation can not deal with partial reads, because a new random
1689 * protected key blob is generated with each read. In case of partial reads
1690 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1692 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1693 loff_t off, size_t count)
1696 struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
1698 if (off != 0 || count < sizeof(struct secaeskeytoken))
1701 if (count < 2 * sizeof(struct secaeskeytoken))
1704 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1710 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1714 return 2 * sizeof(struct secaeskeytoken);
1717 return sizeof(struct secaeskeytoken);
1720 static ssize_t ccadata_aes_128_read(struct file *filp,
1721 struct kobject *kobj,
1722 struct bin_attribute *attr,
1723 char *buf, loff_t off,
1726 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1730 static ssize_t ccadata_aes_192_read(struct file *filp,
1731 struct kobject *kobj,
1732 struct bin_attribute *attr,
1733 char *buf, loff_t off,
1736 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1740 static ssize_t ccadata_aes_256_read(struct file *filp,
1741 struct kobject *kobj,
1742 struct bin_attribute *attr,
1743 char *buf, loff_t off,
1746 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1750 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1751 struct kobject *kobj,
1752 struct bin_attribute *attr,
1753 char *buf, loff_t off,
1756 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1760 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1761 struct kobject *kobj,
1762 struct bin_attribute *attr,
1763 char *buf, loff_t off,
1766 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1770 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1771 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1772 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1773 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1774 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1776 static struct bin_attribute *ccadata_attrs[] = {
1777 &bin_attr_ccadata_aes_128,
1778 &bin_attr_ccadata_aes_192,
1779 &bin_attr_ccadata_aes_256,
1780 &bin_attr_ccadata_aes_128_xts,
1781 &bin_attr_ccadata_aes_256_xts,
1785 static struct attribute_group ccadata_attr_group = {
1787 .bin_attrs = ccadata_attrs,
1790 #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80)
1793 * Sysfs attribute read function for all secure key ccacipher binary attributes.
1794 * The implementation can not deal with partial reads, because a new random
1795 * secure key blob is generated with each read. In case of partial reads
1796 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1798 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1799 bool is_xts, char *buf, loff_t off,
1802 int i, rc, card, dom;
1803 u32 nr_apqns, *apqns = NULL;
1804 size_t keysize = CCACIPHERTOKENSIZE;
1806 if (off != 0 || count < CCACIPHERTOKENSIZE)
1809 if (count < 2 * CCACIPHERTOKENSIZE)
1812 /* build a list of apqns able to generate an cipher key */
1813 rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1814 ZCRYPT_CEX6, 0, 0, 0, 0);
1818 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1820 /* simple try all apqns from the list */
1821 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1822 card = apqns[i] >> 16;
1823 dom = apqns[i] & 0xFFFF;
1824 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1832 keysize = CCACIPHERTOKENSIZE;
1833 buf += CCACIPHERTOKENSIZE;
1834 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1836 return 2 * CCACIPHERTOKENSIZE;
1839 return CCACIPHERTOKENSIZE;
1842 static ssize_t ccacipher_aes_128_read(struct file *filp,
1843 struct kobject *kobj,
1844 struct bin_attribute *attr,
1845 char *buf, loff_t off,
1848 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1852 static ssize_t ccacipher_aes_192_read(struct file *filp,
1853 struct kobject *kobj,
1854 struct bin_attribute *attr,
1855 char *buf, loff_t off,
1858 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1862 static ssize_t ccacipher_aes_256_read(struct file *filp,
1863 struct kobject *kobj,
1864 struct bin_attribute *attr,
1865 char *buf, loff_t off,
1868 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1872 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1873 struct kobject *kobj,
1874 struct bin_attribute *attr,
1875 char *buf, loff_t off,
1878 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1882 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1883 struct kobject *kobj,
1884 struct bin_attribute *attr,
1885 char *buf, loff_t off,
1888 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1892 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1893 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1894 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1895 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1896 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1898 static struct bin_attribute *ccacipher_attrs[] = {
1899 &bin_attr_ccacipher_aes_128,
1900 &bin_attr_ccacipher_aes_192,
1901 &bin_attr_ccacipher_aes_256,
1902 &bin_attr_ccacipher_aes_128_xts,
1903 &bin_attr_ccacipher_aes_256_xts,
1907 static struct attribute_group ccacipher_attr_group = {
1908 .name = "ccacipher",
1909 .bin_attrs = ccacipher_attrs,
1913 * Sysfs attribute read function for all ep11 aes key binary attributes.
1914 * The implementation can not deal with partial reads, because a new random
1915 * secure key blob is generated with each read. In case of partial reads
1916 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1917 * This function and the sysfs attributes using it provide EP11 key blobs
1918 * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1921 static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1922 bool is_xts, char *buf, loff_t off,
1925 int i, rc, card, dom;
1926 u32 nr_apqns, *apqns = NULL;
1927 size_t keysize = MAXEP11AESKEYBLOBSIZE;
1929 if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1932 if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1935 /* build a list of apqns able to generate an cipher key */
1936 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1937 ZCRYPT_CEX7, EP11_API_V, NULL);
1941 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1943 /* simple try all apqns from the list */
1944 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1945 card = apqns[i] >> 16;
1946 dom = apqns[i] & 0xFFFF;
1947 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1955 keysize = MAXEP11AESKEYBLOBSIZE;
1956 buf += MAXEP11AESKEYBLOBSIZE;
1957 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1959 return 2 * MAXEP11AESKEYBLOBSIZE;
1962 return MAXEP11AESKEYBLOBSIZE;
1965 static ssize_t ep11_aes_128_read(struct file *filp,
1966 struct kobject *kobj,
1967 struct bin_attribute *attr,
1968 char *buf, loff_t off,
1971 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1975 static ssize_t ep11_aes_192_read(struct file *filp,
1976 struct kobject *kobj,
1977 struct bin_attribute *attr,
1978 char *buf, loff_t off,
1981 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1985 static ssize_t ep11_aes_256_read(struct file *filp,
1986 struct kobject *kobj,
1987 struct bin_attribute *attr,
1988 char *buf, loff_t off,
1991 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1995 static ssize_t ep11_aes_128_xts_read(struct file *filp,
1996 struct kobject *kobj,
1997 struct bin_attribute *attr,
1998 char *buf, loff_t off,
2001 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
2005 static ssize_t ep11_aes_256_xts_read(struct file *filp,
2006 struct kobject *kobj,
2007 struct bin_attribute *attr,
2008 char *buf, loff_t off,
2011 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
2015 static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2016 static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2017 static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2018 static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2019 static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2021 static struct bin_attribute *ep11_attrs[] = {
2022 &bin_attr_ep11_aes_128,
2023 &bin_attr_ep11_aes_192,
2024 &bin_attr_ep11_aes_256,
2025 &bin_attr_ep11_aes_128_xts,
2026 &bin_attr_ep11_aes_256_xts,
2030 static struct attribute_group ep11_attr_group = {
2032 .bin_attrs = ep11_attrs,
2035 static const struct attribute_group *pkey_attr_groups[] = {
2036 &protkey_attr_group,
2037 &ccadata_attr_group,
2038 &ccacipher_attr_group,
2043 static const struct file_operations pkey_fops = {
2044 .owner = THIS_MODULE,
2045 .open = nonseekable_open,
2046 .llseek = no_llseek,
2047 .unlocked_ioctl = pkey_unlocked_ioctl,
2050 static struct miscdevice pkey_dev = {
2052 .minor = MISC_DYNAMIC_MINOR,
2055 .groups = pkey_attr_groups,
2061 static int __init pkey_init(void)
2063 cpacf_mask_t func_mask;
2066 * The pckmo instruction should be available - even if we don't
2067 * actually invoke it. This instruction comes with MSA 3 which
2068 * is also the minimum level for the kmc instructions which
2069 * are able to work with protected keys.
2071 if (!cpacf_query(CPACF_PCKMO, &func_mask))
2074 /* check for kmc instructions available */
2075 if (!cpacf_query(CPACF_KMC, &func_mask))
2077 if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) ||
2078 !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) ||
2079 !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256))
2084 return misc_register(&pkey_dev);
2090 static void __exit pkey_exit(void)
2092 misc_deregister(&pkey_dev);
2096 module_cpu_feature_match(MSA, pkey_init);
2097 module_exit(pkey_exit);