1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2019
4 * Author(s): Harald Freudenberger <freude@linux.ibm.com>
6 * Collection of EP11 misc functions used by zcrypt and pkey
9 #define KMSG_COMPONENT "zcrypt"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <asm/zcrypt.h>
18 #include <crypto/aes.h>
21 #include "zcrypt_api.h"
22 #include "zcrypt_debug.h"
23 #include "zcrypt_msgtype6.h"
24 #include "zcrypt_ep11misc.h"
25 #include "zcrypt_ccamisc.h"
27 #define EP11_PINBLOB_V1_BYTES 56
29 /* default iv used here */
30 static const u8 def_iv
[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
31 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
33 /* ep11 card info cache */
34 struct card_list_entry
{
35 struct list_head list
;
37 struct ep11_card_info info
;
39 static LIST_HEAD(card_list
);
40 static DEFINE_SPINLOCK(card_list_lock
);
42 static int card_cache_fetch(u16 cardnr
, struct ep11_card_info
*ci
)
45 struct card_list_entry
*ptr
;
47 spin_lock_bh(&card_list_lock
);
48 list_for_each_entry(ptr
, &card_list
, list
) {
49 if (ptr
->cardnr
== cardnr
) {
50 memcpy(ci
, &ptr
->info
, sizeof(*ci
));
55 spin_unlock_bh(&card_list_lock
);
60 static void card_cache_update(u16 cardnr
, const struct ep11_card_info
*ci
)
63 struct card_list_entry
*ptr
;
65 spin_lock_bh(&card_list_lock
);
66 list_for_each_entry(ptr
, &card_list
, list
) {
67 if (ptr
->cardnr
== cardnr
) {
68 memcpy(&ptr
->info
, ci
, sizeof(*ci
));
74 ptr
= kmalloc(sizeof(*ptr
), GFP_ATOMIC
);
76 spin_unlock_bh(&card_list_lock
);
80 memcpy(&ptr
->info
, ci
, sizeof(*ci
));
81 list_add(&ptr
->list
, &card_list
);
83 spin_unlock_bh(&card_list_lock
);
86 static void card_cache_scrub(u16 cardnr
)
88 struct card_list_entry
*ptr
;
90 spin_lock_bh(&card_list_lock
);
91 list_for_each_entry(ptr
, &card_list
, list
) {
92 if (ptr
->cardnr
== cardnr
) {
98 spin_unlock_bh(&card_list_lock
);
101 static void __exit
card_cache_free(void)
103 struct card_list_entry
*ptr
, *pnext
;
105 spin_lock_bh(&card_list_lock
);
106 list_for_each_entry_safe(ptr
, pnext
, &card_list
, list
) {
107 list_del(&ptr
->list
);
110 spin_unlock_bh(&card_list_lock
);
113 static int ep11_kb_split(const u8
*kb
, size_t kblen
, u32 kbver
,
114 struct ep11kblob_header
**kbhdr
, size_t *kbhdrsize
,
115 u8
**kbpl
, size_t *kbplsize
)
117 struct ep11kblob_header
*hdr
= NULL
;
118 size_t hdrsize
, plsize
= 0;
122 if (kblen
< sizeof(struct ep11kblob_header
))
124 hdr
= (struct ep11kblob_header
*)kb
;
127 case TOKVER_EP11_AES
:
128 /* header overlays the payload */
131 case TOKVER_EP11_ECC_WITH_HEADER
:
132 case TOKVER_EP11_AES_WITH_HEADER
:
133 /* payload starts after the header */
134 hdrsize
= sizeof(struct ep11kblob_header
);
140 plsize
= kblen
- hdrsize
;
141 pl
= (u8
*)kb
+ hdrsize
;
146 *kbhdrsize
= hdrsize
;
157 static int ep11_kb_decode(const u8
*kb
, size_t kblen
,
158 struct ep11kblob_header
**kbhdr
, size_t *kbhdrsize
,
159 struct ep11keyblob
**kbpl
, size_t *kbplsize
)
161 struct ep11kblob_header
*tmph
, *hdr
= NULL
;
162 size_t hdrsize
= 0, plsize
= 0;
163 struct ep11keyblob
*pl
= NULL
;
167 if (kblen
< sizeof(struct ep11kblob_header
))
169 tmph
= (struct ep11kblob_header
*)kb
;
171 if (tmph
->type
!= TOKTYPE_NON_CCA
&&
175 if (ep11_kb_split(kb
, kblen
, tmph
->version
,
176 &hdr
, &hdrsize
, &tmpp
, &plsize
))
179 if (plsize
< sizeof(struct ep11keyblob
))
182 if (!is_ep11_keyblob(tmpp
))
185 pl
= (struct ep11keyblob
*)tmpp
;
186 plsize
= hdr
->len
- hdrsize
;
191 *kbhdrsize
= hdrsize
;
203 * For valid ep11 keyblobs, returns a reference to the wrappingkey verification
204 * pattern. Otherwise NULL.
206 const u8
*ep11_kb_wkvp(const u8
*keyblob
, u32 keybloblen
)
208 struct ep11keyblob
*kb
;
210 if (ep11_kb_decode(keyblob
, keybloblen
, NULL
, NULL
, &kb
, NULL
))
214 EXPORT_SYMBOL(ep11_kb_wkvp
);
217 * Simple check if the key blob is a valid EP11 AES key blob with header.
219 int ep11_check_aes_key_with_hdr(debug_info_t
*dbg
, int dbflvl
,
220 const u8
*key
, u32 keylen
, int checkcpacfexp
)
222 struct ep11kblob_header
*hdr
= (struct ep11kblob_header
*)key
;
223 struct ep11keyblob
*kb
= (struct ep11keyblob
*)(key
+ sizeof(*hdr
));
225 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
227 if (keylen
< sizeof(*hdr
) + sizeof(*kb
)) {
228 DBF("%s key check failed, keylen %u < %zu\n",
229 __func__
, keylen
, sizeof(*hdr
) + sizeof(*kb
));
233 if (hdr
->type
!= TOKTYPE_NON_CCA
) {
235 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
236 __func__
, (int)hdr
->type
, TOKTYPE_NON_CCA
);
239 if (hdr
->hver
!= 0x00) {
241 DBF("%s key check failed, header version 0x%02x != 0x00\n",
242 __func__
, (int)hdr
->hver
);
245 if (hdr
->version
!= TOKVER_EP11_AES_WITH_HEADER
) {
247 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
248 __func__
, (int)hdr
->version
, TOKVER_EP11_AES_WITH_HEADER
);
251 if (hdr
->len
> keylen
) {
253 DBF("%s key check failed, header len %d keylen %u mismatch\n",
254 __func__
, (int)hdr
->len
, keylen
);
257 if (hdr
->len
< sizeof(*hdr
) + sizeof(*kb
)) {
259 DBF("%s key check failed, header len %d < %zu\n",
260 __func__
, (int)hdr
->len
, sizeof(*hdr
) + sizeof(*kb
));
264 if (kb
->version
!= EP11_STRUCT_MAGIC
) {
266 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
267 __func__
, (int)kb
->version
, EP11_STRUCT_MAGIC
);
270 if (checkcpacfexp
&& !(kb
->attr
& EP11_BLOB_PKEY_EXTRACTABLE
)) {
272 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
281 EXPORT_SYMBOL(ep11_check_aes_key_with_hdr
);
284 * Simple check if the key blob is a valid EP11 ECC key blob with header.
286 int ep11_check_ecc_key_with_hdr(debug_info_t
*dbg
, int dbflvl
,
287 const u8
*key
, u32 keylen
, int checkcpacfexp
)
289 struct ep11kblob_header
*hdr
= (struct ep11kblob_header
*)key
;
290 struct ep11keyblob
*kb
= (struct ep11keyblob
*)(key
+ sizeof(*hdr
));
292 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
294 if (keylen
< sizeof(*hdr
) + sizeof(*kb
)) {
295 DBF("%s key check failed, keylen %u < %zu\n",
296 __func__
, keylen
, sizeof(*hdr
) + sizeof(*kb
));
300 if (hdr
->type
!= TOKTYPE_NON_CCA
) {
302 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
303 __func__
, (int)hdr
->type
, TOKTYPE_NON_CCA
);
306 if (hdr
->hver
!= 0x00) {
308 DBF("%s key check failed, header version 0x%02x != 0x00\n",
309 __func__
, (int)hdr
->hver
);
312 if (hdr
->version
!= TOKVER_EP11_ECC_WITH_HEADER
) {
314 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
315 __func__
, (int)hdr
->version
, TOKVER_EP11_ECC_WITH_HEADER
);
318 if (hdr
->len
> keylen
) {
320 DBF("%s key check failed, header len %d keylen %u mismatch\n",
321 __func__
, (int)hdr
->len
, keylen
);
324 if (hdr
->len
< sizeof(*hdr
) + sizeof(*kb
)) {
326 DBF("%s key check failed, header len %d < %zu\n",
327 __func__
, (int)hdr
->len
, sizeof(*hdr
) + sizeof(*kb
));
331 if (kb
->version
!= EP11_STRUCT_MAGIC
) {
333 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
334 __func__
, (int)kb
->version
, EP11_STRUCT_MAGIC
);
337 if (checkcpacfexp
&& !(kb
->attr
& EP11_BLOB_PKEY_EXTRACTABLE
)) {
339 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
348 EXPORT_SYMBOL(ep11_check_ecc_key_with_hdr
);
351 * Simple check if the key blob is a valid EP11 AES key blob with
352 * the header in the session field (old style EP11 AES key).
354 int ep11_check_aes_key(debug_info_t
*dbg
, int dbflvl
,
355 const u8
*key
, u32 keylen
, int checkcpacfexp
)
357 struct ep11keyblob
*kb
= (struct ep11keyblob
*)key
;
359 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
361 if (keylen
< sizeof(*kb
)) {
362 DBF("%s key check failed, keylen %u < %zu\n",
363 __func__
, keylen
, sizeof(*kb
));
367 if (kb
->head
.type
!= TOKTYPE_NON_CCA
) {
369 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
370 __func__
, (int)kb
->head
.type
, TOKTYPE_NON_CCA
);
373 if (kb
->head
.version
!= TOKVER_EP11_AES
) {
375 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
376 __func__
, (int)kb
->head
.version
, TOKVER_EP11_AES
);
379 if (kb
->head
.len
> keylen
) {
381 DBF("%s key check failed, header len %d keylen %u mismatch\n",
382 __func__
, (int)kb
->head
.len
, keylen
);
385 if (kb
->head
.len
< sizeof(*kb
)) {
387 DBF("%s key check failed, header len %d < %zu\n",
388 __func__
, (int)kb
->head
.len
, sizeof(*kb
));
392 if (kb
->version
!= EP11_STRUCT_MAGIC
) {
394 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
395 __func__
, (int)kb
->version
, EP11_STRUCT_MAGIC
);
398 if (checkcpacfexp
&& !(kb
->attr
& EP11_BLOB_PKEY_EXTRACTABLE
)) {
400 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
409 EXPORT_SYMBOL(ep11_check_aes_key
);
412 * Allocate and prepare ep11 cprb plus additional payload.
414 static inline struct ep11_cprb
*alloc_cprb(size_t payload_len
)
416 size_t len
= sizeof(struct ep11_cprb
) + payload_len
;
417 struct ep11_cprb
*cprb
;
419 cprb
= kzalloc(len
, GFP_KERNEL
);
423 cprb
->cprb_len
= sizeof(struct ep11_cprb
);
424 cprb
->cprb_ver_id
= 0x04;
425 memcpy(cprb
->func_id
, "T4", 2);
426 cprb
->ret_code
= 0xFFFFFFFF;
427 cprb
->payload_len
= payload_len
;
433 * Some helper functions related to ASN1 encoding.
434 * Limited to length info <= 2 byte.
437 #define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
439 static int asn1tag_write(u8
*ptr
, u8 tag
, const u8
*pvalue
, u16 valuelen
)
442 if (valuelen
> 255) {
444 *((u16
*)(ptr
+ 2)) = valuelen
;
445 memcpy(ptr
+ 4, pvalue
, valuelen
);
448 if (valuelen
> 127) {
450 ptr
[2] = (u8
)valuelen
;
451 memcpy(ptr
+ 3, pvalue
, valuelen
);
454 ptr
[1] = (u8
)valuelen
;
455 memcpy(ptr
+ 2, pvalue
, valuelen
);
459 /* EP11 payload > 127 bytes starts with this struct */
472 /* prep ep11 payload head helper function */
473 static inline void prep_head(struct pl_head
*h
,
474 size_t pl_size
, int api
, int func
)
478 h
->len
= pl_size
- 4;
480 h
->func_len
= sizeof(u32
);
481 h
->func
= (api
<< 16) + func
;
483 h
->dom_len
= sizeof(u32
);
486 /* prep urb helper function */
487 static inline void prep_urb(struct ep11_urb
*u
,
488 struct ep11_target_dev
*t
, int nt
,
489 struct ep11_cprb
*req
, size_t req_len
,
490 struct ep11_cprb
*rep
, size_t rep_len
)
492 u
->targets
= (u8 __user
*)t
;
494 u
->req
= (u8 __user
*)req
;
495 u
->req_len
= req_len
;
496 u
->resp
= (u8 __user
*)rep
;
497 u
->resp_len
= rep_len
;
500 /* Check ep11 reply payload, return 0 or suggested errno value. */
501 static int check_reply_pl(const u8
*pl
, const char *func
)
508 ZCRYPT_DBF_ERR("%s reply start tag mismatch\n", func
);
512 /* payload length format */
516 } else if (*pl
== 0x81) {
520 } else if (*pl
== 0x82) {
525 ZCRYPT_DBF_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
530 /* len should cover at least 3 fields with 32 bit value each */
532 ZCRYPT_DBF_ERR("%s reply length %d too small\n", func
, len
);
536 /* function tag, length and value */
537 if (pl
[0] != 0x04 || pl
[1] != 0x04) {
538 ZCRYPT_DBF_ERR("%s function tag or length mismatch\n", func
);
543 /* dom tag, length and value */
544 if (pl
[0] != 0x04 || pl
[1] != 0x04) {
545 ZCRYPT_DBF_ERR("%s dom tag or length mismatch\n", func
);
550 /* return value tag, length and value */
551 if (pl
[0] != 0x04 || pl
[1] != 0x04) {
552 ZCRYPT_DBF_ERR("%s return value tag or length mismatch\n",
559 ZCRYPT_DBF_ERR("%s return value 0x%08x != 0\n", func
, ret
);
566 /* Check ep11 reply cprb, return 0 or suggested errno value. */
567 static int check_reply_cprb(const struct ep11_cprb
*rep
, const char *func
)
569 /* check ep11 reply return code field */
571 ZCRYPT_DBF_ERR("%s ep11 reply ret_code=0x%08x\n", __func__
,
573 if (rep
->ret_code
== 0x000c0003)
583 * Helper function which does an ep11 query with given query type.
585 static int ep11_query_info(u16 cardnr
, u16 domain
, u32 query_type
,
586 size_t buflen
, u8
*buf
)
588 struct ep11_info_req_pl
{
593 u8 query_subtype_tag
;
594 u8 query_subtype_len
;
597 struct ep11_info_rep_pl
{
606 struct ep11_cprb
*req
= NULL
, *rep
= NULL
;
607 struct ep11_target_dev target
;
608 struct ep11_urb
*urb
= NULL
;
609 int api
= EP11_API_V1
, rc
= -ENOMEM
;
611 /* request cprb and payload */
612 req
= alloc_cprb(sizeof(struct ep11_info_req_pl
));
615 req_pl
= (struct ep11_info_req_pl
*)(((u8
*)req
) + sizeof(*req
));
616 prep_head(&req_pl
->head
, sizeof(*req_pl
), api
, 38); /* get xcp info */
617 req_pl
->query_type_tag
= 0x04;
618 req_pl
->query_type_len
= sizeof(u32
);
619 req_pl
->query_type
= query_type
;
620 req_pl
->query_subtype_tag
= 0x04;
621 req_pl
->query_subtype_len
= sizeof(u32
);
623 /* reply cprb and payload */
624 rep
= alloc_cprb(sizeof(struct ep11_info_rep_pl
) + buflen
);
627 rep_pl
= (struct ep11_info_rep_pl
*)(((u8
*)rep
) + sizeof(*rep
));
630 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
633 target
.ap_id
= cardnr
;
634 target
.dom_id
= domain
;
635 prep_urb(urb
, &target
, 1,
636 req
, sizeof(*req
) + sizeof(*req_pl
),
637 rep
, sizeof(*rep
) + sizeof(*rep_pl
) + buflen
);
639 rc
= zcrypt_send_ep11_cprb(urb
);
641 ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
642 __func__
, (int)cardnr
, (int)domain
, rc
);
646 /* check ep11 reply cprb */
647 rc
= check_reply_cprb(rep
, __func__
);
652 rc
= check_reply_pl((u8
*)rep_pl
, __func__
);
655 if (rep_pl
->data_tag
!= 0x04 || rep_pl
->data_lenfmt
!= 0x82) {
656 ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__
);
660 if (rep_pl
->data_len
> buflen
) {
661 ZCRYPT_DBF_ERR("%s mismatch between reply data len and buffer len\n",
667 memcpy(buf
, ((u8
*)rep_pl
) + sizeof(*rep_pl
), rep_pl
->data_len
);
677 * Provide information about an EP11 card.
679 int ep11_get_card_info(u16 card
, struct ep11_card_info
*info
, int verify
)
682 struct ep11_module_query_info
{
690 u8 xcp_config_hash
[32];
691 u8 CSP_config_hash
[32];
693 u8 module_date_time
[16];
699 u32 digest_state_bytes
;
702 u32 priv_key_blob_bytes
;
704 u32 max_payload_bytes
;
705 u32 CP_profile_bytes
;
707 } __packed
* pmqi
= NULL
;
709 rc
= card_cache_fetch(card
, info
);
711 pmqi
= kmalloc(sizeof(*pmqi
), GFP_KERNEL
);
714 rc
= ep11_query_info(card
, AUTOSEL_DOM
,
715 0x01 /* module info query */,
716 sizeof(*pmqi
), (u8
*)pmqi
);
719 card_cache_scrub(card
);
722 memset(info
, 0, sizeof(*info
));
723 info
->API_ord_nr
= pmqi
->API_ord_nr
;
725 (pmqi
->FW_major_vers
<< 8) + pmqi
->FW_minor_vers
;
726 memcpy(info
->serial
, pmqi
->serial
, sizeof(info
->serial
));
727 info
->op_mode
= pmqi
->op_mode
;
728 card_cache_update(card
, info
);
735 EXPORT_SYMBOL(ep11_get_card_info
);
738 * Provide information about a domain within an EP11 card.
740 int ep11_get_domain_info(u16 card
, u16 domain
, struct ep11_domain_info
*info
)
743 struct ep11_domain_query_info
{
749 } __packed
* p_dom_info
;
751 p_dom_info
= kmalloc(sizeof(*p_dom_info
), GFP_KERNEL
);
755 rc
= ep11_query_info(card
, domain
, 0x03 /* domain info query */,
756 sizeof(*p_dom_info
), (u8
*)p_dom_info
);
760 memset(info
, 0, sizeof(*info
));
761 info
->cur_wk_state
= '0';
762 info
->new_wk_state
= '0';
763 if (p_dom_info
->dom_flags
& 0x10 /* left imprint mode */) {
764 if (p_dom_info
->dom_flags
& 0x02 /* cur wk valid */) {
765 info
->cur_wk_state
= '1';
766 memcpy(info
->cur_wkvp
, p_dom_info
->cur_WK_VP
, 32);
768 if (p_dom_info
->dom_flags
& 0x04 || /* new wk present */
769 p_dom_info
->dom_flags
& 0x08 /* new wk committed */) {
771 p_dom_info
->dom_flags
& 0x08 ? '2' : '1';
772 memcpy(info
->new_wkvp
, p_dom_info
->new_WK_VP
, 32);
775 info
->op_mode
= p_dom_info
->op_mode
;
781 EXPORT_SYMBOL(ep11_get_domain_info
);
784 * Default EP11 AES key generate attributes, used when no keygenflags given:
785 * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
787 #define KEY_ATTR_DEFAULTS 0x00200c00
789 static int _ep11_genaeskey(u16 card
, u16 domain
,
790 u32 keybitsize
, u32 keygenflags
,
791 u8
*keybuf
, size_t *keybufsize
)
793 struct keygen_req_pl
{
809 u32 attr_val_len_type
;
810 u32 attr_val_len_value
;
811 /* followed by empty pin tag or empty pinblob tag */
813 struct keygen_rep_pl
{
823 struct ep11_cprb
*req
= NULL
, *rep
= NULL
;
824 size_t req_pl_size
, pinblob_size
= 0;
825 struct ep11_target_dev target
;
826 struct ep11_urb
*urb
= NULL
;
827 int api
, rc
= -ENOMEM
;
830 switch (keybitsize
) {
836 ZCRYPT_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
837 __func__
, keybitsize
);
842 /* request cprb and payload */
843 api
= (!keygenflags
|| keygenflags
& 0x00200000) ?
844 EP11_API_V4
: EP11_API_V1
;
845 if (ap_is_se_guest()) {
847 * genkey within SE environment requires API ordinal 6
851 pinblob_size
= EP11_PINBLOB_V1_BYTES
;
853 req_pl_size
= sizeof(struct keygen_req_pl
) + ASN1TAGLEN(pinblob_size
);
854 req
= alloc_cprb(req_pl_size
);
857 req_pl
= (struct keygen_req_pl
*)(((u8
*)req
) + sizeof(*req
));
858 prep_head(&req_pl
->head
, req_pl_size
, api
, 21); /* GenerateKey */
859 req_pl
->var_tag
= 0x04;
860 req_pl
->var_len
= sizeof(u32
);
861 req_pl
->keybytes_tag
= 0x04;
862 req_pl
->keybytes_len
= sizeof(u32
);
863 req_pl
->keybytes
= keybitsize
/ 8;
864 req_pl
->mech_tag
= 0x04;
865 req_pl
->mech_len
= sizeof(u32
);
866 req_pl
->mech
= 0x00001080; /* CKM_AES_KEY_GEN */
867 req_pl
->attr_tag
= 0x04;
868 req_pl
->attr_len
= 5 * sizeof(u32
);
869 req_pl
->attr_header
= 0x10010000;
870 req_pl
->attr_bool_mask
= keygenflags
? keygenflags
: KEY_ATTR_DEFAULTS
;
871 req_pl
->attr_bool_bits
= keygenflags
? keygenflags
: KEY_ATTR_DEFAULTS
;
872 req_pl
->attr_val_len_type
= 0x00000161; /* CKA_VALUE_LEN */
873 req_pl
->attr_val_len_value
= keybitsize
/ 8;
874 p
= ((u8
*)req_pl
) + sizeof(*req_pl
);
879 /* reply cprb and payload */
880 rep
= alloc_cprb(sizeof(struct keygen_rep_pl
));
883 rep_pl
= (struct keygen_rep_pl
*)(((u8
*)rep
) + sizeof(*rep
));
886 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
890 target
.dom_id
= domain
;
891 prep_urb(urb
, &target
, 1,
892 req
, sizeof(*req
) + req_pl_size
,
893 rep
, sizeof(*rep
) + sizeof(*rep_pl
));
895 rc
= zcrypt_send_ep11_cprb(urb
);
897 ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
898 __func__
, (int)card
, (int)domain
, rc
);
902 /* check ep11 reply cprb */
903 rc
= check_reply_cprb(rep
, __func__
);
908 rc
= check_reply_pl((u8
*)rep_pl
, __func__
);
911 if (rep_pl
->data_tag
!= 0x04 || rep_pl
->data_lenfmt
!= 0x82) {
912 ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__
);
916 if (rep_pl
->data_len
> *keybufsize
) {
917 ZCRYPT_DBF_ERR("%s mismatch reply data len / key buffer len\n",
924 memcpy(keybuf
, rep_pl
->data
, rep_pl
->data_len
);
925 *keybufsize
= rep_pl
->data_len
;
934 int ep11_genaeskey(u16 card
, u16 domain
, u32 keybitsize
, u32 keygenflags
,
935 u8
*keybuf
, u32
*keybufsize
, u32 keybufver
)
937 struct ep11kblob_header
*hdr
;
938 size_t hdr_size
, pl_size
;
943 case TOKVER_EP11_AES
:
944 case TOKVER_EP11_AES_WITH_HEADER
:
950 rc
= ep11_kb_split(keybuf
, *keybufsize
, keybufver
,
951 &hdr
, &hdr_size
, &pl
, &pl_size
);
955 rc
= _ep11_genaeskey(card
, domain
, keybitsize
, keygenflags
,
960 *keybufsize
= hdr_size
+ pl_size
;
962 /* update header information */
963 hdr
->type
= TOKTYPE_NON_CCA
;
964 hdr
->len
= *keybufsize
;
965 hdr
->version
= keybufver
;
966 hdr
->bitlen
= keybitsize
;
970 EXPORT_SYMBOL(ep11_genaeskey
);
972 static int ep11_cryptsingle(u16 card
, u16 domain
,
973 u16 mode
, u32 mech
, const u8
*iv
,
974 const u8
*key
, size_t keysize
,
975 const u8
*inbuf
, size_t inbufsize
,
976 u8
*outbuf
, size_t *outbufsize
)
978 struct crypt_req_pl
{
987 * maybe followed by iv data
988 * followed by key tag + key blob
989 * followed by plaintext tag + plaintext
992 struct crypt_rep_pl
{
1000 } __packed
* rep_pl
;
1001 struct ep11_cprb
*req
= NULL
, *rep
= NULL
;
1002 struct ep11_target_dev target
;
1003 struct ep11_urb
*urb
= NULL
;
1004 size_t req_pl_size
, rep_pl_size
;
1005 int n
, api
= EP11_API_V1
, rc
= -ENOMEM
;
1008 /* the simple asn1 coding used has length limits */
1009 if (keysize
> 0xFFFF || inbufsize
> 0xFFFF)
1012 /* request cprb and payload */
1013 req_pl_size
= sizeof(struct crypt_req_pl
) + (iv
? 16 : 0)
1014 + ASN1TAGLEN(keysize
) + ASN1TAGLEN(inbufsize
);
1015 req
= alloc_cprb(req_pl_size
);
1018 req_pl
= (struct crypt_req_pl
*)(((u8
*)req
) + sizeof(*req
));
1019 prep_head(&req_pl
->head
, req_pl_size
, api
, (mode
? 20 : 19));
1020 req_pl
->var_tag
= 0x04;
1021 req_pl
->var_len
= sizeof(u32
);
1022 /* mech is mech + mech params (iv here) */
1023 req_pl
->mech_tag
= 0x04;
1024 req_pl
->mech_len
= sizeof(u32
) + (iv
? 16 : 0);
1025 req_pl
->mech
= (mech
? mech
: 0x00001085); /* CKM_AES_CBC_PAD */
1026 p
= ((u8
*)req_pl
) + sizeof(*req_pl
);
1031 /* key and input data */
1032 p
+= asn1tag_write(p
, 0x04, key
, keysize
);
1033 p
+= asn1tag_write(p
, 0x04, inbuf
, inbufsize
);
1035 /* reply cprb and payload, assume out data size <= in data size + 32 */
1036 rep_pl_size
= sizeof(struct crypt_rep_pl
) + ASN1TAGLEN(inbufsize
+ 32);
1037 rep
= alloc_cprb(rep_pl_size
);
1040 rep_pl
= (struct crypt_rep_pl
*)(((u8
*)rep
) + sizeof(*rep
));
1042 /* urb and target */
1043 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
1046 target
.ap_id
= card
;
1047 target
.dom_id
= domain
;
1048 prep_urb(urb
, &target
, 1,
1049 req
, sizeof(*req
) + req_pl_size
,
1050 rep
, sizeof(*rep
) + rep_pl_size
);
1052 rc
= zcrypt_send_ep11_cprb(urb
);
1054 ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1055 __func__
, (int)card
, (int)domain
, rc
);
1059 /* check ep11 reply cprb */
1060 rc
= check_reply_cprb(rep
, __func__
);
1065 rc
= check_reply_pl((u8
*)rep_pl
, __func__
);
1068 if (rep_pl
->data_tag
!= 0x04) {
1069 ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__
);
1073 p
= ((u8
*)rep_pl
) + sizeof(*rep_pl
);
1074 if (rep_pl
->data_lenfmt
<= 127) {
1075 n
= rep_pl
->data_lenfmt
;
1076 } else if (rep_pl
->data_lenfmt
== 0x81) {
1078 } else if (rep_pl
->data_lenfmt
== 0x82) {
1082 ZCRYPT_DBF_ERR("%s unknown reply data length format 0x%02hhx\n",
1083 __func__
, rep_pl
->data_lenfmt
);
1087 if (n
> *outbufsize
) {
1088 ZCRYPT_DBF_ERR("%s mismatch reply data len %d / output buffer %zu\n",
1089 __func__
, n
, *outbufsize
);
1094 memcpy(outbuf
, p
, n
);
1104 static int _ep11_unwrapkey(u16 card
, u16 domain
,
1105 const u8
*kek
, size_t keksize
,
1106 const u8
*enckey
, size_t enckeysize
,
1107 u32 mech
, const u8
*iv
,
1108 u32 keybitsize
, u32 keygenflags
,
1109 u8
*keybuf
, size_t *keybufsize
)
1112 struct pl_head head
;
1119 u32 attr_key_type_value
;
1121 u32 attr_val_len_value
;
1126 * maybe followed by iv data
1127 * followed by kek tag + kek blob
1128 * followed by empty mac tag
1129 * followed by empty pin tag or empty pinblob tag
1130 * followed by encryted key tag + bytes
1132 } __packed
* req_pl
;
1134 struct pl_head head
;
1142 } __packed
* rep_pl
;
1143 struct ep11_cprb
*req
= NULL
, *rep
= NULL
;
1144 size_t req_pl_size
, pinblob_size
= 0;
1145 struct ep11_target_dev target
;
1146 struct ep11_urb
*urb
= NULL
;
1147 int api
, rc
= -ENOMEM
;
1150 /* request cprb and payload */
1151 api
= (!keygenflags
|| keygenflags
& 0x00200000) ?
1152 EP11_API_V4
: EP11_API_V1
;
1153 if (ap_is_se_guest()) {
1155 * unwrap within SE environment requires API ordinal 6
1156 * with empty pinblob
1159 pinblob_size
= EP11_PINBLOB_V1_BYTES
;
1161 req_pl_size
= sizeof(struct uw_req_pl
) + (iv
? 16 : 0)
1162 + ASN1TAGLEN(keksize
) + ASN1TAGLEN(0)
1163 + ASN1TAGLEN(pinblob_size
) + ASN1TAGLEN(enckeysize
);
1164 req
= alloc_cprb(req_pl_size
);
1167 req_pl
= (struct uw_req_pl
*)(((u8
*)req
) + sizeof(*req
));
1168 prep_head(&req_pl
->head
, req_pl_size
, api
, 34); /* UnwrapKey */
1169 req_pl
->attr_tag
= 0x04;
1170 req_pl
->attr_len
= 7 * sizeof(u32
);
1171 req_pl
->attr_header
= 0x10020000;
1172 req_pl
->attr_bool_mask
= keygenflags
? keygenflags
: KEY_ATTR_DEFAULTS
;
1173 req_pl
->attr_bool_bits
= keygenflags
? keygenflags
: KEY_ATTR_DEFAULTS
;
1174 req_pl
->attr_key_type
= 0x00000100; /* CKA_KEY_TYPE */
1175 req_pl
->attr_key_type_value
= 0x0000001f; /* CKK_AES */
1176 req_pl
->attr_val_len
= 0x00000161; /* CKA_VALUE_LEN */
1177 req_pl
->attr_val_len_value
= keybitsize
/ 8;
1178 /* mech is mech + mech params (iv here) */
1179 req_pl
->mech_tag
= 0x04;
1180 req_pl
->mech_len
= sizeof(u32
) + (iv
? 16 : 0);
1181 req_pl
->mech
= (mech
? mech
: 0x00001085); /* CKM_AES_CBC_PAD */
1182 p
= ((u8
*)req_pl
) + sizeof(*req_pl
);
1188 p
+= asn1tag_write(p
, 0x04, kek
, keksize
);
1189 /* empty mac key tag */
1194 *p
++ = pinblob_size
;
1196 /* encrypted key value tag and bytes */
1197 p
+= asn1tag_write(p
, 0x04, enckey
, enckeysize
);
1199 /* reply cprb and payload */
1200 rep
= alloc_cprb(sizeof(struct uw_rep_pl
));
1203 rep_pl
= (struct uw_rep_pl
*)(((u8
*)rep
) + sizeof(*rep
));
1205 /* urb and target */
1206 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
1209 target
.ap_id
= card
;
1210 target
.dom_id
= domain
;
1211 prep_urb(urb
, &target
, 1,
1212 req
, sizeof(*req
) + req_pl_size
,
1213 rep
, sizeof(*rep
) + sizeof(*rep_pl
));
1215 rc
= zcrypt_send_ep11_cprb(urb
);
1217 ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1218 __func__
, (int)card
, (int)domain
, rc
);
1222 /* check ep11 reply cprb */
1223 rc
= check_reply_cprb(rep
, __func__
);
1228 rc
= check_reply_pl((u8
*)rep_pl
, __func__
);
1231 if (rep_pl
->data_tag
!= 0x04 || rep_pl
->data_lenfmt
!= 0x82) {
1232 ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__
);
1236 if (rep_pl
->data_len
> *keybufsize
) {
1237 ZCRYPT_DBF_ERR("%s mismatch reply data len / key buffer len\n",
1244 memcpy(keybuf
, rep_pl
->data
, rep_pl
->data_len
);
1245 *keybufsize
= rep_pl
->data_len
;
1254 static int ep11_unwrapkey(u16 card
, u16 domain
,
1255 const u8
*kek
, size_t keksize
,
1256 const u8
*enckey
, size_t enckeysize
,
1257 u32 mech
, const u8
*iv
,
1258 u32 keybitsize
, u32 keygenflags
,
1259 u8
*keybuf
, u32
*keybufsize
,
1262 struct ep11kblob_header
*hdr
;
1263 size_t hdr_size
, pl_size
;
1267 rc
= ep11_kb_split(keybuf
, *keybufsize
, keybufver
,
1268 &hdr
, &hdr_size
, &pl
, &pl_size
);
1272 rc
= _ep11_unwrapkey(card
, domain
, kek
, keksize
, enckey
, enckeysize
,
1273 mech
, iv
, keybitsize
, keygenflags
,
1278 *keybufsize
= hdr_size
+ pl_size
;
1280 /* update header information */
1281 hdr
= (struct ep11kblob_header
*)keybuf
;
1282 hdr
->type
= TOKTYPE_NON_CCA
;
1283 hdr
->len
= *keybufsize
;
1284 hdr
->version
= keybufver
;
1285 hdr
->bitlen
= keybitsize
;
1290 static int _ep11_wrapkey(u16 card
, u16 domain
,
1291 const u8
*key
, size_t keysize
,
1292 u32 mech
, const u8
*iv
,
1293 u8
*databuf
, size_t *datasize
)
1296 struct pl_head head
;
1304 * followed by iv data
1305 * followed by key tag + key blob
1306 * followed by dummy kek param
1307 * followed by dummy mac param
1309 } __packed
* req_pl
;
1311 struct pl_head head
;
1319 } __packed
* rep_pl
;
1320 struct ep11_cprb
*req
= NULL
, *rep
= NULL
;
1321 struct ep11_target_dev target
;
1322 struct ep11_urb
*urb
= NULL
;
1324 int api
, rc
= -ENOMEM
;
1327 /* request cprb and payload */
1328 req_pl_size
= sizeof(struct wk_req_pl
) + (iv
? 16 : 0)
1329 + ASN1TAGLEN(keysize
) + 4;
1330 req
= alloc_cprb(req_pl_size
);
1333 if (!mech
|| mech
== 0x80060001)
1334 req
->flags
|= 0x20; /* CPACF_WRAP needs special bit */
1335 req_pl
= (struct wk_req_pl
*)(((u8
*)req
) + sizeof(*req
));
1336 api
= (!mech
|| mech
== 0x80060001) ? /* CKM_IBM_CPACF_WRAP */
1337 EP11_API_V4
: EP11_API_V1
;
1338 prep_head(&req_pl
->head
, req_pl_size
, api
, 33); /* WrapKey */
1339 req_pl
->var_tag
= 0x04;
1340 req_pl
->var_len
= sizeof(u32
);
1341 /* mech is mech + mech params (iv here) */
1342 req_pl
->mech_tag
= 0x04;
1343 req_pl
->mech_len
= sizeof(u32
) + (iv
? 16 : 0);
1344 req_pl
->mech
= (mech
? mech
: 0x80060001); /* CKM_IBM_CPACF_WRAP */
1345 p
= ((u8
*)req_pl
) + sizeof(*req_pl
);
1351 p
+= asn1tag_write(p
, 0x04, key
, keysize
);
1359 /* reply cprb and payload */
1360 rep
= alloc_cprb(sizeof(struct wk_rep_pl
));
1363 rep_pl
= (struct wk_rep_pl
*)(((u8
*)rep
) + sizeof(*rep
));
1365 /* urb and target */
1366 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
1369 target
.ap_id
= card
;
1370 target
.dom_id
= domain
;
1371 prep_urb(urb
, &target
, 1,
1372 req
, sizeof(*req
) + req_pl_size
,
1373 rep
, sizeof(*rep
) + sizeof(*rep_pl
));
1375 rc
= zcrypt_send_ep11_cprb(urb
);
1377 ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1378 __func__
, (int)card
, (int)domain
, rc
);
1382 /* check ep11 reply cprb */
1383 rc
= check_reply_cprb(rep
, __func__
);
1388 rc
= check_reply_pl((u8
*)rep_pl
, __func__
);
1391 if (rep_pl
->data_tag
!= 0x04 || rep_pl
->data_lenfmt
!= 0x82) {
1392 ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__
);
1396 if (rep_pl
->data_len
> *datasize
) {
1397 ZCRYPT_DBF_ERR("%s mismatch reply data len / data buffer len\n",
1403 /* copy the data from the cprb to the data buffer */
1404 memcpy(databuf
, rep_pl
->data
, rep_pl
->data_len
);
1405 *datasize
= rep_pl
->data_len
;
1414 int ep11_clr2keyblob(u16 card
, u16 domain
, u32 keybitsize
, u32 keygenflags
,
1415 const u8
*clrkey
, u8
*keybuf
, u32
*keybufsize
,
1419 u8 encbuf
[64], *kek
= NULL
;
1420 size_t clrkeylen
, keklen
, encbuflen
= sizeof(encbuf
);
1422 if (keybitsize
== 128 || keybitsize
== 192 || keybitsize
== 256) {
1423 clrkeylen
= keybitsize
/ 8;
1425 ZCRYPT_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
1426 __func__
, keybitsize
);
1430 /* allocate memory for the temp kek */
1431 keklen
= MAXEP11AESKEYBLOBSIZE
;
1432 kek
= kmalloc(keklen
, GFP_ATOMIC
);
1438 /* Step 1: generate AES 256 bit random kek key */
1439 rc
= _ep11_genaeskey(card
, domain
, 256,
1440 0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1443 ZCRYPT_DBF_ERR("%s generate kek key failed, rc=%d\n",
1448 /* Step 2: encrypt clear key value with the kek key */
1449 rc
= ep11_cryptsingle(card
, domain
, 0, 0, def_iv
, kek
, keklen
,
1450 clrkey
, clrkeylen
, encbuf
, &encbuflen
);
1452 ZCRYPT_DBF_ERR("%s encrypting key value with kek key failed, rc=%d\n",
1457 /* Step 3: import the encrypted key value as a new key */
1458 rc
= ep11_unwrapkey(card
, domain
, kek
, keklen
,
1459 encbuf
, encbuflen
, 0, def_iv
,
1460 keybitsize
, 0, keybuf
, keybufsize
, keytype
);
1462 ZCRYPT_DBF_ERR("%s importing key value as new key failed,, rc=%d\n",
1471 EXPORT_SYMBOL(ep11_clr2keyblob
);
1473 int ep11_kblob2protkey(u16 card
, u16 dom
,
1474 const u8
*keyblob
, u32 keybloblen
,
1475 u8
*protkey
, u32
*protkeylen
, u32
*protkeytype
)
1477 struct ep11kblob_header
*hdr
;
1478 struct ep11keyblob
*key
;
1479 size_t wkbuflen
, keylen
;
1492 if (ep11_kb_decode((u8
*)keyblob
, keybloblen
, &hdr
, NULL
, &key
, &keylen
))
1495 if (hdr
->version
== TOKVER_EP11_AES
) {
1496 /* wipe overlayed header */
1497 memset(hdr
, 0, sizeof(*hdr
));
1499 /* !!! hdr is no longer a valid header !!! */
1501 /* alloc temp working buffer */
1502 wkbuflen
= (keylen
+ AES_BLOCK_SIZE
) & (~(AES_BLOCK_SIZE
- 1));
1503 wkbuf
= kmalloc(wkbuflen
, GFP_ATOMIC
);
1507 /* ep11 secure key -> protected key + info */
1508 rc
= _ep11_wrapkey(card
, dom
, (u8
*)key
, keylen
,
1509 0, def_iv
, wkbuf
, &wkbuflen
);
1511 ZCRYPT_DBF_ERR("%s rewrapping ep11 key to pkey failed, rc=%d\n",
1515 wki
= (struct wk_info
*)wkbuf
;
1517 /* check struct version and pkey type */
1518 if (wki
->version
!= 1 || wki
->pkeytype
< 1 || wki
->pkeytype
> 5) {
1519 ZCRYPT_DBF_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
1520 __func__
, (int)wki
->version
, (int)wki
->pkeytype
);
1525 /* check protected key type field */
1526 switch (wki
->pkeytype
) {
1528 switch (wki
->pkeysize
) {
1530 /* AES 128 protected key */
1532 *protkeytype
= PKEY_KEYTYPE_AES_128
;
1535 /* AES 192 protected key */
1537 *protkeytype
= PKEY_KEYTYPE_AES_192
;
1540 /* AES 256 protected key */
1542 *protkeytype
= PKEY_KEYTYPE_AES_256
;
1545 ZCRYPT_DBF_ERR("%s unknown/unsupported AES pkeysize %d\n",
1546 __func__
, (int)wki
->pkeysize
);
1555 *protkeytype
= PKEY_KEYTYPE_ECC
;
1559 ZCRYPT_DBF_ERR("%s unknown/unsupported key type %d\n",
1560 __func__
, (int)wki
->pkeytype
);
1565 /* copy the translated protected key */
1566 if (wki
->pkeysize
> *protkeylen
) {
1567 ZCRYPT_DBF_ERR("%s wk info pkeysize %llu > protkeysize %u\n",
1568 __func__
, wki
->pkeysize
, *protkeylen
);
1572 memcpy(protkey
, wki
->pkey
, wki
->pkeysize
);
1573 *protkeylen
= wki
->pkeysize
;
1579 EXPORT_SYMBOL(ep11_kblob2protkey
);
1581 int ep11_findcard2(u32
**apqns
, u32
*nr_apqns
, u16 cardnr
, u16 domain
,
1582 int minhwtype
, int minapi
, const u8
*wkvp
)
1584 struct zcrypt_device_status_ext
*device_status
;
1585 u32
*_apqns
= NULL
, _nr_apqns
= 0;
1586 int i
, card
, dom
, rc
= -ENOMEM
;
1587 struct ep11_domain_info edi
;
1588 struct ep11_card_info eci
;
1590 /* fetch status of all crypto cards */
1591 device_status
= kvcalloc(MAX_ZDEV_ENTRIES_EXT
,
1592 sizeof(struct zcrypt_device_status_ext
),
1596 zcrypt_device_status_mask_ext(device_status
);
1598 /* allocate 1k space for up to 256 apqns */
1599 _apqns
= kmalloc_array(256, sizeof(u32
), GFP_KERNEL
);
1601 kvfree(device_status
);
1605 /* walk through all the crypto apqnss */
1606 for (i
= 0; i
< MAX_ZDEV_ENTRIES_EXT
; i
++) {
1607 card
= AP_QID_CARD(device_status
[i
].qid
);
1608 dom
= AP_QID_QUEUE(device_status
[i
].qid
);
1609 /* check online state */
1610 if (!device_status
[i
].online
)
1612 /* check for ep11 functions */
1613 if (!(device_status
[i
].functions
& 0x01))
1616 if (cardnr
!= 0xFFFF && card
!= cardnr
)
1619 if (domain
!= 0xFFFF && dom
!= domain
)
1621 /* check min hardware type */
1622 if (minhwtype
&& device_status
[i
].hwtype
< minhwtype
)
1624 /* check min api version if given */
1626 if (ep11_get_card_info(card
, &eci
, 0))
1628 if (minapi
> eci
.API_ord_nr
)
1631 /* check wkvp if given */
1633 if (ep11_get_domain_info(card
, dom
, &edi
))
1635 if (edi
.cur_wk_state
!= '1')
1637 if (memcmp(wkvp
, edi
.cur_wkvp
, 16))
1640 /* apqn passed all filtering criterons, add to the array */
1641 if (_nr_apqns
< 256)
1642 _apqns
[_nr_apqns
++] = (((u16
)card
) << 16) | ((u16
)dom
);
1645 /* nothing found ? */
1650 /* no re-allocation, simple return the _apqns array */
1652 *nr_apqns
= _nr_apqns
;
1656 kvfree(device_status
);
1659 EXPORT_SYMBOL(ep11_findcard2
);
1661 void __exit
zcrypt_ep11misc_exit(void)