blob: 8dda5bb34a2f2710c6d0f8fc40b291d3e848d99e [file] [log] [blame]
Harald Freudenbergere80d4af2016-11-02 14:37:20 +01001/*
2 * pkey device driver
3 *
4 * Copyright IBM Corp. 2017
5 * Author(s): Harald Freudenberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
10 *
11 */
12
13#define KMSG_COMPONENT "pkey"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16#include <linux/fs.h>
17#include <linux/init.h>
18#include <linux/miscdevice.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/kallsyms.h>
22#include <linux/debugfs.h>
23#include <asm/zcrypt.h>
24#include <asm/cpacf.h>
25#include <asm/pkey.h>
26
27#include "zcrypt_api.h"
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("IBM Corporation");
31MODULE_DESCRIPTION("s390 protected key interface");
32
33/* Size of parameter block used for all cca requests/replies */
34#define PARMBSIZE 512
35
36/* Size of vardata block used for some of the cca requests/replies */
37#define VARDATASIZE 4096
38
39/*
40 * debug feature data and functions
41 */
42
43static debug_info_t *debug_info;
44
45#define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
46#define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
47#define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
48#define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
49
50static void __init pkey_debug_init(void)
51{
52 debug_info = debug_register("pkey", 1, 1, 4 * sizeof(long));
53 debug_register_view(debug_info, &debug_sprintf_view);
54 debug_set_level(debug_info, 3);
55}
56
57static void __exit pkey_debug_exit(void)
58{
59 debug_unregister(debug_info);
60}
61
62/* inside view of a secure key token (only type 0x01 version 0x04) */
63struct secaeskeytoken {
64 u8 type; /* 0x01 for internal key token */
65 u8 res0[3];
66 u8 version; /* should be 0x04 */
67 u8 res1[1];
68 u8 flag; /* key flags */
69 u8 res2[1];
70 u64 mkvp; /* master key verification pattern */
71 u8 key[32]; /* key value (encrypted) */
72 u8 cv[8]; /* control vector */
73 u16 bitsize; /* key bit size */
74 u16 keysize; /* key byte size */
75 u8 tvv[4]; /* token validation value */
76} __packed;
77
78/*
79 * Simple check if the token is a valid CCA secure AES key
80 * token. If keybitsize is given, the bitsize of the key is
81 * also checked. Returns 0 on success or errno value on failure.
82 */
Harald Freudenbergere61a6132017-03-15 11:08:27 +010083static int check_secaeskeytoken(const u8 *token, int keybitsize)
Harald Freudenbergere80d4af2016-11-02 14:37:20 +010084{
85 struct secaeskeytoken *t = (struct secaeskeytoken *) token;
86
87 if (t->type != 0x01) {
88 DEBUG_ERR(
89 "check_secaeskeytoken secure token check failed, type mismatch 0x%02x != 0x01\n",
90 (int) t->type);
91 return -EINVAL;
92 }
93 if (t->version != 0x04) {
94 DEBUG_ERR(
95 "check_secaeskeytoken secure token check failed, version mismatch 0x%02x != 0x04\n",
96 (int) t->version);
97 return -EINVAL;
98 }
99 if (keybitsize > 0 && t->bitsize != keybitsize) {
100 DEBUG_ERR(
101 "check_secaeskeytoken secure token check failed, bitsize mismatch %d != %d\n",
102 (int) t->bitsize, keybitsize);
103 return -EINVAL;
104 }
105
106 return 0;
107}
108
109/*
110 * Allocate consecutive memory for request CPRB, request param
111 * block, reply CPRB and reply param block and fill in values
112 * for the common fields. Returns 0 on success or errno value
113 * on failure.
114 */
115static int alloc_and_prep_cprbmem(size_t paramblen,
116 u8 **pcprbmem,
117 struct CPRBX **preqCPRB,
118 struct CPRBX **prepCPRB)
119{
120 u8 *cprbmem;
121 size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen;
122 struct CPRBX *preqcblk, *prepcblk;
123
124 /*
125 * allocate consecutive memory for request CPRB, request param
126 * block, reply CPRB and reply param block
127 */
Vasyl Gomonovych0bb6bba2017-10-11 21:52:46 +0200128 cprbmem = kzalloc(2 * cprbplusparamblen, GFP_KERNEL);
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100129 if (!cprbmem)
130 return -ENOMEM;
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100131
132 preqcblk = (struct CPRBX *) cprbmem;
133 prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen);
134
135 /* fill request cprb struct */
136 preqcblk->cprb_len = sizeof(struct CPRBX);
137 preqcblk->cprb_ver_id = 0x02;
138 memcpy(preqcblk->func_id, "T2", 2);
139 preqcblk->rpl_msgbl = cprbplusparamblen;
140 if (paramblen) {
141 preqcblk->req_parmb =
142 ((u8 *) preqcblk) + sizeof(struct CPRBX);
143 preqcblk->rpl_parmb =
144 ((u8 *) prepcblk) + sizeof(struct CPRBX);
145 }
146
147 *pcprbmem = cprbmem;
148 *preqCPRB = preqcblk;
149 *prepCPRB = prepcblk;
150
151 return 0;
152}
153
154/*
155 * Free the cprb memory allocated with the function above.
156 * If the scrub value is not zero, the memory is filled
157 * with zeros before freeing (useful if there was some
158 * clear key material in there).
159 */
160static void free_cprbmem(void *mem, size_t paramblen, int scrub)
161{
162 if (scrub)
163 memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen));
164 kfree(mem);
165}
166
167/*
168 * Helper function to prepare the xcrb struct
169 */
170static inline void prep_xcrb(struct ica_xcRB *pxcrb,
171 u16 cardnr,
172 struct CPRBX *preqcblk,
173 struct CPRBX *prepcblk)
174{
175 memset(pxcrb, 0, sizeof(*pxcrb));
176 pxcrb->agent_ID = 0x4341; /* 'CA' */
177 pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr);
178 pxcrb->request_control_blk_length =
179 preqcblk->cprb_len + preqcblk->req_parml;
Heiko Carstens7a003632017-05-09 12:42:26 +0200180 pxcrb->request_control_blk_addr = (void __user *) preqcblk;
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100181 pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl;
Heiko Carstens7a003632017-05-09 12:42:26 +0200182 pxcrb->reply_control_blk_addr = (void __user *) prepcblk;
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100183}
184
185/*
186 * Helper function which calls zcrypt_send_cprb with
187 * memory management segment adjusted to kernel space
188 * so that the copy_from_user called within this
189 * function do in fact copy from kernel space.
190 */
191static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb)
192{
193 int rc;
194 mm_segment_t old_fs = get_fs();
195
196 set_fs(KERNEL_DS);
197 rc = zcrypt_send_cprb(xcrb);
198 set_fs(old_fs);
199
200 return rc;
201}
202
203/*
204 * Generate (random) AES secure key.
205 */
206int pkey_genseckey(u16 cardnr, u16 domain,
207 u32 keytype, struct pkey_seckey *seckey)
208{
209 int i, rc, keysize;
210 int seckeysize;
211 u8 *mem;
212 struct CPRBX *preqcblk, *prepcblk;
213 struct ica_xcRB xcrb;
214 struct kgreqparm {
215 u8 subfunc_code[2];
216 u16 rule_array_len;
217 struct lv1 {
218 u16 len;
219 char key_form[8];
220 char key_length[8];
221 char key_type1[8];
222 char key_type2[8];
223 } lv1;
224 struct lv2 {
225 u16 len;
226 struct keyid {
227 u16 len;
228 u16 attr;
229 u8 data[SECKEYBLOBSIZE];
230 } keyid[6];
231 } lv2;
232 } *preqparm;
233 struct kgrepparm {
234 u8 subfunc_code[2];
235 u16 rule_array_len;
236 struct lv3 {
237 u16 len;
238 u16 keyblocklen;
239 struct {
240 u16 toklen;
241 u16 tokattr;
242 u8 tok[0];
243 /* ... some more data ... */
244 } keyblock;
245 } lv3;
246 } *prepparm;
247
248 /* get already prepared memory for 2 cprbs with param block each */
249 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
250 if (rc)
251 return rc;
252
253 /* fill request cprb struct */
254 preqcblk->domain = domain;
255
256 /* fill request cprb param block with KG request */
257 preqparm = (struct kgreqparm *) preqcblk->req_parmb;
258 memcpy(preqparm->subfunc_code, "KG", 2);
259 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
260 preqparm->lv1.len = sizeof(struct lv1);
261 memcpy(preqparm->lv1.key_form, "OP ", 8);
262 switch (keytype) {
263 case PKEY_KEYTYPE_AES_128:
264 keysize = 16;
265 memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8);
266 break;
267 case PKEY_KEYTYPE_AES_192:
268 keysize = 24;
269 memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8);
270 break;
271 case PKEY_KEYTYPE_AES_256:
272 keysize = 32;
273 memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8);
274 break;
275 default:
276 DEBUG_ERR(
277 "pkey_genseckey unknown/unsupported keytype %d\n",
278 keytype);
279 rc = -EINVAL;
280 goto out;
281 }
282 memcpy(preqparm->lv1.key_type1, "AESDATA ", 8);
283 preqparm->lv2.len = sizeof(struct lv2);
284 for (i = 0; i < 6; i++) {
285 preqparm->lv2.keyid[i].len = sizeof(struct keyid);
286 preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10);
287 }
288 preqcblk->req_parml = sizeof(struct kgreqparm);
289
290 /* fill xcrb struct */
291 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
292
293 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
294 rc = _zcrypt_send_cprb(&xcrb);
295 if (rc) {
296 DEBUG_ERR(
297 "pkey_genseckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
298 (int) cardnr, (int) domain, rc);
299 goto out;
300 }
301
302 /* check response returncode and reasoncode */
303 if (prepcblk->ccp_rtcode != 0) {
304 DEBUG_ERR(
305 "pkey_genseckey secure key generate failure, card response %d/%d\n",
306 (int) prepcblk->ccp_rtcode,
307 (int) prepcblk->ccp_rscode);
308 rc = -EIO;
309 goto out;
310 }
311
312 /* process response cprb param block */
313 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
314 prepparm = (struct kgrepparm *) prepcblk->rpl_parmb;
315
316 /* check length of the returned secure key token */
317 seckeysize = prepparm->lv3.keyblock.toklen
318 - sizeof(prepparm->lv3.keyblock.toklen)
319 - sizeof(prepparm->lv3.keyblock.tokattr);
320 if (seckeysize != SECKEYBLOBSIZE) {
321 DEBUG_ERR(
322 "pkey_genseckey secure token size mismatch %d != %d bytes\n",
323 seckeysize, SECKEYBLOBSIZE);
324 rc = -EIO;
325 goto out;
326 }
327
328 /* check secure key token */
329 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
330 if (rc) {
331 rc = -EIO;
332 goto out;
333 }
334
335 /* copy the generated secure key token */
336 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
337
338out:
339 free_cprbmem(mem, PARMBSIZE, 0);
340 return rc;
341}
342EXPORT_SYMBOL(pkey_genseckey);
343
344/*
345 * Generate an AES secure key with given key value.
346 */
347int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype,
348 const struct pkey_clrkey *clrkey,
349 struct pkey_seckey *seckey)
350{
351 int rc, keysize, seckeysize;
352 u8 *mem;
353 struct CPRBX *preqcblk, *prepcblk;
354 struct ica_xcRB xcrb;
355 struct cmreqparm {
356 u8 subfunc_code[2];
357 u16 rule_array_len;
358 char rule_array[8];
359 struct lv1 {
360 u16 len;
361 u8 clrkey[0];
362 } lv1;
363 struct lv2 {
364 u16 len;
365 struct keyid {
366 u16 len;
367 u16 attr;
368 u8 data[SECKEYBLOBSIZE];
369 } keyid;
370 } lv2;
371 } *preqparm;
372 struct lv2 *plv2;
373 struct cmrepparm {
374 u8 subfunc_code[2];
375 u16 rule_array_len;
376 struct lv3 {
377 u16 len;
378 u16 keyblocklen;
379 struct {
380 u16 toklen;
381 u16 tokattr;
382 u8 tok[0];
383 /* ... some more data ... */
384 } keyblock;
385 } lv3;
386 } *prepparm;
387
388 /* get already prepared memory for 2 cprbs with param block each */
389 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
390 if (rc)
391 return rc;
392
393 /* fill request cprb struct */
394 preqcblk->domain = domain;
395
396 /* fill request cprb param block with CM request */
397 preqparm = (struct cmreqparm *) preqcblk->req_parmb;
398 memcpy(preqparm->subfunc_code, "CM", 2);
399 memcpy(preqparm->rule_array, "AES ", 8);
400 preqparm->rule_array_len =
401 sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
402 switch (keytype) {
403 case PKEY_KEYTYPE_AES_128:
404 keysize = 16;
405 break;
406 case PKEY_KEYTYPE_AES_192:
407 keysize = 24;
408 break;
409 case PKEY_KEYTYPE_AES_256:
410 keysize = 32;
411 break;
412 default:
413 DEBUG_ERR(
414 "pkey_clr2seckey unknown/unsupported keytype %d\n",
415 keytype);
416 rc = -EINVAL;
417 goto out;
418 }
419 preqparm->lv1.len = sizeof(struct lv1) + keysize;
420 memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize);
421 plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize);
422 plv2->len = sizeof(struct lv2);
423 plv2->keyid.len = sizeof(struct keyid);
424 plv2->keyid.attr = 0x30;
425 preqcblk->req_parml = sizeof(struct cmreqparm) + keysize;
426
427 /* fill xcrb struct */
428 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
429
430 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
431 rc = _zcrypt_send_cprb(&xcrb);
432 if (rc) {
433 DEBUG_ERR(
434 "pkey_clr2seckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
435 (int) cardnr, (int) domain, rc);
436 goto out;
437 }
438
439 /* check response returncode and reasoncode */
440 if (prepcblk->ccp_rtcode != 0) {
441 DEBUG_ERR(
442 "pkey_clr2seckey clear key import failure, card response %d/%d\n",
443 (int) prepcblk->ccp_rtcode,
444 (int) prepcblk->ccp_rscode);
445 rc = -EIO;
446 goto out;
447 }
448
449 /* process response cprb param block */
450 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
451 prepparm = (struct cmrepparm *) prepcblk->rpl_parmb;
452
453 /* check length of the returned secure key token */
454 seckeysize = prepparm->lv3.keyblock.toklen
455 - sizeof(prepparm->lv3.keyblock.toklen)
456 - sizeof(prepparm->lv3.keyblock.tokattr);
457 if (seckeysize != SECKEYBLOBSIZE) {
458 DEBUG_ERR(
459 "pkey_clr2seckey secure token size mismatch %d != %d bytes\n",
460 seckeysize, SECKEYBLOBSIZE);
461 rc = -EIO;
462 goto out;
463 }
464
465 /* check secure key token */
466 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
467 if (rc) {
468 rc = -EIO;
469 goto out;
470 }
471
472 /* copy the generated secure key token */
473 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
474
475out:
476 free_cprbmem(mem, PARMBSIZE, 1);
477 return rc;
478}
479EXPORT_SYMBOL(pkey_clr2seckey);
480
481/*
482 * Derive a proteced key from the secure key blob.
483 */
484int pkey_sec2protkey(u16 cardnr, u16 domain,
485 const struct pkey_seckey *seckey,
486 struct pkey_protkey *protkey)
487{
488 int rc;
489 u8 *mem;
490 struct CPRBX *preqcblk, *prepcblk;
491 struct ica_xcRB xcrb;
492 struct uskreqparm {
493 u8 subfunc_code[2];
494 u16 rule_array_len;
495 struct lv1 {
496 u16 len;
497 u16 attr_len;
498 u16 attr_flags;
499 } lv1;
500 struct lv2 {
501 u16 len;
502 u16 attr_len;
503 u16 attr_flags;
504 u8 token[0]; /* cca secure key token */
505 } lv2 __packed;
506 } *preqparm;
507 struct uskrepparm {
508 u8 subfunc_code[2];
509 u16 rule_array_len;
510 struct lv3 {
511 u16 len;
512 u16 attr_len;
513 u16 attr_flags;
514 struct cpacfkeyblock {
515 u8 version; /* version of this struct */
516 u8 flags[2];
517 u8 algo;
518 u8 form;
519 u8 pad1[3];
520 u16 keylen;
521 u8 key[64]; /* the key (keylen bytes) */
522 u16 keyattrlen;
523 u8 keyattr[32];
524 u8 pad2[1];
525 u8 vptype;
526 u8 vp[32]; /* verification pattern */
527 } keyblock;
528 } lv3 __packed;
529 } *prepparm;
530
531 /* get already prepared memory for 2 cprbs with param block each */
532 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
533 if (rc)
534 return rc;
535
536 /* fill request cprb struct */
537 preqcblk->domain = domain;
538
539 /* fill request cprb param block with USK request */
540 preqparm = (struct uskreqparm *) preqcblk->req_parmb;
541 memcpy(preqparm->subfunc_code, "US", 2);
542 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
543 preqparm->lv1.len = sizeof(struct lv1);
544 preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len);
545 preqparm->lv1.attr_flags = 0x0001;
546 preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE;
547 preqparm->lv2.attr_len = sizeof(struct lv2)
548 - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE;
549 preqparm->lv2.attr_flags = 0x0000;
550 memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE);
551 preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE;
552
553 /* fill xcrb struct */
554 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
555
556 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
557 rc = _zcrypt_send_cprb(&xcrb);
558 if (rc) {
559 DEBUG_ERR(
560 "pkey_sec2protkey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
561 (int) cardnr, (int) domain, rc);
562 goto out;
563 }
564
565 /* check response returncode and reasoncode */
566 if (prepcblk->ccp_rtcode != 0) {
567 DEBUG_ERR(
568 "pkey_sec2protkey unwrap secure key failure, card response %d/%d\n",
569 (int) prepcblk->ccp_rtcode,
570 (int) prepcblk->ccp_rscode);
571 rc = -EIO;
572 goto out;
573 }
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100574 if (prepcblk->ccp_rscode != 0) {
575 DEBUG_WARN(
576 "pkey_sec2protkey unwrap secure key warning, card response %d/%d\n",
577 (int) prepcblk->ccp_rtcode,
578 (int) prepcblk->ccp_rscode);
579 }
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100580
581 /* process response cprb param block */
582 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
583 prepparm = (struct uskrepparm *) prepcblk->rpl_parmb;
584
585 /* check the returned keyblock */
586 if (prepparm->lv3.keyblock.version != 0x01) {
587 DEBUG_ERR(
588 "pkey_sec2protkey reply param keyblock version mismatch 0x%02x != 0x01\n",
589 (int) prepparm->lv3.keyblock.version);
590 rc = -EIO;
591 goto out;
592 }
593
594 /* copy the tanslated protected key */
595 switch (prepparm->lv3.keyblock.keylen) {
596 case 16+32:
597 protkey->type = PKEY_KEYTYPE_AES_128;
598 break;
599 case 24+32:
600 protkey->type = PKEY_KEYTYPE_AES_192;
601 break;
602 case 32+32:
603 protkey->type = PKEY_KEYTYPE_AES_256;
604 break;
605 default:
606 DEBUG_ERR("pkey_sec2protkey unknown/unsupported keytype %d\n",
607 prepparm->lv3.keyblock.keylen);
608 rc = -EIO;
609 goto out;
610 }
611 protkey->len = prepparm->lv3.keyblock.keylen;
612 memcpy(protkey->protkey, prepparm->lv3.keyblock.key, protkey->len);
613
614out:
615 free_cprbmem(mem, PARMBSIZE, 0);
616 return rc;
617}
618EXPORT_SYMBOL(pkey_sec2protkey);
619
620/*
621 * Create a protected key from a clear key value.
622 */
623int pkey_clr2protkey(u32 keytype,
624 const struct pkey_clrkey *clrkey,
625 struct pkey_protkey *protkey)
626{
627 long fc;
628 int keysize;
629 u8 paramblock[64];
630
631 switch (keytype) {
632 case PKEY_KEYTYPE_AES_128:
633 keysize = 16;
634 fc = CPACF_PCKMO_ENC_AES_128_KEY;
635 break;
636 case PKEY_KEYTYPE_AES_192:
637 keysize = 24;
638 fc = CPACF_PCKMO_ENC_AES_192_KEY;
639 break;
640 case PKEY_KEYTYPE_AES_256:
641 keysize = 32;
642 fc = CPACF_PCKMO_ENC_AES_256_KEY;
643 break;
644 default:
645 DEBUG_ERR("pkey_clr2protkey unknown/unsupported keytype %d\n",
646 keytype);
647 return -EINVAL;
648 }
649
650 /* prepare param block */
651 memset(paramblock, 0, sizeof(paramblock));
652 memcpy(paramblock, clrkey->clrkey, keysize);
653
654 /* call the pckmo instruction */
655 cpacf_pckmo(fc, paramblock);
656
657 /* copy created protected key */
658 protkey->type = keytype;
659 protkey->len = keysize + 32;
660 memcpy(protkey->protkey, paramblock, keysize + 32);
661
662 return 0;
663}
664EXPORT_SYMBOL(pkey_clr2protkey);
665
666/*
667 * query cryptographic facility from adapter
668 */
669static int query_crypto_facility(u16 cardnr, u16 domain,
670 const char *keyword,
671 u8 *rarray, size_t *rarraylen,
672 u8 *varray, size_t *varraylen)
673{
674 int rc;
675 u16 len;
676 u8 *mem, *ptr;
677 struct CPRBX *preqcblk, *prepcblk;
678 struct ica_xcRB xcrb;
679 struct fqreqparm {
680 u8 subfunc_code[2];
681 u16 rule_array_len;
682 char rule_array[8];
683 struct lv1 {
684 u16 len;
685 u8 data[VARDATASIZE];
686 } lv1;
687 u16 dummylen;
688 } *preqparm;
689 size_t parmbsize = sizeof(struct fqreqparm);
690 struct fqrepparm {
691 u8 subfunc_code[2];
692 u8 lvdata[0];
693 } *prepparm;
694
695 /* get already prepared memory for 2 cprbs with param block each */
696 rc = alloc_and_prep_cprbmem(parmbsize, &mem, &preqcblk, &prepcblk);
697 if (rc)
698 return rc;
699
700 /* fill request cprb struct */
701 preqcblk->domain = domain;
702
703 /* fill request cprb param block with FQ request */
704 preqparm = (struct fqreqparm *) preqcblk->req_parmb;
705 memcpy(preqparm->subfunc_code, "FQ", 2);
706 strncpy(preqparm->rule_array, keyword, sizeof(preqparm->rule_array));
707 preqparm->rule_array_len =
708 sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
709 preqparm->lv1.len = sizeof(preqparm->lv1);
710 preqparm->dummylen = sizeof(preqparm->dummylen);
711 preqcblk->req_parml = parmbsize;
712
713 /* fill xcrb struct */
714 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
715
716 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
717 rc = _zcrypt_send_cprb(&xcrb);
718 if (rc) {
719 DEBUG_ERR(
720 "query_crypto_facility zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
721 (int) cardnr, (int) domain, rc);
722 goto out;
723 }
724
725 /* check response returncode and reasoncode */
726 if (prepcblk->ccp_rtcode != 0) {
727 DEBUG_ERR(
728 "query_crypto_facility unwrap secure key failure, card response %d/%d\n",
729 (int) prepcblk->ccp_rtcode,
730 (int) prepcblk->ccp_rscode);
731 rc = -EIO;
732 goto out;
733 }
734
735 /* process response cprb param block */
736 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
737 prepparm = (struct fqrepparm *) prepcblk->rpl_parmb;
738 ptr = prepparm->lvdata;
739
740 /* check and possibly copy reply rule array */
741 len = *((u16 *) ptr);
742 if (len > sizeof(u16)) {
743 ptr += sizeof(u16);
744 len -= sizeof(u16);
745 if (rarray && rarraylen && *rarraylen > 0) {
746 *rarraylen = (len > *rarraylen ? *rarraylen : len);
747 memcpy(rarray, ptr, *rarraylen);
748 }
749 ptr += len;
750 }
751 /* check and possible copy reply var array */
752 len = *((u16 *) ptr);
753 if (len > sizeof(u16)) {
754 ptr += sizeof(u16);
755 len -= sizeof(u16);
756 if (varray && varraylen && *varraylen > 0) {
757 *varraylen = (len > *varraylen ? *varraylen : len);
758 memcpy(varray, ptr, *varraylen);
759 }
760 ptr += len;
761 }
762
763out:
764 free_cprbmem(mem, parmbsize, 0);
765 return rc;
766}
767
768/*
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100769 * Fetch the current and old mkvp values via
770 * query_crypto_facility from adapter.
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100771 */
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100772static int fetch_mkvp(u16 cardnr, u16 domain, u64 mkvp[2])
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100773{
774 int rc, found = 0;
775 size_t rlen, vlen;
776 u8 *rarray, *varray, *pg;
777
778 pg = (u8 *) __get_free_page(GFP_KERNEL);
779 if (!pg)
780 return -ENOMEM;
781 rarray = pg;
782 varray = pg + PAGE_SIZE/2;
783 rlen = vlen = PAGE_SIZE/2;
784
785 rc = query_crypto_facility(cardnr, domain, "STATICSA",
786 rarray, &rlen, varray, &vlen);
787 if (rc == 0 && rlen > 8*8 && vlen > 184+8) {
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100788 if (rarray[8*8] == '2') {
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100789 /* current master key state is valid */
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100790 mkvp[0] = *((u64 *)(varray + 184));
791 mkvp[1] = *((u64 *)(varray + 172));
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100792 found = 1;
793 }
794 }
795
796 free_page((unsigned long) pg);
797
798 return found ? 0 : -ENOENT;
799}
800
801/* struct to hold cached mkvp info for each card/domain */
802struct mkvp_info {
803 struct list_head list;
804 u16 cardnr;
805 u16 domain;
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100806 u64 mkvp[2];
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100807};
808
809/* a list with mkvp_info entries */
810static LIST_HEAD(mkvp_list);
811static DEFINE_SPINLOCK(mkvp_list_lock);
812
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100813static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 mkvp[2])
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100814{
815 int rc = -ENOENT;
816 struct mkvp_info *ptr;
817
818 spin_lock_bh(&mkvp_list_lock);
819 list_for_each_entry(ptr, &mkvp_list, list) {
820 if (ptr->cardnr == cardnr &&
821 ptr->domain == domain) {
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100822 memcpy(mkvp, ptr->mkvp, 2 * sizeof(u64));
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100823 rc = 0;
824 break;
825 }
826 }
827 spin_unlock_bh(&mkvp_list_lock);
828
829 return rc;
830}
831
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100832static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp[2])
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100833{
834 int found = 0;
835 struct mkvp_info *ptr;
836
837 spin_lock_bh(&mkvp_list_lock);
838 list_for_each_entry(ptr, &mkvp_list, list) {
839 if (ptr->cardnr == cardnr &&
840 ptr->domain == domain) {
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100841 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100842 found = 1;
843 break;
844 }
845 }
846 if (!found) {
847 ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
848 if (!ptr) {
849 spin_unlock_bh(&mkvp_list_lock);
850 return;
851 }
852 ptr->cardnr = cardnr;
853 ptr->domain = domain;
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100854 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100855 list_add(&ptr->list, &mkvp_list);
856 }
857 spin_unlock_bh(&mkvp_list_lock);
858}
859
860static void mkvp_cache_scrub(u16 cardnr, u16 domain)
861{
862 struct mkvp_info *ptr;
863
864 spin_lock_bh(&mkvp_list_lock);
865 list_for_each_entry(ptr, &mkvp_list, list) {
866 if (ptr->cardnr == cardnr &&
867 ptr->domain == domain) {
868 list_del(&ptr->list);
869 kfree(ptr);
870 break;
871 }
872 }
873 spin_unlock_bh(&mkvp_list_lock);
874}
875
876static void __exit mkvp_cache_free(void)
877{
878 struct mkvp_info *ptr, *pnext;
879
880 spin_lock_bh(&mkvp_list_lock);
881 list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) {
882 list_del(&ptr->list);
883 kfree(ptr);
884 }
885 spin_unlock_bh(&mkvp_list_lock);
886}
887
888/*
889 * Search for a matching crypto card based on the Master Key
890 * Verification Pattern provided inside a secure key.
891 */
892int pkey_findcard(const struct pkey_seckey *seckey,
893 u16 *pcardnr, u16 *pdomain, int verify)
894{
895 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
896 struct zcrypt_device_matrix *device_matrix;
897 u16 card, dom;
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100898 u64 mkvp[2];
899 int i, rc, oi = -1;
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100900
901 /* mkvp must not be zero */
902 if (t->mkvp == 0)
903 return -EINVAL;
904
905 /* fetch status of all crypto cards */
906 device_matrix = kmalloc(sizeof(struct zcrypt_device_matrix),
907 GFP_KERNEL);
908 if (!device_matrix)
909 return -ENOMEM;
910 zcrypt_device_status_mask(device_matrix);
911
912 /* walk through all crypto cards */
913 for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
914 card = AP_QID_CARD(device_matrix->device[i].qid);
915 dom = AP_QID_QUEUE(device_matrix->device[i].qid);
916 if (device_matrix->device[i].online &&
917 device_matrix->device[i].functions & 0x04) {
918 /* an enabled CCA Coprocessor card */
919 /* try cached mkvp */
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100920 if (mkvp_cache_fetch(card, dom, mkvp) == 0 &&
921 t->mkvp == mkvp[0]) {
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100922 if (!verify)
923 break;
924 /* verify: fetch mkvp from adapter */
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100925 if (fetch_mkvp(card, dom, mkvp) == 0) {
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100926 mkvp_cache_update(card, dom, mkvp);
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100927 if (t->mkvp == mkvp[0])
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100928 break;
929 }
930 }
931 } else {
932 /* Card is offline and/or not a CCA card. */
933 /* del mkvp entry from cache if it exists */
934 mkvp_cache_scrub(card, dom);
935 }
936 }
937 if (i >= MAX_ZDEV_ENTRIES) {
938 /* nothing found, so this time without cache */
939 for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
940 if (!(device_matrix->device[i].online &&
941 device_matrix->device[i].functions & 0x04))
942 continue;
943 card = AP_QID_CARD(device_matrix->device[i].qid);
944 dom = AP_QID_QUEUE(device_matrix->device[i].qid);
945 /* fresh fetch mkvp from adapter */
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100946 if (fetch_mkvp(card, dom, mkvp) == 0) {
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100947 mkvp_cache_update(card, dom, mkvp);
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100948 if (t->mkvp == mkvp[0])
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100949 break;
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100950 if (t->mkvp == mkvp[1] && oi < 0)
951 oi = i;
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100952 }
953 }
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100954 if (i >= MAX_ZDEV_ENTRIES && oi >= 0) {
955 /* old mkvp matched, use this card then */
956 card = AP_QID_CARD(device_matrix->device[oi].qid);
957 dom = AP_QID_QUEUE(device_matrix->device[oi].qid);
958 }
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100959 }
Harald Freudenbergerca681ec2017-03-15 10:58:07 +0100960 if (i < MAX_ZDEV_ENTRIES || oi >= 0) {
Harald Freudenbergere80d4af2016-11-02 14:37:20 +0100961 if (pcardnr)
962 *pcardnr = card;
963 if (pdomain)
964 *pdomain = dom;
965 rc = 0;
966 } else
967 rc = -ENODEV;
968
969 kfree(device_matrix);
970 return rc;
971}
972EXPORT_SYMBOL(pkey_findcard);
973
974/*
975 * Find card and transform secure key into protected key.
976 */
977int pkey_skey2pkey(const struct pkey_seckey *seckey,
978 struct pkey_protkey *protkey)
979{
980 u16 cardnr, domain;
981 int rc, verify;
982
983 /*
984 * The pkey_sec2protkey call may fail when a card has been
985 * addressed where the master key was changed after last fetch
986 * of the mkvp into the cache. So first try without verify then
987 * with verify enabled (thus refreshing the mkvp for each card).
988 */
989 for (verify = 0; verify < 2; verify++) {
990 rc = pkey_findcard(seckey, &cardnr, &domain, verify);
991 if (rc)
992 continue;
993 rc = pkey_sec2protkey(cardnr, domain, seckey, protkey);
994 if (rc == 0)
995 break;
996 }
997
998 if (rc)
999 DEBUG_DBG("pkey_skey2pkey failed rc=%d\n", rc);
1000
1001 return rc;
1002}
1003EXPORT_SYMBOL(pkey_skey2pkey);
1004
1005/*
Harald Freudenbergere61a6132017-03-15 11:08:27 +01001006 * Verify key and give back some info about the key.
1007 */
1008int pkey_verifykey(const struct pkey_seckey *seckey,
1009 u16 *pcardnr, u16 *pdomain,
1010 u16 *pkeysize, u32 *pattributes)
1011{
1012 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
1013 u16 cardnr, domain;
1014 u64 mkvp[2];
1015 int rc;
1016
1017 /* check the secure key for valid AES secure key */
1018 rc = check_secaeskeytoken((u8 *) seckey, 0);
1019 if (rc)
1020 goto out;
1021 if (pattributes)
1022 *pattributes = PKEY_VERIFY_ATTR_AES;
1023 if (pkeysize)
1024 *pkeysize = t->bitsize;
1025
1026 /* try to find a card which can handle this key */
1027 rc = pkey_findcard(seckey, &cardnr, &domain, 1);
1028 if (rc)
1029 goto out;
1030
1031 /* check mkvp for old mkvp match */
1032 rc = mkvp_cache_fetch(cardnr, domain, mkvp);
1033 if (rc)
1034 goto out;
1035 if (t->mkvp == mkvp[1]) {
1036 DEBUG_DBG("pkey_verifykey secure key has old mkvp\n");
1037 if (pattributes)
1038 *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
1039 }
1040
1041 if (pcardnr)
1042 *pcardnr = cardnr;
1043 if (pdomain)
1044 *pdomain = domain;
1045
1046out:
1047 DEBUG_DBG("pkey_verifykey rc=%d\n", rc);
1048 return rc;
1049}
1050EXPORT_SYMBOL(pkey_verifykey);
1051
1052/*
Harald Freudenbergere80d4af2016-11-02 14:37:20 +01001053 * File io functions
1054 */
1055
1056static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1057 unsigned long arg)
1058{
1059 int rc;
1060
1061 switch (cmd) {
1062 case PKEY_GENSECK: {
1063 struct pkey_genseck __user *ugs = (void __user *) arg;
1064 struct pkey_genseck kgs;
1065
1066 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1067 return -EFAULT;
1068 rc = pkey_genseckey(kgs.cardnr, kgs.domain,
1069 kgs.keytype, &kgs.seckey);
1070 DEBUG_DBG("pkey_ioctl pkey_genseckey()=%d\n", rc);
1071 if (rc)
1072 break;
1073 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1074 return -EFAULT;
1075 break;
1076 }
1077 case PKEY_CLR2SECK: {
1078 struct pkey_clr2seck __user *ucs = (void __user *) arg;
1079 struct pkey_clr2seck kcs;
1080
1081 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1082 return -EFAULT;
1083 rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1084 &kcs.clrkey, &kcs.seckey);
1085 DEBUG_DBG("pkey_ioctl pkey_clr2seckey()=%d\n", rc);
1086 if (rc)
1087 break;
1088 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1089 return -EFAULT;
1090 memzero_explicit(&kcs, sizeof(kcs));
1091 break;
1092 }
1093 case PKEY_SEC2PROTK: {
1094 struct pkey_sec2protk __user *usp = (void __user *) arg;
1095 struct pkey_sec2protk ksp;
1096
1097 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1098 return -EFAULT;
1099 rc = pkey_sec2protkey(ksp.cardnr, ksp.domain,
1100 &ksp.seckey, &ksp.protkey);
1101 DEBUG_DBG("pkey_ioctl pkey_sec2protkey()=%d\n", rc);
1102 if (rc)
1103 break;
1104 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1105 return -EFAULT;
1106 break;
1107 }
1108 case PKEY_CLR2PROTK: {
1109 struct pkey_clr2protk __user *ucp = (void __user *) arg;
1110 struct pkey_clr2protk kcp;
1111
1112 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1113 return -EFAULT;
1114 rc = pkey_clr2protkey(kcp.keytype,
1115 &kcp.clrkey, &kcp.protkey);
1116 DEBUG_DBG("pkey_ioctl pkey_clr2protkey()=%d\n", rc);
1117 if (rc)
1118 break;
1119 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1120 return -EFAULT;
1121 memzero_explicit(&kcp, sizeof(kcp));
1122 break;
1123 }
1124 case PKEY_FINDCARD: {
1125 struct pkey_findcard __user *ufc = (void __user *) arg;
1126 struct pkey_findcard kfc;
1127
1128 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1129 return -EFAULT;
1130 rc = pkey_findcard(&kfc.seckey,
1131 &kfc.cardnr, &kfc.domain, 1);
1132 DEBUG_DBG("pkey_ioctl pkey_findcard()=%d\n", rc);
1133 if (rc)
1134 break;
1135 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1136 return -EFAULT;
1137 break;
1138 }
1139 case PKEY_SKEY2PKEY: {
1140 struct pkey_skey2pkey __user *usp = (void __user *) arg;
1141 struct pkey_skey2pkey ksp;
1142
1143 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1144 return -EFAULT;
1145 rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
1146 DEBUG_DBG("pkey_ioctl pkey_skey2pkey()=%d\n", rc);
1147 if (rc)
1148 break;
1149 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1150 return -EFAULT;
1151 break;
1152 }
Harald Freudenbergere61a6132017-03-15 11:08:27 +01001153 case PKEY_VERIFYKEY: {
1154 struct pkey_verifykey __user *uvk = (void __user *) arg;
1155 struct pkey_verifykey kvk;
1156
1157 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1158 return -EFAULT;
1159 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1160 &kvk.keysize, &kvk.attributes);
1161 DEBUG_DBG("pkey_ioctl pkey_verifykey()=%d\n", rc);
1162 if (rc)
1163 break;
1164 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1165 return -EFAULT;
1166 break;
1167 }
Harald Freudenbergere80d4af2016-11-02 14:37:20 +01001168 default:
1169 /* unknown/unsupported ioctl cmd */
1170 return -ENOTTY;
1171 }
1172
1173 return rc;
1174}
1175
1176/*
1177 * Sysfs and file io operations
1178 */
1179static const struct file_operations pkey_fops = {
1180 .owner = THIS_MODULE,
1181 .open = nonseekable_open,
1182 .llseek = no_llseek,
1183 .unlocked_ioctl = pkey_unlocked_ioctl,
1184};
1185
1186static struct miscdevice pkey_dev = {
1187 .name = "pkey",
1188 .minor = MISC_DYNAMIC_MINOR,
1189 .mode = 0666,
1190 .fops = &pkey_fops,
1191};
1192
1193/*
1194 * Module init
1195 */
Heiko Carstenscb4ef3c2017-05-09 12:44:21 +02001196static int __init pkey_init(void)
Harald Freudenbergere80d4af2016-11-02 14:37:20 +01001197{
1198 cpacf_mask_t pckmo_functions;
1199
1200 /* check for pckmo instructions available */
1201 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
1202 return -EOPNOTSUPP;
1203 if (!cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_128_KEY) ||
1204 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_192_KEY) ||
1205 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_256_KEY))
1206 return -EOPNOTSUPP;
1207
1208 pkey_debug_init();
1209
1210 return misc_register(&pkey_dev);
1211}
1212
1213/*
1214 * Module exit
1215 */
1216static void __exit pkey_exit(void)
1217{
1218 misc_deregister(&pkey_dev);
1219 mkvp_cache_free();
1220 pkey_debug_exit();
1221}
1222
1223module_init(pkey_init);
1224module_exit(pkey_exit);