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);
467 * Helper function which does an ep11 query with given query type.
469 static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
470 size_t buflen, u8 *buf)
472 struct ep11_info_req_pl {
477 u8 query_subtype_tag;
478 u8 query_subtype_len;
481 struct ep11_info_rep_pl {
490 struct ep11_cprb *req = NULL, *rep = NULL;
491 struct ep11_target_dev target;
492 struct ep11_urb *urb = NULL;
493 int api = 1, rc = -ENOMEM;
495 /* request cprb and payload */
496 req = alloc_cprb(sizeof(struct ep11_info_req_pl));
499 req_pl = (struct ep11_info_req_pl *) (((u8 *) req) + sizeof(*req));
500 prep_head(&req_pl->head, sizeof(*req_pl), api, 38); /* get xcp info */
501 req_pl->query_type_tag = 0x04;
502 req_pl->query_type_len = sizeof(u32);
503 req_pl->query_type = query_type;
504 req_pl->query_subtype_tag = 0x04;
505 req_pl->query_subtype_len = sizeof(u32);
507 /* reply cprb and payload */
508 rep = alloc_cprb(sizeof(struct ep11_info_rep_pl) + buflen);
511 rep_pl = (struct ep11_info_rep_pl *) (((u8 *) rep) + sizeof(*rep));
514 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
517 target.ap_id = cardnr;
518 target.dom_id = domain;
519 prep_urb(urb, &target, 1,
520 req, sizeof(*req) + sizeof(*req_pl),
521 rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
523 rc = zcrypt_send_ep11_cprb(urb);
526 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
527 __func__, (int) cardnr, (int) domain, rc);
531 rc = check_reply_pl((u8 *)rep_pl, __func__);
534 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
535 DEBUG_ERR("%s unknown reply data format\n", __func__);
539 if (rep_pl->data_len > buflen) {
540 DEBUG_ERR("%s mismatch between reply data len and buffer len\n",
546 memcpy(buf, ((u8 *) rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
556 * Provide information about an EP11 card.
558 int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify)
561 struct ep11_module_query_info {
569 u8 xcp_config_hash[32];
570 u8 CSP_config_hash[32];
572 u8 module_date_time[16];
578 u32 digest_state_bytes;
581 u32 priv_key_blob_bytes;
583 u32 max_payload_bytes;
584 u32 CP_profile_bytes;
586 } __packed * pmqi = NULL;
588 rc = card_cache_fetch(card, info);
590 pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
593 rc = ep11_query_info(card, AUTOSEL_DOM,
594 0x01 /* module info query */,
595 sizeof(*pmqi), (u8 *) pmqi);
598 card_cache_scrub(card);
601 memset(info, 0, sizeof(*info));
602 info->API_ord_nr = pmqi->API_ord_nr;
604 (pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
605 memcpy(info->serial, pmqi->serial, sizeof(info->serial));
606 info->op_mode = pmqi->op_mode;
607 card_cache_update(card, info);
614 EXPORT_SYMBOL(ep11_get_card_info);
617 * Provide information about a domain within an EP11 card.
619 int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
622 struct ep11_domain_query_info {
628 } __packed * p_dom_info;
630 p_dom_info = kmalloc(sizeof(*p_dom_info), GFP_KERNEL);
634 rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
635 sizeof(*p_dom_info), (u8 *) p_dom_info);
639 memset(info, 0, sizeof(*info));
640 info->cur_wk_state = '0';
641 info->new_wk_state = '0';
642 if (p_dom_info->dom_flags & 0x10 /* left imprint mode */) {
643 if (p_dom_info->dom_flags & 0x02 /* cur wk valid */) {
644 info->cur_wk_state = '1';
645 memcpy(info->cur_wkvp, p_dom_info->cur_WK_VP, 32);
647 if (p_dom_info->dom_flags & 0x04 /* new wk present */
648 || p_dom_info->dom_flags & 0x08 /* new wk committed */) {
650 p_dom_info->dom_flags & 0x08 ? '2' : '1';
651 memcpy(info->new_wkvp, p_dom_info->new_WK_VP, 32);
654 info->op_mode = p_dom_info->op_mode;
660 EXPORT_SYMBOL(ep11_get_domain_info);
663 * Default EP11 AES key generate attributes, used when no keygenflags given:
664 * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
666 #define KEY_ATTR_DEFAULTS 0x00200c00
668 int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
669 u8 *keybuf, size_t *keybufsize)
671 struct keygen_req_pl {
687 u32 attr_val_len_type;
688 u32 attr_val_len_value;
692 struct keygen_rep_pl {
702 struct ep11_cprb *req = NULL, *rep = NULL;
703 struct ep11_target_dev target;
704 struct ep11_urb *urb = NULL;
705 struct ep11keyblob *kb;
706 int api, rc = -ENOMEM;
708 switch (keybitsize) {
715 "%s unknown/unsupported keybitsize %d\n",
716 __func__, keybitsize);
721 /* request cprb and payload */
722 req = alloc_cprb(sizeof(struct keygen_req_pl));
725 req_pl = (struct keygen_req_pl *) (((u8 *) req) + sizeof(*req));
726 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
727 prep_head(&req_pl->head, sizeof(*req_pl), api, 21); /* GenerateKey */
728 req_pl->var_tag = 0x04;
729 req_pl->var_len = sizeof(u32);
730 req_pl->keybytes_tag = 0x04;
731 req_pl->keybytes_len = sizeof(u32);
732 req_pl->keybytes = keybitsize / 8;
733 req_pl->mech_tag = 0x04;
734 req_pl->mech_len = sizeof(u32);
735 req_pl->mech = 0x00001080; /* CKM_AES_KEY_GEN */
736 req_pl->attr_tag = 0x04;
737 req_pl->attr_len = 5 * sizeof(u32);
738 req_pl->attr_header = 0x10010000;
739 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
740 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
741 req_pl->attr_val_len_type = 0x00000161; /* CKA_VALUE_LEN */
742 req_pl->attr_val_len_value = keybitsize / 8;
743 req_pl->pin_tag = 0x04;
745 /* reply cprb and payload */
746 rep = alloc_cprb(sizeof(struct keygen_rep_pl));
749 rep_pl = (struct keygen_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) + sizeof(*req_pl),
759 rep, sizeof(*rep) + sizeof(*rep_pl));
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 || rep_pl->data_lenfmt != 0x82) {
773 DEBUG_ERR("%s unknown reply data format\n", __func__);
777 if (rep_pl->data_len > *keybufsize) {
778 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
784 /* copy key blob and set header values */
785 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
786 *keybufsize = rep_pl->data_len;
787 kb = (struct ep11keyblob *) keybuf;
788 kb->head.type = TOKTYPE_NON_CCA;
789 kb->head.len = rep_pl->data_len;
790 kb->head.version = TOKVER_EP11_AES;
791 kb->head.bitlen = keybitsize;
799 EXPORT_SYMBOL(ep11_genaeskey);
801 static int ep11_cryptsingle(u16 card, u16 domain,
802 u16 mode, u32 mech, const u8 *iv,
803 const u8 *key, size_t keysize,
804 const u8 *inbuf, size_t inbufsize,
805 u8 *outbuf, size_t *outbufsize)
807 struct crypt_req_pl {
816 * maybe followed by iv data
817 * followed by key tag + key blob
818 * followed by plaintext tag + plaintext
821 struct crypt_rep_pl {
830 struct ep11_cprb *req = NULL, *rep = NULL;
831 struct ep11_target_dev target;
832 struct ep11_urb *urb = NULL;
833 size_t req_pl_size, rep_pl_size;
834 int n, api = 1, rc = -ENOMEM;
837 /* the simple asn1 coding used has length limits */
838 if (keysize > 0xFFFF || inbufsize > 0xFFFF)
841 /* request cprb and payload */
842 req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
843 + ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
844 req = alloc_cprb(req_pl_size);
847 req_pl = (struct crypt_req_pl *) (((u8 *) req) + sizeof(*req));
848 prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
849 req_pl->var_tag = 0x04;
850 req_pl->var_len = sizeof(u32);
851 /* mech is mech + mech params (iv here) */
852 req_pl->mech_tag = 0x04;
853 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
854 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
855 p = ((u8 *) req_pl) + sizeof(*req_pl);
860 /* key and input data */
861 p += asn1tag_write(p, 0x04, key, keysize);
862 p += asn1tag_write(p, 0x04, inbuf, inbufsize);
864 /* reply cprb and payload, assume out data size <= in data size + 32 */
865 rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
866 rep = alloc_cprb(rep_pl_size);
869 rep_pl = (struct crypt_rep_pl *) (((u8 *) rep) + sizeof(*rep));
872 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
876 target.dom_id = domain;
877 prep_urb(urb, &target, 1,
878 req, sizeof(*req) + req_pl_size,
879 rep, sizeof(*rep) + rep_pl_size);
881 rc = zcrypt_send_ep11_cprb(urb);
884 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
885 __func__, (int) card, (int) domain, rc);
889 rc = check_reply_pl((u8 *)rep_pl, __func__);
892 if (rep_pl->data_tag != 0x04) {
893 DEBUG_ERR("%s unknown reply data format\n", __func__);
897 p = ((u8 *) rep_pl) + sizeof(*rep_pl);
898 if (rep_pl->data_lenfmt <= 127)
899 n = rep_pl->data_lenfmt;
900 else if (rep_pl->data_lenfmt == 0x81)
902 else if (rep_pl->data_lenfmt == 0x82) {
906 DEBUG_ERR("%s unknown reply data length format 0x%02hhx\n",
907 __func__, rep_pl->data_lenfmt);
911 if (n > *outbufsize) {
912 DEBUG_ERR("%s mismatch reply data len %d / output buffer %zu\n",
913 __func__, n, *outbufsize);
918 memcpy(outbuf, p, n);
928 static int ep11_unwrapkey(u16 card, u16 domain,
929 const u8 *kek, size_t keksize,
930 const u8 *enckey, size_t enckeysize,
931 u32 mech, const u8 *iv,
932 u32 keybitsize, u32 keygenflags,
933 u8 *keybuf, size_t *keybufsize)
943 u32 attr_key_type_value;
945 u32 attr_val_len_value;
950 * maybe followed by iv data
951 * followed by kek tag + kek blob
952 * followed by empty mac tag
953 * followed by empty pin tag
954 * followed by encryted key tag + bytes
967 struct ep11_cprb *req = NULL, *rep = NULL;
968 struct ep11_target_dev target;
969 struct ep11_urb *urb = NULL;
970 struct ep11keyblob *kb;
972 int api, rc = -ENOMEM;
975 /* request cprb and payload */
976 req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
977 + ASN1TAGLEN(keksize) + 4 + ASN1TAGLEN(enckeysize);
978 req = alloc_cprb(req_pl_size);
981 req_pl = (struct uw_req_pl *) (((u8 *) req) + sizeof(*req));
982 api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
983 prep_head(&req_pl->head, req_pl_size, api, 34); /* UnwrapKey */
984 req_pl->attr_tag = 0x04;
985 req_pl->attr_len = 7 * sizeof(u32);
986 req_pl->attr_header = 0x10020000;
987 req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
988 req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
989 req_pl->attr_key_type = 0x00000100; /* CKA_KEY_TYPE */
990 req_pl->attr_key_type_value = 0x0000001f; /* CKK_AES */
991 req_pl->attr_val_len = 0x00000161; /* CKA_VALUE_LEN */
992 req_pl->attr_val_len_value = keybitsize / 8;
993 /* mech is mech + mech params (iv here) */
994 req_pl->mech_tag = 0x04;
995 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
996 req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
997 p = ((u8 *) req_pl) + sizeof(*req_pl);
1003 p += asn1tag_write(p, 0x04, kek, keksize);
1004 /* empty mac key tag */
1010 /* encrypted key value tag and bytes */
1011 p += asn1tag_write(p, 0x04, enckey, enckeysize);
1013 /* reply cprb and payload */
1014 rep = alloc_cprb(sizeof(struct uw_rep_pl));
1017 rep_pl = (struct uw_rep_pl *) (((u8 *) rep) + sizeof(*rep));
1019 /* urb and target */
1020 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
1023 target.ap_id = card;
1024 target.dom_id = domain;
1025 prep_urb(urb, &target, 1,
1026 req, sizeof(*req) + req_pl_size,
1027 rep, sizeof(*rep) + sizeof(*rep_pl));
1029 rc = zcrypt_send_ep11_cprb(urb);
1032 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1033 __func__, (int) card, (int) domain, rc);
1037 rc = check_reply_pl((u8 *)rep_pl, __func__);
1040 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1041 DEBUG_ERR("%s unknown reply data format\n", __func__);
1045 if (rep_pl->data_len > *keybufsize) {
1046 DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
1052 /* copy key blob and set header values */
1053 memcpy(keybuf, rep_pl->data, rep_pl->data_len);
1054 *keybufsize = rep_pl->data_len;
1055 kb = (struct ep11keyblob *) keybuf;
1056 kb->head.type = TOKTYPE_NON_CCA;
1057 kb->head.len = rep_pl->data_len;
1058 kb->head.version = TOKVER_EP11_AES;
1059 kb->head.bitlen = keybitsize;
1068 static int ep11_wrapkey(u16 card, u16 domain,
1069 const u8 *key, size_t keysize,
1070 u32 mech, const u8 *iv,
1071 u8 *databuf, size_t *datasize)
1074 struct pl_head head;
1082 * followed by iv data
1083 * followed by key tag + key blob
1084 * followed by dummy kek param
1085 * followed by dummy mac param
1087 } __packed * req_pl;
1089 struct pl_head head;
1097 } __packed * rep_pl;
1098 struct ep11_cprb *req = NULL, *rep = NULL;
1099 struct ep11_target_dev target;
1100 struct ep11_urb *urb = NULL;
1101 struct ep11keyblob *kb;
1103 int api, rc = -ENOMEM;
1104 bool has_header = false;
1107 /* maybe the session field holds a header with key info */
1108 kb = (struct ep11keyblob *) key;
1109 if (kb->head.type == TOKTYPE_NON_CCA &&
1110 kb->head.version == TOKVER_EP11_AES) {
1112 keysize = kb->head.len < keysize ? kb->head.len : keysize;
1115 /* request cprb and payload */
1116 req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
1117 + ASN1TAGLEN(keysize) + 4;
1118 req = alloc_cprb(req_pl_size);
1121 if (!mech || mech == 0x80060001)
1122 req->flags |= 0x20; /* CPACF_WRAP needs special bit */
1123 req_pl = (struct wk_req_pl *) (((u8 *) req) + sizeof(*req));
1124 api = (!mech || mech == 0x80060001) ? 4 : 1; /* CKM_IBM_CPACF_WRAP */
1125 prep_head(&req_pl->head, req_pl_size, api, 33); /* WrapKey */
1126 req_pl->var_tag = 0x04;
1127 req_pl->var_len = sizeof(u32);
1128 /* mech is mech + mech params (iv here) */
1129 req_pl->mech_tag = 0x04;
1130 req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1131 req_pl->mech = (mech ? mech : 0x80060001); /* CKM_IBM_CPACF_WRAP */
1132 p = ((u8 *) req_pl) + sizeof(*req_pl);
1138 p += asn1tag_write(p, 0x04, key, keysize);
1139 /* maybe the key argument needs the head data cleaned out */
1141 kb = (struct ep11keyblob *)(p - keysize);
1142 memset(&kb->head, 0, sizeof(kb->head));
1151 /* reply cprb and payload */
1152 rep = alloc_cprb(sizeof(struct wk_rep_pl));
1155 rep_pl = (struct wk_rep_pl *) (((u8 *) rep) + sizeof(*rep));
1157 /* urb and target */
1158 urb = kmalloc(sizeof(struct ep11_urb), GFP_KERNEL);
1161 target.ap_id = card;
1162 target.dom_id = domain;
1163 prep_urb(urb, &target, 1,
1164 req, sizeof(*req) + req_pl_size,
1165 rep, sizeof(*rep) + sizeof(*rep_pl));
1167 rc = zcrypt_send_ep11_cprb(urb);
1170 "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1171 __func__, (int) card, (int) domain, rc);
1175 rc = check_reply_pl((u8 *)rep_pl, __func__);
1178 if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1179 DEBUG_ERR("%s unknown reply data format\n", __func__);
1183 if (rep_pl->data_len > *datasize) {
1184 DEBUG_ERR("%s mismatch reply data len / data buffer len\n",
1190 /* copy the data from the cprb to the data buffer */
1191 memcpy(databuf, rep_pl->data, rep_pl->data_len);
1192 *datasize = rep_pl->data_len;
1201 int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
1202 const u8 *clrkey, u8 *keybuf, size_t *keybufsize)
1205 struct ep11keyblob *kb;
1206 u8 encbuf[64], *kek = NULL;
1207 size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
1209 if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256)
1210 clrkeylen = keybitsize / 8;
1213 "%s unknown/unsupported keybitsize %d\n",
1214 __func__, keybitsize);
1218 /* allocate memory for the temp kek */
1219 keklen = MAXEP11AESKEYBLOBSIZE;
1220 kek = kmalloc(keklen, GFP_ATOMIC);
1226 /* Step 1: generate AES 256 bit random kek key */
1227 rc = ep11_genaeskey(card, domain, 256,
1228 0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1232 "%s generate kek key failed, rc=%d\n",
1236 kb = (struct ep11keyblob *) kek;
1237 memset(&kb->head, 0, sizeof(kb->head));
1239 /* Step 2: encrypt clear key value with the kek key */
1240 rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
1241 clrkey, clrkeylen, encbuf, &encbuflen);
1244 "%s encrypting key value with kek key failed, rc=%d\n",
1249 /* Step 3: import the encrypted key value as a new key */
1250 rc = ep11_unwrapkey(card, domain, kek, keklen,
1251 encbuf, encbuflen, 0, def_iv,
1252 keybitsize, 0, keybuf, keybufsize);
1255 "%s importing key value as new key failed,, rc=%d\n",
1264 EXPORT_SYMBOL(ep11_clr2keyblob);
1266 int ep11_kblob2protkey(u16 card, u16 dom, const u8 *keyblob, size_t keybloblen,
1267 u8 *protkey, u32 *protkeylen, u32 *protkeytype)
1271 size_t wkbuflen, keylen;
1282 struct ep11kblob_header *hdr;
1284 /* key with or without header ? */
1285 hdr = (struct ep11kblob_header *) keyblob;
1286 if (hdr->type == TOKTYPE_NON_CCA
1287 && (hdr->version == TOKVER_EP11_AES_WITH_HEADER
1288 || hdr->version == TOKVER_EP11_ECC_WITH_HEADER)
1289 && is_ep11_keyblob(keyblob + sizeof(struct ep11kblob_header))) {
1290 /* EP11 AES or ECC key with header */
1291 key = keyblob + sizeof(struct ep11kblob_header);
1292 keylen = hdr->len - sizeof(struct ep11kblob_header);
1293 } else if (hdr->type == TOKTYPE_NON_CCA
1294 && hdr->version == TOKVER_EP11_AES
1295 && is_ep11_keyblob(keyblob)) {
1296 /* EP11 AES key (old style) */
1299 } else if (is_ep11_keyblob(keyblob)) {
1300 /* raw EP11 key blob */
1302 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)