4 * Copyright IBM Corp. 2017
5 * Author(s): Harald Freudenberger
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.
13 #define KMSG_COMPONENT "pkey"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
27 #include "zcrypt_api.h"
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("IBM Corporation");
31 MODULE_DESCRIPTION("s390 protected key interface");
33 /* Size of parameter block used for all cca requests/replies */
36 /* Size of vardata block used for some of the cca requests/replies */
37 #define VARDATASIZE 4096
40 * debug feature data and functions
43 static debug_info_t *debug_info;
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__)
50 static void __init pkey_debug_init(void)
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);
57 static void __exit pkey_debug_exit(void)
59 debug_unregister(debug_info);
62 /* inside view of a secure key token (only type 0x01 version 0x04) */
63 struct secaeskeytoken {
64 u8 type; /* 0x01 for internal key token */
66 u8 version; /* should be 0x04 */
68 u8 flag; /* key flags */
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 */
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.
83 static int check_secaeskeytoken(u8 *token, int keybitsize)
85 struct secaeskeytoken *t = (struct secaeskeytoken *) token;
87 if (t->type != 0x01) {
89 "check_secaeskeytoken secure token check failed, type mismatch 0x%02x != 0x01\n",
93 if (t->version != 0x04) {
95 "check_secaeskeytoken secure token check failed, version mismatch 0x%02x != 0x04\n",
99 if (keybitsize > 0 && t->bitsize != keybitsize) {
101 "check_secaeskeytoken secure token check failed, bitsize mismatch %d != %d\n",
102 (int) t->bitsize, keybitsize);
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
115 static int alloc_and_prep_cprbmem(size_t paramblen,
117 struct CPRBX **preqCPRB,
118 struct CPRBX **prepCPRB)
121 size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen;
122 struct CPRBX *preqcblk, *prepcblk;
125 * allocate consecutive memory for request CPRB, request param
126 * block, reply CPRB and reply param block
128 cprbmem = kmalloc(2 * cprbplusparamblen, GFP_KERNEL);
131 memset(cprbmem, 0, 2 * cprbplusparamblen);
133 preqcblk = (struct CPRBX *) cprbmem;
134 prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen);
136 /* fill request cprb struct */
137 preqcblk->cprb_len = sizeof(struct CPRBX);
138 preqcblk->cprb_ver_id = 0x02;
139 memcpy(preqcblk->func_id, "T2", 2);
140 preqcblk->rpl_msgbl = cprbplusparamblen;
142 preqcblk->req_parmb =
143 ((u8 *) preqcblk) + sizeof(struct CPRBX);
144 preqcblk->rpl_parmb =
145 ((u8 *) prepcblk) + sizeof(struct CPRBX);
149 *preqCPRB = preqcblk;
150 *prepCPRB = prepcblk;
156 * Free the cprb memory allocated with the function above.
157 * If the scrub value is not zero, the memory is filled
158 * with zeros before freeing (useful if there was some
159 * clear key material in there).
161 static void free_cprbmem(void *mem, size_t paramblen, int scrub)
164 memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen));
169 * Helper function to prepare the xcrb struct
171 static inline void prep_xcrb(struct ica_xcRB *pxcrb,
173 struct CPRBX *preqcblk,
174 struct CPRBX *prepcblk)
176 memset(pxcrb, 0, sizeof(*pxcrb));
177 pxcrb->agent_ID = 0x4341; /* 'CA' */
178 pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr);
179 pxcrb->request_control_blk_length =
180 preqcblk->cprb_len + preqcblk->req_parml;
181 pxcrb->request_control_blk_addr = (void *) preqcblk;
182 pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl;
183 pxcrb->reply_control_blk_addr = (void *) prepcblk;
187 * Helper function which calls zcrypt_send_cprb with
188 * memory management segment adjusted to kernel space
189 * so that the copy_from_user called within this
190 * function do in fact copy from kernel space.
192 static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb)
195 mm_segment_t old_fs = get_fs();
198 rc = zcrypt_send_cprb(xcrb);
205 * Generate (random) AES secure key.
207 int pkey_genseckey(u16 cardnr, u16 domain,
208 u32 keytype, struct pkey_seckey *seckey)
213 struct CPRBX *preqcblk, *prepcblk;
214 struct ica_xcRB xcrb;
230 u8 data[SECKEYBLOBSIZE];
244 /* ... some more data ... */
249 /* get already prepared memory for 2 cprbs with param block each */
250 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
254 /* fill request cprb struct */
255 preqcblk->domain = domain;
257 /* fill request cprb param block with KG request */
258 preqparm = (struct kgreqparm *) preqcblk->req_parmb;
259 memcpy(preqparm->subfunc_code, "KG", 2);
260 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
261 preqparm->lv1.len = sizeof(struct lv1);
262 memcpy(preqparm->lv1.key_form, "OP ", 8);
264 case PKEY_KEYTYPE_AES_128:
266 memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8);
268 case PKEY_KEYTYPE_AES_192:
270 memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8);
272 case PKEY_KEYTYPE_AES_256:
274 memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8);
278 "pkey_genseckey unknown/unsupported keytype %d\n",
283 memcpy(preqparm->lv1.key_type1, "AESDATA ", 8);
284 preqparm->lv2.len = sizeof(struct lv2);
285 for (i = 0; i < 6; i++) {
286 preqparm->lv2.keyid[i].len = sizeof(struct keyid);
287 preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10);
289 preqcblk->req_parml = sizeof(struct kgreqparm);
291 /* fill xcrb struct */
292 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
294 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
295 rc = _zcrypt_send_cprb(&xcrb);
298 "pkey_genseckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
299 (int) cardnr, (int) domain, rc);
303 /* check response returncode and reasoncode */
304 if (prepcblk->ccp_rtcode != 0) {
306 "pkey_genseckey secure key generate failure, card response %d/%d\n",
307 (int) prepcblk->ccp_rtcode,
308 (int) prepcblk->ccp_rscode);
313 /* process response cprb param block */
314 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
315 prepparm = (struct kgrepparm *) prepcblk->rpl_parmb;
317 /* check length of the returned secure key token */
318 seckeysize = prepparm->lv3.keyblock.toklen
319 - sizeof(prepparm->lv3.keyblock.toklen)
320 - sizeof(prepparm->lv3.keyblock.tokattr);
321 if (seckeysize != SECKEYBLOBSIZE) {
323 "pkey_genseckey secure token size mismatch %d != %d bytes\n",
324 seckeysize, SECKEYBLOBSIZE);
329 /* check secure key token */
330 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
336 /* copy the generated secure key token */
337 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
340 free_cprbmem(mem, PARMBSIZE, 0);
343 EXPORT_SYMBOL(pkey_genseckey);
346 * Generate an AES secure key with given key value.
348 int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype,
349 const struct pkey_clrkey *clrkey,
350 struct pkey_seckey *seckey)
352 int rc, keysize, seckeysize;
354 struct CPRBX *preqcblk, *prepcblk;
355 struct ica_xcRB xcrb;
369 u8 data[SECKEYBLOBSIZE];
384 /* ... some more data ... */
389 /* get already prepared memory for 2 cprbs with param block each */
390 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
394 /* fill request cprb struct */
395 preqcblk->domain = domain;
397 /* fill request cprb param block with CM request */
398 preqparm = (struct cmreqparm *) preqcblk->req_parmb;
399 memcpy(preqparm->subfunc_code, "CM", 2);
400 memcpy(preqparm->rule_array, "AES ", 8);
401 preqparm->rule_array_len =
402 sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
404 case PKEY_KEYTYPE_AES_128:
407 case PKEY_KEYTYPE_AES_192:
410 case PKEY_KEYTYPE_AES_256:
415 "pkey_clr2seckey unknown/unsupported keytype %d\n",
420 preqparm->lv1.len = sizeof(struct lv1) + keysize;
421 memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize);
422 plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize);
423 plv2->len = sizeof(struct lv2);
424 plv2->keyid.len = sizeof(struct keyid);
425 plv2->keyid.attr = 0x30;
426 preqcblk->req_parml = sizeof(struct cmreqparm) + keysize;
428 /* fill xcrb struct */
429 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
431 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
432 rc = _zcrypt_send_cprb(&xcrb);
435 "pkey_clr2seckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
436 (int) cardnr, (int) domain, rc);
440 /* check response returncode and reasoncode */
441 if (prepcblk->ccp_rtcode != 0) {
443 "pkey_clr2seckey clear key import failure, card response %d/%d\n",
444 (int) prepcblk->ccp_rtcode,
445 (int) prepcblk->ccp_rscode);
450 /* process response cprb param block */
451 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
452 prepparm = (struct cmrepparm *) prepcblk->rpl_parmb;
454 /* check length of the returned secure key token */
455 seckeysize = prepparm->lv3.keyblock.toklen
456 - sizeof(prepparm->lv3.keyblock.toklen)
457 - sizeof(prepparm->lv3.keyblock.tokattr);
458 if (seckeysize != SECKEYBLOBSIZE) {
460 "pkey_clr2seckey secure token size mismatch %d != %d bytes\n",
461 seckeysize, SECKEYBLOBSIZE);
466 /* check secure key token */
467 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
473 /* copy the generated secure key token */
474 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
477 free_cprbmem(mem, PARMBSIZE, 1);
480 EXPORT_SYMBOL(pkey_clr2seckey);
483 * Derive a proteced key from the secure key blob.
485 int pkey_sec2protkey(u16 cardnr, u16 domain,
486 const struct pkey_seckey *seckey,
487 struct pkey_protkey *protkey)
491 struct CPRBX *preqcblk, *prepcblk;
492 struct ica_xcRB xcrb;
505 u8 token[0]; /* cca secure key token */
515 struct cpacfkeyblock {
516 u8 version; /* version of this struct */
522 u8 key[64]; /* the key (keylen bytes) */
527 u8 vp[32]; /* verification pattern */
532 /* get already prepared memory for 2 cprbs with param block each */
533 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
537 /* fill request cprb struct */
538 preqcblk->domain = domain;
540 /* fill request cprb param block with USK request */
541 preqparm = (struct uskreqparm *) preqcblk->req_parmb;
542 memcpy(preqparm->subfunc_code, "US", 2);
543 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
544 preqparm->lv1.len = sizeof(struct lv1);
545 preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len);
546 preqparm->lv1.attr_flags = 0x0001;
547 preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE;
548 preqparm->lv2.attr_len = sizeof(struct lv2)
549 - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE;
550 preqparm->lv2.attr_flags = 0x0000;
551 memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE);
552 preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE;
554 /* fill xcrb struct */
555 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
557 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
558 rc = _zcrypt_send_cprb(&xcrb);
561 "pkey_sec2protkey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
562 (int) cardnr, (int) domain, rc);
566 /* check response returncode and reasoncode */
567 if (prepcblk->ccp_rtcode != 0) {
569 "pkey_sec2protkey unwrap secure key failure, card response %d/%d\n",
570 (int) prepcblk->ccp_rtcode,
571 (int) prepcblk->ccp_rscode);
575 if (prepcblk->ccp_rscode != 0) {
577 "pkey_sec2protkey unwrap secure key warning, card response %d/%d\n",
578 (int) prepcblk->ccp_rtcode,
579 (int) prepcblk->ccp_rscode);
582 /* process response cprb param block */
583 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
584 prepparm = (struct uskrepparm *) prepcblk->rpl_parmb;
586 /* check the returned keyblock */
587 if (prepparm->lv3.keyblock.version != 0x01) {
589 "pkey_sec2protkey reply param keyblock version mismatch 0x%02x != 0x01\n",
590 (int) prepparm->lv3.keyblock.version);
595 /* copy the tanslated protected key */
596 switch (prepparm->lv3.keyblock.keylen) {
598 protkey->type = PKEY_KEYTYPE_AES_128;
601 protkey->type = PKEY_KEYTYPE_AES_192;
604 protkey->type = PKEY_KEYTYPE_AES_256;
607 DEBUG_ERR("pkey_sec2protkey unknown/unsupported keytype %d\n",
608 prepparm->lv3.keyblock.keylen);
612 protkey->len = prepparm->lv3.keyblock.keylen;
613 memcpy(protkey->protkey, prepparm->lv3.keyblock.key, protkey->len);
616 free_cprbmem(mem, PARMBSIZE, 0);
619 EXPORT_SYMBOL(pkey_sec2protkey);
622 * Create a protected key from a clear key value.
624 int pkey_clr2protkey(u32 keytype,
625 const struct pkey_clrkey *clrkey,
626 struct pkey_protkey *protkey)
633 case PKEY_KEYTYPE_AES_128:
635 fc = CPACF_PCKMO_ENC_AES_128_KEY;
637 case PKEY_KEYTYPE_AES_192:
639 fc = CPACF_PCKMO_ENC_AES_192_KEY;
641 case PKEY_KEYTYPE_AES_256:
643 fc = CPACF_PCKMO_ENC_AES_256_KEY;
646 DEBUG_ERR("pkey_clr2protkey unknown/unsupported keytype %d\n",
651 /* prepare param block */
652 memset(paramblock, 0, sizeof(paramblock));
653 memcpy(paramblock, clrkey->clrkey, keysize);
655 /* call the pckmo instruction */
656 cpacf_pckmo(fc, paramblock);
658 /* copy created protected key */
659 protkey->type = keytype;
660 protkey->len = keysize + 32;
661 memcpy(protkey->protkey, paramblock, keysize + 32);
665 EXPORT_SYMBOL(pkey_clr2protkey);
668 * query cryptographic facility from adapter
670 static int query_crypto_facility(u16 cardnr, u16 domain,
672 u8 *rarray, size_t *rarraylen,
673 u8 *varray, size_t *varraylen)
678 struct CPRBX *preqcblk, *prepcblk;
679 struct ica_xcRB xcrb;
686 u8 data[VARDATASIZE];
690 size_t parmbsize = sizeof(struct fqreqparm);
696 /* get already prepared memory for 2 cprbs with param block each */
697 rc = alloc_and_prep_cprbmem(parmbsize, &mem, &preqcblk, &prepcblk);
701 /* fill request cprb struct */
702 preqcblk->domain = domain;
704 /* fill request cprb param block with FQ request */
705 preqparm = (struct fqreqparm *) preqcblk->req_parmb;
706 memcpy(preqparm->subfunc_code, "FQ", 2);
707 strncpy(preqparm->rule_array, keyword, sizeof(preqparm->rule_array));
708 preqparm->rule_array_len =
709 sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
710 preqparm->lv1.len = sizeof(preqparm->lv1);
711 preqparm->dummylen = sizeof(preqparm->dummylen);
712 preqcblk->req_parml = parmbsize;
714 /* fill xcrb struct */
715 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
717 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
718 rc = _zcrypt_send_cprb(&xcrb);
721 "query_crypto_facility zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
722 (int) cardnr, (int) domain, rc);
726 /* check response returncode and reasoncode */
727 if (prepcblk->ccp_rtcode != 0) {
729 "query_crypto_facility unwrap secure key failure, card response %d/%d\n",
730 (int) prepcblk->ccp_rtcode,
731 (int) prepcblk->ccp_rscode);
736 /* process response cprb param block */
737 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
738 prepparm = (struct fqrepparm *) prepcblk->rpl_parmb;
739 ptr = prepparm->lvdata;
741 /* check and possibly copy reply rule array */
742 len = *((u16 *) ptr);
743 if (len > sizeof(u16)) {
746 if (rarray && rarraylen && *rarraylen > 0) {
747 *rarraylen = (len > *rarraylen ? *rarraylen : len);
748 memcpy(rarray, ptr, *rarraylen);
752 /* check and possible copy reply var array */
753 len = *((u16 *) ptr);
754 if (len > sizeof(u16)) {
757 if (varray && varraylen && *varraylen > 0) {
758 *varraylen = (len > *varraylen ? *varraylen : len);
759 memcpy(varray, ptr, *varraylen);
765 free_cprbmem(mem, parmbsize, 0);
770 * Fetch the current and old mkvp values via
771 * query_crypto_facility from adapter.
773 static int fetch_mkvp(u16 cardnr, u16 domain, u64 mkvp[2])
777 u8 *rarray, *varray, *pg;
779 pg = (u8 *) __get_free_page(GFP_KERNEL);
783 varray = pg + PAGE_SIZE/2;
784 rlen = vlen = PAGE_SIZE/2;
786 rc = query_crypto_facility(cardnr, domain, "STATICSA",
787 rarray, &rlen, varray, &vlen);
788 if (rc == 0 && rlen > 8*8 && vlen > 184+8) {
789 if (rarray[8*8] == '2') {
790 /* current master key state is valid */
791 mkvp[0] = *((u64 *)(varray + 184));
792 mkvp[1] = *((u64 *)(varray + 172));
797 free_page((unsigned long) pg);
799 return found ? 0 : -ENOENT;
802 /* struct to hold cached mkvp info for each card/domain */
804 struct list_head list;
810 /* a list with mkvp_info entries */
811 static LIST_HEAD(mkvp_list);
812 static DEFINE_SPINLOCK(mkvp_list_lock);
814 static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 mkvp[2])
817 struct mkvp_info *ptr;
819 spin_lock_bh(&mkvp_list_lock);
820 list_for_each_entry(ptr, &mkvp_list, list) {
821 if (ptr->cardnr == cardnr &&
822 ptr->domain == domain) {
823 memcpy(mkvp, ptr->mkvp, 2 * sizeof(u64));
828 spin_unlock_bh(&mkvp_list_lock);
833 static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp[2])
836 struct mkvp_info *ptr;
838 spin_lock_bh(&mkvp_list_lock);
839 list_for_each_entry(ptr, &mkvp_list, list) {
840 if (ptr->cardnr == cardnr &&
841 ptr->domain == domain) {
842 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
848 ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
850 spin_unlock_bh(&mkvp_list_lock);
853 ptr->cardnr = cardnr;
854 ptr->domain = domain;
855 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
856 list_add(&ptr->list, &mkvp_list);
858 spin_unlock_bh(&mkvp_list_lock);
861 static void mkvp_cache_scrub(u16 cardnr, u16 domain)
863 struct mkvp_info *ptr;
865 spin_lock_bh(&mkvp_list_lock);
866 list_for_each_entry(ptr, &mkvp_list, list) {
867 if (ptr->cardnr == cardnr &&
868 ptr->domain == domain) {
869 list_del(&ptr->list);
874 spin_unlock_bh(&mkvp_list_lock);
877 static void __exit mkvp_cache_free(void)
879 struct mkvp_info *ptr, *pnext;
881 spin_lock_bh(&mkvp_list_lock);
882 list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) {
883 list_del(&ptr->list);
886 spin_unlock_bh(&mkvp_list_lock);
890 * Search for a matching crypto card based on the Master Key
891 * Verification Pattern provided inside a secure key.
893 int pkey_findcard(const struct pkey_seckey *seckey,
894 u16 *pcardnr, u16 *pdomain, int verify)
896 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
897 struct zcrypt_device_matrix *device_matrix;
902 /* mkvp must not be zero */
906 /* fetch status of all crypto cards */
907 device_matrix = kmalloc(sizeof(struct zcrypt_device_matrix),
911 zcrypt_device_status_mask(device_matrix);
913 /* walk through all crypto cards */
914 for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
915 card = AP_QID_CARD(device_matrix->device[i].qid);
916 dom = AP_QID_QUEUE(device_matrix->device[i].qid);
917 if (device_matrix->device[i].online &&
918 device_matrix->device[i].functions & 0x04) {
919 /* an enabled CCA Coprocessor card */
920 /* try cached mkvp */
921 if (mkvp_cache_fetch(card, dom, mkvp) == 0 &&
922 t->mkvp == mkvp[0]) {
925 /* verify: fetch mkvp from adapter */
926 if (fetch_mkvp(card, dom, mkvp) == 0) {
927 mkvp_cache_update(card, dom, mkvp);
928 if (t->mkvp == mkvp[0])
933 /* Card is offline and/or not a CCA card. */
934 /* del mkvp entry from cache if it exists */
935 mkvp_cache_scrub(card, dom);
938 if (i >= MAX_ZDEV_ENTRIES) {
939 /* nothing found, so this time without cache */
940 for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
941 if (!(device_matrix->device[i].online &&
942 device_matrix->device[i].functions & 0x04))
944 card = AP_QID_CARD(device_matrix->device[i].qid);
945 dom = AP_QID_QUEUE(device_matrix->device[i].qid);
946 /* fresh fetch mkvp from adapter */
947 if (fetch_mkvp(card, dom, mkvp) == 0) {
948 mkvp_cache_update(card, dom, mkvp);
949 if (t->mkvp == mkvp[0])
951 if (t->mkvp == mkvp[1] && oi < 0)
955 if (i >= MAX_ZDEV_ENTRIES && oi >= 0) {
956 /* old mkvp matched, use this card then */
957 card = AP_QID_CARD(device_matrix->device[oi].qid);
958 dom = AP_QID_QUEUE(device_matrix->device[oi].qid);
961 if (i < MAX_ZDEV_ENTRIES || oi >= 0) {
970 kfree(device_matrix);
973 EXPORT_SYMBOL(pkey_findcard);
976 * Find card and transform secure key into protected key.
978 int pkey_skey2pkey(const struct pkey_seckey *seckey,
979 struct pkey_protkey *protkey)
985 * The pkey_sec2protkey call may fail when a card has been
986 * addressed where the master key was changed after last fetch
987 * of the mkvp into the cache. So first try without verify then
988 * with verify enabled (thus refreshing the mkvp for each card).
990 for (verify = 0; verify < 2; verify++) {
991 rc = pkey_findcard(seckey, &cardnr, &domain, verify);
994 rc = pkey_sec2protkey(cardnr, domain, seckey, protkey);
1000 DEBUG_DBG("pkey_skey2pkey failed rc=%d\n", rc);
1004 EXPORT_SYMBOL(pkey_skey2pkey);
1010 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1016 case PKEY_GENSECK: {
1017 struct pkey_genseck __user *ugs = (void __user *) arg;
1018 struct pkey_genseck kgs;
1020 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1022 rc = pkey_genseckey(kgs.cardnr, kgs.domain,
1023 kgs.keytype, &kgs.seckey);
1024 DEBUG_DBG("pkey_ioctl pkey_genseckey()=%d\n", rc);
1027 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1031 case PKEY_CLR2SECK: {
1032 struct pkey_clr2seck __user *ucs = (void __user *) arg;
1033 struct pkey_clr2seck kcs;
1035 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1037 rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1038 &kcs.clrkey, &kcs.seckey);
1039 DEBUG_DBG("pkey_ioctl pkey_clr2seckey()=%d\n", rc);
1042 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1044 memzero_explicit(&kcs, sizeof(kcs));
1047 case PKEY_SEC2PROTK: {
1048 struct pkey_sec2protk __user *usp = (void __user *) arg;
1049 struct pkey_sec2protk ksp;
1051 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1053 rc = pkey_sec2protkey(ksp.cardnr, ksp.domain,
1054 &ksp.seckey, &ksp.protkey);
1055 DEBUG_DBG("pkey_ioctl pkey_sec2protkey()=%d\n", rc);
1058 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1062 case PKEY_CLR2PROTK: {
1063 struct pkey_clr2protk __user *ucp = (void __user *) arg;
1064 struct pkey_clr2protk kcp;
1066 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1068 rc = pkey_clr2protkey(kcp.keytype,
1069 &kcp.clrkey, &kcp.protkey);
1070 DEBUG_DBG("pkey_ioctl pkey_clr2protkey()=%d\n", rc);
1073 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1075 memzero_explicit(&kcp, sizeof(kcp));
1078 case PKEY_FINDCARD: {
1079 struct pkey_findcard __user *ufc = (void __user *) arg;
1080 struct pkey_findcard kfc;
1082 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1084 rc = pkey_findcard(&kfc.seckey,
1085 &kfc.cardnr, &kfc.domain, 1);
1086 DEBUG_DBG("pkey_ioctl pkey_findcard()=%d\n", rc);
1089 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1093 case PKEY_SKEY2PKEY: {
1094 struct pkey_skey2pkey __user *usp = (void __user *) arg;
1095 struct pkey_skey2pkey ksp;
1097 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1099 rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
1100 DEBUG_DBG("pkey_ioctl pkey_skey2pkey()=%d\n", rc);
1103 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1108 /* unknown/unsupported ioctl cmd */
1116 * Sysfs and file io operations
1118 static const struct file_operations pkey_fops = {
1119 .owner = THIS_MODULE,
1120 .open = nonseekable_open,
1121 .llseek = no_llseek,
1122 .unlocked_ioctl = pkey_unlocked_ioctl,
1125 static struct miscdevice pkey_dev = {
1127 .minor = MISC_DYNAMIC_MINOR,
1135 int __init pkey_init(void)
1137 cpacf_mask_t pckmo_functions;
1139 /* check for pckmo instructions available */
1140 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
1142 if (!cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_128_KEY) ||
1143 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_192_KEY) ||
1144 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_256_KEY))
1149 return misc_register(&pkey_dev);
1155 static void __exit pkey_exit(void)
1157 misc_deregister(&pkey_dev);
1162 module_init(pkey_init);
1163 module_exit(pkey_exit);