xref: /linux/drivers/s390/crypto/pkey_api.c (revision d457a0e329b0bfd3a1450e0b1a18cd2b47a25a08)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  pkey device driver
4  *
5  *  Copyright IBM Corp. 2017,2019
6  *  Author(s): Harald Freudenberger
7  */
8 
9 #define KMSG_COMPONENT "pkey"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/fs.h>
13 #include <linux/init.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/kallsyms.h>
18 #include <linux/debugfs.h>
19 #include <linux/random.h>
20 #include <linux/cpufeature.h>
21 #include <asm/zcrypt.h>
22 #include <asm/cpacf.h>
23 #include <asm/pkey.h>
24 #include <crypto/aes.h>
25 
26 #include "zcrypt_api.h"
27 #include "zcrypt_ccamisc.h"
28 #include "zcrypt_ep11misc.h"
29 
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("IBM Corporation");
32 MODULE_DESCRIPTION("s390 protected key interface");
33 
34 #define KEYBLOBBUFSIZE 8192	/* key buffer size used for internal processing */
35 #define PROTKEYBLOBBUFSIZE 256	/* protected key buffer size used internal */
36 #define MAXAPQNSINLIST 64	/* max 64 apqns within a apqn list */
37 
38 /*
39  * debug feature data and functions
40  */
41 
42 static debug_info_t *debug_info;
43 
44 #define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
45 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
46 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
47 #define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
48 
49 static void __init pkey_debug_init(void)
50 {
51 	/* 5 arguments per dbf entry (including the format string ptr) */
52 	debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
53 	debug_register_view(debug_info, &debug_sprintf_view);
54 	debug_set_level(debug_info, 3);
55 }
56 
57 static void __exit pkey_debug_exit(void)
58 {
59 	debug_unregister(debug_info);
60 }
61 
62 /* inside view of a protected key token (only type 0x00 version 0x01) */
63 struct protaeskeytoken {
64 	u8  type;     /* 0x00 for PAES specific key tokens */
65 	u8  res0[3];
66 	u8  version;  /* should be 0x01 for protected AES key token */
67 	u8  res1[3];
68 	u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
69 	u32 len;      /* bytes actually stored in protkey[] */
70 	u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
71 } __packed;
72 
73 /* inside view of a clear key token (type 0x00 version 0x02) */
74 struct clearaeskeytoken {
75 	u8  type;	 /* 0x00 for PAES specific key tokens */
76 	u8  res0[3];
77 	u8  version;	 /* 0x02 for clear AES key token */
78 	u8  res1[3];
79 	u32 keytype;	 /* key type, one of the PKEY_KEYTYPE values */
80 	u32 len;	 /* bytes actually stored in clearkey[] */
81 	u8  clearkey[]; /* clear key value */
82 } __packed;
83 
84 /*
85  * Create a protected key from a clear key value.
86  */
87 static int pkey_clr2protkey(u32 keytype,
88 			    const struct pkey_clrkey *clrkey,
89 			    struct pkey_protkey *protkey)
90 {
91 	/* mask of available pckmo subfunctions */
92 	static cpacf_mask_t pckmo_functions;
93 
94 	long fc;
95 	int keysize;
96 	u8 paramblock[64];
97 
98 	switch (keytype) {
99 	case PKEY_KEYTYPE_AES_128:
100 		keysize = 16;
101 		fc = CPACF_PCKMO_ENC_AES_128_KEY;
102 		break;
103 	case PKEY_KEYTYPE_AES_192:
104 		keysize = 24;
105 		fc = CPACF_PCKMO_ENC_AES_192_KEY;
106 		break;
107 	case PKEY_KEYTYPE_AES_256:
108 		keysize = 32;
109 		fc = CPACF_PCKMO_ENC_AES_256_KEY;
110 		break;
111 	default:
112 		DEBUG_ERR("%s unknown/unsupported keytype %d\n",
113 			  __func__, keytype);
114 		return -EINVAL;
115 	}
116 
117 	/* Did we already check for PCKMO ? */
118 	if (!pckmo_functions.bytes[0]) {
119 		/* no, so check now */
120 		if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
121 			return -ENODEV;
122 	}
123 	/* check for the pckmo subfunction we need now */
124 	if (!cpacf_test_func(&pckmo_functions, fc)) {
125 		DEBUG_ERR("%s pckmo functions not available\n", __func__);
126 		return -ENODEV;
127 	}
128 
129 	/* prepare param block */
130 	memset(paramblock, 0, sizeof(paramblock));
131 	memcpy(paramblock, clrkey->clrkey, keysize);
132 
133 	/* call the pckmo instruction */
134 	cpacf_pckmo(fc, paramblock);
135 
136 	/* copy created protected key */
137 	protkey->type = keytype;
138 	protkey->len = keysize + 32;
139 	memcpy(protkey->protkey, paramblock, keysize + 32);
140 
141 	return 0;
142 }
143 
144 /*
145  * Find card and transform secure key into protected key.
146  */
147 static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
148 {
149 	int rc, verify;
150 	u16 cardnr, domain;
151 	struct keytoken_header *hdr = (struct keytoken_header *)key;
152 
153 	zcrypt_wait_api_operational();
154 
155 	/*
156 	 * The cca_xxx2protkey call may fail when a card has been
157 	 * addressed where the master key was changed after last fetch
158 	 * of the mkvp into the cache. Try 3 times: First without verify
159 	 * then with verify and last round with verify and old master
160 	 * key verification pattern match not ignored.
161 	 */
162 	for (verify = 0; verify < 3; verify++) {
163 		rc = cca_findcard(key, &cardnr, &domain, verify);
164 		if (rc < 0)
165 			continue;
166 		if (rc > 0 && verify < 2)
167 			continue;
168 		switch (hdr->version) {
169 		case TOKVER_CCA_AES:
170 			rc = cca_sec2protkey(cardnr, domain,
171 					     key, pkey->protkey,
172 					     &pkey->len, &pkey->type);
173 			break;
174 		case TOKVER_CCA_VLSC:
175 			rc = cca_cipher2protkey(cardnr, domain,
176 						key, pkey->protkey,
177 						&pkey->len, &pkey->type);
178 			break;
179 		default:
180 			return -EINVAL;
181 		}
182 		if (rc == 0)
183 			break;
184 	}
185 
186 	if (rc)
187 		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
188 
189 	return rc;
190 }
191 
192 /*
193  * Construct EP11 key with given clear key value.
194  */
195 static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
196 			    u8 *keybuf, size_t *keybuflen)
197 {
198 	int i, rc;
199 	u16 card, dom;
200 	u32 nr_apqns, *apqns = NULL;
201 
202 	zcrypt_wait_api_operational();
203 
204 	/* build a list of apqns suitable for ep11 keys with cpacf support */
205 	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
206 			    ZCRYPT_CEX7, EP11_API_V, NULL);
207 	if (rc)
208 		goto out;
209 
210 	/* go through the list of apqns and try to bild an ep11 key */
211 	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
212 		card = apqns[i] >> 16;
213 		dom = apqns[i] & 0xFFFF;
214 		rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
215 				      0, clrkey, keybuf, keybuflen);
216 		if (rc == 0)
217 			break;
218 	}
219 
220 out:
221 	kfree(apqns);
222 	if (rc)
223 		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
224 	return rc;
225 }
226 
227 /*
228  * Find card and transform EP11 secure key into protected key.
229  */
230 static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
231 {
232 	int i, rc;
233 	u16 card, dom;
234 	u32 nr_apqns, *apqns = NULL;
235 	struct ep11keyblob *kb = (struct ep11keyblob *)key;
236 
237 	zcrypt_wait_api_operational();
238 
239 	/* build a list of apqns suitable for this key */
240 	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
241 			    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
242 	if (rc)
243 		goto out;
244 
245 	/* go through the list of apqns and try to derive an pkey */
246 	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
247 		card = apqns[i] >> 16;
248 		dom = apqns[i] & 0xFFFF;
249 		pkey->len = sizeof(pkey->protkey);
250 		rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
251 					pkey->protkey, &pkey->len, &pkey->type);
252 		if (rc == 0)
253 			break;
254 	}
255 
256 out:
257 	kfree(apqns);
258 	if (rc)
259 		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
260 	return rc;
261 }
262 
263 /*
264  * Verify key and give back some info about the key.
265  */
266 static int pkey_verifykey(const struct pkey_seckey *seckey,
267 			  u16 *pcardnr, u16 *pdomain,
268 			  u16 *pkeysize, u32 *pattributes)
269 {
270 	struct secaeskeytoken *t = (struct secaeskeytoken *)seckey;
271 	u16 cardnr, domain;
272 	int rc;
273 
274 	/* check the secure key for valid AES secure key */
275 	rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *)seckey, 0);
276 	if (rc)
277 		goto out;
278 	if (pattributes)
279 		*pattributes = PKEY_VERIFY_ATTR_AES;
280 	if (pkeysize)
281 		*pkeysize = t->bitsize;
282 
283 	/* try to find a card which can handle this key */
284 	rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
285 	if (rc < 0)
286 		goto out;
287 
288 	if (rc > 0) {
289 		/* key mkvp matches to old master key mkvp */
290 		DEBUG_DBG("%s secure key has old mkvp\n", __func__);
291 		if (pattributes)
292 			*pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
293 		rc = 0;
294 	}
295 
296 	if (pcardnr)
297 		*pcardnr = cardnr;
298 	if (pdomain)
299 		*pdomain = domain;
300 
301 out:
302 	DEBUG_DBG("%s rc=%d\n", __func__, rc);
303 	return rc;
304 }
305 
306 /*
307  * Generate a random protected key
308  */
309 static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
310 {
311 	struct pkey_clrkey clrkey;
312 	int keysize;
313 	int rc;
314 
315 	switch (keytype) {
316 	case PKEY_KEYTYPE_AES_128:
317 		keysize = 16;
318 		break;
319 	case PKEY_KEYTYPE_AES_192:
320 		keysize = 24;
321 		break;
322 	case PKEY_KEYTYPE_AES_256:
323 		keysize = 32;
324 		break;
325 	default:
326 		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
327 			  keytype);
328 		return -EINVAL;
329 	}
330 
331 	/* generate a dummy random clear key */
332 	get_random_bytes(clrkey.clrkey, keysize);
333 
334 	/* convert it to a dummy protected key */
335 	rc = pkey_clr2protkey(keytype, &clrkey, protkey);
336 	if (rc)
337 		return rc;
338 
339 	/* replace the key part of the protected key with random bytes */
340 	get_random_bytes(protkey->protkey, keysize);
341 
342 	return 0;
343 }
344 
345 /*
346  * Verify if a protected key is still valid
347  */
348 static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
349 {
350 	unsigned long fc;
351 	struct {
352 		u8 iv[AES_BLOCK_SIZE];
353 		u8 key[MAXPROTKEYSIZE];
354 	} param;
355 	u8 null_msg[AES_BLOCK_SIZE];
356 	u8 dest_buf[AES_BLOCK_SIZE];
357 	unsigned int k;
358 
359 	switch (protkey->type) {
360 	case PKEY_KEYTYPE_AES_128:
361 		fc = CPACF_KMC_PAES_128;
362 		break;
363 	case PKEY_KEYTYPE_AES_192:
364 		fc = CPACF_KMC_PAES_192;
365 		break;
366 	case PKEY_KEYTYPE_AES_256:
367 		fc = CPACF_KMC_PAES_256;
368 		break;
369 	default:
370 		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
371 			  protkey->type);
372 		return -EINVAL;
373 	}
374 
375 	memset(null_msg, 0, sizeof(null_msg));
376 
377 	memset(param.iv, 0, sizeof(param.iv));
378 	memcpy(param.key, protkey->protkey, sizeof(param.key));
379 
380 	k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
381 		      sizeof(null_msg));
382 	if (k != sizeof(null_msg)) {
383 		DEBUG_ERR("%s protected key is not valid\n", __func__);
384 		return -EKEYREJECTED;
385 	}
386 
387 	return 0;
388 }
389 
390 /*
391  * Transform a non-CCA key token into a protected key
392  */
393 static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
394 			       struct pkey_protkey *protkey)
395 {
396 	int rc = -EINVAL;
397 	u8 *tmpbuf = NULL;
398 	struct keytoken_header *hdr = (struct keytoken_header *)key;
399 
400 	switch (hdr->version) {
401 	case TOKVER_PROTECTED_KEY: {
402 		struct protaeskeytoken *t;
403 
404 		if (keylen != sizeof(struct protaeskeytoken))
405 			goto out;
406 		t = (struct protaeskeytoken *)key;
407 		protkey->len = t->len;
408 		protkey->type = t->keytype;
409 		memcpy(protkey->protkey, t->protkey,
410 		       sizeof(protkey->protkey));
411 		rc = pkey_verifyprotkey(protkey);
412 		break;
413 	}
414 	case TOKVER_CLEAR_KEY: {
415 		struct clearaeskeytoken *t;
416 		struct pkey_clrkey ckey;
417 		union u_tmpbuf {
418 			u8 skey[SECKEYBLOBSIZE];
419 			u8 ep11key[MAXEP11AESKEYBLOBSIZE];
420 		};
421 		size_t tmpbuflen = sizeof(union u_tmpbuf);
422 
423 		if (keylen < sizeof(struct clearaeskeytoken))
424 			goto out;
425 		t = (struct clearaeskeytoken *)key;
426 		if (keylen != sizeof(*t) + t->len)
427 			goto out;
428 		if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16) ||
429 		    (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24) ||
430 		    (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
431 			memcpy(ckey.clrkey, t->clearkey, t->len);
432 		else
433 			goto out;
434 		/* alloc temp key buffer space */
435 		tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
436 		if (!tmpbuf) {
437 			rc = -ENOMEM;
438 			goto out;
439 		}
440 		/* try direct way with the PCKMO instruction */
441 		rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
442 		if (rc == 0)
443 			break;
444 		/* PCKMO failed, so try the CCA secure key way */
445 		zcrypt_wait_api_operational();
446 		rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
447 				    ckey.clrkey, tmpbuf);
448 		if (rc == 0)
449 			rc = pkey_skey2pkey(tmpbuf, protkey);
450 		if (rc == 0)
451 			break;
452 		/* if the CCA way also failed, let's try via EP11 */
453 		rc = pkey_clr2ep11key(ckey.clrkey, t->len,
454 				      tmpbuf, &tmpbuflen);
455 		if (rc == 0)
456 			rc = pkey_ep11key2pkey(tmpbuf, protkey);
457 		/* now we should really have an protected key */
458 		DEBUG_ERR("%s unable to build protected key from clear",
459 			  __func__);
460 		break;
461 	}
462 	case TOKVER_EP11_AES: {
463 		/* check ep11 key for exportable as protected key */
464 		rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
465 		if (rc)
466 			goto out;
467 		rc = pkey_ep11key2pkey(key, protkey);
468 		break;
469 	}
470 	case TOKVER_EP11_AES_WITH_HEADER:
471 		/* check ep11 key with header for exportable as protected key */
472 		rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
473 		if (rc)
474 			goto out;
475 		rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
476 				       protkey);
477 		break;
478 	default:
479 		DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
480 			  __func__, hdr->version);
481 		rc = -EINVAL;
482 	}
483 
484 out:
485 	kfree(tmpbuf);
486 	return rc;
487 }
488 
489 /*
490  * Transform a CCA internal key token into a protected key
491  */
492 static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
493 			       struct pkey_protkey *protkey)
494 {
495 	struct keytoken_header *hdr = (struct keytoken_header *)key;
496 
497 	switch (hdr->version) {
498 	case TOKVER_CCA_AES:
499 		if (keylen != sizeof(struct secaeskeytoken))
500 			return -EINVAL;
501 		break;
502 	case TOKVER_CCA_VLSC:
503 		if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
504 			return -EINVAL;
505 		break;
506 	default:
507 		DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
508 			  __func__, hdr->version);
509 		return -EINVAL;
510 	}
511 
512 	return pkey_skey2pkey(key, protkey);
513 }
514 
515 /*
516  * Transform a key blob (of any type) into a protected key
517  */
518 int pkey_keyblob2pkey(const u8 *key, u32 keylen,
519 		      struct pkey_protkey *protkey)
520 {
521 	int rc;
522 	struct keytoken_header *hdr = (struct keytoken_header *)key;
523 
524 	if (keylen < sizeof(struct keytoken_header)) {
525 		DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
526 		return -EINVAL;
527 	}
528 
529 	switch (hdr->type) {
530 	case TOKTYPE_NON_CCA:
531 		rc = pkey_nonccatok2pkey(key, keylen, protkey);
532 		break;
533 	case TOKTYPE_CCA_INTERNAL:
534 		rc = pkey_ccainttok2pkey(key, keylen, protkey);
535 		break;
536 	default:
537 		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
538 			  __func__, hdr->type);
539 		return -EINVAL;
540 	}
541 
542 	DEBUG_DBG("%s rc=%d\n", __func__, rc);
543 	return rc;
544 }
545 EXPORT_SYMBOL(pkey_keyblob2pkey);
546 
547 static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
548 			   enum pkey_key_type ktype, enum pkey_key_size ksize,
549 			   u32 kflags, u8 *keybuf, size_t *keybufsize)
550 {
551 	int i, card, dom, rc;
552 
553 	/* check for at least one apqn given */
554 	if (!apqns || !nr_apqns)
555 		return -EINVAL;
556 
557 	/* check key type and size */
558 	switch (ktype) {
559 	case PKEY_TYPE_CCA_DATA:
560 	case PKEY_TYPE_CCA_CIPHER:
561 		if (*keybufsize < SECKEYBLOBSIZE)
562 			return -EINVAL;
563 		break;
564 	case PKEY_TYPE_EP11:
565 		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
566 			return -EINVAL;
567 		break;
568 	default:
569 		return -EINVAL;
570 	}
571 	switch (ksize) {
572 	case PKEY_SIZE_AES_128:
573 	case PKEY_SIZE_AES_192:
574 	case PKEY_SIZE_AES_256:
575 		break;
576 	default:
577 		return -EINVAL;
578 	}
579 
580 	/* simple try all apqns from the list */
581 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
582 		card = apqns[i].card;
583 		dom = apqns[i].domain;
584 		if (ktype == PKEY_TYPE_EP11) {
585 			rc = ep11_genaeskey(card, dom, ksize, kflags,
586 					    keybuf, keybufsize);
587 		} else if (ktype == PKEY_TYPE_CCA_DATA) {
588 			rc = cca_genseckey(card, dom, ksize, keybuf);
589 			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
590 		} else {
591 			/* TOKVER_CCA_VLSC */
592 			rc = cca_gencipherkey(card, dom, ksize, kflags,
593 					      keybuf, keybufsize);
594 		}
595 		if (rc == 0)
596 			break;
597 	}
598 
599 	return rc;
600 }
601 
602 static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
603 			    enum pkey_key_type ktype, enum pkey_key_size ksize,
604 			    u32 kflags, const u8 *clrkey,
605 			    u8 *keybuf, size_t *keybufsize)
606 {
607 	int i, card, dom, rc;
608 
609 	/* check for at least one apqn given */
610 	if (!apqns || !nr_apqns)
611 		return -EINVAL;
612 
613 	/* check key type and size */
614 	switch (ktype) {
615 	case PKEY_TYPE_CCA_DATA:
616 	case PKEY_TYPE_CCA_CIPHER:
617 		if (*keybufsize < SECKEYBLOBSIZE)
618 			return -EINVAL;
619 		break;
620 	case PKEY_TYPE_EP11:
621 		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
622 			return -EINVAL;
623 		break;
624 	default:
625 		return -EINVAL;
626 	}
627 	switch (ksize) {
628 	case PKEY_SIZE_AES_128:
629 	case PKEY_SIZE_AES_192:
630 	case PKEY_SIZE_AES_256:
631 		break;
632 	default:
633 		return -EINVAL;
634 	}
635 
636 	zcrypt_wait_api_operational();
637 
638 	/* simple try all apqns from the list */
639 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
640 		card = apqns[i].card;
641 		dom = apqns[i].domain;
642 		if (ktype == PKEY_TYPE_EP11) {
643 			rc = ep11_clr2keyblob(card, dom, ksize, kflags,
644 					      clrkey, keybuf, keybufsize);
645 		} else if (ktype == PKEY_TYPE_CCA_DATA) {
646 			rc = cca_clr2seckey(card, dom, ksize,
647 					    clrkey, keybuf);
648 			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
649 		} else {
650 			/* TOKVER_CCA_VLSC */
651 			rc = cca_clr2cipherkey(card, dom, ksize, kflags,
652 					       clrkey, keybuf, keybufsize);
653 		}
654 		if (rc == 0)
655 			break;
656 	}
657 
658 	return rc;
659 }
660 
661 static int pkey_verifykey2(const u8 *key, size_t keylen,
662 			   u16 *cardnr, u16 *domain,
663 			   enum pkey_key_type *ktype,
664 			   enum pkey_key_size *ksize, u32 *flags)
665 {
666 	int rc;
667 	u32 _nr_apqns, *_apqns = NULL;
668 	struct keytoken_header *hdr = (struct keytoken_header *)key;
669 
670 	if (keylen < sizeof(struct keytoken_header))
671 		return -EINVAL;
672 
673 	if (hdr->type == TOKTYPE_CCA_INTERNAL &&
674 	    hdr->version == TOKVER_CCA_AES) {
675 		struct secaeskeytoken *t = (struct secaeskeytoken *)key;
676 
677 		rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
678 		if (rc)
679 			goto out;
680 		if (ktype)
681 			*ktype = PKEY_TYPE_CCA_DATA;
682 		if (ksize)
683 			*ksize = (enum pkey_key_size)t->bitsize;
684 
685 		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
686 				   ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
687 		if (rc == 0 && flags)
688 			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
689 		if (rc == -ENODEV) {
690 			rc = cca_findcard2(&_apqns, &_nr_apqns,
691 					   *cardnr, *domain,
692 					   ZCRYPT_CEX3C, AES_MK_SET,
693 					   0, t->mkvp, 1);
694 			if (rc == 0 && flags)
695 				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
696 		}
697 		if (rc)
698 			goto out;
699 
700 		*cardnr = ((struct pkey_apqn *)_apqns)->card;
701 		*domain = ((struct pkey_apqn *)_apqns)->domain;
702 
703 	} else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
704 		   hdr->version == TOKVER_CCA_VLSC) {
705 		struct cipherkeytoken *t = (struct cipherkeytoken *)key;
706 
707 		rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
708 		if (rc)
709 			goto out;
710 		if (ktype)
711 			*ktype = PKEY_TYPE_CCA_CIPHER;
712 		if (ksize) {
713 			*ksize = PKEY_SIZE_UNKNOWN;
714 			if (!t->plfver && t->wpllen == 512)
715 				*ksize = PKEY_SIZE_AES_128;
716 			else if (!t->plfver && t->wpllen == 576)
717 				*ksize = PKEY_SIZE_AES_192;
718 			else if (!t->plfver && t->wpllen == 640)
719 				*ksize = PKEY_SIZE_AES_256;
720 		}
721 
722 		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
723 				   ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
724 		if (rc == 0 && flags)
725 			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
726 		if (rc == -ENODEV) {
727 			rc = cca_findcard2(&_apqns, &_nr_apqns,
728 					   *cardnr, *domain,
729 					   ZCRYPT_CEX6, AES_MK_SET,
730 					   0, t->mkvp0, 1);
731 			if (rc == 0 && flags)
732 				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
733 		}
734 		if (rc)
735 			goto out;
736 
737 		*cardnr = ((struct pkey_apqn *)_apqns)->card;
738 		*domain = ((struct pkey_apqn *)_apqns)->domain;
739 
740 	} else if (hdr->type == TOKTYPE_NON_CCA &&
741 		   hdr->version == TOKVER_EP11_AES) {
742 		struct ep11keyblob *kb = (struct ep11keyblob *)key;
743 
744 		rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
745 		if (rc)
746 			goto out;
747 		if (ktype)
748 			*ktype = PKEY_TYPE_EP11;
749 		if (ksize)
750 			*ksize = kb->head.keybitlen;
751 
752 		rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
753 				    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
754 		if (rc)
755 			goto out;
756 
757 		if (flags)
758 			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
759 
760 		*cardnr = ((struct pkey_apqn *)_apqns)->card;
761 		*domain = ((struct pkey_apqn *)_apqns)->domain;
762 
763 	} else {
764 		rc = -EINVAL;
765 	}
766 
767 out:
768 	kfree(_apqns);
769 	return rc;
770 }
771 
772 static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
773 			      const u8 *key, size_t keylen,
774 			      struct pkey_protkey *pkey)
775 {
776 	int i, card, dom, rc;
777 	struct keytoken_header *hdr = (struct keytoken_header *)key;
778 
779 	/* check for at least one apqn given */
780 	if (!apqns || !nr_apqns)
781 		return -EINVAL;
782 
783 	if (keylen < sizeof(struct keytoken_header))
784 		return -EINVAL;
785 
786 	if (hdr->type == TOKTYPE_CCA_INTERNAL) {
787 		if (hdr->version == TOKVER_CCA_AES) {
788 			if (keylen != sizeof(struct secaeskeytoken))
789 				return -EINVAL;
790 			if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
791 				return -EINVAL;
792 		} else if (hdr->version == TOKVER_CCA_VLSC) {
793 			if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
794 				return -EINVAL;
795 			if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
796 				return -EINVAL;
797 		} else {
798 			DEBUG_ERR("%s unknown CCA internal token version %d\n",
799 				  __func__, hdr->version);
800 			return -EINVAL;
801 		}
802 	} else if (hdr->type == TOKTYPE_NON_CCA) {
803 		if (hdr->version == TOKVER_EP11_AES) {
804 			if (keylen < sizeof(struct ep11keyblob))
805 				return -EINVAL;
806 			if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
807 				return -EINVAL;
808 		} else {
809 			return pkey_nonccatok2pkey(key, keylen, pkey);
810 		}
811 	} else {
812 		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
813 			  __func__, hdr->type);
814 		return -EINVAL;
815 	}
816 
817 	zcrypt_wait_api_operational();
818 
819 	/* simple try all apqns from the list */
820 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
821 		card = apqns[i].card;
822 		dom = apqns[i].domain;
823 		if (hdr->type == TOKTYPE_CCA_INTERNAL &&
824 		    hdr->version == TOKVER_CCA_AES) {
825 			rc = cca_sec2protkey(card, dom, key, pkey->protkey,
826 					     &pkey->len, &pkey->type);
827 		} else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
828 			   hdr->version == TOKVER_CCA_VLSC) {
829 			rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
830 						&pkey->len, &pkey->type);
831 		} else {
832 			/* EP11 AES secure key blob */
833 			struct ep11keyblob *kb = (struct ep11keyblob *)key;
834 
835 			pkey->len = sizeof(pkey->protkey);
836 			rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
837 						pkey->protkey, &pkey->len,
838 						&pkey->type);
839 		}
840 		if (rc == 0)
841 			break;
842 	}
843 
844 	return rc;
845 }
846 
847 static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
848 			  struct pkey_apqn *apqns, size_t *nr_apqns)
849 {
850 	int rc;
851 	u32 _nr_apqns, *_apqns = NULL;
852 	struct keytoken_header *hdr = (struct keytoken_header *)key;
853 
854 	if (keylen < sizeof(struct keytoken_header) || flags == 0)
855 		return -EINVAL;
856 
857 	zcrypt_wait_api_operational();
858 
859 	if (hdr->type == TOKTYPE_NON_CCA &&
860 	    (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
861 	     hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
862 	    is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
863 		int minhwtype = 0, api = 0;
864 		struct ep11keyblob *kb = (struct ep11keyblob *)
865 			(key + sizeof(struct ep11kblob_header));
866 
867 		if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
868 			return -EINVAL;
869 		if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
870 			minhwtype = ZCRYPT_CEX7;
871 			api = EP11_API_V;
872 		}
873 		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
874 				    minhwtype, api, kb->wkvp);
875 		if (rc)
876 			goto out;
877 	} else if (hdr->type == TOKTYPE_NON_CCA &&
878 		   hdr->version == TOKVER_EP11_AES &&
879 		   is_ep11_keyblob(key)) {
880 		int minhwtype = 0, api = 0;
881 		struct ep11keyblob *kb = (struct ep11keyblob *)key;
882 
883 		if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
884 			return -EINVAL;
885 		if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
886 			minhwtype = ZCRYPT_CEX7;
887 			api = EP11_API_V;
888 		}
889 		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
890 				    minhwtype, api, kb->wkvp);
891 		if (rc)
892 			goto out;
893 	} else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
894 		int minhwtype = ZCRYPT_CEX3C;
895 		u64 cur_mkvp = 0, old_mkvp = 0;
896 
897 		if (hdr->version == TOKVER_CCA_AES) {
898 			struct secaeskeytoken *t = (struct secaeskeytoken *)key;
899 
900 			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
901 				cur_mkvp = t->mkvp;
902 			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
903 				old_mkvp = t->mkvp;
904 		} else if (hdr->version == TOKVER_CCA_VLSC) {
905 			struct cipherkeytoken *t = (struct cipherkeytoken *)key;
906 
907 			minhwtype = ZCRYPT_CEX6;
908 			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
909 				cur_mkvp = t->mkvp0;
910 			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
911 				old_mkvp = t->mkvp0;
912 		} else {
913 			/* unknown cca internal token type */
914 			return -EINVAL;
915 		}
916 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
917 				   minhwtype, AES_MK_SET,
918 				   cur_mkvp, old_mkvp, 1);
919 		if (rc)
920 			goto out;
921 	} else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
922 		u64 cur_mkvp = 0, old_mkvp = 0;
923 		struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
924 
925 		if (t->secid == 0x20) {
926 			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
927 				cur_mkvp = t->mkvp;
928 			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
929 				old_mkvp = t->mkvp;
930 		} else {
931 			/* unknown cca internal 2 token type */
932 			return -EINVAL;
933 		}
934 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
935 				   ZCRYPT_CEX7, APKA_MK_SET,
936 				   cur_mkvp, old_mkvp, 1);
937 		if (rc)
938 			goto out;
939 	} else {
940 		return -EINVAL;
941 	}
942 
943 	if (apqns) {
944 		if (*nr_apqns < _nr_apqns)
945 			rc = -ENOSPC;
946 		else
947 			memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
948 	}
949 	*nr_apqns = _nr_apqns;
950 
951 out:
952 	kfree(_apqns);
953 	return rc;
954 }
955 
956 static int pkey_apqns4keytype(enum pkey_key_type ktype,
957 			      u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
958 			      struct pkey_apqn *apqns, size_t *nr_apqns)
959 {
960 	int rc;
961 	u32 _nr_apqns, *_apqns = NULL;
962 
963 	zcrypt_wait_api_operational();
964 
965 	if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
966 		u64 cur_mkvp = 0, old_mkvp = 0;
967 		int minhwtype = ZCRYPT_CEX3C;
968 
969 		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
970 			cur_mkvp = *((u64 *)cur_mkvp);
971 		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
972 			old_mkvp = *((u64 *)alt_mkvp);
973 		if (ktype == PKEY_TYPE_CCA_CIPHER)
974 			minhwtype = ZCRYPT_CEX6;
975 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
976 				   minhwtype, AES_MK_SET,
977 				   cur_mkvp, old_mkvp, 1);
978 		if (rc)
979 			goto out;
980 	} else if (ktype == PKEY_TYPE_CCA_ECC) {
981 		u64 cur_mkvp = 0, old_mkvp = 0;
982 
983 		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
984 			cur_mkvp = *((u64 *)cur_mkvp);
985 		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
986 			old_mkvp = *((u64 *)alt_mkvp);
987 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
988 				   ZCRYPT_CEX7, APKA_MK_SET,
989 				   cur_mkvp, old_mkvp, 1);
990 		if (rc)
991 			goto out;
992 
993 	} else if (ktype == PKEY_TYPE_EP11 ||
994 		   ktype == PKEY_TYPE_EP11_AES ||
995 		   ktype == PKEY_TYPE_EP11_ECC) {
996 		u8 *wkvp = NULL;
997 
998 		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
999 			wkvp = cur_mkvp;
1000 		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
1001 				    ZCRYPT_CEX7, EP11_API_V, wkvp);
1002 		if (rc)
1003 			goto out;
1004 
1005 	} else {
1006 		return -EINVAL;
1007 	}
1008 
1009 	if (apqns) {
1010 		if (*nr_apqns < _nr_apqns)
1011 			rc = -ENOSPC;
1012 		else
1013 			memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
1014 	}
1015 	*nr_apqns = _nr_apqns;
1016 
1017 out:
1018 	kfree(_apqns);
1019 	return rc;
1020 }
1021 
1022 static int pkey_keyblob2pkey3(const struct pkey_apqn *apqns, size_t nr_apqns,
1023 			      const u8 *key, size_t keylen, u32 *protkeytype,
1024 			      u8 *protkey, u32 *protkeylen)
1025 {
1026 	int i, card, dom, rc;
1027 	struct keytoken_header *hdr = (struct keytoken_header *)key;
1028 
1029 	/* check for at least one apqn given */
1030 	if (!apqns || !nr_apqns)
1031 		return -EINVAL;
1032 
1033 	if (keylen < sizeof(struct keytoken_header))
1034 		return -EINVAL;
1035 
1036 	if (hdr->type == TOKTYPE_NON_CCA &&
1037 	    hdr->version == TOKVER_EP11_AES_WITH_HEADER &&
1038 	    is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1039 		/* EP11 AES key blob with header */
1040 		if (ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1))
1041 			return -EINVAL;
1042 	} else if (hdr->type == TOKTYPE_NON_CCA &&
1043 		   hdr->version == TOKVER_EP11_ECC_WITH_HEADER &&
1044 		   is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1045 		/* EP11 ECC key blob with header */
1046 		if (ep11_check_ecc_key_with_hdr(debug_info, 3, key, keylen, 1))
1047 			return -EINVAL;
1048 	} else if (hdr->type == TOKTYPE_NON_CCA &&
1049 		   hdr->version == TOKVER_EP11_AES &&
1050 		   is_ep11_keyblob(key)) {
1051 		/* EP11 AES key blob with header in session field */
1052 		if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
1053 			return -EINVAL;
1054 	} else	if (hdr->type == TOKTYPE_CCA_INTERNAL) {
1055 		if (hdr->version == TOKVER_CCA_AES) {
1056 			/* CCA AES data key */
1057 			if (keylen != sizeof(struct secaeskeytoken))
1058 				return -EINVAL;
1059 			if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
1060 				return -EINVAL;
1061 		} else if (hdr->version == TOKVER_CCA_VLSC) {
1062 			/* CCA AES cipher key */
1063 			if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
1064 				return -EINVAL;
1065 			if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
1066 				return -EINVAL;
1067 		} else {
1068 			DEBUG_ERR("%s unknown CCA internal token version %d\n",
1069 				  __func__, hdr->version);
1070 			return -EINVAL;
1071 		}
1072 	} else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
1073 		/* CCA ECC (private) key */
1074 		if (keylen < sizeof(struct eccprivkeytoken))
1075 			return -EINVAL;
1076 		if (cca_check_sececckeytoken(debug_info, 3, key, keylen, 1))
1077 			return -EINVAL;
1078 	} else if (hdr->type == TOKTYPE_NON_CCA) {
1079 		struct pkey_protkey pkey;
1080 
1081 		rc = pkey_nonccatok2pkey(key, keylen, &pkey);
1082 		if (rc)
1083 			return rc;
1084 		memcpy(protkey, pkey.protkey, pkey.len);
1085 		*protkeylen = pkey.len;
1086 		*protkeytype = pkey.type;
1087 		return 0;
1088 	} else {
1089 		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
1090 			  __func__, hdr->type);
1091 		return -EINVAL;
1092 	}
1093 
1094 	/* simple try all apqns from the list */
1095 	for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
1096 		card = apqns[i].card;
1097 		dom = apqns[i].domain;
1098 		if (hdr->type == TOKTYPE_NON_CCA &&
1099 		    (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
1100 		     hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
1101 		    is_ep11_keyblob(key + sizeof(struct ep11kblob_header)))
1102 			rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1103 						protkey, protkeylen, protkeytype);
1104 		else if (hdr->type == TOKTYPE_NON_CCA &&
1105 			 hdr->version == TOKVER_EP11_AES &&
1106 			 is_ep11_keyblob(key))
1107 			rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1108 						protkey, protkeylen, protkeytype);
1109 		else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1110 			 hdr->version == TOKVER_CCA_AES)
1111 			rc = cca_sec2protkey(card, dom, key, protkey,
1112 					     protkeylen, protkeytype);
1113 		else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1114 			 hdr->version == TOKVER_CCA_VLSC)
1115 			rc = cca_cipher2protkey(card, dom, key, protkey,
1116 						protkeylen, protkeytype);
1117 		else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA)
1118 			rc = cca_ecc2protkey(card, dom, key, protkey,
1119 					     protkeylen, protkeytype);
1120 		else
1121 			return -EINVAL;
1122 	}
1123 
1124 	return rc;
1125 }
1126 
1127 /*
1128  * File io functions
1129  */
1130 
1131 static void *_copy_key_from_user(void __user *ukey, size_t keylen)
1132 {
1133 	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
1134 		return ERR_PTR(-EINVAL);
1135 
1136 	return memdup_user(ukey, keylen);
1137 }
1138 
1139 static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
1140 {
1141 	if (!uapqns || nr_apqns == 0)
1142 		return NULL;
1143 
1144 	return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
1145 }
1146 
1147 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1148 				unsigned long arg)
1149 {
1150 	int rc;
1151 
1152 	switch (cmd) {
1153 	case PKEY_GENSECK: {
1154 		struct pkey_genseck __user *ugs = (void __user *)arg;
1155 		struct pkey_genseck kgs;
1156 
1157 		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1158 			return -EFAULT;
1159 		rc = cca_genseckey(kgs.cardnr, kgs.domain,
1160 				   kgs.keytype, kgs.seckey.seckey);
1161 		DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
1162 		if (rc)
1163 			break;
1164 		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1165 			return -EFAULT;
1166 		break;
1167 	}
1168 	case PKEY_CLR2SECK: {
1169 		struct pkey_clr2seck __user *ucs = (void __user *)arg;
1170 		struct pkey_clr2seck kcs;
1171 
1172 		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1173 			return -EFAULT;
1174 		rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1175 				    kcs.clrkey.clrkey, kcs.seckey.seckey);
1176 		DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
1177 		if (rc)
1178 			break;
1179 		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1180 			return -EFAULT;
1181 		memzero_explicit(&kcs, sizeof(kcs));
1182 		break;
1183 	}
1184 	case PKEY_SEC2PROTK: {
1185 		struct pkey_sec2protk __user *usp = (void __user *)arg;
1186 		struct pkey_sec2protk ksp;
1187 
1188 		if (copy_from_user(&ksp, usp, sizeof(ksp)))
1189 			return -EFAULT;
1190 		rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
1191 				     ksp.seckey.seckey, ksp.protkey.protkey,
1192 				     &ksp.protkey.len, &ksp.protkey.type);
1193 		DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
1194 		if (rc)
1195 			break;
1196 		if (copy_to_user(usp, &ksp, sizeof(ksp)))
1197 			return -EFAULT;
1198 		break;
1199 	}
1200 	case PKEY_CLR2PROTK: {
1201 		struct pkey_clr2protk __user *ucp = (void __user *)arg;
1202 		struct pkey_clr2protk kcp;
1203 
1204 		if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1205 			return -EFAULT;
1206 		rc = pkey_clr2protkey(kcp.keytype,
1207 				      &kcp.clrkey, &kcp.protkey);
1208 		DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1209 		if (rc)
1210 			break;
1211 		if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1212 			return -EFAULT;
1213 		memzero_explicit(&kcp, sizeof(kcp));
1214 		break;
1215 	}
1216 	case PKEY_FINDCARD: {
1217 		struct pkey_findcard __user *ufc = (void __user *)arg;
1218 		struct pkey_findcard kfc;
1219 
1220 		if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1221 			return -EFAULT;
1222 		rc = cca_findcard(kfc.seckey.seckey,
1223 				  &kfc.cardnr, &kfc.domain, 1);
1224 		DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
1225 		if (rc < 0)
1226 			break;
1227 		if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1228 			return -EFAULT;
1229 		break;
1230 	}
1231 	case PKEY_SKEY2PKEY: {
1232 		struct pkey_skey2pkey __user *usp = (void __user *)arg;
1233 		struct pkey_skey2pkey ksp;
1234 
1235 		if (copy_from_user(&ksp, usp, sizeof(ksp)))
1236 			return -EFAULT;
1237 		rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
1238 		DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1239 		if (rc)
1240 			break;
1241 		if (copy_to_user(usp, &ksp, sizeof(ksp)))
1242 			return -EFAULT;
1243 		break;
1244 	}
1245 	case PKEY_VERIFYKEY: {
1246 		struct pkey_verifykey __user *uvk = (void __user *)arg;
1247 		struct pkey_verifykey kvk;
1248 
1249 		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1250 			return -EFAULT;
1251 		rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1252 				    &kvk.keysize, &kvk.attributes);
1253 		DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1254 		if (rc)
1255 			break;
1256 		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1257 			return -EFAULT;
1258 		break;
1259 	}
1260 	case PKEY_GENPROTK: {
1261 		struct pkey_genprotk __user *ugp = (void __user *)arg;
1262 		struct pkey_genprotk kgp;
1263 
1264 		if (copy_from_user(&kgp, ugp, sizeof(kgp)))
1265 			return -EFAULT;
1266 		rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
1267 		DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
1268 		if (rc)
1269 			break;
1270 		if (copy_to_user(ugp, &kgp, sizeof(kgp)))
1271 			return -EFAULT;
1272 		break;
1273 	}
1274 	case PKEY_VERIFYPROTK: {
1275 		struct pkey_verifyprotk __user *uvp = (void __user *)arg;
1276 		struct pkey_verifyprotk kvp;
1277 
1278 		if (copy_from_user(&kvp, uvp, sizeof(kvp)))
1279 			return -EFAULT;
1280 		rc = pkey_verifyprotkey(&kvp.protkey);
1281 		DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
1282 		break;
1283 	}
1284 	case PKEY_KBLOB2PROTK: {
1285 		struct pkey_kblob2pkey __user *utp = (void __user *)arg;
1286 		struct pkey_kblob2pkey ktp;
1287 		u8 *kkey;
1288 
1289 		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1290 			return -EFAULT;
1291 		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1292 		if (IS_ERR(kkey))
1293 			return PTR_ERR(kkey);
1294 		rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
1295 		DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
1296 		memzero_explicit(kkey, ktp.keylen);
1297 		kfree(kkey);
1298 		if (rc)
1299 			break;
1300 		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1301 			return -EFAULT;
1302 		break;
1303 	}
1304 	case PKEY_GENSECK2: {
1305 		struct pkey_genseck2 __user *ugs = (void __user *)arg;
1306 		struct pkey_genseck2 kgs;
1307 		struct pkey_apqn *apqns;
1308 		size_t klen = KEYBLOBBUFSIZE;
1309 		u8 *kkey;
1310 
1311 		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1312 			return -EFAULT;
1313 		apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1314 		if (IS_ERR(apqns))
1315 			return PTR_ERR(apqns);
1316 		kkey = kmalloc(klen, GFP_KERNEL);
1317 		if (!kkey) {
1318 			kfree(apqns);
1319 			return -ENOMEM;
1320 		}
1321 		rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1322 				     kgs.type, kgs.size, kgs.keygenflags,
1323 				     kkey, &klen);
1324 		DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1325 		kfree(apqns);
1326 		if (rc) {
1327 			kfree(kkey);
1328 			break;
1329 		}
1330 		if (kgs.key) {
1331 			if (kgs.keylen < klen) {
1332 				kfree(kkey);
1333 				return -EINVAL;
1334 			}
1335 			if (copy_to_user(kgs.key, kkey, klen)) {
1336 				kfree(kkey);
1337 				return -EFAULT;
1338 			}
1339 		}
1340 		kgs.keylen = klen;
1341 		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1342 			rc = -EFAULT;
1343 		kfree(kkey);
1344 		break;
1345 	}
1346 	case PKEY_CLR2SECK2: {
1347 		struct pkey_clr2seck2 __user *ucs = (void __user *)arg;
1348 		struct pkey_clr2seck2 kcs;
1349 		struct pkey_apqn *apqns;
1350 		size_t klen = KEYBLOBBUFSIZE;
1351 		u8 *kkey;
1352 
1353 		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1354 			return -EFAULT;
1355 		apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1356 		if (IS_ERR(apqns))
1357 			return PTR_ERR(apqns);
1358 		kkey = kmalloc(klen, GFP_KERNEL);
1359 		if (!kkey) {
1360 			kfree(apqns);
1361 			return -ENOMEM;
1362 		}
1363 		rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1364 				      kcs.type, kcs.size, kcs.keygenflags,
1365 				      kcs.clrkey.clrkey, kkey, &klen);
1366 		DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1367 		kfree(apqns);
1368 		if (rc) {
1369 			kfree(kkey);
1370 			break;
1371 		}
1372 		if (kcs.key) {
1373 			if (kcs.keylen < klen) {
1374 				kfree(kkey);
1375 				return -EINVAL;
1376 			}
1377 			if (copy_to_user(kcs.key, kkey, klen)) {
1378 				kfree(kkey);
1379 				return -EFAULT;
1380 			}
1381 		}
1382 		kcs.keylen = klen;
1383 		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1384 			rc = -EFAULT;
1385 		memzero_explicit(&kcs, sizeof(kcs));
1386 		kfree(kkey);
1387 		break;
1388 	}
1389 	case PKEY_VERIFYKEY2: {
1390 		struct pkey_verifykey2 __user *uvk = (void __user *)arg;
1391 		struct pkey_verifykey2 kvk;
1392 		u8 *kkey;
1393 
1394 		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1395 			return -EFAULT;
1396 		kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1397 		if (IS_ERR(kkey))
1398 			return PTR_ERR(kkey);
1399 		rc = pkey_verifykey2(kkey, kvk.keylen,
1400 				     &kvk.cardnr, &kvk.domain,
1401 				     &kvk.type, &kvk.size, &kvk.flags);
1402 		DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1403 		kfree(kkey);
1404 		if (rc)
1405 			break;
1406 		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1407 			return -EFAULT;
1408 		break;
1409 	}
1410 	case PKEY_KBLOB2PROTK2: {
1411 		struct pkey_kblob2pkey2 __user *utp = (void __user *)arg;
1412 		struct pkey_kblob2pkey2 ktp;
1413 		struct pkey_apqn *apqns = NULL;
1414 		u8 *kkey;
1415 
1416 		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1417 			return -EFAULT;
1418 		apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1419 		if (IS_ERR(apqns))
1420 			return PTR_ERR(apqns);
1421 		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1422 		if (IS_ERR(kkey)) {
1423 			kfree(apqns);
1424 			return PTR_ERR(kkey);
1425 		}
1426 		rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1427 					kkey, ktp.keylen, &ktp.protkey);
1428 		DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1429 		kfree(apqns);
1430 		memzero_explicit(kkey, ktp.keylen);
1431 		kfree(kkey);
1432 		if (rc)
1433 			break;
1434 		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1435 			return -EFAULT;
1436 		break;
1437 	}
1438 	case PKEY_APQNS4K: {
1439 		struct pkey_apqns4key __user *uak = (void __user *)arg;
1440 		struct pkey_apqns4key kak;
1441 		struct pkey_apqn *apqns = NULL;
1442 		size_t nr_apqns, len;
1443 		u8 *kkey;
1444 
1445 		if (copy_from_user(&kak, uak, sizeof(kak)))
1446 			return -EFAULT;
1447 		nr_apqns = kak.apqn_entries;
1448 		if (nr_apqns) {
1449 			apqns = kmalloc_array(nr_apqns,
1450 					      sizeof(struct pkey_apqn),
1451 					      GFP_KERNEL);
1452 			if (!apqns)
1453 				return -ENOMEM;
1454 		}
1455 		kkey = _copy_key_from_user(kak.key, kak.keylen);
1456 		if (IS_ERR(kkey)) {
1457 			kfree(apqns);
1458 			return PTR_ERR(kkey);
1459 		}
1460 		rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1461 				    apqns, &nr_apqns);
1462 		DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1463 		kfree(kkey);
1464 		if (rc && rc != -ENOSPC) {
1465 			kfree(apqns);
1466 			break;
1467 		}
1468 		if (!rc && kak.apqns) {
1469 			if (nr_apqns > kak.apqn_entries) {
1470 				kfree(apqns);
1471 				return -EINVAL;
1472 			}
1473 			len = nr_apqns * sizeof(struct pkey_apqn);
1474 			if (len) {
1475 				if (copy_to_user(kak.apqns, apqns, len)) {
1476 					kfree(apqns);
1477 					return -EFAULT;
1478 				}
1479 			}
1480 		}
1481 		kak.apqn_entries = nr_apqns;
1482 		if (copy_to_user(uak, &kak, sizeof(kak)))
1483 			rc = -EFAULT;
1484 		kfree(apqns);
1485 		break;
1486 	}
1487 	case PKEY_APQNS4KT: {
1488 		struct pkey_apqns4keytype __user *uat = (void __user *)arg;
1489 		struct pkey_apqns4keytype kat;
1490 		struct pkey_apqn *apqns = NULL;
1491 		size_t nr_apqns, len;
1492 
1493 		if (copy_from_user(&kat, uat, sizeof(kat)))
1494 			return -EFAULT;
1495 		nr_apqns = kat.apqn_entries;
1496 		if (nr_apqns) {
1497 			apqns = kmalloc_array(nr_apqns,
1498 					      sizeof(struct pkey_apqn),
1499 					      GFP_KERNEL);
1500 			if (!apqns)
1501 				return -ENOMEM;
1502 		}
1503 		rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1504 					kat.flags, apqns, &nr_apqns);
1505 		DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1506 		if (rc && rc != -ENOSPC) {
1507 			kfree(apqns);
1508 			break;
1509 		}
1510 		if (!rc && kat.apqns) {
1511 			if (nr_apqns > kat.apqn_entries) {
1512 				kfree(apqns);
1513 				return -EINVAL;
1514 			}
1515 			len = nr_apqns * sizeof(struct pkey_apqn);
1516 			if (len) {
1517 				if (copy_to_user(kat.apqns, apqns, len)) {
1518 					kfree(apqns);
1519 					return -EFAULT;
1520 				}
1521 			}
1522 		}
1523 		kat.apqn_entries = nr_apqns;
1524 		if (copy_to_user(uat, &kat, sizeof(kat)))
1525 			rc = -EFAULT;
1526 		kfree(apqns);
1527 		break;
1528 	}
1529 	case PKEY_KBLOB2PROTK3: {
1530 		struct pkey_kblob2pkey3 __user *utp = (void __user *)arg;
1531 		struct pkey_kblob2pkey3 ktp;
1532 		struct pkey_apqn *apqns = NULL;
1533 		u32 protkeylen = PROTKEYBLOBBUFSIZE;
1534 		u8 *kkey, *protkey;
1535 
1536 		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1537 			return -EFAULT;
1538 		apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1539 		if (IS_ERR(apqns))
1540 			return PTR_ERR(apqns);
1541 		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1542 		if (IS_ERR(kkey)) {
1543 			kfree(apqns);
1544 			return PTR_ERR(kkey);
1545 		}
1546 		protkey = kmalloc(protkeylen, GFP_KERNEL);
1547 		if (!protkey) {
1548 			kfree(apqns);
1549 			kfree(kkey);
1550 			return -ENOMEM;
1551 		}
1552 		rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
1553 					ktp.keylen, &ktp.pkeytype,
1554 					protkey, &protkeylen);
1555 		DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
1556 		kfree(apqns);
1557 		memzero_explicit(kkey, ktp.keylen);
1558 		kfree(kkey);
1559 		if (rc) {
1560 			kfree(protkey);
1561 			break;
1562 		}
1563 		if (ktp.pkey && ktp.pkeylen) {
1564 			if (protkeylen > ktp.pkeylen) {
1565 				kfree(protkey);
1566 				return -EINVAL;
1567 			}
1568 			if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
1569 				kfree(protkey);
1570 				return -EFAULT;
1571 			}
1572 		}
1573 		kfree(protkey);
1574 		ktp.pkeylen = protkeylen;
1575 		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1576 			return -EFAULT;
1577 		break;
1578 	}
1579 	default:
1580 		/* unknown/unsupported ioctl cmd */
1581 		return -ENOTTY;
1582 	}
1583 
1584 	return rc;
1585 }
1586 
1587 /*
1588  * Sysfs and file io operations
1589  */
1590 
1591 /*
1592  * Sysfs attribute read function for all protected key binary attributes.
1593  * The implementation can not deal with partial reads, because a new random
1594  * protected key blob is generated with each read. In case of partial reads
1595  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1596  */
1597 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1598 					  loff_t off, size_t count)
1599 {
1600 	struct protaeskeytoken protkeytoken;
1601 	struct pkey_protkey protkey;
1602 	int rc;
1603 
1604 	if (off != 0 || count < sizeof(protkeytoken))
1605 		return -EINVAL;
1606 	if (is_xts)
1607 		if (count < 2 * sizeof(protkeytoken))
1608 			return -EINVAL;
1609 
1610 	memset(&protkeytoken, 0, sizeof(protkeytoken));
1611 	protkeytoken.type = TOKTYPE_NON_CCA;
1612 	protkeytoken.version = TOKVER_PROTECTED_KEY;
1613 	protkeytoken.keytype = keytype;
1614 
1615 	rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1616 	if (rc)
1617 		return rc;
1618 
1619 	protkeytoken.len = protkey.len;
1620 	memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1621 
1622 	memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1623 
1624 	if (is_xts) {
1625 		rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1626 		if (rc)
1627 			return rc;
1628 
1629 		protkeytoken.len = protkey.len;
1630 		memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1631 
1632 		memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1633 		       sizeof(protkeytoken));
1634 
1635 		return 2 * sizeof(protkeytoken);
1636 	}
1637 
1638 	return sizeof(protkeytoken);
1639 }
1640 
1641 static ssize_t protkey_aes_128_read(struct file *filp,
1642 				    struct kobject *kobj,
1643 				    struct bin_attribute *attr,
1644 				    char *buf, loff_t off,
1645 				    size_t count)
1646 {
1647 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1648 					  off, count);
1649 }
1650 
1651 static ssize_t protkey_aes_192_read(struct file *filp,
1652 				    struct kobject *kobj,
1653 				    struct bin_attribute *attr,
1654 				    char *buf, loff_t off,
1655 				    size_t count)
1656 {
1657 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1658 					  off, count);
1659 }
1660 
1661 static ssize_t protkey_aes_256_read(struct file *filp,
1662 				    struct kobject *kobj,
1663 				    struct bin_attribute *attr,
1664 				    char *buf, loff_t off,
1665 				    size_t count)
1666 {
1667 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1668 					  off, count);
1669 }
1670 
1671 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1672 					struct kobject *kobj,
1673 					struct bin_attribute *attr,
1674 					char *buf, loff_t off,
1675 					size_t count)
1676 {
1677 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1678 					  off, count);
1679 }
1680 
1681 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1682 					struct kobject *kobj,
1683 					struct bin_attribute *attr,
1684 					char *buf, loff_t off,
1685 					size_t count)
1686 {
1687 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1688 					  off, count);
1689 }
1690 
1691 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1692 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1693 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1694 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1695 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1696 
1697 static struct bin_attribute *protkey_attrs[] = {
1698 	&bin_attr_protkey_aes_128,
1699 	&bin_attr_protkey_aes_192,
1700 	&bin_attr_protkey_aes_256,
1701 	&bin_attr_protkey_aes_128_xts,
1702 	&bin_attr_protkey_aes_256_xts,
1703 	NULL
1704 };
1705 
1706 static struct attribute_group protkey_attr_group = {
1707 	.name	   = "protkey",
1708 	.bin_attrs = protkey_attrs,
1709 };
1710 
1711 /*
1712  * Sysfs attribute read function for all secure key ccadata binary attributes.
1713  * The implementation can not deal with partial reads, because a new random
1714  * protected key blob is generated with each read. In case of partial reads
1715  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1716  */
1717 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1718 					  loff_t off, size_t count)
1719 {
1720 	int rc;
1721 	struct pkey_seckey *seckey = (struct pkey_seckey *)buf;
1722 
1723 	if (off != 0 || count < sizeof(struct secaeskeytoken))
1724 		return -EINVAL;
1725 	if (is_xts)
1726 		if (count < 2 * sizeof(struct secaeskeytoken))
1727 			return -EINVAL;
1728 
1729 	rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1730 	if (rc)
1731 		return rc;
1732 
1733 	if (is_xts) {
1734 		seckey++;
1735 		rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1736 		if (rc)
1737 			return rc;
1738 
1739 		return 2 * sizeof(struct secaeskeytoken);
1740 	}
1741 
1742 	return sizeof(struct secaeskeytoken);
1743 }
1744 
1745 static ssize_t ccadata_aes_128_read(struct file *filp,
1746 				    struct kobject *kobj,
1747 				    struct bin_attribute *attr,
1748 				    char *buf, loff_t off,
1749 				    size_t count)
1750 {
1751 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1752 					  off, count);
1753 }
1754 
1755 static ssize_t ccadata_aes_192_read(struct file *filp,
1756 				    struct kobject *kobj,
1757 				    struct bin_attribute *attr,
1758 				    char *buf, loff_t off,
1759 				    size_t count)
1760 {
1761 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1762 					  off, count);
1763 }
1764 
1765 static ssize_t ccadata_aes_256_read(struct file *filp,
1766 				    struct kobject *kobj,
1767 				    struct bin_attribute *attr,
1768 				    char *buf, loff_t off,
1769 				    size_t count)
1770 {
1771 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1772 					  off, count);
1773 }
1774 
1775 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1776 					struct kobject *kobj,
1777 					struct bin_attribute *attr,
1778 					char *buf, loff_t off,
1779 					size_t count)
1780 {
1781 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1782 					  off, count);
1783 }
1784 
1785 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1786 					struct kobject *kobj,
1787 					struct bin_attribute *attr,
1788 					char *buf, loff_t off,
1789 					size_t count)
1790 {
1791 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1792 					  off, count);
1793 }
1794 
1795 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1796 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1797 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1798 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1799 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1800 
1801 static struct bin_attribute *ccadata_attrs[] = {
1802 	&bin_attr_ccadata_aes_128,
1803 	&bin_attr_ccadata_aes_192,
1804 	&bin_attr_ccadata_aes_256,
1805 	&bin_attr_ccadata_aes_128_xts,
1806 	&bin_attr_ccadata_aes_256_xts,
1807 	NULL
1808 };
1809 
1810 static struct attribute_group ccadata_attr_group = {
1811 	.name	   = "ccadata",
1812 	.bin_attrs = ccadata_attrs,
1813 };
1814 
1815 #define CCACIPHERTOKENSIZE	(sizeof(struct cipherkeytoken) + 80)
1816 
1817 /*
1818  * Sysfs attribute read function for all secure key ccacipher binary attributes.
1819  * The implementation can not deal with partial reads, because a new random
1820  * secure key blob is generated with each read. In case of partial reads
1821  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1822  */
1823 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1824 					    bool is_xts, char *buf, loff_t off,
1825 					    size_t count)
1826 {
1827 	int i, rc, card, dom;
1828 	u32 nr_apqns, *apqns = NULL;
1829 	size_t keysize = CCACIPHERTOKENSIZE;
1830 
1831 	if (off != 0 || count < CCACIPHERTOKENSIZE)
1832 		return -EINVAL;
1833 	if (is_xts)
1834 		if (count < 2 * CCACIPHERTOKENSIZE)
1835 			return -EINVAL;
1836 
1837 	/* build a list of apqns able to generate an cipher key */
1838 	rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1839 			   ZCRYPT_CEX6, 0, 0, 0, 0);
1840 	if (rc)
1841 		return rc;
1842 
1843 	memset(buf, 0, is_xts ? 2 * keysize : keysize);
1844 
1845 	/* simple try all apqns from the list */
1846 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1847 		card = apqns[i] >> 16;
1848 		dom = apqns[i] & 0xFFFF;
1849 		rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1850 		if (rc == 0)
1851 			break;
1852 	}
1853 	if (rc)
1854 		return rc;
1855 
1856 	if (is_xts) {
1857 		keysize = CCACIPHERTOKENSIZE;
1858 		buf += CCACIPHERTOKENSIZE;
1859 		rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1860 		if (rc == 0)
1861 			return 2 * CCACIPHERTOKENSIZE;
1862 	}
1863 
1864 	return CCACIPHERTOKENSIZE;
1865 }
1866 
1867 static ssize_t ccacipher_aes_128_read(struct file *filp,
1868 				      struct kobject *kobj,
1869 				      struct bin_attribute *attr,
1870 				      char *buf, loff_t off,
1871 				      size_t count)
1872 {
1873 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1874 					    off, count);
1875 }
1876 
1877 static ssize_t ccacipher_aes_192_read(struct file *filp,
1878 				      struct kobject *kobj,
1879 				      struct bin_attribute *attr,
1880 				      char *buf, loff_t off,
1881 				      size_t count)
1882 {
1883 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1884 					    off, count);
1885 }
1886 
1887 static ssize_t ccacipher_aes_256_read(struct file *filp,
1888 				      struct kobject *kobj,
1889 				      struct bin_attribute *attr,
1890 				      char *buf, loff_t off,
1891 				      size_t count)
1892 {
1893 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1894 					    off, count);
1895 }
1896 
1897 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1898 					  struct kobject *kobj,
1899 					  struct bin_attribute *attr,
1900 					  char *buf, loff_t off,
1901 					  size_t count)
1902 {
1903 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1904 					    off, count);
1905 }
1906 
1907 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1908 					  struct kobject *kobj,
1909 					  struct bin_attribute *attr,
1910 					  char *buf, loff_t off,
1911 					  size_t count)
1912 {
1913 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1914 					    off, count);
1915 }
1916 
1917 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1918 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1919 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1920 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1921 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1922 
1923 static struct bin_attribute *ccacipher_attrs[] = {
1924 	&bin_attr_ccacipher_aes_128,
1925 	&bin_attr_ccacipher_aes_192,
1926 	&bin_attr_ccacipher_aes_256,
1927 	&bin_attr_ccacipher_aes_128_xts,
1928 	&bin_attr_ccacipher_aes_256_xts,
1929 	NULL
1930 };
1931 
1932 static struct attribute_group ccacipher_attr_group = {
1933 	.name	   = "ccacipher",
1934 	.bin_attrs = ccacipher_attrs,
1935 };
1936 
1937 /*
1938  * Sysfs attribute read function for all ep11 aes key binary attributes.
1939  * The implementation can not deal with partial reads, because a new random
1940  * secure key blob is generated with each read. In case of partial reads
1941  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1942  * This function and the sysfs attributes using it provide EP11 key blobs
1943  * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1944  * 320 bytes.
1945  */
1946 static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1947 				       bool is_xts, char *buf, loff_t off,
1948 				       size_t count)
1949 {
1950 	int i, rc, card, dom;
1951 	u32 nr_apqns, *apqns = NULL;
1952 	size_t keysize = MAXEP11AESKEYBLOBSIZE;
1953 
1954 	if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1955 		return -EINVAL;
1956 	if (is_xts)
1957 		if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1958 			return -EINVAL;
1959 
1960 	/* build a list of apqns able to generate an cipher key */
1961 	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1962 			    ZCRYPT_CEX7, EP11_API_V, NULL);
1963 	if (rc)
1964 		return rc;
1965 
1966 	memset(buf, 0, is_xts ? 2 * keysize : keysize);
1967 
1968 	/* simple try all apqns from the list */
1969 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1970 		card = apqns[i] >> 16;
1971 		dom = apqns[i] & 0xFFFF;
1972 		rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1973 		if (rc == 0)
1974 			break;
1975 	}
1976 	if (rc)
1977 		return rc;
1978 
1979 	if (is_xts) {
1980 		keysize = MAXEP11AESKEYBLOBSIZE;
1981 		buf += MAXEP11AESKEYBLOBSIZE;
1982 		rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1983 		if (rc == 0)
1984 			return 2 * MAXEP11AESKEYBLOBSIZE;
1985 	}
1986 
1987 	return MAXEP11AESKEYBLOBSIZE;
1988 }
1989 
1990 static ssize_t ep11_aes_128_read(struct file *filp,
1991 				 struct kobject *kobj,
1992 				 struct bin_attribute *attr,
1993 				 char *buf, loff_t off,
1994 				 size_t count)
1995 {
1996 	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1997 				       off, count);
1998 }
1999 
2000 static ssize_t ep11_aes_192_read(struct file *filp,
2001 				 struct kobject *kobj,
2002 				 struct bin_attribute *attr,
2003 				 char *buf, loff_t off,
2004 				 size_t count)
2005 {
2006 	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
2007 				       off, count);
2008 }
2009 
2010 static ssize_t ep11_aes_256_read(struct file *filp,
2011 				 struct kobject *kobj,
2012 				 struct bin_attribute *attr,
2013 				 char *buf, loff_t off,
2014 				 size_t count)
2015 {
2016 	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
2017 				       off, count);
2018 }
2019 
2020 static ssize_t ep11_aes_128_xts_read(struct file *filp,
2021 				     struct kobject *kobj,
2022 				     struct bin_attribute *attr,
2023 				     char *buf, loff_t off,
2024 				     size_t count)
2025 {
2026 	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
2027 				       off, count);
2028 }
2029 
2030 static ssize_t ep11_aes_256_xts_read(struct file *filp,
2031 				     struct kobject *kobj,
2032 				     struct bin_attribute *attr,
2033 				     char *buf, loff_t off,
2034 				     size_t count)
2035 {
2036 	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
2037 				       off, count);
2038 }
2039 
2040 static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2041 static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2042 static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2043 static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2044 static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2045 
2046 static struct bin_attribute *ep11_attrs[] = {
2047 	&bin_attr_ep11_aes_128,
2048 	&bin_attr_ep11_aes_192,
2049 	&bin_attr_ep11_aes_256,
2050 	&bin_attr_ep11_aes_128_xts,
2051 	&bin_attr_ep11_aes_256_xts,
2052 	NULL
2053 };
2054 
2055 static struct attribute_group ep11_attr_group = {
2056 	.name	   = "ep11",
2057 	.bin_attrs = ep11_attrs,
2058 };
2059 
2060 static const struct attribute_group *pkey_attr_groups[] = {
2061 	&protkey_attr_group,
2062 	&ccadata_attr_group,
2063 	&ccacipher_attr_group,
2064 	&ep11_attr_group,
2065 	NULL,
2066 };
2067 
2068 static const struct file_operations pkey_fops = {
2069 	.owner		= THIS_MODULE,
2070 	.open		= nonseekable_open,
2071 	.llseek		= no_llseek,
2072 	.unlocked_ioctl = pkey_unlocked_ioctl,
2073 };
2074 
2075 static struct miscdevice pkey_dev = {
2076 	.name	= "pkey",
2077 	.minor	= MISC_DYNAMIC_MINOR,
2078 	.mode	= 0666,
2079 	.fops	= &pkey_fops,
2080 	.groups = pkey_attr_groups,
2081 };
2082 
2083 /*
2084  * Module init
2085  */
2086 static int __init pkey_init(void)
2087 {
2088 	cpacf_mask_t func_mask;
2089 
2090 	/*
2091 	 * The pckmo instruction should be available - even if we don't
2092 	 * actually invoke it. This instruction comes with MSA 3 which
2093 	 * is also the minimum level for the kmc instructions which
2094 	 * are able to work with protected keys.
2095 	 */
2096 	if (!cpacf_query(CPACF_PCKMO, &func_mask))
2097 		return -ENODEV;
2098 
2099 	/* check for kmc instructions available */
2100 	if (!cpacf_query(CPACF_KMC, &func_mask))
2101 		return -ENODEV;
2102 	if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) ||
2103 	    !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) ||
2104 	    !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256))
2105 		return -ENODEV;
2106 
2107 	pkey_debug_init();
2108 
2109 	return misc_register(&pkey_dev);
2110 }
2111 
2112 /*
2113  * Module exit
2114  */
2115 static void __exit pkey_exit(void)
2116 {
2117 	misc_deregister(&pkey_dev);
2118 	pkey_debug_exit();
2119 }
2120 
2121 module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
2122 module_exit(pkey_exit);
2123