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 */
38 /* mask of available pckmo subfunctions, fetched once at module init */
39 static cpacf_mask_t pckmo_functions;
42 * debug feature data and functions
45 static debug_info_t *debug_info;
47 #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
48 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
49 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
50 #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
52 static void __init pkey_debug_init(void)
54 /* 5 arguments per dbf entry (including the format string ptr) */
55 debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
56 debug_register_view(debug_info, &debug_sprintf_view);
57 debug_set_level(debug_info, 3);
60 static void __exit pkey_debug_exit(void)
62 debug_unregister(debug_info);
65 /* inside view of a protected key token (only type 0x00 version 0x01) */
66 struct protaeskeytoken {
67 u8 type; /* 0x00 for PAES specific key tokens */
69 u8 version; /* should be 0x01 for protected AES key token */
71 u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
72 u32 len; /* bytes actually stored in protkey[] */
73 u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */
76 /* inside view of a clear key token (type 0x00 version 0x02) */
77 struct clearaeskeytoken {
78 u8 type; /* 0x00 for PAES specific key tokens */
80 u8 version; /* 0x02 for clear AES key token */
82 u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
83 u32 len; /* bytes actually stored in clearkey[] */
84 u8 clearkey[]; /* clear key value */
88 * Create a protected key from a clear key value.
90 static int pkey_clr2protkey(u32 keytype,
91 const struct pkey_clrkey *clrkey,
92 struct pkey_protkey *protkey)
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",
118 * Check if the needed pckmo subfunction is available.
119 * These subfunctions can be enabled/disabled by customers
120 * in the LPAR profile or may even change on the fly.
122 if (!cpacf_test_func(&pckmo_functions, fc)) {
123 DEBUG_ERR("%s pckmo functions not available\n", __func__);
127 /* prepare param block */
128 memset(paramblock, 0, sizeof(paramblock));
129 memcpy(paramblock, clrkey->clrkey, keysize);
131 /* call the pckmo instruction */
132 cpacf_pckmo(fc, paramblock);
134 /* copy created protected key */
135 protkey->type = keytype;
136 protkey->len = keysize + 32;
137 memcpy(protkey->protkey, paramblock, keysize + 32);
143 * Find card and transform secure key into protected key.
145 static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
149 struct keytoken_header *hdr = (struct keytoken_header *)key;
152 * The cca_xxx2protkey call may fail when a card has been
153 * addressed where the master key was changed after last fetch
154 * of the mkvp into the cache. Try 3 times: First witout verify
155 * then with verify and last round with verify and old master
156 * key verification pattern match not ignored.
158 for (verify = 0; verify < 3; verify++) {
159 rc = cca_findcard(key, &cardnr, &domain, verify);
162 if (rc > 0 && verify < 2)
164 switch (hdr->version) {
166 rc = cca_sec2protkey(cardnr, domain,
168 &pkey->len, &pkey->type);
170 case TOKVER_CCA_VLSC:
171 rc = cca_cipher2protkey(cardnr, domain,
173 &pkey->len, &pkey->type);
183 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
189 * Construct EP11 key with given clear key value.
191 static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
192 u8 *keybuf, size_t *keybuflen)
196 u32 nr_apqns, *apqns = NULL;
198 /* build a list of apqns suitable for ep11 keys with cpacf support */
199 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
200 ZCRYPT_CEX7, EP11_API_V, NULL);
204 /* go through the list of apqns and try to bild an ep11 key */
205 for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
206 card = apqns[i] >> 16;
207 dom = apqns[i] & 0xFFFF;
208 rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
209 0, clrkey, keybuf, keybuflen);
217 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
222 * Find card and transform EP11 secure key into protected key.
224 static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
228 u32 nr_apqns, *apqns = NULL;
229 struct ep11keyblob *kb = (struct ep11keyblob *) key;
231 /* build a list of apqns suitable for this key */
232 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
233 ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
237 /* go through the list of apqns and try to derive an pkey */
238 for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
239 card = apqns[i] >> 16;
240 dom = apqns[i] & 0xFFFF;
241 pkey->len = sizeof(pkey->protkey);
242 rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
243 pkey->protkey, &pkey->len, &pkey->type);
251 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
256 * Verify key and give back some info about the key.
258 static int pkey_verifykey(const struct pkey_seckey *seckey,
259 u16 *pcardnr, u16 *pdomain,
260 u16 *pkeysize, u32 *pattributes)
262 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
266 /* check the secure key for valid AES secure key */
267 rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
271 *pattributes = PKEY_VERIFY_ATTR_AES;
273 *pkeysize = t->bitsize;
275 /* try to find a card which can handle this key */
276 rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
281 /* key mkvp matches to old master key mkvp */
282 DEBUG_DBG("%s secure key has old mkvp\n", __func__);
284 *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
294 DEBUG_DBG("%s rc=%d\n", __func__, rc);
299 * Generate a random protected key
301 static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
303 struct pkey_clrkey clrkey;
308 case PKEY_KEYTYPE_AES_128:
311 case PKEY_KEYTYPE_AES_192:
314 case PKEY_KEYTYPE_AES_256:
318 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
323 /* generate a dummy random clear key */
324 get_random_bytes(clrkey.clrkey, keysize);
326 /* convert it to a dummy protected key */
327 rc = pkey_clr2protkey(keytype, &clrkey, protkey);
331 /* replace the key part of the protected key with random bytes */
332 get_random_bytes(protkey->protkey, keysize);
338 * Verify if a protected key is still valid
340 static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
344 u8 iv[AES_BLOCK_SIZE];
345 u8 key[MAXPROTKEYSIZE];
347 u8 null_msg[AES_BLOCK_SIZE];
348 u8 dest_buf[AES_BLOCK_SIZE];
351 switch (protkey->type) {
352 case PKEY_KEYTYPE_AES_128:
353 fc = CPACF_KMC_PAES_128;
355 case PKEY_KEYTYPE_AES_192:
356 fc = CPACF_KMC_PAES_192;
358 case PKEY_KEYTYPE_AES_256:
359 fc = CPACF_KMC_PAES_256;
362 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
367 memset(null_msg, 0, sizeof(null_msg));
369 memset(param.iv, 0, sizeof(param.iv));
370 memcpy(param.key, protkey->protkey, sizeof(param.key));
372 k = cpacf_kmc(fc | CPACF_ENCRYPT, ¶m, null_msg, dest_buf,
374 if (k != sizeof(null_msg)) {
375 DEBUG_ERR("%s protected key is not valid\n", __func__);
376 return -EKEYREJECTED;
383 * Transform a non-CCA key token into a protected key
385 static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
386 struct pkey_protkey *protkey)
390 struct keytoken_header *hdr = (struct keytoken_header *)key;
392 switch (hdr->version) {
393 case TOKVER_PROTECTED_KEY: {
394 struct protaeskeytoken *t;
396 if (keylen != sizeof(struct protaeskeytoken))
398 t = (struct protaeskeytoken *)key;
399 protkey->len = t->len;
400 protkey->type = t->keytype;
401 memcpy(protkey->protkey, t->protkey,
402 sizeof(protkey->protkey));
403 rc = pkey_verifyprotkey(protkey);
406 case TOKVER_CLEAR_KEY: {
407 struct clearaeskeytoken *t;
408 struct pkey_clrkey ckey;
410 u8 skey[SECKEYBLOBSIZE];
411 u8 ep11key[MAXEP11AESKEYBLOBSIZE];
413 size_t tmpbuflen = sizeof(union u_tmpbuf);
415 if (keylen < sizeof(struct clearaeskeytoken))
417 t = (struct clearaeskeytoken *)key;
418 if (keylen != sizeof(*t) + t->len)
420 if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16)
421 || (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24)
422 || (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
423 memcpy(ckey.clrkey, t->clearkey, t->len);
426 /* alloc temp key buffer space */
427 tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
432 /* try direct way with the PCKMO instruction */
433 rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
436 /* PCKMO failed, so try the CCA secure key way */
437 rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
438 ckey.clrkey, tmpbuf);
440 rc = pkey_skey2pkey(tmpbuf, protkey);
443 /* if the CCA way also failed, let's try via EP11 */
444 rc = pkey_clr2ep11key(ckey.clrkey, t->len,
447 rc = pkey_ep11key2pkey(tmpbuf, protkey);
448 /* now we should really have an protected key */
449 DEBUG_ERR("%s unable to build protected key from clear",
453 case TOKVER_EP11_AES: {
454 /* check ep11 key for exportable as protected key */
455 rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
458 rc = pkey_ep11key2pkey(key, protkey);
461 case TOKVER_EP11_AES_WITH_HEADER:
462 /* check ep11 key with header for exportable as protected key */
463 rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
466 rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
470 DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
471 __func__, hdr->version);
481 * Transform a CCA internal key token into a protected key
483 static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
484 struct pkey_protkey *protkey)
486 struct keytoken_header *hdr = (struct keytoken_header *)key;
488 switch (hdr->version) {
490 if (keylen != sizeof(struct secaeskeytoken))
493 case TOKVER_CCA_VLSC:
494 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
498 DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
499 __func__, hdr->version);
503 return pkey_skey2pkey(key, protkey);
507 * Transform a key blob (of any type) into a protected key
509 int pkey_keyblob2pkey(const u8 *key, u32 keylen,
510 struct pkey_protkey *protkey)
513 struct keytoken_header *hdr = (struct keytoken_header *)key;
515 if (keylen < sizeof(struct keytoken_header)) {
516 DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
521 case TOKTYPE_NON_CCA:
522 rc = pkey_nonccatok2pkey(key, keylen, protkey);
524 case TOKTYPE_CCA_INTERNAL:
525 rc = pkey_ccainttok2pkey(key, keylen, protkey);
528 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
529 __func__, hdr->type);
533 DEBUG_DBG("%s rc=%d\n", __func__, rc);
537 EXPORT_SYMBOL(pkey_keyblob2pkey);
539 static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
540 enum pkey_key_type ktype, enum pkey_key_size ksize,
541 u32 kflags, u8 *keybuf, size_t *keybufsize)
543 int i, card, dom, rc;
545 /* check for at least one apqn given */
546 if (!apqns || !nr_apqns)
549 /* check key type and size */
551 case PKEY_TYPE_CCA_DATA:
552 case PKEY_TYPE_CCA_CIPHER:
553 if (*keybufsize < SECKEYBLOBSIZE)
557 if (*keybufsize < MINEP11AESKEYBLOBSIZE)
564 case PKEY_SIZE_AES_128:
565 case PKEY_SIZE_AES_192:
566 case PKEY_SIZE_AES_256:
572 /* simple try all apqns from the list */
573 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
574 card = apqns[i].card;
575 dom = apqns[i].domain;
576 if (ktype == PKEY_TYPE_EP11) {
577 rc = ep11_genaeskey(card, dom, ksize, kflags,
579 } else if (ktype == PKEY_TYPE_CCA_DATA) {
580 rc = cca_genseckey(card, dom, ksize, keybuf);
581 *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
582 } else /* TOKVER_CCA_VLSC */
583 rc = cca_gencipherkey(card, dom, ksize, kflags,
592 static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
593 enum pkey_key_type ktype, enum pkey_key_size ksize,
594 u32 kflags, const u8 *clrkey,
595 u8 *keybuf, size_t *keybufsize)
597 int i, card, dom, rc;
599 /* check for at least one apqn given */
600 if (!apqns || !nr_apqns)
603 /* check key type and size */
605 case PKEY_TYPE_CCA_DATA:
606 case PKEY_TYPE_CCA_CIPHER:
607 if (*keybufsize < SECKEYBLOBSIZE)
611 if (*keybufsize < MINEP11AESKEYBLOBSIZE)
618 case PKEY_SIZE_AES_128:
619 case PKEY_SIZE_AES_192:
620 case PKEY_SIZE_AES_256:
626 /* simple try all apqns from the list */
627 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
628 card = apqns[i].card;
629 dom = apqns[i].domain;
630 if (ktype == PKEY_TYPE_EP11) {
631 rc = ep11_clr2keyblob(card, dom, ksize, kflags,
632 clrkey, keybuf, keybufsize);
633 } else if (ktype == PKEY_TYPE_CCA_DATA) {
634 rc = cca_clr2seckey(card, dom, ksize,
636 *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
637 } else /* TOKVER_CCA_VLSC */
638 rc = cca_clr2cipherkey(card, dom, ksize, kflags,
639 clrkey, keybuf, keybufsize);
647 static int pkey_verifykey2(const u8 *key, size_t keylen,
648 u16 *cardnr, u16 *domain,
649 enum pkey_key_type *ktype,
650 enum pkey_key_size *ksize, u32 *flags)
653 u32 _nr_apqns, *_apqns = NULL;
654 struct keytoken_header *hdr = (struct keytoken_header *)key;
656 if (keylen < sizeof(struct keytoken_header))
659 if (hdr->type == TOKTYPE_CCA_INTERNAL
660 && hdr->version == TOKVER_CCA_AES) {
661 struct secaeskeytoken *t = (struct secaeskeytoken *)key;
663 rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
667 *ktype = PKEY_TYPE_CCA_DATA;
669 *ksize = (enum pkey_key_size) t->bitsize;
671 rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
672 ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
673 if (rc == 0 && flags)
674 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
676 rc = cca_findcard2(&_apqns, &_nr_apqns,
678 ZCRYPT_CEX3C, AES_MK_SET,
680 if (rc == 0 && flags)
681 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
686 *cardnr = ((struct pkey_apqn *)_apqns)->card;
687 *domain = ((struct pkey_apqn *)_apqns)->domain;
689 } else if (hdr->type == TOKTYPE_CCA_INTERNAL
690 && hdr->version == TOKVER_CCA_VLSC) {
691 struct cipherkeytoken *t = (struct cipherkeytoken *)key;
693 rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
697 *ktype = PKEY_TYPE_CCA_CIPHER;
699 *ksize = PKEY_SIZE_UNKNOWN;
700 if (!t->plfver && t->wpllen == 512)
701 *ksize = PKEY_SIZE_AES_128;
702 else if (!t->plfver && t->wpllen == 576)
703 *ksize = PKEY_SIZE_AES_192;
704 else if (!t->plfver && t->wpllen == 640)
705 *ksize = PKEY_SIZE_AES_256;
708 rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
709 ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
710 if (rc == 0 && flags)
711 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
713 rc = cca_findcard2(&_apqns, &_nr_apqns,
715 ZCRYPT_CEX6, AES_MK_SET,
717 if (rc == 0 && flags)
718 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
723 *cardnr = ((struct pkey_apqn *)_apqns)->card;
724 *domain = ((struct pkey_apqn *)_apqns)->domain;
726 } else if (hdr->type == TOKTYPE_NON_CCA
727 && hdr->version == TOKVER_EP11_AES) {
728 struct ep11keyblob *kb = (struct ep11keyblob *)key;
730 rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
734 *ktype = PKEY_TYPE_EP11;
736 *ksize = kb->head.keybitlen;
738 rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
739 ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
744 *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
746 *cardnr = ((struct pkey_apqn *)_apqns)->card;
747 *domain = ((struct pkey_apqn *)_apqns)->domain;
757 static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
758 const u8 *key, size_t keylen,
759 struct pkey_protkey *pkey)
761 int i, card, dom, rc;
762 struct keytoken_header *hdr = (struct keytoken_header *)key;
764 /* check for at least one apqn given */
765 if (!apqns || !nr_apqns)
768 if (keylen < sizeof(struct keytoken_header))
771 if (hdr->type == TOKTYPE_CCA_INTERNAL) {
772 if (hdr->version == TOKVER_CCA_AES) {
773 if (keylen != sizeof(struct secaeskeytoken))
775 if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
777 } else if (hdr->version == TOKVER_CCA_VLSC) {
778 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
780 if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
783 DEBUG_ERR("%s unknown CCA internal token version %d\n",
784 __func__, hdr->version);
787 } else if (hdr->type == TOKTYPE_NON_CCA) {
788 if (hdr->version == TOKVER_EP11_AES) {
789 if (keylen < sizeof(struct ep11keyblob))
791 if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
794 return pkey_nonccatok2pkey(key, keylen, pkey);
797 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
798 __func__, hdr->type);
802 /* simple try all apqns from the list */
803 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
804 card = apqns[i].card;
805 dom = apqns[i].domain;
806 if (hdr->type == TOKTYPE_CCA_INTERNAL
807 && hdr->version == TOKVER_CCA_AES)
808 rc = cca_sec2protkey(card, dom, key, pkey->protkey,
809 &pkey->len, &pkey->type);
810 else if (hdr->type == TOKTYPE_CCA_INTERNAL
811 && hdr->version == TOKVER_CCA_VLSC)
812 rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
813 &pkey->len, &pkey->type);
814 else { /* EP11 AES secure key blob */
815 struct ep11keyblob *kb = (struct ep11keyblob *) key;
817 pkey->len = sizeof(pkey->protkey);
818 rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
819 pkey->protkey, &pkey->len,
829 static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
830 struct pkey_apqn *apqns, size_t *nr_apqns)
833 u32 _nr_apqns, *_apqns = NULL;
834 struct keytoken_header *hdr = (struct keytoken_header *)key;
836 if (keylen < sizeof(struct keytoken_header) || flags == 0)
839 if (hdr->type == TOKTYPE_NON_CCA
840 && (hdr->version == TOKVER_EP11_AES_WITH_HEADER
841 || hdr->version == TOKVER_EP11_ECC_WITH_HEADER)
842 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
843 int minhwtype = 0, api = 0;
844 struct ep11keyblob *kb = (struct ep11keyblob *)
845 (key + sizeof(struct ep11kblob_header));
847 if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
849 if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
850 minhwtype = ZCRYPT_CEX7;
853 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
854 minhwtype, api, kb->wkvp);
857 } else if (hdr->type == TOKTYPE_NON_CCA
858 && hdr->version == TOKVER_EP11_AES
859 && is_ep11_keyblob(key)) {
860 int minhwtype = 0, api = 0;
861 struct ep11keyblob *kb = (struct ep11keyblob *) key;
863 if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
865 if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
866 minhwtype = ZCRYPT_CEX7;
869 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
870 minhwtype, api, kb->wkvp);
873 } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
874 int minhwtype = ZCRYPT_CEX3C;
875 u64 cur_mkvp = 0, old_mkvp = 0;
877 if (hdr->version == TOKVER_CCA_AES) {
878 struct secaeskeytoken *t = (struct secaeskeytoken *)key;
880 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
882 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
884 } else if (hdr->version == TOKVER_CCA_VLSC) {
885 struct cipherkeytoken *t = (struct cipherkeytoken *)key;
887 minhwtype = ZCRYPT_CEX6;
888 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
890 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
893 /* unknown cca internal token type */
896 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
897 minhwtype, AES_MK_SET,
898 cur_mkvp, old_mkvp, 1);
901 } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
902 u64 cur_mkvp = 0, old_mkvp = 0;
903 struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
905 if (t->secid == 0x20) {
906 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
908 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
911 /* unknown cca internal 2 token type */
914 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
915 ZCRYPT_CEX7, APKA_MK_SET,
916 cur_mkvp, old_mkvp, 1);
923 if (*nr_apqns < _nr_apqns)
926 memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
928 *nr_apqns = _nr_apqns;
935 static int pkey_apqns4keytype(enum pkey_key_type ktype,
936 u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
937 struct pkey_apqn *apqns, size_t *nr_apqns)
940 u32 _nr_apqns, *_apqns = NULL;
942 if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
943 u64 cur_mkvp = 0, old_mkvp = 0;
944 int minhwtype = ZCRYPT_CEX3C;
946 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
947 cur_mkvp = *((u64 *) cur_mkvp);
948 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
949 old_mkvp = *((u64 *) alt_mkvp);
950 if (ktype == PKEY_TYPE_CCA_CIPHER)
951 minhwtype = ZCRYPT_CEX6;
952 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
953 minhwtype, AES_MK_SET,
954 cur_mkvp, old_mkvp, 1);
957 } else if (ktype == PKEY_TYPE_CCA_ECC) {
958 u64 cur_mkvp = 0, old_mkvp = 0;
960 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
961 cur_mkvp = *((u64 *) cur_mkvp);
962 if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
963 old_mkvp = *((u64 *) alt_mkvp);
964 rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
965 ZCRYPT_CEX7, APKA_MK_SET,
966 cur_mkvp, old_mkvp, 1);
970 } else if (ktype == PKEY_TYPE_EP11 ||
971 ktype == PKEY_TYPE_EP11_AES ||
972 ktype == PKEY_TYPE_EP11_ECC) {
975 if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
977 rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
978 ZCRYPT_CEX7, EP11_API_V, wkvp);
986 if (*nr_apqns < _nr_apqns)
989 memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
991 *nr_apqns = _nr_apqns;
998 static int pkey_keyblob2pkey3(const struct pkey_apqn *apqns, size_t nr_apqns,
999 const u8 *key, size_t keylen, u32 *protkeytype,
1000 u8 *protkey, u32 *protkeylen)
1002 int i, card, dom, rc;
1003 struct keytoken_header *hdr = (struct keytoken_header *)key;
1005 /* check for at least one apqn given */
1006 if (!apqns || !nr_apqns)
1009 if (keylen < sizeof(struct keytoken_header))
1012 if (hdr->type == TOKTYPE_NON_CCA
1013 && hdr->version == TOKVER_EP11_AES_WITH_HEADER
1014 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1015 /* EP11 AES key blob with header */
1016 if (ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1))
1018 } else if (hdr->type == TOKTYPE_NON_CCA
1019 && hdr->version == TOKVER_EP11_ECC_WITH_HEADER
1020 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1021 /* EP11 ECC key blob with header */
1022 if (ep11_check_ecc_key_with_hdr(debug_info, 3, key, keylen, 1))
1024 } else if (hdr->type == TOKTYPE_NON_CCA
1025 && hdr->version == TOKVER_EP11_AES
1026 && is_ep11_keyblob(key)) {
1027 /* EP11 AES key blob with header in session field */
1028 if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
1030 } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
1031 if (hdr->version == TOKVER_CCA_AES) {
1032 /* CCA AES data key */
1033 if (keylen != sizeof(struct secaeskeytoken))
1035 if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
1037 } else if (hdr->version == TOKVER_CCA_VLSC) {
1038 /* CCA AES cipher key */
1039 if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
1041 if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
1044 DEBUG_ERR("%s unknown CCA internal token version %d\n",
1045 __func__, hdr->version);
1048 } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
1049 /* CCA ECC (private) key */
1050 if (keylen < sizeof(struct eccprivkeytoken))
1052 if (cca_check_sececckeytoken(debug_info, 3, key, keylen, 1))
1054 } else if (hdr->type == TOKTYPE_NON_CCA) {
1055 struct pkey_protkey pkey;
1057 rc = pkey_nonccatok2pkey(key, keylen, &pkey);
1060 memcpy(protkey, pkey.protkey, pkey.len);
1061 *protkeylen = pkey.len;
1062 *protkeytype = pkey.type;
1065 DEBUG_ERR("%s unknown/unsupported blob type %d\n",
1066 __func__, hdr->type);
1070 /* simple try all apqns from the list */
1071 for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
1072 card = apqns[i].card;
1073 dom = apqns[i].domain;
1074 if (hdr->type == TOKTYPE_NON_CCA
1075 && (hdr->version == TOKVER_EP11_AES_WITH_HEADER
1076 || hdr->version == TOKVER_EP11_ECC_WITH_HEADER)
1077 && is_ep11_keyblob(key + sizeof(struct ep11kblob_header)))
1078 rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1079 protkey, protkeylen, protkeytype);
1080 else if (hdr->type == TOKTYPE_NON_CCA
1081 && hdr->version == TOKVER_EP11_AES
1082 && is_ep11_keyblob(key))
1083 rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1084 protkey, protkeylen, protkeytype);
1085 else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1086 hdr->version == TOKVER_CCA_AES)
1087 rc = cca_sec2protkey(card, dom, key, protkey,
1088 protkeylen, protkeytype);
1089 else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1090 hdr->version == TOKVER_CCA_VLSC)
1091 rc = cca_cipher2protkey(card, dom, key, protkey,
1092 protkeylen, protkeytype);
1093 else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA)
1094 rc = cca_ecc2protkey(card, dom, key, protkey,
1095 protkeylen, protkeytype);
1107 static void *_copy_key_from_user(void __user *ukey, size_t keylen)
1109 if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
1110 return ERR_PTR(-EINVAL);
1112 return memdup_user(ukey, keylen);
1115 static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
1117 if (!uapqns || nr_apqns == 0)
1120 return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
1123 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1129 case PKEY_GENSECK: {
1130 struct pkey_genseck __user *ugs = (void __user *) arg;
1131 struct pkey_genseck kgs;
1133 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1135 rc = cca_genseckey(kgs.cardnr, kgs.domain,
1136 kgs.keytype, kgs.seckey.seckey);
1137 DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
1140 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1144 case PKEY_CLR2SECK: {
1145 struct pkey_clr2seck __user *ucs = (void __user *) arg;
1146 struct pkey_clr2seck kcs;
1148 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1150 rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1151 kcs.clrkey.clrkey, kcs.seckey.seckey);
1152 DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
1155 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1157 memzero_explicit(&kcs, sizeof(kcs));
1160 case PKEY_SEC2PROTK: {
1161 struct pkey_sec2protk __user *usp = (void __user *) arg;
1162 struct pkey_sec2protk ksp;
1164 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1166 rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
1167 ksp.seckey.seckey, ksp.protkey.protkey,
1168 &ksp.protkey.len, &ksp.protkey.type);
1169 DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
1172 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1176 case PKEY_CLR2PROTK: {
1177 struct pkey_clr2protk __user *ucp = (void __user *) arg;
1178 struct pkey_clr2protk kcp;
1180 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1182 rc = pkey_clr2protkey(kcp.keytype,
1183 &kcp.clrkey, &kcp.protkey);
1184 DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1187 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1189 memzero_explicit(&kcp, sizeof(kcp));
1192 case PKEY_FINDCARD: {
1193 struct pkey_findcard __user *ufc = (void __user *) arg;
1194 struct pkey_findcard kfc;
1196 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1198 rc = cca_findcard(kfc.seckey.seckey,
1199 &kfc.cardnr, &kfc.domain, 1);
1200 DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
1203 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1207 case PKEY_SKEY2PKEY: {
1208 struct pkey_skey2pkey __user *usp = (void __user *) arg;
1209 struct pkey_skey2pkey ksp;
1211 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1213 rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
1214 DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1217 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1221 case PKEY_VERIFYKEY: {
1222 struct pkey_verifykey __user *uvk = (void __user *) arg;
1223 struct pkey_verifykey kvk;
1225 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1227 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1228 &kvk.keysize, &kvk.attributes);
1229 DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1232 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1236 case PKEY_GENPROTK: {
1237 struct pkey_genprotk __user *ugp = (void __user *) arg;
1238 struct pkey_genprotk kgp;
1240 if (copy_from_user(&kgp, ugp, sizeof(kgp)))
1242 rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
1243 DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
1246 if (copy_to_user(ugp, &kgp, sizeof(kgp)))
1250 case PKEY_VERIFYPROTK: {
1251 struct pkey_verifyprotk __user *uvp = (void __user *) arg;
1252 struct pkey_verifyprotk kvp;
1254 if (copy_from_user(&kvp, uvp, sizeof(kvp)))
1256 rc = pkey_verifyprotkey(&kvp.protkey);
1257 DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
1260 case PKEY_KBLOB2PROTK: {
1261 struct pkey_kblob2pkey __user *utp = (void __user *) arg;
1262 struct pkey_kblob2pkey ktp;
1265 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1267 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1269 return PTR_ERR(kkey);
1270 rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
1271 DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
1275 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1279 case PKEY_GENSECK2: {
1280 struct pkey_genseck2 __user *ugs = (void __user *) arg;
1281 struct pkey_genseck2 kgs;
1282 struct pkey_apqn *apqns;
1283 size_t klen = KEYBLOBBUFSIZE;
1286 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1288 apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1290 return PTR_ERR(apqns);
1291 kkey = kmalloc(klen, GFP_KERNEL);
1296 rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1297 kgs.type, kgs.size, kgs.keygenflags,
1299 DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1306 if (kgs.keylen < klen) {
1310 if (copy_to_user(kgs.key, kkey, klen)) {
1316 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1321 case PKEY_CLR2SECK2: {
1322 struct pkey_clr2seck2 __user *ucs = (void __user *) arg;
1323 struct pkey_clr2seck2 kcs;
1324 struct pkey_apqn *apqns;
1325 size_t klen = KEYBLOBBUFSIZE;
1328 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1330 apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1332 return PTR_ERR(apqns);
1333 kkey = kmalloc(klen, GFP_KERNEL);
1338 rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1339 kcs.type, kcs.size, kcs.keygenflags,
1340 kcs.clrkey.clrkey, kkey, &klen);
1341 DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1348 if (kcs.keylen < klen) {
1352 if (copy_to_user(kcs.key, kkey, klen)) {
1358 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1360 memzero_explicit(&kcs, sizeof(kcs));
1364 case PKEY_VERIFYKEY2: {
1365 struct pkey_verifykey2 __user *uvk = (void __user *) arg;
1366 struct pkey_verifykey2 kvk;
1369 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1371 kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1373 return PTR_ERR(kkey);
1374 rc = pkey_verifykey2(kkey, kvk.keylen,
1375 &kvk.cardnr, &kvk.domain,
1376 &kvk.type, &kvk.size, &kvk.flags);
1377 DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1381 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1385 case PKEY_KBLOB2PROTK2: {
1386 struct pkey_kblob2pkey2 __user *utp = (void __user *) arg;
1387 struct pkey_kblob2pkey2 ktp;
1388 struct pkey_apqn *apqns = NULL;
1391 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1393 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1395 return PTR_ERR(apqns);
1396 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1399 return PTR_ERR(kkey);
1401 rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1402 kkey, ktp.keylen, &ktp.protkey);
1403 DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1408 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1412 case PKEY_APQNS4K: {
1413 struct pkey_apqns4key __user *uak = (void __user *) arg;
1414 struct pkey_apqns4key kak;
1415 struct pkey_apqn *apqns = NULL;
1416 size_t nr_apqns, len;
1419 if (copy_from_user(&kak, uak, sizeof(kak)))
1421 nr_apqns = kak.apqn_entries;
1423 apqns = kmalloc_array(nr_apqns,
1424 sizeof(struct pkey_apqn),
1429 kkey = _copy_key_from_user(kak.key, kak.keylen);
1432 return PTR_ERR(kkey);
1434 rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1436 DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1438 if (rc && rc != -ENOSPC) {
1442 if (!rc && kak.apqns) {
1443 if (nr_apqns > kak.apqn_entries) {
1447 len = nr_apqns * sizeof(struct pkey_apqn);
1449 if (copy_to_user(kak.apqns, apqns, len)) {
1455 kak.apqn_entries = nr_apqns;
1456 if (copy_to_user(uak, &kak, sizeof(kak)))
1461 case PKEY_APQNS4KT: {
1462 struct pkey_apqns4keytype __user *uat = (void __user *) arg;
1463 struct pkey_apqns4keytype kat;
1464 struct pkey_apqn *apqns = NULL;
1465 size_t nr_apqns, len;
1467 if (copy_from_user(&kat, uat, sizeof(kat)))
1469 nr_apqns = kat.apqn_entries;
1471 apqns = kmalloc_array(nr_apqns,
1472 sizeof(struct pkey_apqn),
1477 rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1478 kat.flags, apqns, &nr_apqns);
1479 DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1480 if (rc && rc != -ENOSPC) {
1484 if (!rc && kat.apqns) {
1485 if (nr_apqns > kat.apqn_entries) {
1489 len = nr_apqns * sizeof(struct pkey_apqn);
1491 if (copy_to_user(kat.apqns, apqns, len)) {
1497 kat.apqn_entries = nr_apqns;
1498 if (copy_to_user(uat, &kat, sizeof(kat)))
1503 case PKEY_KBLOB2PROTK3: {
1504 struct pkey_kblob2pkey3 __user *utp = (void __user *) arg;
1505 struct pkey_kblob2pkey3 ktp;
1506 struct pkey_apqn *apqns = NULL;
1507 u32 protkeylen = PROTKEYBLOBBUFSIZE;
1510 if (copy_from_user(&ktp, utp, sizeof(ktp)))
1512 apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1514 return PTR_ERR(apqns);
1515 kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1518 return PTR_ERR(kkey);
1520 protkey = kmalloc(protkeylen, GFP_KERNEL);
1526 rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
1527 ktp.keylen, &ktp.pkeytype,
1528 protkey, &protkeylen);
1529 DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
1536 if (ktp.pkey && ktp.pkeylen) {
1537 if (protkeylen > ktp.pkeylen) {
1541 if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
1547 ktp.pkeylen = protkeylen;
1548 if (copy_to_user(utp, &ktp, sizeof(ktp)))
1553 /* unknown/unsupported ioctl cmd */
1561 * Sysfs and file io operations
1565 * Sysfs attribute read function for all protected key binary attributes.
1566 * The implementation can not deal with partial reads, because a new random
1567 * protected key blob is generated with each read. In case of partial reads
1568 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1570 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1571 loff_t off, size_t count)
1573 struct protaeskeytoken protkeytoken;
1574 struct pkey_protkey protkey;
1577 if (off != 0 || count < sizeof(protkeytoken))
1580 if (count < 2 * sizeof(protkeytoken))
1583 memset(&protkeytoken, 0, sizeof(protkeytoken));
1584 protkeytoken.type = TOKTYPE_NON_CCA;
1585 protkeytoken.version = TOKVER_PROTECTED_KEY;
1586 protkeytoken.keytype = keytype;
1588 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1592 protkeytoken.len = protkey.len;
1593 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1595 memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1598 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1602 protkeytoken.len = protkey.len;
1603 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1605 memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1606 sizeof(protkeytoken));
1608 return 2 * sizeof(protkeytoken);
1611 return sizeof(protkeytoken);
1614 static ssize_t protkey_aes_128_read(struct file *filp,
1615 struct kobject *kobj,
1616 struct bin_attribute *attr,
1617 char *buf, loff_t off,
1620 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1624 static ssize_t protkey_aes_192_read(struct file *filp,
1625 struct kobject *kobj,
1626 struct bin_attribute *attr,
1627 char *buf, loff_t off,
1630 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1634 static ssize_t protkey_aes_256_read(struct file *filp,
1635 struct kobject *kobj,
1636 struct bin_attribute *attr,
1637 char *buf, loff_t off,
1640 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1644 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1645 struct kobject *kobj,
1646 struct bin_attribute *attr,
1647 char *buf, loff_t off,
1650 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1654 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1655 struct kobject *kobj,
1656 struct bin_attribute *attr,
1657 char *buf, loff_t off,
1660 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1664 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1665 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1666 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1667 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1668 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1670 static struct bin_attribute *protkey_attrs[] = {
1671 &bin_attr_protkey_aes_128,
1672 &bin_attr_protkey_aes_192,
1673 &bin_attr_protkey_aes_256,
1674 &bin_attr_protkey_aes_128_xts,
1675 &bin_attr_protkey_aes_256_xts,
1679 static struct attribute_group protkey_attr_group = {
1681 .bin_attrs = protkey_attrs,
1685 * Sysfs attribute read function for all secure key ccadata binary attributes.
1686 * The implementation can not deal with partial reads, because a new random
1687 * protected key blob is generated with each read. In case of partial reads
1688 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1690 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1691 loff_t off, size_t count)
1694 struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
1696 if (off != 0 || count < sizeof(struct secaeskeytoken))
1699 if (count < 2 * sizeof(struct secaeskeytoken))
1702 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1708 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1712 return 2 * sizeof(struct secaeskeytoken);
1715 return sizeof(struct secaeskeytoken);
1718 static ssize_t ccadata_aes_128_read(struct file *filp,
1719 struct kobject *kobj,
1720 struct bin_attribute *attr,
1721 char *buf, loff_t off,
1724 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1728 static ssize_t ccadata_aes_192_read(struct file *filp,
1729 struct kobject *kobj,
1730 struct bin_attribute *attr,
1731 char *buf, loff_t off,
1734 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1738 static ssize_t ccadata_aes_256_read(struct file *filp,
1739 struct kobject *kobj,
1740 struct bin_attribute *attr,
1741 char *buf, loff_t off,
1744 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1748 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1749 struct kobject *kobj,
1750 struct bin_attribute *attr,
1751 char *buf, loff_t off,
1754 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1758 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1759 struct kobject *kobj,
1760 struct bin_attribute *attr,
1761 char *buf, loff_t off,
1764 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1768 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1769 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1770 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1771 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1772 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1774 static struct bin_attribute *ccadata_attrs[] = {
1775 &bin_attr_ccadata_aes_128,
1776 &bin_attr_ccadata_aes_192,
1777 &bin_attr_ccadata_aes_256,
1778 &bin_attr_ccadata_aes_128_xts,
1779 &bin_attr_ccadata_aes_256_xts,
1783 static struct attribute_group ccadata_attr_group = {
1785 .bin_attrs = ccadata_attrs,
1788 #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80)
1791 * Sysfs attribute read function for all secure key ccacipher binary attributes.
1792 * The implementation can not deal with partial reads, because a new random
1793 * secure key blob is generated with each read. In case of partial reads
1794 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1796 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1797 bool is_xts, char *buf, loff_t off,
1800 int i, rc, card, dom;
1801 u32 nr_apqns, *apqns = NULL;
1802 size_t keysize = CCACIPHERTOKENSIZE;
1804 if (off != 0 || count < CCACIPHERTOKENSIZE)
1807 if (count < 2 * CCACIPHERTOKENSIZE)
1810 /* build a list of apqns able to generate an cipher key */
1811 rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1812 ZCRYPT_CEX6, 0, 0, 0, 0);
1816 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1818 /* simple try all apqns from the list */
1819 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1820 card = apqns[i] >> 16;
1821 dom = apqns[i] & 0xFFFF;
1822 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1830 keysize = CCACIPHERTOKENSIZE;
1831 buf += CCACIPHERTOKENSIZE;
1832 rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1834 return 2 * CCACIPHERTOKENSIZE;
1837 return CCACIPHERTOKENSIZE;
1840 static ssize_t ccacipher_aes_128_read(struct file *filp,
1841 struct kobject *kobj,
1842 struct bin_attribute *attr,
1843 char *buf, loff_t off,
1846 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1850 static ssize_t ccacipher_aes_192_read(struct file *filp,
1851 struct kobject *kobj,
1852 struct bin_attribute *attr,
1853 char *buf, loff_t off,
1856 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1860 static ssize_t ccacipher_aes_256_read(struct file *filp,
1861 struct kobject *kobj,
1862 struct bin_attribute *attr,
1863 char *buf, loff_t off,
1866 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1870 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1871 struct kobject *kobj,
1872 struct bin_attribute *attr,
1873 char *buf, loff_t off,
1876 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1880 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1881 struct kobject *kobj,
1882 struct bin_attribute *attr,
1883 char *buf, loff_t off,
1886 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1890 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1891 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1892 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1893 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1894 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1896 static struct bin_attribute *ccacipher_attrs[] = {
1897 &bin_attr_ccacipher_aes_128,
1898 &bin_attr_ccacipher_aes_192,
1899 &bin_attr_ccacipher_aes_256,
1900 &bin_attr_ccacipher_aes_128_xts,
1901 &bin_attr_ccacipher_aes_256_xts,
1905 static struct attribute_group ccacipher_attr_group = {
1906 .name = "ccacipher",
1907 .bin_attrs = ccacipher_attrs,
1911 * Sysfs attribute read function for all ep11 aes key binary attributes.
1912 * The implementation can not deal with partial reads, because a new random
1913 * secure key blob is generated with each read. In case of partial reads
1914 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1915 * This function and the sysfs attributes using it provide EP11 key blobs
1916 * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1919 static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1920 bool is_xts, char *buf, loff_t off,
1923 int i, rc, card, dom;
1924 u32 nr_apqns, *apqns = NULL;
1925 size_t keysize = MAXEP11AESKEYBLOBSIZE;
1927 if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1930 if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1933 /* build a list of apqns able to generate an cipher key */
1934 rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1935 ZCRYPT_CEX7, EP11_API_V, NULL);
1939 memset(buf, 0, is_xts ? 2 * keysize : keysize);
1941 /* simple try all apqns from the list */
1942 for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1943 card = apqns[i] >> 16;
1944 dom = apqns[i] & 0xFFFF;
1945 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1953 keysize = MAXEP11AESKEYBLOBSIZE;
1954 buf += MAXEP11AESKEYBLOBSIZE;
1955 rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1957 return 2 * MAXEP11AESKEYBLOBSIZE;
1960 return MAXEP11AESKEYBLOBSIZE;
1963 static ssize_t ep11_aes_128_read(struct file *filp,
1964 struct kobject *kobj,
1965 struct bin_attribute *attr,
1966 char *buf, loff_t off,
1969 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1973 static ssize_t ep11_aes_192_read(struct file *filp,
1974 struct kobject *kobj,
1975 struct bin_attribute *attr,
1976 char *buf, loff_t off,
1979 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1983 static ssize_t ep11_aes_256_read(struct file *filp,
1984 struct kobject *kobj,
1985 struct bin_attribute *attr,
1986 char *buf, loff_t off,
1989 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1993 static ssize_t ep11_aes_128_xts_read(struct file *filp,
1994 struct kobject *kobj,
1995 struct bin_attribute *attr,
1996 char *buf, loff_t off,
1999 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
2003 static ssize_t ep11_aes_256_xts_read(struct file *filp,
2004 struct kobject *kobj,
2005 struct bin_attribute *attr,
2006 char *buf, loff_t off,
2009 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
2013 static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2014 static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2015 static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2016 static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2017 static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2019 static struct bin_attribute *ep11_attrs[] = {
2020 &bin_attr_ep11_aes_128,
2021 &bin_attr_ep11_aes_192,
2022 &bin_attr_ep11_aes_256,
2023 &bin_attr_ep11_aes_128_xts,
2024 &bin_attr_ep11_aes_256_xts,
2028 static struct attribute_group ep11_attr_group = {
2030 .bin_attrs = ep11_attrs,
2033 static const struct attribute_group *pkey_attr_groups[] = {
2034 &protkey_attr_group,
2035 &ccadata_attr_group,
2036 &ccacipher_attr_group,
2041 static const struct file_operations pkey_fops = {
2042 .owner = THIS_MODULE,
2043 .open = nonseekable_open,
2044 .llseek = no_llseek,
2045 .unlocked_ioctl = pkey_unlocked_ioctl,
2048 static struct miscdevice pkey_dev = {
2050 .minor = MISC_DYNAMIC_MINOR,
2053 .groups = pkey_attr_groups,
2059 static int __init pkey_init(void)
2061 cpacf_mask_t kmc_functions;
2064 * The pckmo instruction should be available - even if we don't
2065 * actually invoke it. This instruction comes with MSA 3 which
2066 * is also the minimum level for the kmc instructions which
2067 * are able to work with protected keys.
2069 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
2072 /* check for kmc instructions available */
2073 if (!cpacf_query(CPACF_KMC, &kmc_functions))
2075 if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
2076 !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
2077 !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
2082 return misc_register(&pkey_dev);
2088 static void __exit pkey_exit(void)
2090 misc_deregister(&pkey_dev);
2094 module_cpu_feature_match(MSA, pkey_init);
2095 module_exit(pkey_exit);