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>
20 #include "zcrypt_api.h"
21 #include "zcrypt_debug.h"
22 #include "zcrypt_msgtype6.h"
23 #include "zcrypt_ep11misc.h"
24 #include "zcrypt_ccamisc.h"
26 #define DEBUG_DBG(...) ZCRYPT_DBF(DBF_DEBUG, ##__VA_ARGS__)
27 #define DEBUG_INFO(...) ZCRYPT_DBF(DBF_INFO, ##__VA_ARGS__)
28 #define DEBUG_WARN(...) ZCRYPT_DBF(DBF_WARN, ##__VA_ARGS__)
29 #define DEBUG_ERR(...) ZCRYPT_DBF(DBF_ERR, ##__VA_ARGS__)
31 /* default iv used here */
32 static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
33 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
35 /* ep11 card info cache */
36 struct card_list_entry {
37 struct list_head list;
39 struct ep11_card_info info;
41 static LIST_HEAD(card_list);
42 static DEFINE_SPINLOCK(card_list_lock);
44 static int card_cache_fetch(u16 cardnr, struct ep11_card_info *ci)
47 struct card_list_entry *ptr;
49 spin_lock_bh(&card_list_lock);
50 list_for_each_entry(ptr, &card_list, list) {
51 if (ptr->cardnr == cardnr) {
52 memcpy(ci, &ptr->info, sizeof(*ci));
57 spin_unlock_bh(&card_list_lock);
62 static void card_cache_update(u16 cardnr, const struct ep11_card_info *ci)
65 struct card_list_entry *ptr;
67 spin_lock_bh(&card_list_lock);
68 list_for_each_entry(ptr, &card_list, list) {
69 if (ptr->cardnr == cardnr) {
70 memcpy(&ptr->info, ci, sizeof(*ci));
76 ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
78 spin_unlock_bh(&card_list_lock);
82 memcpy(&ptr->info, ci, sizeof(*ci));
83 list_add(&ptr->list, &card_list);
85 spin_unlock_bh(&card_list_lock);
88 static void card_cache_scrub(u16 cardnr)
90 struct card_list_entry *ptr;
92 spin_lock_bh(&card_list_lock);
93 list_for_each_entry(ptr, &card_list, list) {
94 if (ptr->cardnr == cardnr) {
100 spin_unlock_bh(&card_list_lock);
103 static void __exit card_cache_free(void)
105 struct card_list_entry *ptr, *pnext;
107 spin_lock_bh(&card_list_lock);
108 list_for_each_entry_safe(ptr, pnext, &card_list, list) {
109 list_del(&ptr->list);
112 spin_unlock_bh(&card_list_lock);
116 * Simple check if the key blob is a valid EP11 secure AES key.
118 int ep11_check_aeskeyblob(debug_info_t *dbg, int dbflvl,
119 const u8 *key, int keybitsize,
120 int checkcpacfexport)
122 struct ep11keyblob *kb = (struct ep11keyblob *) key;
124 #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
126 if (kb->head.type != TOKTYPE_NON_CCA) {
128 DBF("%s key check failed, type 0x%02x != 0x%02x\n",
129 __func__, (int) kb->head.type, TOKTYPE_NON_CCA);
132 if (kb->head.version != TOKVER_EP11_AES) {
134 DBF("%s key check failed, version 0x%02x != 0x%02x\n",
135 __func__, (int) kb->head.version, TOKVER_EP11_AES);
138 if (kb->version != EP11_STRUCT_MAGIC) {
140 DBF("%s key check failed, magic 0x%04x != 0x%04x\n",
141 __func__, (int) kb->version, EP11_STRUCT_MAGIC);
144 switch (kb->head.keybitlen) {
151 DBF("%s key check failed, keybitlen %d invalid\n",
152 __func__, (int) kb->head.keybitlen);
155 if (keybitsize > 0 && keybitsize != (int) kb->head.keybitlen) {
156 DBF("%s key check failed, keybitsize %d\n",
157 __func__, keybitsize);
160 if (checkcpacfexport && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
162 DBF("%s key check failed, PKEY_EXTRACTABLE is 0\n",
170 EXPORT_SYMBOL(ep11_check_aeskeyblob);
173 * Helper function which calls zcrypt_send_ep11_cprb with
174 * memory management segment adjusted to kernel space
175 * so that the copy_from_user called within this
176 * function do in fact copy from kernel space.
178 static inline int _zcrypt_send_ep11_cprb(struct ep11_urb *urb)
181 mm_segment_t old_fs = get_fs();
184 rc = zcrypt_send_ep11_cprb(urb);
191 * Allocate and prepare ep11 cprb plus additional payload.
193 static inline struct ep11_cprb *alloc_cprb(size_t payload_len)
195 size_t len = sizeof(struct ep11_cprb) + payload_len;
196 struct ep11_cprb *cprb;
198 cprb = kmalloc(len, GFP_KERNEL);
202 memset(cprb, 0, len);
203 cprb->cprb_len = sizeof(struct ep11_cprb);
204 cprb->cprb_ver_id = 0x04;
205 memcpy(cprb->func_id, "T4", 2);
206 cprb->ret_code = 0xFFFFFFFF;
207 cprb->payload_len = payload_len;
213 * Some helper functions related to ASN1 encoding.
214 * Limited to length info <= 2 byte.
217 #define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
219 static int asn1tag_write(u8 *ptr, u8 tag, const u8 *pvalue, u16 valuelen)
222 if (valuelen > 255) {
224 *((u16 *)(ptr + 2)) = valuelen;
225 memcpy(ptr + 4, pvalue, valuelen);
228 if (valuelen > 127) {
230 ptr[2] = (u8) valuelen;
231 memcpy(ptr + 3, pvalue, valuelen);
234 ptr[1] = (u8) valuelen;
235 memcpy(ptr + 2, pvalue, valuelen);
239 /* EP11 payload > 127 bytes starts with this struct */
252 /* prep ep11 payload head helper function */
253 static inline void prep_head(struct pl_head *h,
254 size_t pl_size, int api, int func)
258 h->len = pl_size - 4;
260 h->func_len = sizeof(u32);
261 h->func = (api << 16) + func;
263 h->dom_len = sizeof(u32);
266 /* prep urb helper function */
267 static inline void prep_urb(struct ep11_urb *u,
268 struct ep11_target_dev *t, int nt,
269 struct ep11_cprb *req, size_t req_len,
270 struct ep11_cprb *rep, size_t rep_len)
272 u->targets = (u8 __user *) t;
274 u->req = (u8 __user *) req;
275 u->req_len = req_len;
276 u->resp = (u8 __user *) rep;
277 u->resp_len = rep_len;
280 /* Check ep11 reply payload, return 0 or suggested errno value. */
281 static int check_reply_pl(const u8 *pl, const char *func)
288 DEBUG_ERR("%s reply start tag mismatch\n", func);
292 /* payload length format */
296 } else if (*pl == 0x81) {
300 } else if (*pl == 0x82) {
305 DEBUG_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
310 /* len should cover at least 3 fields with 32 bit value each */
312 DEBUG_ERR("%s reply length %d too small\n", func, len);
316 /* function tag, length and value */
317 if (pl[0] != 0x04 || pl[1] != 0x04) {
318 DEBUG_ERR("%s function tag or length mismatch\n", func);
323 /* dom tag, length and value */
324 if (pl[0] != 0x04 || pl[1] != 0x04) {
325 DEBUG_ERR("%s dom tag or length mismatch\n", func);
330 /* return value tag, length and value */
331 if (pl[0] != 0x04 || pl[1] != 0x04) {
332 DEBUG_ERR("%s return value tag or length mismatch\n", func);
338 DEBUG_ERR("%s return value 0x%04x != 0\n", func, ret);
347 * Helper function which does an ep11 query with given query type.
349 static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
350 size_t buflen, u8 *buf)
352 struct ep11_info_req_pl {
357 u8 query_subtype_tag;
358 u8 query_subtype_len;
361 struct ep11_info_rep_pl {
370 struct ep11_cprb *req = NULL, *rep = NULL;
371 struct ep11_target_dev target;
372 struct ep11_urb *urb = NULL;
373 int api = 1, rc = -ENOMEM;
375 /* request cprb and payload */
376 req = alloc_cprb(sizeof(struct ep11_info_req_pl));
379 req_pl = (struct ep11_info_req_pl *) (((u8 *) req) + sizeof(*req));
380 prep_head(&req_pl->head, sizeof(*req_pl), api, 38); /* get xcp info */
381 req_pl->query_type_tag = 0x04;
382 req_pl->query_type_len = sizeof(u32);
383 req_pl->query_type = query_type;
384 req_pl->query_subtype_tag = 0x04;
385 req_pl->query_subtype_len = sizeof(u32);
387 /* reply cprb and payload */
388 rep = alloc_cprb(sizeof(struct ep11_info_rep_pl) + buflen);
391 rep_pl = (struct ep11_info_rep_pl *) (((u8 *) rep) + sizeof(*rep));
394 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
397 target.ap_id = cardnr;
398 target.dom_id = domain;
399 prep_urb(urb, &target, 1,
400 req, sizeof(*req) + sizeof(*req_pl),
401 rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
403 rc = _zcrypt_send_ep11_cprb(urb);
406 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
407 __func__, (int) cardnr, (int) domain, rc);
411 rc = check_reply_pl((u8 *)rep_pl, __func__);
414 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
415 DEBUG_ERR("%s unknown reply data format\n", __func__);
419 if (rep_pl->data_len > buflen) {
420 DEBUG_ERR("%s mismatch between reply data len and buffer len\n",
426 memcpy(buf, ((u8 *) rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
436 * Provide information about an EP11 card.
438 int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify)
441 struct ep11_module_query_info {
449 u8 xcp_config_hash[32];
450 u8 CSP_config_hash[32];
452 u8 module_date_time[16];
458 u32 digest_state_bytes;
461 u32 priv_key_blob_bytes;
463 u32 max_payload_bytes;
464 u32 CP_profile_bytes;
466 } __packed * pmqi = NULL;
468 rc = card_cache_fetch(card, info);
470 pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
473 rc = ep11_query_info(card, AUTOSEL_DOM,
474 0x01 /* module info query */,
475 sizeof(*pmqi), (u8 *) pmqi);
478 card_cache_scrub(card);
481 memset(info, 0, sizeof(*info));
482 info->API_ord_nr = pmqi->API_ord_nr;
484 (pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
485 memcpy(info->serial, pmqi->serial, sizeof(info->serial));
486 info->op_mode = pmqi->op_mode;
487 card_cache_update(card, info);
494 EXPORT_SYMBOL(ep11_get_card_info);
497 * Provide information about a domain within an EP11 card.
499 int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
502 struct ep11_domain_query_info {
508 } __packed * p_dom_info;
510 p_dom_info = kmalloc(sizeof(*p_dom_info), GFP_KERNEL);
514 rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
515 sizeof(*p_dom_info), (u8 *) p_dom_info);
519 memset(info, 0, sizeof(*info));
520 info->cur_wk_state = '0';
521 info->new_wk_state = '0';
522 if (p_dom_info->dom_flags & 0x10 /* left imprint mode */) {
523 if (p_dom_info->dom_flags & 0x02 /* cur wk valid */) {
524 info->cur_wk_state = '1';
525 memcpy(info->cur_wkvp, p_dom_info->cur_WK_VP, 32);
527 if (p_dom_info->dom_flags & 0x04 /* new wk present */
528 || p_dom_info->dom_flags & 0x08 /* new wk committed */) {
530 p_dom_info->dom_flags & 0x08 ? '2' : '1';
531 memcpy(info->new_wkvp, p_dom_info->new_WK_VP, 32);
534 info->op_mode = p_dom_info->op_mode;
540 EXPORT_SYMBOL(ep11_get_domain_info);
543 * Default EP11 AES key generate attributes, used when no keygenflags given:
544 * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
546 #define KEY_ATTR_DEFAULTS 0x00200c00
548 int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
549 u8 *keybuf, size_t *keybufsize)
551 struct keygen_req_pl {
567 u32 attr_val_len_type;
568 u32 attr_val_len_value;
572 struct keygen_rep_pl {
582 struct ep11_cprb *req = NULL, *rep = NULL;
583 struct ep11_target_dev target;
584 struct ep11_urb *urb = NULL;
585 struct ep11keyblob *kb;
586 int api, rc = -ENOMEM;
588 switch (keybitsize) {
595 "%s unknown/unsupported keybitsize %d\n",
596 __func__, keybitsize);
601 /* request cprb and payload */
602 req = alloc_cprb(sizeof(struct keygen_req_pl));
605 req_pl = (struct keygen_req_pl *) (((u8 *) req) + sizeof(*req));
606 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
607 prep_head(&req_pl->head, sizeof(*req_pl), api, 21); /* GenerateKey */
608 req_pl->var_tag = 0x04;
609 req_pl->var_len = sizeof(u32);
610 req_pl->keybytes_tag = 0x04;
611 req_pl->keybytes_len = sizeof(u32);
612 req_pl->keybytes = keybitsize / 8;
613 req_pl->mech_tag = 0x04;
614 req_pl->mech_len = sizeof(u32);
615 req_pl->mech = 0x00001080; /* CKM_AES_KEY_GEN */
616 req_pl->attr_tag = 0x04;
617 req_pl->attr_len = 5 * sizeof(u32);
618 req_pl->attr_header = 0x10010000;
619 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
620 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
621 req_pl->attr_val_len_type = 0x00000161; /* CKA_VALUE_LEN */
622 req_pl->attr_val_len_value = keybitsize / 8;
623 req_pl->pin_tag = 0x04;
625 /* reply cprb and payload */
626 rep = alloc_cprb(sizeof(struct keygen_rep_pl));
629 rep_pl = (struct keygen_rep_pl *) (((u8 *) rep) + sizeof(*rep));
632 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
636 target.dom_id = domain;
637 prep_urb(urb, &target, 1,
638 req, sizeof(*req) + sizeof(*req_pl),
639 rep, sizeof(*rep) + sizeof(*rep_pl));
641 rc = _zcrypt_send_ep11_cprb(urb);
644 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
645 __func__, (int) card, (int) domain, rc);
649 rc = check_reply_pl((u8 *)rep_pl, __func__);
652 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
653 DEBUG_ERR("%s unknown reply data format\n", __func__);
657 if (rep_pl->data_len > *keybufsize) {
658 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
664 /* copy key blob and set header values */
665 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
666 *keybufsize = rep_pl->data_len;
667 kb = (struct ep11keyblob *) keybuf;
668 kb->head.type = TOKTYPE_NON_CCA;
669 kb->head.len = rep_pl->data_len;
670 kb->head.version = TOKVER_EP11_AES;
671 kb->head.keybitlen = keybitsize;
679 EXPORT_SYMBOL(ep11_genaeskey);
681 static int ep11_cryptsingle(u16 card, u16 domain,
682 u16 mode, u32 mech, const u8 *iv,
683 const u8 *key, size_t keysize,
684 const u8 *inbuf, size_t inbufsize,
685 u8 *outbuf, size_t *outbufsize)
687 struct crypt_req_pl {
696 * maybe followed by iv data
697 * followed by key tag + key blob
698 * followed by plaintext tag + plaintext
701 struct crypt_rep_pl {
710 struct ep11_cprb *req = NULL, *rep = NULL;
711 struct ep11_target_dev target;
712 struct ep11_urb *urb = NULL;
713 size_t req_pl_size, rep_pl_size;
714 int n, api = 1, rc = -ENOMEM;
717 /* the simple asn1 coding used has length limits */
718 if (keysize > 0xFFFF || inbufsize > 0xFFFF)
721 /* request cprb and payload */
722 req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
723 + ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
724 req = alloc_cprb(req_pl_size);
727 req_pl = (struct crypt_req_pl *) (((u8 *) req) + sizeof(*req));
728 prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
729 req_pl->var_tag = 0x04;
730 req_pl->var_len = sizeof(u32);
731 /* mech is mech + mech params (iv here) */
732 req_pl->mech_tag = 0x04;
733 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
734 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
735 p = ((u8 *) req_pl) + sizeof(*req_pl);
740 /* key and input data */
741 p += asn1tag_write(p, 0x04, key, keysize);
742 p += asn1tag_write(p, 0x04, inbuf, inbufsize);
744 /* reply cprb and payload, assume out data size <= in data size + 32 */
745 rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
746 rep = alloc_cprb(rep_pl_size);
749 rep_pl = (struct crypt_rep_pl *) (((u8 *) rep) + sizeof(*rep));
752 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
756 target.dom_id = domain;
757 prep_urb(urb, &target, 1,
758 req, sizeof(*req) + req_pl_size,
759 rep, sizeof(*rep) + rep_pl_size);
761 rc = _zcrypt_send_ep11_cprb(urb);
764 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
765 __func__, (int) card, (int) domain, rc);
769 rc = check_reply_pl((u8 *)rep_pl, __func__);
772 if (rep_pl->data_tag != 0x04) {
773 DEBUG_ERR("%s unknown reply data format\n", __func__);
777 p = ((u8 *) rep_pl) + sizeof(*rep_pl);
778 if (rep_pl->data_lenfmt <= 127)
779 n = rep_pl->data_lenfmt;
780 else if (rep_pl->data_lenfmt == 0x81)
782 else if (rep_pl->data_lenfmt == 0x82) {
786 DEBUG_ERR("%s unknown reply data length format 0x%02hhx\n",
787 __func__, rep_pl->data_lenfmt);
791 if (n > *outbufsize) {
792 DEBUG_ERR("%s mismatch reply data len %d / output buffer %zu\n",
793 __func__, n, *outbufsize);
798 memcpy(outbuf, p, n);
808 static int ep11_unwrapkey(u16 card, u16 domain,
809 const u8 *kek, size_t keksize,
810 const u8 *enckey, size_t enckeysize,
811 u32 mech, const u8 *iv,
812 u32 keybitsize, u32 keygenflags,
813 u8 *keybuf, size_t *keybufsize)
823 u32 attr_key_type_value;
825 u32 attr_val_len_value;
830 * maybe followed by iv data
831 * followed by kek tag + kek blob
832 * followed by empty mac tag
833 * followed by empty pin tag
834 * followed by encryted key tag + bytes
847 struct ep11_cprb *req = NULL, *rep = NULL;
848 struct ep11_target_dev target;
849 struct ep11_urb *urb = NULL;
850 struct ep11keyblob *kb;
852 int api, rc = -ENOMEM;
855 /* request cprb and payload */
856 req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
857 + ASN1TAGLEN(keksize) + 4 + ASN1TAGLEN(enckeysize);
858 req = alloc_cprb(req_pl_size);
861 req_pl = (struct uw_req_pl *) (((u8 *) req) + sizeof(*req));
862 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
863 prep_head(&req_pl->head, req_pl_size, api, 34); /* UnwrapKey */
864 req_pl->attr_tag = 0x04;
865 req_pl->attr_len = 7 * sizeof(u32);
866 req_pl->attr_header = 0x10020000;
867 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
868 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
869 req_pl->attr_key_type = 0x00000100; /* CKA_KEY_TYPE */
870 req_pl->attr_key_type_value = 0x0000001f; /* CKK_AES */
871 req_pl->attr_val_len = 0x00000161; /* CKA_VALUE_LEN */
872 req_pl->attr_val_len_value = keybitsize / 8;
873 /* mech is mech + mech params (iv here) */
874 req_pl->mech_tag = 0x04;
875 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
876 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
877 p = ((u8 *) req_pl) + sizeof(*req_pl);
883 p += asn1tag_write(p, 0x04, kek, keksize);
884 /* empty mac key tag */
890 /* encrypted key value tag and bytes */
891 p += asn1tag_write(p, 0x04, enckey, enckeysize);
893 /* reply cprb and payload */
894 rep = alloc_cprb(sizeof(struct uw_rep_pl));
897 rep_pl = (struct uw_rep_pl *) (((u8 *) rep) + sizeof(*rep));
900 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
904 target.dom_id = domain;
905 prep_urb(urb, &target, 1,
906 req, sizeof(*req) + req_pl_size,
907 rep, sizeof(*rep) + sizeof(*rep_pl));
909 rc = _zcrypt_send_ep11_cprb(urb);
912 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
913 __func__, (int) card, (int) domain, rc);
917 rc = check_reply_pl((u8 *)rep_pl, __func__);
920 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
921 DEBUG_ERR("%s unknown reply data format\n", __func__);
925 if (rep_pl->data_len > *keybufsize) {
926 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
932 /* copy key blob and set header values */
933 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
934 *keybufsize = rep_pl->data_len;
935 kb = (struct ep11keyblob *) keybuf;
936 kb->head.type = TOKTYPE_NON_CCA;
937 kb->head.len = rep_pl->data_len;
938 kb->head.version = TOKVER_EP11_AES;
939 kb->head.keybitlen = keybitsize;
948 static int ep11_wrapkey(u16 card, u16 domain,
949 const u8 *key, size_t keysize,
950 u32 mech, const u8 *iv,
951 u8 *databuf, size_t *datasize)
962 * followed by iv data
963 * followed by key tag + key blob
964 * followed by dummy kek param
965 * followed by dummy mac param
978 struct ep11_cprb *req = NULL, *rep = NULL;
979 struct ep11_target_dev target;
980 struct ep11_urb *urb = NULL;
981 struct ep11keyblob *kb;
983 int api, rc = -ENOMEM;
986 /* request cprb and payload */
987 req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
988 + ASN1TAGLEN(keysize) + 4;
989 req = alloc_cprb(req_pl_size);
992 if (!mech || mech == 0x80060001)
993 req->flags |= 0x20; /* CPACF_WRAP needs special bit */
994 req_pl = (struct wk_req_pl *) (((u8 *) req) + sizeof(*req));
995 api = (!mech || mech == 0x80060001) ? 4 : 1; /* CKM_IBM_CPACF_WRAP */
996 prep_head(&req_pl->head, req_pl_size, api, 33); /* WrapKey */
997 req_pl->var_tag = 0x04;
998 req_pl->var_len = sizeof(u32);
999 /* mech is mech + mech params (iv here) */
1000 req_pl->mech_tag = 0x04;
1001 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1002 req_pl->mech = (mech ? mech : 0x80060001); /* CKM_IBM_CPACF_WRAP */
1003 p = ((u8 *) req_pl) + sizeof(*req_pl);
1009 p += asn1tag_write(p, 0x04, key, keysize);
1010 /* maybe the key argument needs the head data cleaned out */
1011 kb = (struct ep11keyblob *)(p - keysize);
1012 if (kb->head.version == TOKVER_EP11_AES)
1013 memset(&kb->head, 0, sizeof(kb->head));
1021 /* reply cprb and payload */
1022 rep = alloc_cprb(sizeof(struct wk_rep_pl));
1025 rep_pl = (struct wk_rep_pl *) (((u8 *) rep) + sizeof(*rep));
1027 /* urb and target */
1028 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
1031 target.ap_id = card;
1032 target.dom_id = domain;
1033 prep_urb(urb, &target, 1,
1034 req, sizeof(*req) + req_pl_size,
1035 rep, sizeof(*rep) + sizeof(*rep_pl));
1037 rc = _zcrypt_send_ep11_cprb(urb);
1040 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1041 __func__, (int) card, (int) domain, rc);
1045 rc = check_reply_pl((u8 *)rep_pl, __func__);
1048 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1049 DEBUG_ERR("%s unknown reply data format\n", __func__);
1053 if (rep_pl->data_len > *datasize) {
1054 DEBUG_ERR("%s mismatch reply data len / data buffer len\n",
1060 /* copy the data from the cprb to the data buffer */
1061 memcpy(databuf, rep_pl->data, rep_pl->data_len);
1062 *datasize = rep_pl->data_len;
1071 int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
1072 const u8 *clrkey, u8 *keybuf, size_t *keybufsize)
1075 struct ep11keyblob *kb;
1076 u8 encbuf[64], *kek = NULL;
1077 size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
1079 if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256)
1080 clrkeylen = keybitsize / 8;
1083 "%s unknown/unsupported keybitsize %d\n",
1084 __func__, keybitsize);
1088 /* allocate memory for the temp kek */
1089 keklen = MAXEP11AESKEYBLOBSIZE;
1090 kek = kmalloc(keklen, GFP_ATOMIC);
1096 /* Step 1: generate AES 256 bit random kek key */
1097 rc = ep11_genaeskey(card, domain, 256,
1098 0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1102 "%s generate kek key failed, rc=%d\n",
1106 kb = (struct ep11keyblob *) kek;
1107 memset(&kb->head, 0, sizeof(kb->head));
1109 /* Step 2: encrypt clear key value with the kek key */
1110 rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
1111 clrkey, clrkeylen, encbuf, &encbuflen);
1114 "%s encrypting key value with kek key failed, rc=%d\n",
1119 /* Step 3: import the encrypted key value as a new key */
1120 rc = ep11_unwrapkey(card, domain, kek, keklen,
1121 encbuf, encbuflen, 0, def_iv,
1122 keybitsize, 0, keybuf, keybufsize);
1125 "%s importing key value as new key failed,, rc=%d\n",
1134 EXPORT_SYMBOL(ep11_clr2keyblob);
1136 int ep11_key2protkey(u16 card, u16 dom, const u8 *key, size_t keylen,
1137 u8 *protkey, u32 *protkeylen, u32 *protkeytype)
1141 size_t wkbuflen = 256;
1152 /* alloc temp working buffer */
1153 wkbuf = kmalloc(wkbuflen, GFP_ATOMIC);
1157 /* ep11 secure key -> protected key + info */
1158 rc = ep11_wrapkey(card, dom, key, keylen,
1159 0, def_iv, wkbuf, &wkbuflen);
1162 "%s rewrapping ep11 key to pkey failed, rc=%d\n",
1166 wki = (struct wk_info *) wkbuf;
1168 /* check struct version and pkey type */
1169 if (wki->version != 1 || wki->pkeytype != 1) {
1170 DEBUG_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
1171 __func__, (int) wki->version, (int) wki->pkeytype);
1176 /* copy the tanslated protected key */
1177 switch (wki->pkeysize) {
1179 /* AES 128 protected key */
1181 *protkeytype = PKEY_KEYTYPE_AES_128;
1184 /* AES 192 protected key */
1186 *protkeytype = PKEY_KEYTYPE_AES_192;
1189 /* AES 256 protected key */
1191 *protkeytype = PKEY_KEYTYPE_AES_256;
1194 DEBUG_ERR("%s unknown/unsupported pkeysize %d\n",
1195 __func__, (int) wki->pkeysize);
1199 memcpy(protkey, wki->pkey, wki->pkeysize);
1201 *protkeylen = (u32) wki->pkeysize;
1208 EXPORT_SYMBOL(ep11_key2protkey);
1210 int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1211 int minhwtype, int minapi, const u8 *wkvp)
1213 struct zcrypt_device_status_ext *device_status;
1214 u32 *_apqns = NULL, _nr_apqns = 0;
1215 int i, card, dom, rc = -ENOMEM;
1216 struct ep11_domain_info edi;
1217 struct ep11_card_info eci;
1219 /* fetch status of all crypto cards */
1220 device_status = kmalloc_array(MAX_ZDEV_ENTRIES_EXT,
1221 sizeof(struct zcrypt_device_status_ext),
1225 zcrypt_device_status_mask_ext(device_status);
1227 /* allocate 1k space for up to 256 apqns */
1228 _apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
1230 kfree(device_status);
1234 /* walk through all the crypto apqnss */
1235 for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
1236 card = AP_QID_CARD(device_status[i].qid);
1237 dom = AP_QID_QUEUE(device_status[i].qid);
1238 /* check online state */
1239 if (!device_status[i].online)
1241 /* check for ep11 functions */
1242 if (!(device_status[i].functions & 0x01))
1245 if (cardnr != 0xFFFF && card != cardnr)
1248 if (domain != 0xFFFF && dom != domain)
1250 /* check min hardware type */
1251 if (minhwtype && device_status[i].hwtype < minhwtype)
1253 /* check min api version if given */
1255 if (ep11_get_card_info(card, &eci, 0))
1257 if (minapi > eci.API_ord_nr)
1260 /* check wkvp if given */
1262 if (ep11_get_domain_info(card, dom, &edi))
1264 if (edi.cur_wk_state != '1')
1266 if (memcmp(wkvp, edi.cur_wkvp, 16))
1269 /* apqn passed all filtering criterons, add to the array */
1270 if (_nr_apqns < 256)
1271 _apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16) dom);
1274 /* nothing found ? */
1279 /* no re-allocation, simple return the _apqns array */
1281 *nr_apqns = _nr_apqns;
1285 kfree(device_status);
1288 EXPORT_SYMBOL(ep11_findcard2);
1290 void __exit zcrypt_ep11misc_exit(void)