xref: /openbmc/linux/drivers/s390/crypto/pkey_api.c (revision 7b73a9c8)
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 
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("IBM Corporation");
31 MODULE_DESCRIPTION("s390 protected key interface");
32 
33 #define KEYBLOBBUFSIZE 8192  /* key buffer size used for internal processing */
34 #define MAXAPQNSINLIST 64    /* max 64 apqns within a apqn list */
35 
36 /* mask of available pckmo subfunctions, fetched once at module init */
37 static cpacf_mask_t pckmo_functions;
38 
39 /*
40  * debug feature data and functions
41  */
42 
43 static debug_info_t *debug_info;
44 
45 #define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
46 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
47 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
48 #define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
49 
50 static void __init pkey_debug_init(void)
51 {
52 	/* 5 arguments per dbf entry (including the format string ptr) */
53 	debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
54 	debug_register_view(debug_info, &debug_sprintf_view);
55 	debug_set_level(debug_info, 3);
56 }
57 
58 static void __exit pkey_debug_exit(void)
59 {
60 	debug_unregister(debug_info);
61 }
62 
63 /* inside view of a protected key token (only type 0x00 version 0x01) */
64 struct protaeskeytoken {
65 	u8  type;     /* 0x00 for PAES specific key tokens */
66 	u8  res0[3];
67 	u8  version;  /* should be 0x01 for protected AES key token */
68 	u8  res1[3];
69 	u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
70 	u32 len;      /* bytes actually stored in protkey[] */
71 	u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
72 } __packed;
73 
74 /*
75  * Create a protected key from a clear key value.
76  */
77 static int pkey_clr2protkey(u32 keytype,
78 			    const struct pkey_clrkey *clrkey,
79 			    struct pkey_protkey *protkey)
80 {
81 	long fc;
82 	int keysize;
83 	u8 paramblock[64];
84 
85 	switch (keytype) {
86 	case PKEY_KEYTYPE_AES_128:
87 		keysize = 16;
88 		fc = CPACF_PCKMO_ENC_AES_128_KEY;
89 		break;
90 	case PKEY_KEYTYPE_AES_192:
91 		keysize = 24;
92 		fc = CPACF_PCKMO_ENC_AES_192_KEY;
93 		break;
94 	case PKEY_KEYTYPE_AES_256:
95 		keysize = 32;
96 		fc = CPACF_PCKMO_ENC_AES_256_KEY;
97 		break;
98 	default:
99 		DEBUG_ERR("%s unknown/unsupported keytype %d\n",
100 			  __func__, keytype);
101 		return -EINVAL;
102 	}
103 
104 	/*
105 	 * Check if the needed pckmo subfunction is available.
106 	 * These subfunctions can be enabled/disabled by customers
107 	 * in the LPAR profile or may even change on the fly.
108 	 */
109 	if (!cpacf_test_func(&pckmo_functions, fc)) {
110 		DEBUG_ERR("%s pckmo functions not available\n", __func__);
111 		return -ENODEV;
112 	}
113 
114 	/* prepare param block */
115 	memset(paramblock, 0, sizeof(paramblock));
116 	memcpy(paramblock, clrkey->clrkey, keysize);
117 
118 	/* call the pckmo instruction */
119 	cpacf_pckmo(fc, paramblock);
120 
121 	/* copy created protected key */
122 	protkey->type = keytype;
123 	protkey->len = keysize + 32;
124 	memcpy(protkey->protkey, paramblock, keysize + 32);
125 
126 	return 0;
127 }
128 
129 /*
130  * Find card and transform secure key into protected key.
131  */
132 static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
133 {
134 	int rc, verify;
135 	u16 cardnr, domain;
136 	struct keytoken_header *hdr = (struct keytoken_header *)key;
137 
138 	/*
139 	 * The cca_xxx2protkey call may fail when a card has been
140 	 * addressed where the master key was changed after last fetch
141 	 * of the mkvp into the cache. Try 3 times: First witout verify
142 	 * then with verify and last round with verify and old master
143 	 * key verification pattern match not ignored.
144 	 */
145 	for (verify = 0; verify < 3; verify++) {
146 		rc = cca_findcard(key, &cardnr, &domain, verify);
147 		if (rc < 0)
148 			continue;
149 		if (rc > 0 && verify < 2)
150 			continue;
151 		switch (hdr->version) {
152 		case TOKVER_CCA_AES:
153 			rc = cca_sec2protkey(cardnr, domain,
154 					     key, pkey->protkey,
155 					     &pkey->len, &pkey->type);
156 			break;
157 		case TOKVER_CCA_VLSC:
158 			rc = cca_cipher2protkey(cardnr, domain,
159 						key, pkey->protkey,
160 						&pkey->len, &pkey->type);
161 			break;
162 		default:
163 			return -EINVAL;
164 		}
165 		if (rc == 0)
166 			break;
167 	}
168 
169 	if (rc)
170 		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
171 
172 	return rc;
173 }
174 
175 /*
176  * Verify key and give back some info about the key.
177  */
178 static int pkey_verifykey(const struct pkey_seckey *seckey,
179 			  u16 *pcardnr, u16 *pdomain,
180 			  u16 *pkeysize, u32 *pattributes)
181 {
182 	struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
183 	u16 cardnr, domain;
184 	int rc;
185 
186 	/* check the secure key for valid AES secure key */
187 	rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
188 	if (rc)
189 		goto out;
190 	if (pattributes)
191 		*pattributes = PKEY_VERIFY_ATTR_AES;
192 	if (pkeysize)
193 		*pkeysize = t->bitsize;
194 
195 	/* try to find a card which can handle this key */
196 	rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
197 	if (rc < 0)
198 		goto out;
199 
200 	if (rc > 0) {
201 		/* key mkvp matches to old master key mkvp */
202 		DEBUG_DBG("%s secure key has old mkvp\n", __func__);
203 		if (pattributes)
204 			*pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
205 		rc = 0;
206 	}
207 
208 	if (pcardnr)
209 		*pcardnr = cardnr;
210 	if (pdomain)
211 		*pdomain = domain;
212 
213 out:
214 	DEBUG_DBG("%s rc=%d\n", __func__, rc);
215 	return rc;
216 }
217 
218 /*
219  * Generate a random protected key
220  */
221 static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
222 {
223 	struct pkey_clrkey clrkey;
224 	int keysize;
225 	int rc;
226 
227 	switch (keytype) {
228 	case PKEY_KEYTYPE_AES_128:
229 		keysize = 16;
230 		break;
231 	case PKEY_KEYTYPE_AES_192:
232 		keysize = 24;
233 		break;
234 	case PKEY_KEYTYPE_AES_256:
235 		keysize = 32;
236 		break;
237 	default:
238 		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
239 			  keytype);
240 		return -EINVAL;
241 	}
242 
243 	/* generate a dummy random clear key */
244 	get_random_bytes(clrkey.clrkey, keysize);
245 
246 	/* convert it to a dummy protected key */
247 	rc = pkey_clr2protkey(keytype, &clrkey, protkey);
248 	if (rc)
249 		return rc;
250 
251 	/* replace the key part of the protected key with random bytes */
252 	get_random_bytes(protkey->protkey, keysize);
253 
254 	return 0;
255 }
256 
257 /*
258  * Verify if a protected key is still valid
259  */
260 static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
261 {
262 	unsigned long fc;
263 	struct {
264 		u8 iv[AES_BLOCK_SIZE];
265 		u8 key[MAXPROTKEYSIZE];
266 	} param;
267 	u8 null_msg[AES_BLOCK_SIZE];
268 	u8 dest_buf[AES_BLOCK_SIZE];
269 	unsigned int k;
270 
271 	switch (protkey->type) {
272 	case PKEY_KEYTYPE_AES_128:
273 		fc = CPACF_KMC_PAES_128;
274 		break;
275 	case PKEY_KEYTYPE_AES_192:
276 		fc = CPACF_KMC_PAES_192;
277 		break;
278 	case PKEY_KEYTYPE_AES_256:
279 		fc = CPACF_KMC_PAES_256;
280 		break;
281 	default:
282 		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
283 			  protkey->type);
284 		return -EINVAL;
285 	}
286 
287 	memset(null_msg, 0, sizeof(null_msg));
288 
289 	memset(param.iv, 0, sizeof(param.iv));
290 	memcpy(param.key, protkey->protkey, sizeof(param.key));
291 
292 	k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
293 		      sizeof(null_msg));
294 	if (k != sizeof(null_msg)) {
295 		DEBUG_ERR("%s protected key is not valid\n", __func__);
296 		return -EKEYREJECTED;
297 	}
298 
299 	return 0;
300 }
301 
302 /*
303  * Transform a non-CCA key token into a protected key
304  */
305 static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
306 			       struct pkey_protkey *protkey)
307 {
308 	struct keytoken_header *hdr = (struct keytoken_header *)key;
309 	struct protaeskeytoken *t;
310 
311 	switch (hdr->version) {
312 	case TOKVER_PROTECTED_KEY:
313 		if (keylen != sizeof(struct protaeskeytoken))
314 			return -EINVAL;
315 
316 		t = (struct protaeskeytoken *)key;
317 		protkey->len = t->len;
318 		protkey->type = t->keytype;
319 		memcpy(protkey->protkey, t->protkey,
320 		       sizeof(protkey->protkey));
321 
322 		return pkey_verifyprotkey(protkey);
323 	default:
324 		DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
325 			  __func__, hdr->version);
326 		return -EINVAL;
327 	}
328 }
329 
330 /*
331  * Transform a CCA internal key token into a protected key
332  */
333 static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
334 			       struct pkey_protkey *protkey)
335 {
336 	struct keytoken_header *hdr = (struct keytoken_header *)key;
337 
338 	switch (hdr->version) {
339 	case TOKVER_CCA_AES:
340 		if (keylen != sizeof(struct secaeskeytoken))
341 			return -EINVAL;
342 		break;
343 	case TOKVER_CCA_VLSC:
344 		if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
345 			return -EINVAL;
346 		break;
347 	default:
348 		DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
349 			  __func__, hdr->version);
350 		return -EINVAL;
351 	}
352 
353 	return pkey_skey2pkey(key, protkey);
354 }
355 
356 /*
357  * Transform a key blob (of any type) into a protected key
358  */
359 int pkey_keyblob2pkey(const u8 *key, u32 keylen,
360 		      struct pkey_protkey *protkey)
361 {
362 	int rc;
363 	struct keytoken_header *hdr = (struct keytoken_header *)key;
364 
365 	if (keylen < sizeof(struct keytoken_header)) {
366 		DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
367 		return -EINVAL;
368 	}
369 
370 	switch (hdr->type) {
371 	case TOKTYPE_NON_CCA:
372 		rc = pkey_nonccatok2pkey(key, keylen, protkey);
373 		break;
374 	case TOKTYPE_CCA_INTERNAL:
375 		rc = pkey_ccainttok2pkey(key, keylen, protkey);
376 		break;
377 	default:
378 		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
379 			  __func__, hdr->type);
380 		return -EINVAL;
381 	}
382 
383 	DEBUG_DBG("%s rc=%d\n", __func__, rc);
384 	return rc;
385 
386 }
387 EXPORT_SYMBOL(pkey_keyblob2pkey);
388 
389 static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
390 			   enum pkey_key_type ktype, enum pkey_key_size ksize,
391 			   u32 kflags, u8 *keybuf, size_t *keybufsize)
392 {
393 	int i, card, dom, rc;
394 
395 	/* check for at least one apqn given */
396 	if (!apqns || !nr_apqns)
397 		return -EINVAL;
398 
399 	/* check key type and size */
400 	switch (ktype) {
401 	case PKEY_TYPE_CCA_DATA:
402 	case PKEY_TYPE_CCA_CIPHER:
403 		if (*keybufsize < SECKEYBLOBSIZE)
404 			return -EINVAL;
405 		break;
406 	default:
407 		return -EINVAL;
408 	}
409 	switch (ksize) {
410 	case PKEY_SIZE_AES_128:
411 	case PKEY_SIZE_AES_192:
412 	case PKEY_SIZE_AES_256:
413 		break;
414 	default:
415 		return -EINVAL;
416 	}
417 
418 	/* simple try all apqns from the list */
419 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
420 		card = apqns[i].card;
421 		dom = apqns[i].domain;
422 		if (ktype == PKEY_TYPE_CCA_DATA) {
423 			rc = cca_genseckey(card, dom, ksize, keybuf);
424 			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
425 		} else /* TOKVER_CCA_VLSC */
426 			rc = cca_gencipherkey(card, dom, ksize, kflags,
427 					      keybuf, keybufsize);
428 		if (rc == 0)
429 			break;
430 	}
431 
432 	return rc;
433 }
434 
435 static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
436 			    enum pkey_key_type ktype, enum pkey_key_size ksize,
437 			    u32 kflags, const u8 *clrkey,
438 			    u8 *keybuf, size_t *keybufsize)
439 {
440 	int i, card, dom, rc;
441 
442 	/* check for at least one apqn given */
443 	if (!apqns || !nr_apqns)
444 		return -EINVAL;
445 
446 	/* check key type and size */
447 	switch (ktype) {
448 	case PKEY_TYPE_CCA_DATA:
449 	case PKEY_TYPE_CCA_CIPHER:
450 		if (*keybufsize < SECKEYBLOBSIZE)
451 			return -EINVAL;
452 		break;
453 	default:
454 		return -EINVAL;
455 	}
456 	switch (ksize) {
457 	case PKEY_SIZE_AES_128:
458 	case PKEY_SIZE_AES_192:
459 	case PKEY_SIZE_AES_256:
460 		break;
461 	default:
462 		return -EINVAL;
463 	}
464 
465 	/* simple try all apqns from the list */
466 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
467 		card = apqns[i].card;
468 		dom = apqns[i].domain;
469 		if (ktype == PKEY_TYPE_CCA_DATA) {
470 			rc = cca_clr2seckey(card, dom, ksize,
471 					    clrkey, keybuf);
472 			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
473 		} else /* TOKVER_CCA_VLSC */
474 			rc = cca_clr2cipherkey(card, dom, ksize, kflags,
475 					       clrkey, keybuf, keybufsize);
476 		if (rc == 0)
477 			break;
478 	}
479 
480 	return rc;
481 }
482 
483 static int pkey_verifykey2(const u8 *key, size_t keylen,
484 			   u16 *cardnr, u16 *domain,
485 			   enum pkey_key_type *ktype,
486 			   enum pkey_key_size *ksize, u32 *flags)
487 {
488 	int rc;
489 	u32 _nr_apqns, *_apqns = NULL;
490 	struct keytoken_header *hdr = (struct keytoken_header *)key;
491 
492 	if (keylen < sizeof(struct keytoken_header) ||
493 	    hdr->type != TOKTYPE_CCA_INTERNAL)
494 		return -EINVAL;
495 
496 	if (hdr->version == TOKVER_CCA_AES) {
497 		struct secaeskeytoken *t = (struct secaeskeytoken *)key;
498 
499 		rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
500 		if (rc)
501 			goto out;
502 		if (ktype)
503 			*ktype = PKEY_TYPE_CCA_DATA;
504 		if (ksize)
505 			*ksize = (enum pkey_key_size) t->bitsize;
506 
507 		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
508 				   ZCRYPT_CEX3C, t->mkvp, 0, 1);
509 		if (rc == 0 && flags)
510 			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
511 		if (rc == -ENODEV) {
512 			rc = cca_findcard2(&_apqns, &_nr_apqns,
513 					   *cardnr, *domain,
514 					   ZCRYPT_CEX3C, 0, t->mkvp, 1);
515 			if (rc == 0 && flags)
516 				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
517 		}
518 		if (rc)
519 			goto out;
520 
521 		*cardnr = ((struct pkey_apqn *)_apqns)->card;
522 		*domain = ((struct pkey_apqn *)_apqns)->domain;
523 
524 	} else if (hdr->version == TOKVER_CCA_VLSC) {
525 		struct cipherkeytoken *t = (struct cipherkeytoken *)key;
526 
527 		rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
528 		if (rc)
529 			goto out;
530 		if (ktype)
531 			*ktype = PKEY_TYPE_CCA_CIPHER;
532 		if (ksize) {
533 			*ksize = PKEY_SIZE_UNKNOWN;
534 			if (!t->plfver && t->wpllen == 512)
535 				*ksize = PKEY_SIZE_AES_128;
536 			else if (!t->plfver && t->wpllen == 576)
537 				*ksize = PKEY_SIZE_AES_192;
538 			else if (!t->plfver && t->wpllen == 640)
539 				*ksize = PKEY_SIZE_AES_256;
540 		}
541 
542 		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
543 				   ZCRYPT_CEX6, t->mkvp0, 0, 1);
544 		if (rc == 0 && flags)
545 			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
546 		if (rc == -ENODEV) {
547 			rc = cca_findcard2(&_apqns, &_nr_apqns,
548 					   *cardnr, *domain,
549 					   ZCRYPT_CEX6, 0, t->mkvp0, 1);
550 			if (rc == 0 && flags)
551 				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
552 		}
553 		if (rc)
554 			goto out;
555 
556 		*cardnr = ((struct pkey_apqn *)_apqns)->card;
557 		*domain = ((struct pkey_apqn *)_apqns)->domain;
558 
559 	} else
560 		rc = -EINVAL;
561 
562 out:
563 	kfree(_apqns);
564 	return rc;
565 }
566 
567 static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
568 			      const u8 *key, size_t keylen,
569 			      struct pkey_protkey *pkey)
570 {
571 	int i, card, dom, rc;
572 	struct keytoken_header *hdr = (struct keytoken_header *)key;
573 
574 	/* check for at least one apqn given */
575 	if (!apqns || !nr_apqns)
576 		return -EINVAL;
577 
578 	if (keylen < sizeof(struct keytoken_header))
579 		return -EINVAL;
580 
581 	switch (hdr->type) {
582 	case TOKTYPE_NON_CCA:
583 		return pkey_nonccatok2pkey(key, keylen, pkey);
584 	case TOKTYPE_CCA_INTERNAL:
585 		switch (hdr->version) {
586 		case TOKVER_CCA_AES:
587 			if (keylen != sizeof(struct secaeskeytoken))
588 				return -EINVAL;
589 			if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
590 				return -EINVAL;
591 			break;
592 		case TOKVER_CCA_VLSC:
593 			if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
594 				return -EINVAL;
595 			if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
596 				return -EINVAL;
597 			break;
598 		default:
599 			DEBUG_ERR("%s unknown CCA internal token version %d\n",
600 				  __func__, hdr->version);
601 			return -EINVAL;
602 		}
603 		break;
604 	default:
605 		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
606 			  __func__, hdr->type);
607 		return -EINVAL;
608 	}
609 
610 	/* simple try all apqns from the list */
611 	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
612 		card = apqns[i].card;
613 		dom = apqns[i].domain;
614 		if (hdr->version == TOKVER_CCA_AES)
615 			rc = cca_sec2protkey(card, dom, key, pkey->protkey,
616 					     &pkey->len, &pkey->type);
617 		else /* TOKVER_CCA_VLSC */
618 			rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
619 						&pkey->len, &pkey->type);
620 		if (rc == 0)
621 			break;
622 	}
623 
624 	return rc;
625 }
626 
627 static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
628 			  struct pkey_apqn *apqns, size_t *nr_apqns)
629 {
630 	int rc = EINVAL;
631 	u32 _nr_apqns, *_apqns = NULL;
632 	struct keytoken_header *hdr = (struct keytoken_header *)key;
633 
634 	if (keylen < sizeof(struct keytoken_header) ||
635 	    hdr->type != TOKTYPE_CCA_INTERNAL ||
636 	    flags == 0)
637 		return -EINVAL;
638 
639 	if (hdr->version == TOKVER_CCA_AES || hdr->version == TOKVER_CCA_VLSC) {
640 		int minhwtype = ZCRYPT_CEX3C;
641 		u64 cur_mkvp = 0, old_mkvp = 0;
642 
643 		if (hdr->version == TOKVER_CCA_AES) {
644 			struct secaeskeytoken *t = (struct secaeskeytoken *)key;
645 
646 			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
647 				cur_mkvp = t->mkvp;
648 			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
649 				old_mkvp = t->mkvp;
650 		} else {
651 			struct cipherkeytoken *t = (struct cipherkeytoken *)key;
652 
653 			minhwtype = ZCRYPT_CEX6;
654 			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
655 				cur_mkvp = t->mkvp0;
656 			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
657 				old_mkvp = t->mkvp0;
658 		}
659 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
660 				   minhwtype, cur_mkvp, old_mkvp, 1);
661 		if (rc)
662 			goto out;
663 		if (apqns) {
664 			if (*nr_apqns < _nr_apqns)
665 				rc = -ENOSPC;
666 			else
667 				memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
668 		}
669 		*nr_apqns = _nr_apqns;
670 	}
671 
672 out:
673 	kfree(_apqns);
674 	return rc;
675 }
676 
677 static int pkey_apqns4keytype(enum pkey_key_type ktype,
678 			      u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
679 			      struct pkey_apqn *apqns, size_t *nr_apqns)
680 {
681 	int rc = -EINVAL;
682 	u32 _nr_apqns, *_apqns = NULL;
683 
684 	if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
685 		u64 cur_mkvp = 0, old_mkvp = 0;
686 		int minhwtype = ZCRYPT_CEX3C;
687 
688 		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
689 			cur_mkvp = *((u64 *) cur_mkvp);
690 		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
691 			old_mkvp = *((u64 *) alt_mkvp);
692 		if (ktype == PKEY_TYPE_CCA_CIPHER)
693 			minhwtype = ZCRYPT_CEX6;
694 		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
695 				   minhwtype, cur_mkvp, old_mkvp, 1);
696 		if (rc)
697 			goto out;
698 		if (apqns) {
699 			if (*nr_apqns < _nr_apqns)
700 				rc = -ENOSPC;
701 			else
702 				memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
703 		}
704 		*nr_apqns = _nr_apqns;
705 	}
706 
707 out:
708 	kfree(_apqns);
709 	return rc;
710 }
711 
712 /*
713  * File io functions
714  */
715 
716 static void *_copy_key_from_user(void __user *ukey, size_t keylen)
717 {
718 	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
719 		return ERR_PTR(-EINVAL);
720 
721 	return memdup_user(ukey, keylen);
722 }
723 
724 static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
725 {
726 	if (!uapqns || nr_apqns == 0)
727 		return NULL;
728 
729 	return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
730 }
731 
732 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
733 				unsigned long arg)
734 {
735 	int rc;
736 
737 	switch (cmd) {
738 	case PKEY_GENSECK: {
739 		struct pkey_genseck __user *ugs = (void __user *) arg;
740 		struct pkey_genseck kgs;
741 
742 		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
743 			return -EFAULT;
744 		rc = cca_genseckey(kgs.cardnr, kgs.domain,
745 				   kgs.keytype, kgs.seckey.seckey);
746 		DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
747 		if (rc)
748 			break;
749 		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
750 			return -EFAULT;
751 		break;
752 	}
753 	case PKEY_CLR2SECK: {
754 		struct pkey_clr2seck __user *ucs = (void __user *) arg;
755 		struct pkey_clr2seck kcs;
756 
757 		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
758 			return -EFAULT;
759 		rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
760 				    kcs.clrkey.clrkey, kcs.seckey.seckey);
761 		DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
762 		if (rc)
763 			break;
764 		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
765 			return -EFAULT;
766 		memzero_explicit(&kcs, sizeof(kcs));
767 		break;
768 	}
769 	case PKEY_SEC2PROTK: {
770 		struct pkey_sec2protk __user *usp = (void __user *) arg;
771 		struct pkey_sec2protk ksp;
772 
773 		if (copy_from_user(&ksp, usp, sizeof(ksp)))
774 			return -EFAULT;
775 		rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
776 				     ksp.seckey.seckey, ksp.protkey.protkey,
777 				     NULL, &ksp.protkey.type);
778 		DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
779 		if (rc)
780 			break;
781 		if (copy_to_user(usp, &ksp, sizeof(ksp)))
782 			return -EFAULT;
783 		break;
784 	}
785 	case PKEY_CLR2PROTK: {
786 		struct pkey_clr2protk __user *ucp = (void __user *) arg;
787 		struct pkey_clr2protk kcp;
788 
789 		if (copy_from_user(&kcp, ucp, sizeof(kcp)))
790 			return -EFAULT;
791 		rc = pkey_clr2protkey(kcp.keytype,
792 				      &kcp.clrkey, &kcp.protkey);
793 		DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
794 		if (rc)
795 			break;
796 		if (copy_to_user(ucp, &kcp, sizeof(kcp)))
797 			return -EFAULT;
798 		memzero_explicit(&kcp, sizeof(kcp));
799 		break;
800 	}
801 	case PKEY_FINDCARD: {
802 		struct pkey_findcard __user *ufc = (void __user *) arg;
803 		struct pkey_findcard kfc;
804 
805 		if (copy_from_user(&kfc, ufc, sizeof(kfc)))
806 			return -EFAULT;
807 		rc = cca_findcard(kfc.seckey.seckey,
808 				  &kfc.cardnr, &kfc.domain, 1);
809 		DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
810 		if (rc < 0)
811 			break;
812 		if (copy_to_user(ufc, &kfc, sizeof(kfc)))
813 			return -EFAULT;
814 		break;
815 	}
816 	case PKEY_SKEY2PKEY: {
817 		struct pkey_skey2pkey __user *usp = (void __user *) arg;
818 		struct pkey_skey2pkey ksp;
819 
820 		if (copy_from_user(&ksp, usp, sizeof(ksp)))
821 			return -EFAULT;
822 		rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
823 		DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
824 		if (rc)
825 			break;
826 		if (copy_to_user(usp, &ksp, sizeof(ksp)))
827 			return -EFAULT;
828 		break;
829 	}
830 	case PKEY_VERIFYKEY: {
831 		struct pkey_verifykey __user *uvk = (void __user *) arg;
832 		struct pkey_verifykey kvk;
833 
834 		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
835 			return -EFAULT;
836 		rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
837 				    &kvk.keysize, &kvk.attributes);
838 		DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
839 		if (rc)
840 			break;
841 		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
842 			return -EFAULT;
843 		break;
844 	}
845 	case PKEY_GENPROTK: {
846 		struct pkey_genprotk __user *ugp = (void __user *) arg;
847 		struct pkey_genprotk kgp;
848 
849 		if (copy_from_user(&kgp, ugp, sizeof(kgp)))
850 			return -EFAULT;
851 		rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
852 		DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
853 		if (rc)
854 			break;
855 		if (copy_to_user(ugp, &kgp, sizeof(kgp)))
856 			return -EFAULT;
857 		break;
858 	}
859 	case PKEY_VERIFYPROTK: {
860 		struct pkey_verifyprotk __user *uvp = (void __user *) arg;
861 		struct pkey_verifyprotk kvp;
862 
863 		if (copy_from_user(&kvp, uvp, sizeof(kvp)))
864 			return -EFAULT;
865 		rc = pkey_verifyprotkey(&kvp.protkey);
866 		DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
867 		break;
868 	}
869 	case PKEY_KBLOB2PROTK: {
870 		struct pkey_kblob2pkey __user *utp = (void __user *) arg;
871 		struct pkey_kblob2pkey ktp;
872 		u8 *kkey;
873 
874 		if (copy_from_user(&ktp, utp, sizeof(ktp)))
875 			return -EFAULT;
876 		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
877 		if (IS_ERR(kkey))
878 			return PTR_ERR(kkey);
879 		rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
880 		DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
881 		kfree(kkey);
882 		if (rc)
883 			break;
884 		if (copy_to_user(utp, &ktp, sizeof(ktp)))
885 			return -EFAULT;
886 		break;
887 	}
888 	case PKEY_GENSECK2: {
889 		struct pkey_genseck2 __user *ugs = (void __user *) arg;
890 		struct pkey_genseck2 kgs;
891 		struct pkey_apqn *apqns;
892 		size_t klen = KEYBLOBBUFSIZE;
893 		u8 *kkey;
894 
895 		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
896 			return -EFAULT;
897 		apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
898 		if (IS_ERR(apqns))
899 			return PTR_ERR(apqns);
900 		kkey = kmalloc(klen, GFP_KERNEL);
901 		if (!kkey) {
902 			kfree(apqns);
903 			return -ENOMEM;
904 		}
905 		rc = pkey_genseckey2(apqns, kgs.apqn_entries,
906 				     kgs.type, kgs.size, kgs.keygenflags,
907 				     kkey, &klen);
908 		DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
909 		kfree(apqns);
910 		if (rc) {
911 			kfree(kkey);
912 			break;
913 		}
914 		if (kgs.key) {
915 			if (kgs.keylen < klen) {
916 				kfree(kkey);
917 				return -EINVAL;
918 			}
919 			if (copy_to_user(kgs.key, kkey, klen)) {
920 				kfree(kkey);
921 				return -EFAULT;
922 			}
923 		}
924 		kgs.keylen = klen;
925 		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
926 			rc = -EFAULT;
927 		kfree(kkey);
928 		break;
929 	}
930 	case PKEY_CLR2SECK2: {
931 		struct pkey_clr2seck2 __user *ucs = (void __user *) arg;
932 		struct pkey_clr2seck2 kcs;
933 		struct pkey_apqn *apqns;
934 		size_t klen = KEYBLOBBUFSIZE;
935 		u8 *kkey;
936 
937 		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
938 			return -EFAULT;
939 		apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
940 		if (IS_ERR(apqns))
941 			return PTR_ERR(apqns);
942 		kkey = kmalloc(klen, GFP_KERNEL);
943 		if (!kkey) {
944 			kfree(apqns);
945 			return -ENOMEM;
946 		}
947 		rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
948 				      kcs.type, kcs.size, kcs.keygenflags,
949 				      kcs.clrkey.clrkey, kkey, &klen);
950 		DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
951 		kfree(apqns);
952 		if (rc) {
953 			kfree(kkey);
954 			break;
955 		}
956 		if (kcs.key) {
957 			if (kcs.keylen < klen) {
958 				kfree(kkey);
959 				return -EINVAL;
960 			}
961 			if (copy_to_user(kcs.key, kkey, klen)) {
962 				kfree(kkey);
963 				return -EFAULT;
964 			}
965 		}
966 		kcs.keylen = klen;
967 		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
968 			rc = -EFAULT;
969 		memzero_explicit(&kcs, sizeof(kcs));
970 		kfree(kkey);
971 		break;
972 	}
973 	case PKEY_VERIFYKEY2: {
974 		struct pkey_verifykey2 __user *uvk = (void __user *) arg;
975 		struct pkey_verifykey2 kvk;
976 		u8 *kkey;
977 
978 		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
979 			return -EFAULT;
980 		kkey = _copy_key_from_user(kvk.key, kvk.keylen);
981 		if (IS_ERR(kkey))
982 			return PTR_ERR(kkey);
983 		rc = pkey_verifykey2(kkey, kvk.keylen,
984 				     &kvk.cardnr, &kvk.domain,
985 				     &kvk.type, &kvk.size, &kvk.flags);
986 		DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
987 		kfree(kkey);
988 		if (rc)
989 			break;
990 		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
991 			return -EFAULT;
992 		break;
993 	}
994 	case PKEY_KBLOB2PROTK2: {
995 		struct pkey_kblob2pkey2 __user *utp = (void __user *) arg;
996 		struct pkey_kblob2pkey2 ktp;
997 		struct pkey_apqn *apqns = NULL;
998 		u8 *kkey;
999 
1000 		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1001 			return -EFAULT;
1002 		apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1003 		if (IS_ERR(apqns))
1004 			return PTR_ERR(apqns);
1005 		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1006 		if (IS_ERR(kkey)) {
1007 			kfree(apqns);
1008 			return PTR_ERR(kkey);
1009 		}
1010 		rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1011 					kkey, ktp.keylen, &ktp.protkey);
1012 		DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1013 		kfree(apqns);
1014 		kfree(kkey);
1015 		if (rc)
1016 			break;
1017 		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1018 			return -EFAULT;
1019 		break;
1020 	}
1021 	case PKEY_APQNS4K: {
1022 		struct pkey_apqns4key __user *uak = (void __user *) arg;
1023 		struct pkey_apqns4key kak;
1024 		struct pkey_apqn *apqns = NULL;
1025 		size_t nr_apqns, len;
1026 		u8 *kkey;
1027 
1028 		if (copy_from_user(&kak, uak, sizeof(kak)))
1029 			return -EFAULT;
1030 		nr_apqns = kak.apqn_entries;
1031 		if (nr_apqns) {
1032 			apqns = kmalloc_array(nr_apqns,
1033 					      sizeof(struct pkey_apqn),
1034 					      GFP_KERNEL);
1035 			if (!apqns)
1036 				return -ENOMEM;
1037 		}
1038 		kkey = _copy_key_from_user(kak.key, kak.keylen);
1039 		if (IS_ERR(kkey)) {
1040 			kfree(apqns);
1041 			return PTR_ERR(kkey);
1042 		}
1043 		rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1044 				    apqns, &nr_apqns);
1045 		DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1046 		kfree(kkey);
1047 		if (rc && rc != -ENOSPC) {
1048 			kfree(apqns);
1049 			break;
1050 		}
1051 		if (!rc && kak.apqns) {
1052 			if (nr_apqns > kak.apqn_entries) {
1053 				kfree(apqns);
1054 				return -EINVAL;
1055 			}
1056 			len = nr_apqns * sizeof(struct pkey_apqn);
1057 			if (len) {
1058 				if (copy_to_user(kak.apqns, apqns, len)) {
1059 					kfree(apqns);
1060 					return -EFAULT;
1061 				}
1062 			}
1063 		}
1064 		kak.apqn_entries = nr_apqns;
1065 		if (copy_to_user(uak, &kak, sizeof(kak)))
1066 			rc = -EFAULT;
1067 		kfree(apqns);
1068 		break;
1069 	}
1070 	case PKEY_APQNS4KT: {
1071 		struct pkey_apqns4keytype __user *uat = (void __user *) arg;
1072 		struct pkey_apqns4keytype kat;
1073 		struct pkey_apqn *apqns = NULL;
1074 		size_t nr_apqns, len;
1075 
1076 		if (copy_from_user(&kat, uat, sizeof(kat)))
1077 			return -EFAULT;
1078 		nr_apqns = kat.apqn_entries;
1079 		if (nr_apqns) {
1080 			apqns = kmalloc_array(nr_apqns,
1081 					      sizeof(struct pkey_apqn),
1082 					      GFP_KERNEL);
1083 			if (!apqns)
1084 				return -ENOMEM;
1085 		}
1086 		rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1087 					kat.flags, apqns, &nr_apqns);
1088 		DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1089 		if (rc && rc != -ENOSPC) {
1090 			kfree(apqns);
1091 			break;
1092 		}
1093 		if (!rc && kat.apqns) {
1094 			if (nr_apqns > kat.apqn_entries) {
1095 				kfree(apqns);
1096 				return -EINVAL;
1097 			}
1098 			len = nr_apqns * sizeof(struct pkey_apqn);
1099 			if (len) {
1100 				if (copy_to_user(kat.apqns, apqns, len)) {
1101 					kfree(apqns);
1102 					return -EFAULT;
1103 				}
1104 			}
1105 		}
1106 		kat.apqn_entries = nr_apqns;
1107 		if (copy_to_user(uat, &kat, sizeof(kat)))
1108 			rc = -EFAULT;
1109 		kfree(apqns);
1110 		break;
1111 	}
1112 	default:
1113 		/* unknown/unsupported ioctl cmd */
1114 		return -ENOTTY;
1115 	}
1116 
1117 	return rc;
1118 }
1119 
1120 /*
1121  * Sysfs and file io operations
1122  */
1123 
1124 /*
1125  * Sysfs attribute read function for all protected key binary attributes.
1126  * The implementation can not deal with partial reads, because a new random
1127  * protected key blob is generated with each read. In case of partial reads
1128  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1129  */
1130 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1131 					  loff_t off, size_t count)
1132 {
1133 	struct protaeskeytoken protkeytoken;
1134 	struct pkey_protkey protkey;
1135 	int rc;
1136 
1137 	if (off != 0 || count < sizeof(protkeytoken))
1138 		return -EINVAL;
1139 	if (is_xts)
1140 		if (count < 2 * sizeof(protkeytoken))
1141 			return -EINVAL;
1142 
1143 	memset(&protkeytoken, 0, sizeof(protkeytoken));
1144 	protkeytoken.type = TOKTYPE_NON_CCA;
1145 	protkeytoken.version = TOKVER_PROTECTED_KEY;
1146 	protkeytoken.keytype = keytype;
1147 
1148 	rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1149 	if (rc)
1150 		return rc;
1151 
1152 	protkeytoken.len = protkey.len;
1153 	memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1154 
1155 	memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1156 
1157 	if (is_xts) {
1158 		rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1159 		if (rc)
1160 			return rc;
1161 
1162 		protkeytoken.len = protkey.len;
1163 		memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1164 
1165 		memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1166 		       sizeof(protkeytoken));
1167 
1168 		return 2 * sizeof(protkeytoken);
1169 	}
1170 
1171 	return sizeof(protkeytoken);
1172 }
1173 
1174 static ssize_t protkey_aes_128_read(struct file *filp,
1175 				    struct kobject *kobj,
1176 				    struct bin_attribute *attr,
1177 				    char *buf, loff_t off,
1178 				    size_t count)
1179 {
1180 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1181 					  off, count);
1182 }
1183 
1184 static ssize_t protkey_aes_192_read(struct file *filp,
1185 				    struct kobject *kobj,
1186 				    struct bin_attribute *attr,
1187 				    char *buf, loff_t off,
1188 				    size_t count)
1189 {
1190 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1191 					  off, count);
1192 }
1193 
1194 static ssize_t protkey_aes_256_read(struct file *filp,
1195 				    struct kobject *kobj,
1196 				    struct bin_attribute *attr,
1197 				    char *buf, loff_t off,
1198 				    size_t count)
1199 {
1200 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1201 					  off, count);
1202 }
1203 
1204 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1205 					struct kobject *kobj,
1206 					struct bin_attribute *attr,
1207 					char *buf, loff_t off,
1208 					size_t count)
1209 {
1210 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1211 					  off, count);
1212 }
1213 
1214 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1215 					struct kobject *kobj,
1216 					struct bin_attribute *attr,
1217 					char *buf, loff_t off,
1218 					size_t count)
1219 {
1220 	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1221 					  off, count);
1222 }
1223 
1224 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1225 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1226 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1227 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1228 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1229 
1230 static struct bin_attribute *protkey_attrs[] = {
1231 	&bin_attr_protkey_aes_128,
1232 	&bin_attr_protkey_aes_192,
1233 	&bin_attr_protkey_aes_256,
1234 	&bin_attr_protkey_aes_128_xts,
1235 	&bin_attr_protkey_aes_256_xts,
1236 	NULL
1237 };
1238 
1239 static struct attribute_group protkey_attr_group = {
1240 	.name	   = "protkey",
1241 	.bin_attrs = protkey_attrs,
1242 };
1243 
1244 /*
1245  * Sysfs attribute read function for all secure key ccadata binary attributes.
1246  * The implementation can not deal with partial reads, because a new random
1247  * protected key blob is generated with each read. In case of partial reads
1248  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1249  */
1250 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1251 					  loff_t off, size_t count)
1252 {
1253 	int rc;
1254 	struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
1255 
1256 	if (off != 0 || count < sizeof(struct secaeskeytoken))
1257 		return -EINVAL;
1258 	if (is_xts)
1259 		if (count < 2 * sizeof(struct secaeskeytoken))
1260 			return -EINVAL;
1261 
1262 	rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1263 	if (rc)
1264 		return rc;
1265 
1266 	if (is_xts) {
1267 		seckey++;
1268 		rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1269 		if (rc)
1270 			return rc;
1271 
1272 		return 2 * sizeof(struct secaeskeytoken);
1273 	}
1274 
1275 	return sizeof(struct secaeskeytoken);
1276 }
1277 
1278 static ssize_t ccadata_aes_128_read(struct file *filp,
1279 				    struct kobject *kobj,
1280 				    struct bin_attribute *attr,
1281 				    char *buf, loff_t off,
1282 				    size_t count)
1283 {
1284 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1285 					  off, count);
1286 }
1287 
1288 static ssize_t ccadata_aes_192_read(struct file *filp,
1289 				    struct kobject *kobj,
1290 				    struct bin_attribute *attr,
1291 				    char *buf, loff_t off,
1292 				    size_t count)
1293 {
1294 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1295 					  off, count);
1296 }
1297 
1298 static ssize_t ccadata_aes_256_read(struct file *filp,
1299 				    struct kobject *kobj,
1300 				    struct bin_attribute *attr,
1301 				    char *buf, loff_t off,
1302 				    size_t count)
1303 {
1304 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1305 					  off, count);
1306 }
1307 
1308 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1309 					struct kobject *kobj,
1310 					struct bin_attribute *attr,
1311 					char *buf, loff_t off,
1312 					size_t count)
1313 {
1314 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1315 					  off, count);
1316 }
1317 
1318 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1319 					struct kobject *kobj,
1320 					struct bin_attribute *attr,
1321 					char *buf, loff_t off,
1322 					size_t count)
1323 {
1324 	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1325 					  off, count);
1326 }
1327 
1328 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1329 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1330 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1331 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1332 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1333 
1334 static struct bin_attribute *ccadata_attrs[] = {
1335 	&bin_attr_ccadata_aes_128,
1336 	&bin_attr_ccadata_aes_192,
1337 	&bin_attr_ccadata_aes_256,
1338 	&bin_attr_ccadata_aes_128_xts,
1339 	&bin_attr_ccadata_aes_256_xts,
1340 	NULL
1341 };
1342 
1343 static struct attribute_group ccadata_attr_group = {
1344 	.name	   = "ccadata",
1345 	.bin_attrs = ccadata_attrs,
1346 };
1347 
1348 #define CCACIPHERTOKENSIZE	(sizeof(struct cipherkeytoken) + 80)
1349 
1350 /*
1351  * Sysfs attribute read function for all secure key ccacipher binary attributes.
1352  * The implementation can not deal with partial reads, because a new random
1353  * secure key blob is generated with each read. In case of partial reads
1354  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1355  */
1356 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1357 					    bool is_xts, char *buf, loff_t off,
1358 					    size_t count)
1359 {
1360 	size_t keysize;
1361 	int rc;
1362 
1363 	if (off != 0 || count < CCACIPHERTOKENSIZE)
1364 		return -EINVAL;
1365 	if (is_xts)
1366 		if (count < 2 * CCACIPHERTOKENSIZE)
1367 			return -EINVAL;
1368 
1369 	keysize = CCACIPHERTOKENSIZE;
1370 	rc = cca_gencipherkey(-1, -1, keybits, 0, buf, &keysize);
1371 	if (rc)
1372 		return rc;
1373 	memset(buf + keysize, 0, CCACIPHERTOKENSIZE - keysize);
1374 
1375 	if (is_xts) {
1376 		keysize = CCACIPHERTOKENSIZE;
1377 		rc = cca_gencipherkey(-1, -1, keybits, 0,
1378 				      buf + CCACIPHERTOKENSIZE, &keysize);
1379 		if (rc)
1380 			return rc;
1381 		memset(buf + CCACIPHERTOKENSIZE + keysize, 0,
1382 		       CCACIPHERTOKENSIZE - keysize);
1383 
1384 		return 2 * CCACIPHERTOKENSIZE;
1385 	}
1386 
1387 	return CCACIPHERTOKENSIZE;
1388 }
1389 
1390 static ssize_t ccacipher_aes_128_read(struct file *filp,
1391 				      struct kobject *kobj,
1392 				      struct bin_attribute *attr,
1393 				      char *buf, loff_t off,
1394 				      size_t count)
1395 {
1396 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1397 					    off, count);
1398 }
1399 
1400 static ssize_t ccacipher_aes_192_read(struct file *filp,
1401 				      struct kobject *kobj,
1402 				      struct bin_attribute *attr,
1403 				      char *buf, loff_t off,
1404 				      size_t count)
1405 {
1406 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1407 					    off, count);
1408 }
1409 
1410 static ssize_t ccacipher_aes_256_read(struct file *filp,
1411 				      struct kobject *kobj,
1412 				      struct bin_attribute *attr,
1413 				      char *buf, loff_t off,
1414 				      size_t count)
1415 {
1416 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1417 					    off, count);
1418 }
1419 
1420 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1421 					  struct kobject *kobj,
1422 					  struct bin_attribute *attr,
1423 					  char *buf, loff_t off,
1424 					  size_t count)
1425 {
1426 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1427 					    off, count);
1428 }
1429 
1430 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1431 					  struct kobject *kobj,
1432 					  struct bin_attribute *attr,
1433 					  char *buf, loff_t off,
1434 					  size_t count)
1435 {
1436 	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1437 					    off, count);
1438 }
1439 
1440 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1441 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1442 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1443 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1444 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1445 
1446 static struct bin_attribute *ccacipher_attrs[] = {
1447 	&bin_attr_ccacipher_aes_128,
1448 	&bin_attr_ccacipher_aes_192,
1449 	&bin_attr_ccacipher_aes_256,
1450 	&bin_attr_ccacipher_aes_128_xts,
1451 	&bin_attr_ccacipher_aes_256_xts,
1452 	NULL
1453 };
1454 
1455 static struct attribute_group ccacipher_attr_group = {
1456 	.name	   = "ccacipher",
1457 	.bin_attrs = ccacipher_attrs,
1458 };
1459 
1460 static const struct attribute_group *pkey_attr_groups[] = {
1461 	&protkey_attr_group,
1462 	&ccadata_attr_group,
1463 	&ccacipher_attr_group,
1464 	NULL,
1465 };
1466 
1467 static const struct file_operations pkey_fops = {
1468 	.owner		= THIS_MODULE,
1469 	.open		= nonseekable_open,
1470 	.llseek		= no_llseek,
1471 	.unlocked_ioctl = pkey_unlocked_ioctl,
1472 };
1473 
1474 static struct miscdevice pkey_dev = {
1475 	.name	= "pkey",
1476 	.minor	= MISC_DYNAMIC_MINOR,
1477 	.mode	= 0666,
1478 	.fops	= &pkey_fops,
1479 	.groups = pkey_attr_groups,
1480 };
1481 
1482 /*
1483  * Module init
1484  */
1485 static int __init pkey_init(void)
1486 {
1487 	cpacf_mask_t kmc_functions;
1488 
1489 	/*
1490 	 * The pckmo instruction should be available - even if we don't
1491 	 * actually invoke it. This instruction comes with MSA 3 which
1492 	 * is also the minimum level for the kmc instructions which
1493 	 * are able to work with protected keys.
1494 	 */
1495 	if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
1496 		return -ENODEV;
1497 
1498 	/* check for kmc instructions available */
1499 	if (!cpacf_query(CPACF_KMC, &kmc_functions))
1500 		return -ENODEV;
1501 	if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
1502 	    !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
1503 	    !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
1504 		return -ENODEV;
1505 
1506 	pkey_debug_init();
1507 
1508 	return misc_register(&pkey_dev);
1509 }
1510 
1511 /*
1512  * Module exit
1513  */
1514 static void __exit pkey_exit(void)
1515 {
1516 	misc_deregister(&pkey_dev);
1517 	pkey_debug_exit();
1518 }
1519 
1520 module_cpu_feature_match(MSA, pkey_init);
1521 module_exit(pkey_exit);
1522