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 DEBUG_DBG(...) ZCRYPT_DBF(DBF_DEBUG, ##__VA_ARGS__)
28 #define DEBUG_INFO(...) ZCRYPT_DBF(DBF_INFO, ##__VA_ARGS__)
29 #define DEBUG_WARN(...) ZCRYPT_DBF(DBF_WARN, ##__VA_ARGS__)
30 #define DEBUG_ERR(...) ZCRYPT_DBF(DBF_ERR, ##__VA_ARGS__)
32 /* default iv used here */
33 static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
34 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
36 /* ep11 card info cache */
37 struct card_list_entry {
38 struct list_head list;
40 struct ep11_card_info info;
42 static LIST_HEAD(card_list);
43 static DEFINE_SPINLOCK(card_list_lock);
45 static int card_cache_fetch(u16 cardnr, struct ep11_card_info *ci)
48 struct card_list_entry *ptr;
50 spin_lock_bh(&card_list_lock);
51 list_for_each_entry(ptr, &card_list, list) {
52 if (ptr->cardnr == cardnr) {
53 memcpy(ci, &ptr->info, sizeof(*ci));
58 spin_unlock_bh(&card_list_lock);
63 static void card_cache_update(u16 cardnr, const struct ep11_card_info *ci)
66 struct card_list_entry *ptr;
68 spin_lock_bh(&card_list_lock);
69 list_for_each_entry(ptr, &card_list, list) {
70 if (ptr->cardnr == cardnr) {
71 memcpy(&ptr->info, ci, sizeof(*ci));
77 ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
79 spin_unlock_bh(&card_list_lock);
83 memcpy(&ptr->info, ci, sizeof(*ci));
84 list_add(&ptr->list, &card_list);
86 spin_unlock_bh(&card_list_lock);
89 static void card_cache_scrub(u16 cardnr)
91 struct card_list_entry *ptr;
93 spin_lock_bh(&card_list_lock);
94 list_for_each_entry(ptr, &card_list, list) {
95 if (ptr->cardnr == cardnr) {
101 spin_unlock_bh(&card_list_lock);
104 static void __exit card_cache_free(void)
106 struct card_list_entry *ptr, *pnext;
108 spin_lock_bh(&card_list_lock);
109 list_for_each_entry_safe(ptr, pnext, &card_list, list) {
110 list_del(&ptr->list);
113 spin_unlock_bh(&card_list_lock);
117 * Simple check if the key blob is a valid EP11 AES key blob with header.
119 int ep11_check_aes_key_with_hdr(debug_info_t *dbg, int dbflvl,
120 const u8 *key, size_t keylen, int checkcpacfexp)
122 struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
123 struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
125 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
127 if (keylen < sizeof(*hdr) + sizeof(*kb)) {
128 DBF("%s key check failed, keylen %zu < %zu\n",
129 __func__, keylen, sizeof(*hdr) + sizeof(*kb));
133 if (hdr->type != TOKTYPE_NON_CCA) {
135 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
136 __func__, (int)hdr->type, TOKTYPE_NON_CCA);
139 if (hdr->hver != 0x00) {
141 DBF("%s key check failed, header version 0x%02x != 0x00\n",
142 __func__, (int)hdr->hver);
145 if (hdr->version != TOKVER_EP11_AES_WITH_HEADER) {
147 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
148 __func__, (int)hdr->version, TOKVER_EP11_AES_WITH_HEADER);
151 if (hdr->len > keylen) {
153 DBF("%s key check failed, header len %d keylen %zu mismatch\n",
154 __func__, (int)hdr->len, keylen);
157 if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
159 DBF("%s key check failed, header len %d < %zu\n",
160 __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
164 if (kb->version != EP11_STRUCT_MAGIC) {
166 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
167 __func__, (int)kb->version, EP11_STRUCT_MAGIC);
170 if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
172 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
181 EXPORT_SYMBOL(ep11_check_aes_key_with_hdr);
184 * Simple check if the key blob is a valid EP11 ECC key blob with header.
186 int ep11_check_ecc_key_with_hdr(debug_info_t *dbg, int dbflvl,
187 const u8 *key, size_t keylen, int checkcpacfexp)
189 struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
190 struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
192 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
194 if (keylen < sizeof(*hdr) + sizeof(*kb)) {
195 DBF("%s key check failed, keylen %zu < %zu\n",
196 __func__, keylen, sizeof(*hdr) + sizeof(*kb));
200 if (hdr->type != TOKTYPE_NON_CCA) {
202 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
203 __func__, (int)hdr->type, TOKTYPE_NON_CCA);
206 if (hdr->hver != 0x00) {
208 DBF("%s key check failed, header version 0x%02x != 0x00\n",
209 __func__, (int)hdr->hver);
212 if (hdr->version != TOKVER_EP11_ECC_WITH_HEADER) {
214 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
215 __func__, (int)hdr->version, TOKVER_EP11_ECC_WITH_HEADER);
218 if (hdr->len > keylen) {
220 DBF("%s key check failed, header len %d keylen %zu mismatch\n",
221 __func__, (int)hdr->len, keylen);
224 if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
226 DBF("%s key check failed, header len %d < %zu\n",
227 __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
231 if (kb->version != EP11_STRUCT_MAGIC) {
233 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
234 __func__, (int)kb->version, EP11_STRUCT_MAGIC);
237 if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
239 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
248 EXPORT_SYMBOL(ep11_check_ecc_key_with_hdr);
251 * Simple check if the key blob is a valid EP11 AES key blob with
252 * the header in the session field (old style EP11 AES key).
254 int ep11_check_aes_key(debug_info_t *dbg, int dbflvl,
255 const u8 *key, size_t keylen, int checkcpacfexp)
257 struct ep11keyblob *kb = (struct ep11keyblob *)key;
259 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
261 if (keylen < sizeof(*kb)) {
262 DBF("%s key check failed, keylen %zu < %zu\n",
263 __func__, keylen, sizeof(*kb));
267 if (kb->head.type != TOKTYPE_NON_CCA) {
269 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
270 __func__, (int)kb->head.type, TOKTYPE_NON_CCA);
273 if (kb->head.version != TOKVER_EP11_AES) {
275 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
276 __func__, (int)kb->head.version, TOKVER_EP11_AES);
279 if (kb->head.len > keylen) {
281 DBF("%s key check failed, header len %d keylen %zu mismatch\n",
282 __func__, (int)kb->head.len, keylen);
285 if (kb->head.len < sizeof(*kb)) {
287 DBF("%s key check failed, header len %d < %zu\n",
288 __func__, (int)kb->head.len, sizeof(*kb));
292 if (kb->version != EP11_STRUCT_MAGIC) {
294 DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
295 __func__, (int)kb->version, EP11_STRUCT_MAGIC);
298 if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
300 DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
309 EXPORT_SYMBOL(ep11_check_aes_key);
312 * Allocate and prepare ep11 cprb plus additional payload.
314 static inline struct ep11_cprb *alloc_cprb(size_t payload_len)
316 size_t len = sizeof(struct ep11_cprb) + payload_len;
317 struct ep11_cprb *cprb;
319 cprb = kzalloc(len, GFP_KERNEL);
323 cprb->cprb_len = sizeof(struct ep11_cprb);
324 cprb->cprb_ver_id = 0x04;
325 memcpy(cprb->func_id, "T4", 2);
326 cprb->ret_code = 0xFFFFFFFF;
327 cprb->payload_len = payload_len;
333 * Some helper functions related to ASN1 encoding.
334 * Limited to length info <= 2 byte.
337 #define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
339 static int asn1tag_write(u8 *ptr, u8 tag, const u8 *pvalue, u16 valuelen)
342 if (valuelen > 255) {
344 *((u16 *)(ptr + 2)) = valuelen;
345 memcpy(ptr + 4, pvalue, valuelen);
348 if (valuelen > 127) {
350 ptr[2] = (u8)valuelen;
351 memcpy(ptr + 3, pvalue, valuelen);
354 ptr[1] = (u8)valuelen;
355 memcpy(ptr + 2, pvalue, valuelen);
359 /* EP11 payload > 127 bytes starts with this struct */
372 /* prep ep11 payload head helper function */
373 static inline void prep_head(struct pl_head *h,
374 size_t pl_size, int api, int func)
378 h->len = pl_size - 4;
380 h->func_len = sizeof(u32);
381 h->func = (api << 16) + func;
383 h->dom_len = sizeof(u32);
386 /* prep urb helper function */
387 static inline void prep_urb(struct ep11_urb *u,
388 struct ep11_target_dev *t, int nt,
389 struct ep11_cprb *req, size_t req_len,
390 struct ep11_cprb *rep, size_t rep_len)
392 u->targets = (u8 __user *)t;
394 u->req = (u8 __user *)req;
395 u->req_len = req_len;
396 u->resp = (u8 __user *)rep;
397 u->resp_len = rep_len;
400 /* Check ep11 reply payload, return 0 or suggested errno value. */
401 static int check_reply_pl(const u8 *pl, const char *func)
408 DEBUG_ERR("%s reply start tag mismatch\n", func);
412 /* payload length format */
416 } else if (*pl == 0x81) {
420 } else if (*pl == 0x82) {
425 DEBUG_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
430 /* len should cover at least 3 fields with 32 bit value each */
432 DEBUG_ERR("%s reply length %d too small\n", func, len);
436 /* function tag, length and value */
437 if (pl[0] != 0x04 || pl[1] != 0x04) {
438 DEBUG_ERR("%s function tag or length mismatch\n", func);
443 /* dom tag, length and value */
444 if (pl[0] != 0x04 || pl[1] != 0x04) {
445 DEBUG_ERR("%s dom tag or length mismatch\n", func);
450 /* return value tag, length and value */
451 if (pl[0] != 0x04 || pl[1] != 0x04) {
452 DEBUG_ERR("%s return value tag or length mismatch\n", func);
458 DEBUG_ERR("%s return value 0x%04x != 0\n", func, ret);
466 * Helper function which does an ep11 query with given query type.
468 static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
469 size_t buflen, u8 *buf)
471 struct ep11_info_req_pl {
476 u8 query_subtype_tag;
477 u8 query_subtype_len;
480 struct ep11_info_rep_pl {
489 struct ep11_cprb *req = NULL, *rep = NULL;
490 struct ep11_target_dev target;
491 struct ep11_urb *urb = NULL;
492 int api = 1, rc = -ENOMEM;
494 /* request cprb and payload */
495 req = alloc_cprb(sizeof(struct ep11_info_req_pl));
498 req_pl = (struct ep11_info_req_pl *)(((u8 *)req) + sizeof(*req));
499 prep_head(&req_pl->head, sizeof(*req_pl), api, 38); /* get xcp info */
500 req_pl->query_type_tag = 0x04;
501 req_pl->query_type_len = sizeof(u32);
502 req_pl->query_type = query_type;
503 req_pl->query_subtype_tag = 0x04;
504 req_pl->query_subtype_len = sizeof(u32);
506 /* reply cprb and payload */
507 rep = alloc_cprb(sizeof(struct ep11_info_rep_pl) + buflen);
510 rep_pl = (struct ep11_info_rep_pl *)(((u8 *)rep) + sizeof(*rep));
513 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
516 target.ap_id = cardnr;
517 target.dom_id = domain;
518 prep_urb(urb, &target, 1,
519 req, sizeof(*req) + sizeof(*req_pl),
520 rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
522 rc = zcrypt_send_ep11_cprb(urb);
525 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
526 __func__, (int)cardnr, (int)domain, rc);
530 rc = check_reply_pl((u8 *)rep_pl, __func__);
533 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
534 DEBUG_ERR("%s unknown reply data format\n", __func__);
538 if (rep_pl->data_len > buflen) {
539 DEBUG_ERR("%s mismatch between reply data len and buffer len\n",
545 memcpy(buf, ((u8 *)rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
555 * Provide information about an EP11 card.
557 int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify)
560 struct ep11_module_query_info {
568 u8 xcp_config_hash[32];
569 u8 CSP_config_hash[32];
571 u8 module_date_time[16];
577 u32 digest_state_bytes;
580 u32 priv_key_blob_bytes;
582 u32 max_payload_bytes;
583 u32 CP_profile_bytes;
585 } __packed * pmqi = NULL;
587 rc = card_cache_fetch(card, info);
589 pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
592 rc = ep11_query_info(card, AUTOSEL_DOM,
593 0x01 /* module info query */,
594 sizeof(*pmqi), (u8 *)pmqi);
597 card_cache_scrub(card);
600 memset(info, 0, sizeof(*info));
601 info->API_ord_nr = pmqi->API_ord_nr;
603 (pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
604 memcpy(info->serial, pmqi->serial, sizeof(info->serial));
605 info->op_mode = pmqi->op_mode;
606 card_cache_update(card, info);
613 EXPORT_SYMBOL(ep11_get_card_info);
616 * Provide information about a domain within an EP11 card.
618 int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
621 struct ep11_domain_query_info {
627 } __packed * p_dom_info;
629 p_dom_info = kmalloc(sizeof(*p_dom_info), GFP_KERNEL);
633 rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
634 sizeof(*p_dom_info), (u8 *)p_dom_info);
638 memset(info, 0, sizeof(*info));
639 info->cur_wk_state = '0';
640 info->new_wk_state = '0';
641 if (p_dom_info->dom_flags & 0x10 /* left imprint mode */) {
642 if (p_dom_info->dom_flags & 0x02 /* cur wk valid */) {
643 info->cur_wk_state = '1';
644 memcpy(info->cur_wkvp, p_dom_info->cur_WK_VP, 32);
646 if (p_dom_info->dom_flags & 0x04 || /* new wk present */
647 p_dom_info->dom_flags & 0x08 /* new wk committed */) {
649 p_dom_info->dom_flags & 0x08 ? '2' : '1';
650 memcpy(info->new_wkvp, p_dom_info->new_WK_VP, 32);
653 info->op_mode = p_dom_info->op_mode;
659 EXPORT_SYMBOL(ep11_get_domain_info);
662 * Default EP11 AES key generate attributes, used when no keygenflags given:
663 * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
665 #define KEY_ATTR_DEFAULTS 0x00200c00
667 int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
668 u8 *keybuf, size_t *keybufsize)
670 struct keygen_req_pl {
686 u32 attr_val_len_type;
687 u32 attr_val_len_value;
691 struct keygen_rep_pl {
701 struct ep11_cprb *req = NULL, *rep = NULL;
702 struct ep11_target_dev target;
703 struct ep11_urb *urb = NULL;
704 struct ep11keyblob *kb;
705 int api, rc = -ENOMEM;
707 switch (keybitsize) {
714 "%s unknown/unsupported keybitsize %d\n",
715 __func__, keybitsize);
720 /* request cprb and payload */
721 req = alloc_cprb(sizeof(struct keygen_req_pl));
724 req_pl = (struct keygen_req_pl *)(((u8 *)req) + sizeof(*req));
725 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
726 prep_head(&req_pl->head, sizeof(*req_pl), api, 21); /* GenerateKey */
727 req_pl->var_tag = 0x04;
728 req_pl->var_len = sizeof(u32);
729 req_pl->keybytes_tag = 0x04;
730 req_pl->keybytes_len = sizeof(u32);
731 req_pl->keybytes = keybitsize / 8;
732 req_pl->mech_tag = 0x04;
733 req_pl->mech_len = sizeof(u32);
734 req_pl->mech = 0x00001080; /* CKM_AES_KEY_GEN */
735 req_pl->attr_tag = 0x04;
736 req_pl->attr_len = 5 * sizeof(u32);
737 req_pl->attr_header = 0x10010000;
738 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
739 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
740 req_pl->attr_val_len_type = 0x00000161; /* CKA_VALUE_LEN */
741 req_pl->attr_val_len_value = keybitsize / 8;
742 req_pl->pin_tag = 0x04;
744 /* reply cprb and payload */
745 rep = alloc_cprb(sizeof(struct keygen_rep_pl));
748 rep_pl = (struct keygen_rep_pl *)(((u8 *)rep) + sizeof(*rep));
751 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
755 target.dom_id = domain;
756 prep_urb(urb, &target, 1,
757 req, sizeof(*req) + sizeof(*req_pl),
758 rep, sizeof(*rep) + sizeof(*rep_pl));
760 rc = zcrypt_send_ep11_cprb(urb);
763 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
764 __func__, (int)card, (int)domain, rc);
768 rc = check_reply_pl((u8 *)rep_pl, __func__);
771 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
772 DEBUG_ERR("%s unknown reply data format\n", __func__);
776 if (rep_pl->data_len > *keybufsize) {
777 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
783 /* copy key blob and set header values */
784 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
785 *keybufsize = rep_pl->data_len;
786 kb = (struct ep11keyblob *)keybuf;
787 kb->head.type = TOKTYPE_NON_CCA;
788 kb->head.len = rep_pl->data_len;
789 kb->head.version = TOKVER_EP11_AES;
790 kb->head.keybitlen = keybitsize;
798 EXPORT_SYMBOL(ep11_genaeskey);
800 static int ep11_cryptsingle(u16 card, u16 domain,
801 u16 mode, u32 mech, const u8 *iv,
802 const u8 *key, size_t keysize,
803 const u8 *inbuf, size_t inbufsize,
804 u8 *outbuf, size_t *outbufsize)
806 struct crypt_req_pl {
815 * maybe followed by iv data
816 * followed by key tag + key blob
817 * followed by plaintext tag + plaintext
820 struct crypt_rep_pl {
829 struct ep11_cprb *req = NULL, *rep = NULL;
830 struct ep11_target_dev target;
831 struct ep11_urb *urb = NULL;
832 size_t req_pl_size, rep_pl_size;
833 int n, api = 1, rc = -ENOMEM;
836 /* the simple asn1 coding used has length limits */
837 if (keysize > 0xFFFF || inbufsize > 0xFFFF)
840 /* request cprb and payload */
841 req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
842 + ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
843 req = alloc_cprb(req_pl_size);
846 req_pl = (struct crypt_req_pl *)(((u8 *)req) + sizeof(*req));
847 prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
848 req_pl->var_tag = 0x04;
849 req_pl->var_len = sizeof(u32);
850 /* mech is mech + mech params (iv here) */
851 req_pl->mech_tag = 0x04;
852 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
853 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
854 p = ((u8 *)req_pl) + sizeof(*req_pl);
859 /* key and input data */
860 p += asn1tag_write(p, 0x04, key, keysize);
861 p += asn1tag_write(p, 0x04, inbuf, inbufsize);
863 /* reply cprb and payload, assume out data size <= in data size + 32 */
864 rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
865 rep = alloc_cprb(rep_pl_size);
868 rep_pl = (struct crypt_rep_pl *)(((u8 *)rep) + sizeof(*rep));
871 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
875 target.dom_id = domain;
876 prep_urb(urb, &target, 1,
877 req, sizeof(*req) + req_pl_size,
878 rep, sizeof(*rep) + rep_pl_size);
880 rc = zcrypt_send_ep11_cprb(urb);
883 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
884 __func__, (int)card, (int)domain, rc);
888 rc = check_reply_pl((u8 *)rep_pl, __func__);
891 if (rep_pl->data_tag != 0x04) {
892 DEBUG_ERR("%s unknown reply data format\n", __func__);
896 p = ((u8 *)rep_pl) + sizeof(*rep_pl);
897 if (rep_pl->data_lenfmt <= 127) {
898 n = rep_pl->data_lenfmt;
899 } else if (rep_pl->data_lenfmt == 0x81) {
901 } else if (rep_pl->data_lenfmt == 0x82) {
905 DEBUG_ERR("%s unknown reply data length format 0x%02hhx\n",
906 __func__, rep_pl->data_lenfmt);
910 if (n > *outbufsize) {
911 DEBUG_ERR("%s mismatch reply data len %d / output buffer %zu\n",
912 __func__, n, *outbufsize);
917 memcpy(outbuf, p, n);
927 static int ep11_unwrapkey(u16 card, u16 domain,
928 const u8 *kek, size_t keksize,
929 const u8 *enckey, size_t enckeysize,
930 u32 mech, const u8 *iv,
931 u32 keybitsize, u32 keygenflags,
932 u8 *keybuf, size_t *keybufsize)
942 u32 attr_key_type_value;
944 u32 attr_val_len_value;
949 * maybe followed by iv data
950 * followed by kek tag + kek blob
951 * followed by empty mac tag
952 * followed by empty pin tag
953 * followed by encryted key tag + bytes
966 struct ep11_cprb *req = NULL, *rep = NULL;
967 struct ep11_target_dev target;
968 struct ep11_urb *urb = NULL;
969 struct ep11keyblob *kb;
971 int api, rc = -ENOMEM;
974 /* request cprb and payload */
975 req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
976 + ASN1TAGLEN(keksize) + 4 + ASN1TAGLEN(enckeysize);
977 req = alloc_cprb(req_pl_size);
980 req_pl = (struct uw_req_pl *)(((u8 *)req) + sizeof(*req));
981 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
982 prep_head(&req_pl->head, req_pl_size, api, 34); /* UnwrapKey */
983 req_pl->attr_tag = 0x04;
984 req_pl->attr_len = 7 * sizeof(u32);
985 req_pl->attr_header = 0x10020000;
986 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
987 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
988 req_pl->attr_key_type = 0x00000100; /* CKA_KEY_TYPE */
989 req_pl->attr_key_type_value = 0x0000001f; /* CKK_AES */
990 req_pl->attr_val_len = 0x00000161; /* CKA_VALUE_LEN */
991 req_pl->attr_val_len_value = keybitsize / 8;
992 /* mech is mech + mech params (iv here) */
993 req_pl->mech_tag = 0x04;
994 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
995 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
996 p = ((u8 *)req_pl) + sizeof(*req_pl);
1002 p += asn1tag_write(p, 0x04, kek, keksize);
1003 /* empty mac key tag */
1009 /* encrypted key value tag and bytes */
1010 p += asn1tag_write(p, 0x04, enckey, enckeysize);
1012 /* reply cprb and payload */
1013 rep = alloc_cprb(sizeof(struct uw_rep_pl));
1016 rep_pl = (struct uw_rep_pl *)(((u8 *)rep) + sizeof(*rep));
1018 /* urb and target */
1019 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1022 target.ap_id = card;
1023 target.dom_id = domain;
1024 prep_urb(urb, &target, 1,
1025 req, sizeof(*req) + req_pl_size,
1026 rep, sizeof(*rep) + sizeof(*rep_pl));
1028 rc = zcrypt_send_ep11_cprb(urb);
1031 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1032 __func__, (int)card, (int)domain, rc);
1036 rc = check_reply_pl((u8 *)rep_pl, __func__);
1039 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1040 DEBUG_ERR("%s unknown reply data format\n", __func__);
1044 if (rep_pl->data_len > *keybufsize) {
1045 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
1051 /* copy key blob and set header values */
1052 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
1053 *keybufsize = rep_pl->data_len;
1054 kb = (struct ep11keyblob *)keybuf;
1055 kb->head.type = TOKTYPE_NON_CCA;
1056 kb->head.len = rep_pl->data_len;
1057 kb->head.version = TOKVER_EP11_AES;
1058 kb->head.keybitlen = keybitsize;
1067 static int ep11_wrapkey(u16 card, u16 domain,
1068 const u8 *key, size_t keysize,
1069 u32 mech, const u8 *iv,
1070 u8 *databuf, size_t *datasize)
1073 struct pl_head head;
1081 * followed by iv data
1082 * followed by key tag + key blob
1083 * followed by dummy kek param
1084 * followed by dummy mac param
1086 } __packed * req_pl;
1088 struct pl_head head;
1096 } __packed * rep_pl;
1097 struct ep11_cprb *req = NULL, *rep = NULL;
1098 struct ep11_target_dev target;
1099 struct ep11_urb *urb = NULL;
1100 struct ep11keyblob *kb;
1102 int api, rc = -ENOMEM;
1103 bool has_header = false;
1106 /* maybe the session field holds a header with key info */
1107 kb = (struct ep11keyblob *)key;
1108 if (kb->head.type == TOKTYPE_NON_CCA &&
1109 kb->head.version == TOKVER_EP11_AES) {
1111 keysize = min_t(size_t, kb->head.len, keysize);
1114 /* request cprb and payload */
1115 req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
1116 + ASN1TAGLEN(keysize) + 4;
1117 req = alloc_cprb(req_pl_size);
1120 if (!mech || mech == 0x80060001)
1121 req->flags |= 0x20; /* CPACF_WRAP needs special bit */
1122 req_pl = (struct wk_req_pl *)(((u8 *)req) + sizeof(*req));
1123 api = (!mech || mech == 0x80060001) ? 4 : 1; /* CKM_IBM_CPACF_WRAP */
1124 prep_head(&req_pl->head, req_pl_size, api, 33); /* WrapKey */
1125 req_pl->var_tag = 0x04;
1126 req_pl->var_len = sizeof(u32);
1127 /* mech is mech + mech params (iv here) */
1128 req_pl->mech_tag = 0x04;
1129 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1130 req_pl->mech = (mech ? mech : 0x80060001); /* CKM_IBM_CPACF_WRAP */
1131 p = ((u8 *)req_pl) + sizeof(*req_pl);
1137 p += asn1tag_write(p, 0x04, key, keysize);
1138 /* maybe the key argument needs the head data cleaned out */
1140 kb = (struct ep11keyblob *)(p - keysize);
1141 memset(&kb->head, 0, sizeof(kb->head));
1150 /* reply cprb and payload */
1151 rep = alloc_cprb(sizeof(struct wk_rep_pl));
1154 rep_pl = (struct wk_rep_pl *)(((u8 *)rep) + sizeof(*rep));
1156 /* urb and target */
1157 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1160 target.ap_id = card;
1161 target.dom_id = domain;
1162 prep_urb(urb, &target, 1,
1163 req, sizeof(*req) + req_pl_size,
1164 rep, sizeof(*rep) + sizeof(*rep_pl));
1166 rc = zcrypt_send_ep11_cprb(urb);
1169 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1170 __func__, (int)card, (int)domain, rc);
1174 rc = check_reply_pl((u8 *)rep_pl, __func__);
1177 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1178 DEBUG_ERR("%s unknown reply data format\n", __func__);
1182 if (rep_pl->data_len > *datasize) {
1183 DEBUG_ERR("%s mismatch reply data len / data buffer len\n",
1189 /* copy the data from the cprb to the data buffer */
1190 memcpy(databuf, rep_pl->data, rep_pl->data_len);
1191 *datasize = rep_pl->data_len;
1200 int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
1201 const u8 *clrkey, u8 *keybuf, size_t *keybufsize)
1204 struct ep11keyblob *kb;
1205 u8 encbuf[64], *kek = NULL;
1206 size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
1208 if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256) {
1209 clrkeylen = keybitsize / 8;
1212 "%s unknown/unsupported keybitsize %d\n",
1213 __func__, keybitsize);
1217 /* allocate memory for the temp kek */
1218 keklen = MAXEP11AESKEYBLOBSIZE;
1219 kek = kmalloc(keklen, GFP_ATOMIC);
1225 /* Step 1: generate AES 256 bit random kek key */
1226 rc = ep11_genaeskey(card, domain, 256,
1227 0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1231 "%s generate kek key failed, rc=%d\n",
1235 kb = (struct ep11keyblob *)kek;
1236 memset(&kb->head, 0, sizeof(kb->head));
1238 /* Step 2: encrypt clear key value with the kek key */
1239 rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
1240 clrkey, clrkeylen, encbuf, &encbuflen);
1243 "%s encrypting key value with kek key failed, rc=%d\n",
1248 /* Step 3: import the encrypted key value as a new key */
1249 rc = ep11_unwrapkey(card, domain, kek, keklen,
1250 encbuf, encbuflen, 0, def_iv,
1251 keybitsize, 0, keybuf, keybufsize);
1254 "%s importing key value as new key failed,, rc=%d\n",
1263 EXPORT_SYMBOL(ep11_clr2keyblob);
1265 int ep11_kblob2protkey(u16 card, u16 dom, const u8 *keyblob, size_t keybloblen,
1266 u8 *protkey, u32 *protkeylen, u32 *protkeytype)
1270 size_t wkbuflen, keylen;
1281 struct ep11kblob_header *hdr;
1283 /* key with or without header ? */
1284 hdr = (struct ep11kblob_header *)keyblob;
1285 if (hdr->type == TOKTYPE_NON_CCA &&
1286 (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
1287 hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
1288 is_ep11_keyblob(keyblob + sizeof(struct ep11kblob_header))) {
1289 /* EP11 AES or ECC key with header */
1290 key = keyblob + sizeof(struct ep11kblob_header);
1291 keylen = hdr->len - sizeof(struct ep11kblob_header);
1292 } else if (hdr->type == TOKTYPE_NON_CCA &&
1293 hdr->version == TOKVER_EP11_AES &&
1294 is_ep11_keyblob(keyblob)) {
1295 /* EP11 AES key (old style) */
1298 } else if (is_ep11_keyblob(keyblob)) {
1299 /* raw EP11 key blob */
1301 keylen = keybloblen;
1306 /* alloc temp working buffer */
1307 wkbuflen = (keylen + AES_BLOCK_SIZE) & (~(AES_BLOCK_SIZE - 1));
1308 wkbuf = kmalloc(wkbuflen, GFP_ATOMIC);
1312 /* ep11 secure key -> protected key + info */
1313 rc = ep11_wrapkey(card, dom, key, keylen,
1314 0, def_iv, wkbuf, &wkbuflen);
1317 "%s rewrapping ep11 key to pkey failed, rc=%d\n",
1321 wki = (struct wk_info *)wkbuf;
1323 /* check struct version and pkey type */
1324 if (wki->version != 1 || wki->pkeytype < 1 || wki->pkeytype > 5) {
1325 DEBUG_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
1326 __func__, (int)wki->version, (int)wki->pkeytype);
1331 /* check protected key type field */
1332 switch (wki->pkeytype) {
1334 switch (wki->pkeysize) {
1336 /* AES 128 protected key */
1338 *protkeytype = PKEY_KEYTYPE_AES_128;
1341 /* AES 192 protected key */
1343 *protkeytype = PKEY_KEYTYPE_AES_192;
1346 /* AES 256 protected key */
1348 *protkeytype = PKEY_KEYTYPE_AES_256;
1351 DEBUG_ERR("%s unknown/unsupported AES pkeysize %d\n",
1352 __func__, (int)wki->pkeysize);
1361 *protkeytype = PKEY_KEYTYPE_ECC;
1365 DEBUG_ERR("%s unknown/unsupported key type %d\n",
1366 __func__, (int)wki->pkeytype);
1371 /* copy the tanslated protected key */
1372 if (wki->pkeysize > *protkeylen) {
1373 DEBUG_ERR("%s wk info pkeysize %llu > protkeysize %u\n",
1374 __func__, wki->pkeysize, *protkeylen);
1378 memcpy(protkey, wki->pkey, wki->pkeysize);
1379 *protkeylen = wki->pkeysize;
1385 EXPORT_SYMBOL(ep11_kblob2protkey);
1387 int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1388 int minhwtype, int minapi, const u8 *wkvp)
1390 struct zcrypt_device_status_ext *device_status;
1391 u32 *_apqns = NULL, _nr_apqns = 0;
1392 int i, card, dom, rc = -ENOMEM;
1393 struct ep11_domain_info edi;
1394 struct ep11_card_info eci;
1396 /* fetch status of all crypto cards */
1397 device_status = kvmalloc_array(MAX_ZDEV_ENTRIES_EXT,
1398 sizeof(struct zcrypt_device_status_ext),
1402 zcrypt_device_status_mask_ext(device_status);
1404 /* allocate 1k space for up to 256 apqns */
1405 _apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
1407 kvfree(device_status);
1411 /* walk through all the crypto apqnss */
1412 for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
1413 card = AP_QID_CARD(device_status[i].qid);
1414 dom = AP_QID_QUEUE(device_status[i].qid);
1415 /* check online state */
1416 if (!device_status[i].online)
1418 /* check for ep11 functions */
1419 if (!(device_status[i].functions & 0x01))
1422 if (cardnr != 0xFFFF && card != cardnr)
1425 if (domain != 0xFFFF && dom != domain)
1427 /* check min hardware type */
1428 if (minhwtype && device_status[i].hwtype < minhwtype)
1430 /* check min api version if given */
1432 if (ep11_get_card_info(card, &eci, 0))
1434 if (minapi > eci.API_ord_nr)
1437 /* check wkvp if given */
1439 if (ep11_get_domain_info(card, dom, &edi))
1441 if (edi.cur_wk_state != '1')
1443 if (memcmp(wkvp, edi.cur_wkvp, 16))
1446 /* apqn passed all filtering criterons, add to the array */
1447 if (_nr_apqns < 256)
1448 _apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
1451 /* nothing found ? */
1456 /* no re-allocation, simple return the _apqns array */
1458 *nr_apqns = _nr_apqns;
1462 kvfree(device_status);
1465 EXPORT_SYMBOL(ep11_findcard2);
1467 void __exit zcrypt_ep11misc_exit(void)