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