1// SPDX-License-Identifier: GPL-2.0+
2/*
3 *  Copyright IBM Corp. 2019
4 *  Author(s): Harald Freudenberger <freude@linux.ibm.com>
5 *
6 *  Collection of EP11 misc functions used by zcrypt and pkey
7 */
8
9#define KMSG_COMPONENT "zcrypt"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
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>
17#include <asm/pkey.h>
18#include <crypto/aes.h>
19
20#include "ap_bus.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"
26
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__)
31
32#define EP11_PINBLOB_V1_BYTES 56
33
34/* default iv used here */
35static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
36			       0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
37
38/* ep11 card info cache */
39struct card_list_entry {
40	struct list_head list;
41	u16 cardnr;
42	struct ep11_card_info info;
43};
44static LIST_HEAD(card_list);
45static DEFINE_SPINLOCK(card_list_lock);
46
47static int card_cache_fetch(u16 cardnr, struct ep11_card_info *ci)
48{
49	int rc = -ENOENT;
50	struct card_list_entry *ptr;
51
52	spin_lock_bh(&card_list_lock);
53	list_for_each_entry(ptr, &card_list, list) {
54		if (ptr->cardnr == cardnr) {
55			memcpy(ci, &ptr->info, sizeof(*ci));
56			rc = 0;
57			break;
58		}
59	}
60	spin_unlock_bh(&card_list_lock);
61
62	return rc;
63}
64
65static void card_cache_update(u16 cardnr, const struct ep11_card_info *ci)
66{
67	int found = 0;
68	struct card_list_entry *ptr;
69
70	spin_lock_bh(&card_list_lock);
71	list_for_each_entry(ptr, &card_list, list) {
72		if (ptr->cardnr == cardnr) {
73			memcpy(&ptr->info, ci, sizeof(*ci));
74			found = 1;
75			break;
76		}
77	}
78	if (!found) {
79		ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
80		if (!ptr) {
81			spin_unlock_bh(&card_list_lock);
82			return;
83		}
84		ptr->cardnr = cardnr;
85		memcpy(&ptr->info, ci, sizeof(*ci));
86		list_add(&ptr->list, &card_list);
87	}
88	spin_unlock_bh(&card_list_lock);
89}
90
91static void card_cache_scrub(u16 cardnr)
92{
93	struct card_list_entry *ptr;
94
95	spin_lock_bh(&card_list_lock);
96	list_for_each_entry(ptr, &card_list, list) {
97		if (ptr->cardnr == cardnr) {
98			list_del(&ptr->list);
99			kfree(ptr);
100			break;
101		}
102	}
103	spin_unlock_bh(&card_list_lock);
104}
105
106static void __exit card_cache_free(void)
107{
108	struct card_list_entry *ptr, *pnext;
109
110	spin_lock_bh(&card_list_lock);
111	list_for_each_entry_safe(ptr, pnext, &card_list, list) {
112		list_del(&ptr->list);
113		kfree(ptr);
114	}
115	spin_unlock_bh(&card_list_lock);
116}
117
118static int ep11_kb_split(const u8 *kb, size_t kblen, u32 kbver,
119			 struct ep11kblob_header **kbhdr, size_t *kbhdrsize,
120			 u8 **kbpl, size_t *kbplsize)
121{
122	struct ep11kblob_header *hdr = NULL;
123	size_t hdrsize, plsize = 0;
124	int rc = -EINVAL;
125	u8 *pl = NULL;
126
127	if (kblen < sizeof(struct ep11kblob_header))
128		goto out;
129	hdr = (struct ep11kblob_header *)kb;
130
131	switch (kbver) {
132	case TOKVER_EP11_AES:
133		/* header overlays the payload */
134		hdrsize = 0;
135		break;
136	case TOKVER_EP11_ECC_WITH_HEADER:
137	case TOKVER_EP11_AES_WITH_HEADER:
138		/* payload starts after the header */
139		hdrsize = sizeof(struct ep11kblob_header);
140		break;
141	default:
142		goto out;
143	}
144
145	plsize = kblen - hdrsize;
146	pl = (u8 *)kb + hdrsize;
147
148	if (kbhdr)
149		*kbhdr = hdr;
150	if (kbhdrsize)
151		*kbhdrsize = hdrsize;
152	if (kbpl)
153		*kbpl = pl;
154	if (kbplsize)
155		*kbplsize = plsize;
156
157	rc = 0;
158out:
159	return rc;
160}
161
162static int ep11_kb_decode(const u8 *kb, size_t kblen,
163			  struct ep11kblob_header **kbhdr, size_t *kbhdrsize,
164			  struct ep11keyblob **kbpl, size_t *kbplsize)
165{
166	struct ep11kblob_header *tmph, *hdr = NULL;
167	size_t hdrsize = 0, plsize = 0;
168	struct ep11keyblob *pl = NULL;
169	int rc = -EINVAL;
170	u8 *tmpp;
171
172	if (kblen < sizeof(struct ep11kblob_header))
173		goto out;
174	tmph = (struct ep11kblob_header *)kb;
175
176	if (tmph->type != TOKTYPE_NON_CCA &&
177	    tmph->len > kblen)
178		goto out;
179
180	if (ep11_kb_split(kb, kblen, tmph->version,
181			  &hdr, &hdrsize, &tmpp, &plsize))
182		goto out;
183
184	if (plsize < sizeof(struct ep11keyblob))
185		goto out;
186
187	if (!is_ep11_keyblob(tmpp))
188		goto out;
189
190	pl = (struct ep11keyblob *)tmpp;
191	plsize = hdr->len - hdrsize;
192
193	if (kbhdr)
194		*kbhdr = hdr;
195	if (kbhdrsize)
196		*kbhdrsize = hdrsize;
197	if (kbpl)
198		*kbpl = pl;
199	if (kbplsize)
200		*kbplsize = plsize;
201
202	rc = 0;
203out:
204	return rc;
205}
206
207/*
208 * For valid ep11 keyblobs, returns a reference to the wrappingkey verification
209 * pattern. Otherwise NULL.
210 */
211const u8 *ep11_kb_wkvp(const u8 *keyblob, size_t keybloblen)
212{
213	struct ep11keyblob *kb;
214
215	if (ep11_kb_decode(keyblob, keybloblen, NULL, NULL, &kb, NULL))
216		return NULL;
217	return kb->wkvp;
218}
219EXPORT_SYMBOL(ep11_kb_wkvp);
220
221/*
222 * Simple check if the key blob is a valid EP11 AES key blob with header.
223 */
224int ep11_check_aes_key_with_hdr(debug_info_t *dbg, int dbflvl,
225				const u8 *key, size_t keylen, int checkcpacfexp)
226{
227	struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
228	struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
229
230#define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
231
232	if (keylen < sizeof(*hdr) + sizeof(*kb)) {
233		DBF("%s key check failed, keylen %zu < %zu\n",
234		    __func__, keylen, sizeof(*hdr) + sizeof(*kb));
235		return -EINVAL;
236	}
237
238	if (hdr->type != TOKTYPE_NON_CCA) {
239		if (dbg)
240			DBF("%s key check failed, type 0x%02x != 0x%02x\n",
241			    __func__, (int)hdr->type, TOKTYPE_NON_CCA);
242		return -EINVAL;
243	}
244	if (hdr->hver != 0x00) {
245		if (dbg)
246			DBF("%s key check failed, header version 0x%02x != 0x00\n",
247			    __func__, (int)hdr->hver);
248		return -EINVAL;
249	}
250	if (hdr->version != TOKVER_EP11_AES_WITH_HEADER) {
251		if (dbg)
252			DBF("%s key check failed, version 0x%02x != 0x%02x\n",
253			    __func__, (int)hdr->version, TOKVER_EP11_AES_WITH_HEADER);
254		return -EINVAL;
255	}
256	if (hdr->len > keylen) {
257		if (dbg)
258			DBF("%s key check failed, header len %d keylen %zu mismatch\n",
259			    __func__, (int)hdr->len, keylen);
260		return -EINVAL;
261	}
262	if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
263		if (dbg)
264			DBF("%s key check failed, header len %d < %zu\n",
265			    __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
266		return -EINVAL;
267	}
268
269	if (kb->version != EP11_STRUCT_MAGIC) {
270		if (dbg)
271			DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
272			    __func__, (int)kb->version, EP11_STRUCT_MAGIC);
273		return -EINVAL;
274	}
275	if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
276		if (dbg)
277			DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
278			    __func__);
279		return -EINVAL;
280	}
281
282#undef DBF
283
284	return 0;
285}
286EXPORT_SYMBOL(ep11_check_aes_key_with_hdr);
287
288/*
289 * Simple check if the key blob is a valid EP11 ECC key blob with header.
290 */
291int ep11_check_ecc_key_with_hdr(debug_info_t *dbg, int dbflvl,
292				const u8 *key, size_t keylen, int checkcpacfexp)
293{
294	struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
295	struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
296
297#define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
298
299	if (keylen < sizeof(*hdr) + sizeof(*kb)) {
300		DBF("%s key check failed, keylen %zu < %zu\n",
301		    __func__, keylen, sizeof(*hdr) + sizeof(*kb));
302		return -EINVAL;
303	}
304
305	if (hdr->type != TOKTYPE_NON_CCA) {
306		if (dbg)
307			DBF("%s key check failed, type 0x%02x != 0x%02x\n",
308			    __func__, (int)hdr->type, TOKTYPE_NON_CCA);
309		return -EINVAL;
310	}
311	if (hdr->hver != 0x00) {
312		if (dbg)
313			DBF("%s key check failed, header version 0x%02x != 0x00\n",
314			    __func__, (int)hdr->hver);
315		return -EINVAL;
316	}
317	if (hdr->version != TOKVER_EP11_ECC_WITH_HEADER) {
318		if (dbg)
319			DBF("%s key check failed, version 0x%02x != 0x%02x\n",
320			    __func__, (int)hdr->version, TOKVER_EP11_ECC_WITH_HEADER);
321		return -EINVAL;
322	}
323	if (hdr->len > keylen) {
324		if (dbg)
325			DBF("%s key check failed, header len %d keylen %zu mismatch\n",
326			    __func__, (int)hdr->len, keylen);
327		return -EINVAL;
328	}
329	if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
330		if (dbg)
331			DBF("%s key check failed, header len %d < %zu\n",
332			    __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
333		return -EINVAL;
334	}
335
336	if (kb->version != EP11_STRUCT_MAGIC) {
337		if (dbg)
338			DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
339			    __func__, (int)kb->version, EP11_STRUCT_MAGIC);
340		return -EINVAL;
341	}
342	if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
343		if (dbg)
344			DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
345			    __func__);
346		return -EINVAL;
347	}
348
349#undef DBF
350
351	return 0;
352}
353EXPORT_SYMBOL(ep11_check_ecc_key_with_hdr);
354
355/*
356 * Simple check if the key blob is a valid EP11 AES key blob with
357 * the header in the session field (old style EP11 AES key).
358 */
359int ep11_check_aes_key(debug_info_t *dbg, int dbflvl,
360		       const u8 *key, size_t keylen, int checkcpacfexp)
361{
362	struct ep11keyblob *kb = (struct ep11keyblob *)key;
363
364#define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
365
366	if (keylen < sizeof(*kb)) {
367		DBF("%s key check failed, keylen %zu < %zu\n",
368		    __func__, keylen, sizeof(*kb));
369		return -EINVAL;
370	}
371
372	if (kb->head.type != TOKTYPE_NON_CCA) {
373		if (dbg)
374			DBF("%s key check failed, type 0x%02x != 0x%02x\n",
375			    __func__, (int)kb->head.type, TOKTYPE_NON_CCA);
376		return -EINVAL;
377	}
378	if (kb->head.version != TOKVER_EP11_AES) {
379		if (dbg)
380			DBF("%s key check failed, version 0x%02x != 0x%02x\n",
381			    __func__, (int)kb->head.version, TOKVER_EP11_AES);
382		return -EINVAL;
383	}
384	if (kb->head.len > keylen) {
385		if (dbg)
386			DBF("%s key check failed, header len %d keylen %zu mismatch\n",
387			    __func__, (int)kb->head.len, keylen);
388		return -EINVAL;
389	}
390	if (kb->head.len < sizeof(*kb)) {
391		if (dbg)
392			DBF("%s key check failed, header len %d < %zu\n",
393			    __func__, (int)kb->head.len, sizeof(*kb));
394		return -EINVAL;
395	}
396
397	if (kb->version != EP11_STRUCT_MAGIC) {
398		if (dbg)
399			DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
400			    __func__, (int)kb->version, EP11_STRUCT_MAGIC);
401		return -EINVAL;
402	}
403	if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
404		if (dbg)
405			DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
406			    __func__);
407		return -EINVAL;
408	}
409
410#undef DBF
411
412	return 0;
413}
414EXPORT_SYMBOL(ep11_check_aes_key);
415
416/*
417 * Allocate and prepare ep11 cprb plus additional payload.
418 */
419static inline struct ep11_cprb *alloc_cprb(size_t payload_len)
420{
421	size_t len = sizeof(struct ep11_cprb) + payload_len;
422	struct ep11_cprb *cprb;
423
424	cprb = kzalloc(len, GFP_KERNEL);
425	if (!cprb)
426		return NULL;
427
428	cprb->cprb_len = sizeof(struct ep11_cprb);
429	cprb->cprb_ver_id = 0x04;
430	memcpy(cprb->func_id, "T4", 2);
431	cprb->ret_code = 0xFFFFFFFF;
432	cprb->payload_len = payload_len;
433
434	return cprb;
435}
436
437/*
438 * Some helper functions related to ASN1 encoding.
439 * Limited to length info <= 2 byte.
440 */
441
442#define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
443
444static int asn1tag_write(u8 *ptr, u8 tag, const u8 *pvalue, u16 valuelen)
445{
446	ptr[0] = tag;
447	if (valuelen > 255) {
448		ptr[1] = 0x82;
449		*((u16 *)(ptr + 2)) = valuelen;
450		memcpy(ptr + 4, pvalue, valuelen);
451		return 4 + valuelen;
452	}
453	if (valuelen > 127) {
454		ptr[1] = 0x81;
455		ptr[2] = (u8)valuelen;
456		memcpy(ptr + 3, pvalue, valuelen);
457		return 3 + valuelen;
458	}
459	ptr[1] = (u8)valuelen;
460	memcpy(ptr + 2, pvalue, valuelen);
461	return 2 + valuelen;
462}
463
464/* EP11 payload > 127 bytes starts with this struct */
465struct pl_head {
466	u8  tag;
467	u8  lenfmt;
468	u16 len;
469	u8  func_tag;
470	u8  func_len;
471	u32 func;
472	u8  dom_tag;
473	u8  dom_len;
474	u32 dom;
475} __packed;
476
477/* prep ep11 payload head helper function */
478static inline void prep_head(struct pl_head *h,
479			     size_t pl_size, int api, int func)
480{
481	h->tag = 0x30;
482	h->lenfmt = 0x82;
483	h->len = pl_size - 4;
484	h->func_tag = 0x04;
485	h->func_len = sizeof(u32);
486	h->func = (api << 16) + func;
487	h->dom_tag = 0x04;
488	h->dom_len = sizeof(u32);
489}
490
491/* prep urb helper function */
492static inline void prep_urb(struct ep11_urb *u,
493			    struct ep11_target_dev *t, int nt,
494			    struct ep11_cprb *req, size_t req_len,
495			    struct ep11_cprb *rep, size_t rep_len)
496{
497	u->targets = (u8 __user *)t;
498	u->targets_num = nt;
499	u->req = (u8 __user *)req;
500	u->req_len = req_len;
501	u->resp = (u8 __user *)rep;
502	u->resp_len = rep_len;
503}
504
505/* Check ep11 reply payload, return 0 or suggested errno value. */
506static int check_reply_pl(const u8 *pl, const char *func)
507{
508	int len;
509	u32 ret;
510
511	/* start tag */
512	if (*pl++ != 0x30) {
513		DEBUG_ERR("%s reply start tag mismatch\n", func);
514		return -EIO;
515	}
516
517	/* payload length format */
518	if (*pl < 127) {
519		len = *pl;
520		pl++;
521	} else if (*pl == 0x81) {
522		pl++;
523		len = *pl;
524		pl++;
525	} else if (*pl == 0x82) {
526		pl++;
527		len = *((u16 *)pl);
528		pl += 2;
529	} else {
530		DEBUG_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
531			  func, *pl);
532		return -EIO;
533	}
534
535	/* len should cover at least 3 fields with 32 bit value each */
536	if (len < 3 * 6) {
537		DEBUG_ERR("%s reply length %d too small\n", func, len);
538		return -EIO;
539	}
540
541	/* function tag, length and value */
542	if (pl[0] != 0x04 || pl[1] != 0x04) {
543		DEBUG_ERR("%s function tag or length mismatch\n", func);
544		return -EIO;
545	}
546	pl += 6;
547
548	/* dom tag, length and value */
549	if (pl[0] != 0x04 || pl[1] != 0x04) {
550		DEBUG_ERR("%s dom tag or length mismatch\n", func);
551		return -EIO;
552	}
553	pl += 6;
554
555	/* return value tag, length and value */
556	if (pl[0] != 0x04 || pl[1] != 0x04) {
557		DEBUG_ERR("%s return value tag or length mismatch\n", func);
558		return -EIO;
559	}
560	pl += 2;
561	ret = *((u32 *)pl);
562	if (ret != 0) {
563		DEBUG_ERR("%s return value 0x%04x != 0\n", func, ret);
564		return -EIO;
565	}
566
567	return 0;
568}
569
570/*
571 * Helper function which does an ep11 query with given query type.
572 */
573static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
574			   size_t buflen, u8 *buf)
575{
576	struct ep11_info_req_pl {
577		struct pl_head head;
578		u8  query_type_tag;
579		u8  query_type_len;
580		u32 query_type;
581		u8  query_subtype_tag;
582		u8  query_subtype_len;
583		u32 query_subtype;
584	} __packed * req_pl;
585	struct ep11_info_rep_pl {
586		struct pl_head head;
587		u8  rc_tag;
588		u8  rc_len;
589		u32 rc;
590		u8  data_tag;
591		u8  data_lenfmt;
592		u16 data_len;
593	} __packed * rep_pl;
594	struct ep11_cprb *req = NULL, *rep = NULL;
595	struct ep11_target_dev target;
596	struct ep11_urb *urb = NULL;
597	int api = EP11_API_V1, rc = -ENOMEM;
598
599	/* request cprb and payload */
600	req = alloc_cprb(sizeof(struct ep11_info_req_pl));
601	if (!req)
602		goto out;
603	req_pl = (struct ep11_info_req_pl *)(((u8 *)req) + sizeof(*req));
604	prep_head(&req_pl->head, sizeof(*req_pl), api, 38); /* get xcp info */
605	req_pl->query_type_tag = 0x04;
606	req_pl->query_type_len = sizeof(u32);
607	req_pl->query_type = query_type;
608	req_pl->query_subtype_tag = 0x04;
609	req_pl->query_subtype_len = sizeof(u32);
610
611	/* reply cprb and payload */
612	rep = alloc_cprb(sizeof(struct ep11_info_rep_pl) + buflen);
613	if (!rep)
614		goto out;
615	rep_pl = (struct ep11_info_rep_pl *)(((u8 *)rep) + sizeof(*rep));
616
617	/* urb and target */
618	urb = kmalloc(sizeof(*urb), GFP_KERNEL);
619	if (!urb)
620		goto out;
621	target.ap_id = cardnr;
622	target.dom_id = domain;
623	prep_urb(urb, &target, 1,
624		 req, sizeof(*req) + sizeof(*req_pl),
625		 rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
626
627	rc = zcrypt_send_ep11_cprb(urb);
628	if (rc) {
629		DEBUG_ERR(
630			"%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
631			__func__, (int)cardnr, (int)domain, rc);
632		goto out;
633	}
634
635	rc = check_reply_pl((u8 *)rep_pl, __func__);
636	if (rc)
637		goto out;
638	if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
639		DEBUG_ERR("%s unknown reply data format\n", __func__);
640		rc = -EIO;
641		goto out;
642	}
643	if (rep_pl->data_len > buflen) {
644		DEBUG_ERR("%s mismatch between reply data len and buffer len\n",
645			  __func__);
646		rc = -ENOSPC;
647		goto out;
648	}
649
650	memcpy(buf, ((u8 *)rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
651
652out:
653	kfree(req);
654	kfree(rep);
655	kfree(urb);
656	return rc;
657}
658
659/*
660 * Provide information about an EP11 card.
661 */
662int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify)
663{
664	int rc;
665	struct ep11_module_query_info {
666		u32 API_ord_nr;
667		u32 firmware_id;
668		u8  FW_major_vers;
669		u8  FW_minor_vers;
670		u8  CSP_major_vers;
671		u8  CSP_minor_vers;
672		u8  fwid[32];
673		u8  xcp_config_hash[32];
674		u8  CSP_config_hash[32];
675		u8  serial[16];
676		u8  module_date_time[16];
677		u64 op_mode;
678		u32 PKCS11_flags;
679		u32 ext_flags;
680		u32 domains;
681		u32 sym_state_bytes;
682		u32 digest_state_bytes;
683		u32 pin_blob_bytes;
684		u32 SPKI_bytes;
685		u32 priv_key_blob_bytes;
686		u32 sym_blob_bytes;
687		u32 max_payload_bytes;
688		u32 CP_profile_bytes;
689		u32 max_CP_index;
690	} __packed * pmqi = NULL;
691
692	rc = card_cache_fetch(card, info);
693	if (rc || verify) {
694		pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
695		if (!pmqi)
696			return -ENOMEM;
697		rc = ep11_query_info(card, AUTOSEL_DOM,
698				     0x01 /* module info query */,
699				     sizeof(*pmqi), (u8 *)pmqi);
700		if (rc) {
701			if (rc == -ENODEV)
702				card_cache_scrub(card);
703			goto out;
704		}
705		memset(info, 0, sizeof(*info));
706		info->API_ord_nr = pmqi->API_ord_nr;
707		info->FW_version =
708			(pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
709		memcpy(info->serial, pmqi->serial, sizeof(info->serial));
710		info->op_mode = pmqi->op_mode;
711		card_cache_update(card, info);
712	}
713
714out:
715	kfree(pmqi);
716	return rc;
717}
718EXPORT_SYMBOL(ep11_get_card_info);
719
720/*
721 * Provide information about a domain within an EP11 card.
722 */
723int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
724{
725	int rc;
726	struct ep11_domain_query_info {
727		u32 dom_index;
728		u8  cur_WK_VP[32];
729		u8  new_WK_VP[32];
730		u32 dom_flags;
731		u64 op_mode;
732	} __packed * p_dom_info;
733
734	p_dom_info = kmalloc(sizeof(*p_dom_info), GFP_KERNEL);
735	if (!p_dom_info)
736		return -ENOMEM;
737
738	rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
739			     sizeof(*p_dom_info), (u8 *)p_dom_info);
740	if (rc)
741		goto out;
742
743	memset(info, 0, sizeof(*info));
744	info->cur_wk_state = '0';
745	info->new_wk_state = '0';
746	if (p_dom_info->dom_flags & 0x10 /* left imprint mode */) {
747		if (p_dom_info->dom_flags & 0x02 /* cur wk valid */) {
748			info->cur_wk_state = '1';
749			memcpy(info->cur_wkvp, p_dom_info->cur_WK_VP, 32);
750		}
751		if (p_dom_info->dom_flags & 0x04 || /* new wk present */
752		    p_dom_info->dom_flags & 0x08 /* new wk committed */) {
753			info->new_wk_state =
754				p_dom_info->dom_flags & 0x08 ? '2' : '1';
755			memcpy(info->new_wkvp, p_dom_info->new_WK_VP, 32);
756		}
757	}
758	info->op_mode = p_dom_info->op_mode;
759
760out:
761	kfree(p_dom_info);
762	return rc;
763}
764EXPORT_SYMBOL(ep11_get_domain_info);
765
766/*
767 * Default EP11 AES key generate attributes, used when no keygenflags given:
768 * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
769 */
770#define KEY_ATTR_DEFAULTS 0x00200c00
771
772static int _ep11_genaeskey(u16 card, u16 domain,
773			   u32 keybitsize, u32 keygenflags,
774			   u8 *keybuf, size_t *keybufsize)
775{
776	struct keygen_req_pl {
777		struct pl_head head;
778		u8  var_tag;
779		u8  var_len;
780		u32 var;
781		u8  keybytes_tag;
782		u8  keybytes_len;
783		u32 keybytes;
784		u8  mech_tag;
785		u8  mech_len;
786		u32 mech;
787		u8  attr_tag;
788		u8  attr_len;
789		u32 attr_header;
790		u32 attr_bool_mask;
791		u32 attr_bool_bits;
792		u32 attr_val_len_type;
793		u32 attr_val_len_value;
794		/* followed by empty pin tag or empty pinblob tag */
795	} __packed * req_pl;
796	struct keygen_rep_pl {
797		struct pl_head head;
798		u8  rc_tag;
799		u8  rc_len;
800		u32 rc;
801		u8  data_tag;
802		u8  data_lenfmt;
803		u16 data_len;
804		u8  data[512];
805	} __packed * rep_pl;
806	struct ep11_cprb *req = NULL, *rep = NULL;
807	size_t req_pl_size, pinblob_size = 0;
808	struct ep11_target_dev target;
809	struct ep11_urb *urb = NULL;
810	int api, rc = -ENOMEM;
811	u8 *p;
812
813	switch (keybitsize) {
814	case 128:
815	case 192:
816	case 256:
817		break;
818	default:
819		DEBUG_ERR(
820			"%s unknown/unsupported keybitsize %d\n",
821			__func__, keybitsize);
822		rc = -EINVAL;
823		goto out;
824	}
825
826	/* request cprb and payload */
827	api = (!keygenflags || keygenflags & 0x00200000) ?
828		EP11_API_V4 : EP11_API_V1;
829	if (ap_is_se_guest()) {
830		/*
831		 * genkey within SE environment requires API ordinal 6
832		 * with empty pinblob
833		 */
834		api = EP11_API_V6;
835		pinblob_size = EP11_PINBLOB_V1_BYTES;
836	}
837	req_pl_size = sizeof(struct keygen_req_pl) + ASN1TAGLEN(pinblob_size);
838	req = alloc_cprb(req_pl_size);
839	if (!req)
840		goto out;
841	req_pl = (struct keygen_req_pl *)(((u8 *)req) + sizeof(*req));
842	prep_head(&req_pl->head, req_pl_size, api, 21); /* GenerateKey */
843	req_pl->var_tag = 0x04;
844	req_pl->var_len = sizeof(u32);
845	req_pl->keybytes_tag = 0x04;
846	req_pl->keybytes_len = sizeof(u32);
847	req_pl->keybytes = keybitsize / 8;
848	req_pl->mech_tag = 0x04;
849	req_pl->mech_len = sizeof(u32);
850	req_pl->mech = 0x00001080; /* CKM_AES_KEY_GEN */
851	req_pl->attr_tag = 0x04;
852	req_pl->attr_len = 5 * sizeof(u32);
853	req_pl->attr_header = 0x10010000;
854	req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
855	req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
856	req_pl->attr_val_len_type = 0x00000161; /* CKA_VALUE_LEN */
857	req_pl->attr_val_len_value = keybitsize / 8;
858	p = ((u8 *)req_pl) + sizeof(*req_pl);
859	/* pin tag */
860	*p++ = 0x04;
861	*p++ = pinblob_size;
862
863	/* reply cprb and payload */
864	rep = alloc_cprb(sizeof(struct keygen_rep_pl));
865	if (!rep)
866		goto out;
867	rep_pl = (struct keygen_rep_pl *)(((u8 *)rep) + sizeof(*rep));
868
869	/* urb and target */
870	urb = kmalloc(sizeof(*urb), GFP_KERNEL);
871	if (!urb)
872		goto out;
873	target.ap_id = card;
874	target.dom_id = domain;
875	prep_urb(urb, &target, 1,
876		 req, sizeof(*req) + req_pl_size,
877		 rep, sizeof(*rep) + sizeof(*rep_pl));
878
879	rc = zcrypt_send_ep11_cprb(urb);
880	if (rc) {
881		DEBUG_ERR(
882			"%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
883			__func__, (int)card, (int)domain, rc);
884		goto out;
885	}
886
887	rc = check_reply_pl((u8 *)rep_pl, __func__);
888	if (rc)
889		goto out;
890	if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
891		DEBUG_ERR("%s unknown reply data format\n", __func__);
892		rc = -EIO;
893		goto out;
894	}
895	if (rep_pl->data_len > *keybufsize) {
896		DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
897			  __func__);
898		rc = -ENOSPC;
899		goto out;
900	}
901
902	/* copy key blob */
903	memcpy(keybuf, rep_pl->data, rep_pl->data_len);
904	*keybufsize = rep_pl->data_len;
905
906out:
907	kfree(req);
908	kfree(rep);
909	kfree(urb);
910	return rc;
911}
912
913int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
914		   u8 *keybuf, size_t *keybufsize, u32 keybufver)
915{
916	struct ep11kblob_header *hdr;
917	size_t hdr_size, pl_size;
918	u8 *pl;
919	int rc;
920
921	switch (keybufver) {
922	case TOKVER_EP11_AES:
923	case TOKVER_EP11_AES_WITH_HEADER:
924		break;
925	default:
926		return -EINVAL;
927	}
928
929	rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
930			   &hdr, &hdr_size, &pl, &pl_size);
931	if (rc)
932		return rc;
933
934	rc = _ep11_genaeskey(card, domain, keybitsize, keygenflags,
935			     pl, &pl_size);
936	if (rc)
937		return rc;
938
939	*keybufsize = hdr_size + pl_size;
940
941	/* update header information */
942	hdr->type = TOKTYPE_NON_CCA;
943	hdr->len = *keybufsize;
944	hdr->version = keybufver;
945	hdr->bitlen = keybitsize;
946
947	return 0;
948}
949EXPORT_SYMBOL(ep11_genaeskey);
950
951static int ep11_cryptsingle(u16 card, u16 domain,
952			    u16 mode, u32 mech, const u8 *iv,
953			    const u8 *key, size_t keysize,
954			    const u8 *inbuf, size_t inbufsize,
955			    u8 *outbuf, size_t *outbufsize)
956{
957	struct crypt_req_pl {
958		struct pl_head head;
959		u8  var_tag;
960		u8  var_len;
961		u32 var;
962		u8  mech_tag;
963		u8  mech_len;
964		u32 mech;
965		/*
966		 * maybe followed by iv data
967		 * followed by key tag + key blob
968		 * followed by plaintext tag + plaintext
969		 */
970	} __packed * req_pl;
971	struct crypt_rep_pl {
972		struct pl_head head;
973		u8  rc_tag;
974		u8  rc_len;
975		u32 rc;
976		u8  data_tag;
977		u8  data_lenfmt;
978		/* data follows */
979	} __packed * rep_pl;
980	struct ep11_cprb *req = NULL, *rep = NULL;
981	struct ep11_target_dev target;
982	struct ep11_urb *urb = NULL;
983	size_t req_pl_size, rep_pl_size;
984	int n, api = EP11_API_V1, rc = -ENOMEM;
985	u8 *p;
986
987	/* the simple asn1 coding used has length limits */
988	if (keysize > 0xFFFF || inbufsize > 0xFFFF)
989		return -EINVAL;
990
991	/* request cprb and payload */
992	req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
993		+ ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
994	req = alloc_cprb(req_pl_size);
995	if (!req)
996		goto out;
997	req_pl = (struct crypt_req_pl *)(((u8 *)req) + sizeof(*req));
998	prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
999	req_pl->var_tag = 0x04;
1000	req_pl->var_len = sizeof(u32);
1001	/* mech is mech + mech params (iv here) */
1002	req_pl->mech_tag = 0x04;
1003	req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1004	req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
1005	p = ((u8 *)req_pl) + sizeof(*req_pl);
1006	if (iv) {
1007		memcpy(p, iv, 16);
1008		p += 16;
1009	}
1010	/* key and input data */
1011	p += asn1tag_write(p, 0x04, key, keysize);
1012	p += asn1tag_write(p, 0x04, inbuf, inbufsize);
1013
1014	/* reply cprb and payload, assume out data size <= in data size + 32 */
1015	rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
1016	rep = alloc_cprb(rep_pl_size);
1017	if (!rep)
1018		goto out;
1019	rep_pl = (struct crypt_rep_pl *)(((u8 *)rep) + sizeof(*rep));
1020
1021	/* urb and target */
1022	urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1023	if (!urb)
1024		goto out;
1025	target.ap_id = card;
1026	target.dom_id = domain;
1027	prep_urb(urb, &target, 1,
1028		 req, sizeof(*req) + req_pl_size,
1029		 rep, sizeof(*rep) + rep_pl_size);
1030
1031	rc = zcrypt_send_ep11_cprb(urb);
1032	if (rc) {
1033		DEBUG_ERR(
1034			"%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1035			__func__, (int)card, (int)domain, rc);
1036		goto out;
1037	}
1038
1039	rc = check_reply_pl((u8 *)rep_pl, __func__);
1040	if (rc)
1041		goto out;
1042	if (rep_pl->data_tag != 0x04) {
1043		DEBUG_ERR("%s unknown reply data format\n", __func__);
1044		rc = -EIO;
1045		goto out;
1046	}
1047	p = ((u8 *)rep_pl) + sizeof(*rep_pl);
1048	if (rep_pl->data_lenfmt <= 127) {
1049		n = rep_pl->data_lenfmt;
1050	} else if (rep_pl->data_lenfmt == 0x81) {
1051		n = *p++;
1052	} else if (rep_pl->data_lenfmt == 0x82) {
1053		n = *((u16 *)p);
1054		p += 2;
1055	} else {
1056		DEBUG_ERR("%s unknown reply data length format 0x%02hhx\n",
1057			  __func__, rep_pl->data_lenfmt);
1058		rc = -EIO;
1059		goto out;
1060	}
1061	if (n > *outbufsize) {
1062		DEBUG_ERR("%s mismatch reply data len %d / output buffer %zu\n",
1063			  __func__, n, *outbufsize);
1064		rc = -ENOSPC;
1065		goto out;
1066	}
1067
1068	memcpy(outbuf, p, n);
1069	*outbufsize = n;
1070
1071out:
1072	kfree(req);
1073	kfree(rep);
1074	kfree(urb);
1075	return rc;
1076}
1077
1078static int _ep11_unwrapkey(u16 card, u16 domain,
1079			   const u8 *kek, size_t keksize,
1080			   const u8 *enckey, size_t enckeysize,
1081			   u32 mech, const u8 *iv,
1082			   u32 keybitsize, u32 keygenflags,
1083			   u8 *keybuf, size_t *keybufsize)
1084{
1085	struct uw_req_pl {
1086		struct pl_head head;
1087		u8  attr_tag;
1088		u8  attr_len;
1089		u32 attr_header;
1090		u32 attr_bool_mask;
1091		u32 attr_bool_bits;
1092		u32 attr_key_type;
1093		u32 attr_key_type_value;
1094		u32 attr_val_len;
1095		u32 attr_val_len_value;
1096		u8  mech_tag;
1097		u8  mech_len;
1098		u32 mech;
1099		/*
1100		 * maybe followed by iv data
1101		 * followed by kek tag + kek blob
1102		 * followed by empty mac tag
1103		 * followed by empty pin tag or empty pinblob tag
1104		 * followed by encryted key tag + bytes
1105		 */
1106	} __packed * req_pl;
1107	struct uw_rep_pl {
1108		struct pl_head head;
1109		u8  rc_tag;
1110		u8  rc_len;
1111		u32 rc;
1112		u8  data_tag;
1113		u8  data_lenfmt;
1114		u16 data_len;
1115		u8  data[512];
1116	} __packed * rep_pl;
1117	struct ep11_cprb *req = NULL, *rep = NULL;
1118	size_t req_pl_size, pinblob_size = 0;
1119	struct ep11_target_dev target;
1120	struct ep11_urb *urb = NULL;
1121	int api, rc = -ENOMEM;
1122	u8 *p;
1123
1124	/* request cprb and payload */
1125	api = (!keygenflags || keygenflags & 0x00200000) ?
1126		EP11_API_V4 : EP11_API_V1;
1127	if (ap_is_se_guest()) {
1128		/*
1129		 * unwrap within SE environment requires API ordinal 6
1130		 * with empty pinblob
1131		 */
1132		api = EP11_API_V6;
1133		pinblob_size = EP11_PINBLOB_V1_BYTES;
1134	}
1135	req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
1136		+ ASN1TAGLEN(keksize) + ASN1TAGLEN(0)
1137		+ ASN1TAGLEN(pinblob_size) + ASN1TAGLEN(enckeysize);
1138	req = alloc_cprb(req_pl_size);
1139	if (!req)
1140		goto out;
1141	req_pl = (struct uw_req_pl *)(((u8 *)req) + sizeof(*req));
1142	prep_head(&req_pl->head, req_pl_size, api, 34); /* UnwrapKey */
1143	req_pl->attr_tag = 0x04;
1144	req_pl->attr_len = 7 * sizeof(u32);
1145	req_pl->attr_header = 0x10020000;
1146	req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
1147	req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
1148	req_pl->attr_key_type = 0x00000100; /* CKA_KEY_TYPE */
1149	req_pl->attr_key_type_value = 0x0000001f; /* CKK_AES */
1150	req_pl->attr_val_len = 0x00000161; /* CKA_VALUE_LEN */
1151	req_pl->attr_val_len_value = keybitsize / 8;
1152	/* mech is mech + mech params (iv here) */
1153	req_pl->mech_tag = 0x04;
1154	req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1155	req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
1156	p = ((u8 *)req_pl) + sizeof(*req_pl);
1157	if (iv) {
1158		memcpy(p, iv, 16);
1159		p += 16;
1160	}
1161	/* kek */
1162	p += asn1tag_write(p, 0x04, kek, keksize);
1163	/* empty mac key tag */
1164	*p++ = 0x04;
1165	*p++ = 0;
1166	/* pin tag */
1167	*p++ = 0x04;
1168	*p++ = pinblob_size;
1169	p += pinblob_size;
1170	/* encrypted key value tag and bytes */
1171	p += asn1tag_write(p, 0x04, enckey, enckeysize);
1172
1173	/* reply cprb and payload */
1174	rep = alloc_cprb(sizeof(struct uw_rep_pl));
1175	if (!rep)
1176		goto out;
1177	rep_pl = (struct uw_rep_pl *)(((u8 *)rep) + sizeof(*rep));
1178
1179	/* urb and target */
1180	urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1181	if (!urb)
1182		goto out;
1183	target.ap_id = card;
1184	target.dom_id = domain;
1185	prep_urb(urb, &target, 1,
1186		 req, sizeof(*req) + req_pl_size,
1187		 rep, sizeof(*rep) + sizeof(*rep_pl));
1188
1189	rc = zcrypt_send_ep11_cprb(urb);
1190	if (rc) {
1191		DEBUG_ERR(
1192			"%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1193			__func__, (int)card, (int)domain, rc);
1194		goto out;
1195	}
1196
1197	rc = check_reply_pl((u8 *)rep_pl, __func__);
1198	if (rc)
1199		goto out;
1200	if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1201		DEBUG_ERR("%s unknown reply data format\n", __func__);
1202		rc = -EIO;
1203		goto out;
1204	}
1205	if (rep_pl->data_len > *keybufsize) {
1206		DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
1207			  __func__);
1208		rc = -ENOSPC;
1209		goto out;
1210	}
1211
1212	/* copy key blob */
1213	memcpy(keybuf, rep_pl->data, rep_pl->data_len);
1214	*keybufsize = rep_pl->data_len;
1215
1216out:
1217	kfree(req);
1218	kfree(rep);
1219	kfree(urb);
1220	return rc;
1221}
1222
1223static int ep11_unwrapkey(u16 card, u16 domain,
1224			  const u8 *kek, size_t keksize,
1225			  const u8 *enckey, size_t enckeysize,
1226			  u32 mech, const u8 *iv,
1227			  u32 keybitsize, u32 keygenflags,
1228			  u8 *keybuf, size_t *keybufsize,
1229			  u8 keybufver)
1230{
1231	struct ep11kblob_header *hdr;
1232	size_t hdr_size, pl_size;
1233	u8 *pl;
1234	int rc;
1235
1236	rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
1237			   &hdr, &hdr_size, &pl, &pl_size);
1238	if (rc)
1239		return rc;
1240
1241	rc = _ep11_unwrapkey(card, domain, kek, keksize, enckey, enckeysize,
1242			     mech, iv, keybitsize, keygenflags,
1243			     pl, &pl_size);
1244	if (rc)
1245		return rc;
1246
1247	*keybufsize = hdr_size + pl_size;
1248
1249	/* update header information */
1250	hdr = (struct ep11kblob_header *)keybuf;
1251	hdr->type = TOKTYPE_NON_CCA;
1252	hdr->len = *keybufsize;
1253	hdr->version = keybufver;
1254	hdr->bitlen = keybitsize;
1255
1256	return 0;
1257}
1258
1259static int _ep11_wrapkey(u16 card, u16 domain,
1260			 const u8 *key, size_t keysize,
1261			 u32 mech, const u8 *iv,
1262			 u8 *databuf, size_t *datasize)
1263{
1264	struct wk_req_pl {
1265		struct pl_head head;
1266		u8  var_tag;
1267		u8  var_len;
1268		u32 var;
1269		u8  mech_tag;
1270		u8  mech_len;
1271		u32 mech;
1272		/*
1273		 * followed by iv data
1274		 * followed by key tag + key blob
1275		 * followed by dummy kek param
1276		 * followed by dummy mac param
1277		 */
1278	} __packed * req_pl;
1279	struct wk_rep_pl {
1280		struct pl_head head;
1281		u8  rc_tag;
1282		u8  rc_len;
1283		u32 rc;
1284		u8  data_tag;
1285		u8  data_lenfmt;
1286		u16 data_len;
1287		u8  data[1024];
1288	} __packed * rep_pl;
1289	struct ep11_cprb *req = NULL, *rep = NULL;
1290	struct ep11_target_dev target;
1291	struct ep11_urb *urb = NULL;
1292	size_t req_pl_size;
1293	int api, rc = -ENOMEM;
1294	u8 *p;
1295
1296	/* request cprb and payload */
1297	req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
1298		+ ASN1TAGLEN(keysize) + 4;
1299	req = alloc_cprb(req_pl_size);
1300	if (!req)
1301		goto out;
1302	if (!mech || mech == 0x80060001)
1303		req->flags |= 0x20; /* CPACF_WRAP needs special bit */
1304	req_pl = (struct wk_req_pl *)(((u8 *)req) + sizeof(*req));
1305	api = (!mech || mech == 0x80060001) ? /* CKM_IBM_CPACF_WRAP */
1306		EP11_API_V4 : EP11_API_V1;
1307	prep_head(&req_pl->head, req_pl_size, api, 33); /* WrapKey */
1308	req_pl->var_tag = 0x04;
1309	req_pl->var_len = sizeof(u32);
1310	/* mech is mech + mech params (iv here) */
1311	req_pl->mech_tag = 0x04;
1312	req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
1313	req_pl->mech = (mech ? mech : 0x80060001); /* CKM_IBM_CPACF_WRAP */
1314	p = ((u8 *)req_pl) + sizeof(*req_pl);
1315	if (iv) {
1316		memcpy(p, iv, 16);
1317		p += 16;
1318	}
1319	/* key blob */
1320	p += asn1tag_write(p, 0x04, key, keysize);
1321	/* empty kek tag */
1322	*p++ = 0x04;
1323	*p++ = 0;
1324	/* empty mac tag */
1325	*p++ = 0x04;
1326	*p++ = 0;
1327
1328	/* reply cprb and payload */
1329	rep = alloc_cprb(sizeof(struct wk_rep_pl));
1330	if (!rep)
1331		goto out;
1332	rep_pl = (struct wk_rep_pl *)(((u8 *)rep) + sizeof(*rep));
1333
1334	/* urb and target */
1335	urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1336	if (!urb)
1337		goto out;
1338	target.ap_id = card;
1339	target.dom_id = domain;
1340	prep_urb(urb, &target, 1,
1341		 req, sizeof(*req) + req_pl_size,
1342		 rep, sizeof(*rep) + sizeof(*rep_pl));
1343
1344	rc = zcrypt_send_ep11_cprb(urb);
1345	if (rc) {
1346		DEBUG_ERR(
1347			"%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
1348			__func__, (int)card, (int)domain, rc);
1349		goto out;
1350	}
1351
1352	rc = check_reply_pl((u8 *)rep_pl, __func__);
1353	if (rc)
1354		goto out;
1355	if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
1356		DEBUG_ERR("%s unknown reply data format\n", __func__);
1357		rc = -EIO;
1358		goto out;
1359	}
1360	if (rep_pl->data_len > *datasize) {
1361		DEBUG_ERR("%s mismatch reply data len / data buffer len\n",
1362			  __func__);
1363		rc = -ENOSPC;
1364		goto out;
1365	}
1366
1367	/* copy the data from the cprb to the data buffer */
1368	memcpy(databuf, rep_pl->data, rep_pl->data_len);
1369	*datasize = rep_pl->data_len;
1370
1371out:
1372	kfree(req);
1373	kfree(rep);
1374	kfree(urb);
1375	return rc;
1376}
1377
1378int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
1379		     const u8 *clrkey, u8 *keybuf, size_t *keybufsize,
1380		     u32 keytype)
1381{
1382	int rc;
1383	u8 encbuf[64], *kek = NULL;
1384	size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
1385
1386	if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256) {
1387		clrkeylen = keybitsize / 8;
1388	} else {
1389		DEBUG_ERR(
1390			"%s unknown/unsupported keybitsize %d\n",
1391			__func__, keybitsize);
1392		return -EINVAL;
1393	}
1394
1395	/* allocate memory for the temp kek */
1396	keklen = MAXEP11AESKEYBLOBSIZE;
1397	kek = kmalloc(keklen, GFP_ATOMIC);
1398	if (!kek) {
1399		rc = -ENOMEM;
1400		goto out;
1401	}
1402
1403	/* Step 1: generate AES 256 bit random kek key */
1404	rc = _ep11_genaeskey(card, domain, 256,
1405			     0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1406			     kek, &keklen);
1407	if (rc) {
1408		DEBUG_ERR(
1409			"%s generate kek key failed, rc=%d\n",
1410			__func__, rc);
1411		goto out;
1412	}
1413
1414	/* Step 2: encrypt clear key value with the kek key */
1415	rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
1416			      clrkey, clrkeylen, encbuf, &encbuflen);
1417	if (rc) {
1418		DEBUG_ERR(
1419			"%s encrypting key value with kek key failed, rc=%d\n",
1420			__func__, rc);
1421		goto out;
1422	}
1423
1424	/* Step 3: import the encrypted key value as a new key */
1425	rc = ep11_unwrapkey(card, domain, kek, keklen,
1426			    encbuf, encbuflen, 0, def_iv,
1427			    keybitsize, 0, keybuf, keybufsize, keytype);
1428	if (rc) {
1429		DEBUG_ERR(
1430			"%s importing key value as new key failed,, rc=%d\n",
1431			__func__, rc);
1432		goto out;
1433	}
1434
1435out:
1436	kfree(kek);
1437	return rc;
1438}
1439EXPORT_SYMBOL(ep11_clr2keyblob);
1440
1441int ep11_kblob2protkey(u16 card, u16 dom,
1442		       const u8 *keyblob, size_t keybloblen,
1443		       u8 *protkey, u32 *protkeylen, u32 *protkeytype)
1444{
1445	struct ep11kblob_header *hdr;
1446	struct ep11keyblob *key;
1447	size_t wkbuflen, keylen;
1448	struct wk_info {
1449		u16 version;
1450		u8  res1[16];
1451		u32 pkeytype;
1452		u32 pkeybitsize;
1453		u64 pkeysize;
1454		u8  res2[8];
1455		u8  pkey[];
1456	} __packed * wki;
1457	u8 *wkbuf = NULL;
1458	int rc = -EIO;
1459
1460	if (ep11_kb_decode((u8 *)keyblob, keybloblen, &hdr, NULL, &key, &keylen))
1461		return -EINVAL;
1462
1463	if (hdr->version == TOKVER_EP11_AES) {
1464		/* wipe overlayed header */
1465		memset(hdr, 0, sizeof(*hdr));
1466	}
1467	/* !!! hdr is no longer a valid header !!! */
1468
1469	/* alloc temp working buffer */
1470	wkbuflen = (keylen + AES_BLOCK_SIZE) & (~(AES_BLOCK_SIZE - 1));
1471	wkbuf = kmalloc(wkbuflen, GFP_ATOMIC);
1472	if (!wkbuf)
1473		return -ENOMEM;
1474
1475	/* ep11 secure key -> protected key + info */
1476	rc = _ep11_wrapkey(card, dom, (u8 *)key, keylen,
1477			   0, def_iv, wkbuf, &wkbuflen);
1478	if (rc) {
1479		DEBUG_ERR(
1480			"%s rewrapping ep11 key to pkey failed, rc=%d\n",
1481			__func__, rc);
1482		goto out;
1483	}
1484	wki = (struct wk_info *)wkbuf;
1485
1486	/* check struct version and pkey type */
1487	if (wki->version != 1 || wki->pkeytype < 1 || wki->pkeytype > 5) {
1488		DEBUG_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
1489			  __func__, (int)wki->version, (int)wki->pkeytype);
1490		rc = -EIO;
1491		goto out;
1492	}
1493
1494	/* check protected key type field */
1495	switch (wki->pkeytype) {
1496	case 1: /* AES */
1497		switch (wki->pkeysize) {
1498		case 16 + 32:
1499			/* AES 128 protected key */
1500			if (protkeytype)
1501				*protkeytype = PKEY_KEYTYPE_AES_128;
1502			break;
1503		case 24 + 32:
1504			/* AES 192 protected key */
1505			if (protkeytype)
1506				*protkeytype = PKEY_KEYTYPE_AES_192;
1507			break;
1508		case 32 + 32:
1509			/* AES 256 protected key */
1510			if (protkeytype)
1511				*protkeytype = PKEY_KEYTYPE_AES_256;
1512			break;
1513		default:
1514			DEBUG_ERR("%s unknown/unsupported AES pkeysize %d\n",
1515				  __func__, (int)wki->pkeysize);
1516			rc = -EIO;
1517			goto out;
1518		}
1519		break;
1520	case 3: /* EC-P */
1521	case 4: /* EC-ED */
1522	case 5: /* EC-BP */
1523		if (protkeytype)
1524			*protkeytype = PKEY_KEYTYPE_ECC;
1525		break;
1526	case 2: /* TDES */
1527	default:
1528		DEBUG_ERR("%s unknown/unsupported key type %d\n",
1529			  __func__, (int)wki->pkeytype);
1530		rc = -EIO;
1531		goto out;
1532	}
1533
1534	/* copy the translated protected key */
1535	if (wki->pkeysize > *protkeylen) {
1536		DEBUG_ERR("%s wk info pkeysize %llu > protkeysize %u\n",
1537			  __func__, wki->pkeysize, *protkeylen);
1538		rc = -EINVAL;
1539		goto out;
1540	}
1541	memcpy(protkey, wki->pkey, wki->pkeysize);
1542	*protkeylen = wki->pkeysize;
1543
1544out:
1545	kfree(wkbuf);
1546	return rc;
1547}
1548EXPORT_SYMBOL(ep11_kblob2protkey);
1549
1550int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1551		   int minhwtype, int minapi, const u8 *wkvp)
1552{
1553	struct zcrypt_device_status_ext *device_status;
1554	u32 *_apqns = NULL, _nr_apqns = 0;
1555	int i, card, dom, rc = -ENOMEM;
1556	struct ep11_domain_info edi;
1557	struct ep11_card_info eci;
1558
1559	/* fetch status of all crypto cards */
1560	device_status = kvmalloc_array(MAX_ZDEV_ENTRIES_EXT,
1561				       sizeof(struct zcrypt_device_status_ext),
1562				       GFP_KERNEL);
1563	if (!device_status)
1564		return -ENOMEM;
1565	zcrypt_device_status_mask_ext(device_status);
1566
1567	/* allocate 1k space for up to 256 apqns */
1568	_apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
1569	if (!_apqns) {
1570		kvfree(device_status);
1571		return -ENOMEM;
1572	}
1573
1574	/* walk through all the crypto apqnss */
1575	for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
1576		card = AP_QID_CARD(device_status[i].qid);
1577		dom = AP_QID_QUEUE(device_status[i].qid);
1578		/* check online state */
1579		if (!device_status[i].online)
1580			continue;
1581		/* check for ep11 functions */
1582		if (!(device_status[i].functions & 0x01))
1583			continue;
1584		/* check cardnr */
1585		if (cardnr != 0xFFFF && card != cardnr)
1586			continue;
1587		/* check domain */
1588		if (domain != 0xFFFF && dom != domain)
1589			continue;
1590		/* check min hardware type */
1591		if (minhwtype && device_status[i].hwtype < minhwtype)
1592			continue;
1593		/* check min api version if given */
1594		if (minapi > 0) {
1595			if (ep11_get_card_info(card, &eci, 0))
1596				continue;
1597			if (minapi > eci.API_ord_nr)
1598				continue;
1599		}
1600		/* check wkvp if given */
1601		if (wkvp) {
1602			if (ep11_get_domain_info(card, dom, &edi))
1603				continue;
1604			if (edi.cur_wk_state != '1')
1605				continue;
1606			if (memcmp(wkvp, edi.cur_wkvp, 16))
1607				continue;
1608		}
1609		/* apqn passed all filtering criterons, add to the array */
1610		if (_nr_apqns < 256)
1611			_apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
1612	}
1613
1614	/* nothing found ? */
1615	if (!_nr_apqns) {
1616		kfree(_apqns);
1617		rc = -ENODEV;
1618	} else {
1619		/* no re-allocation, simple return the _apqns array */
1620		*apqns = _apqns;
1621		*nr_apqns = _nr_apqns;
1622		rc = 0;
1623	}
1624
1625	kvfree(device_status);
1626	return rc;
1627}
1628EXPORT_SYMBOL(ep11_findcard2);
1629
1630void __exit zcrypt_ep11misc_exit(void)
1631{
1632	card_cache_free();
1633}
1634