1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2017
6 * Author(s): Harald Freudenberger
9 #define KMSG_COMPONENT "pkey"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
23 #include "zcrypt_api.h"
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("IBM Corporation");
27 MODULE_DESCRIPTION("s390 protected key interface");
29 /* Size of parameter block used for all cca requests/replies */
32 /* Size of vardata block used for some of the cca requests/replies */
33 #define VARDATASIZE 4096
36 * debug feature data and functions
39 static debug_info_t *debug_info;
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__)
46 static void __init pkey_debug_init(void)
48 /* 5 arguments per dbf entry (including the format string ptr) */
49 debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
50 debug_register_view(debug_info, &debug_sprintf_view);
51 debug_set_level(debug_info, 3);
54 static void __exit pkey_debug_exit(void)
56 debug_unregister(debug_info);
59 /* inside view of a secure key token (only type 0x01 version 0x04) */
60 struct secaeskeytoken {
61 u8 type; /* 0x01 for internal key token */
63 u8 version; /* should be 0x04 */
65 u8 flag; /* key flags */
67 u64 mkvp; /* master key verification pattern */
68 u8 key[32]; /* key value (encrypted) */
69 u8 cv[8]; /* control vector */
70 u16 bitsize; /* key bit size */
71 u16 keysize; /* key byte size */
72 u8 tvv[4]; /* token validation value */
76 * Simple check if the token is a valid CCA secure AES key
77 * token. If keybitsize is given, the bitsize of the key is
78 * also checked. Returns 0 on success or errno value on failure.
80 static int check_secaeskeytoken(const u8 *token, int keybitsize)
82 struct secaeskeytoken *t = (struct secaeskeytoken *) token;
84 if (t->type != 0x01) {
86 "%s secure token check failed, type mismatch 0x%02x != 0x01\n",
87 __func__, (int) t->type);
90 if (t->version != 0x04) {
92 "%s secure token check failed, version mismatch 0x%02x != 0x04\n",
93 __func__, (int) t->version);
96 if (keybitsize > 0 && t->bitsize != keybitsize) {
98 "%s secure token check failed, bitsize mismatch %d != %d\n",
99 __func__, (int) t->bitsize, keybitsize);
107 * Allocate consecutive memory for request CPRB, request param
108 * block, reply CPRB and reply param block and fill in values
109 * for the common fields. Returns 0 on success or errno value
112 static int alloc_and_prep_cprbmem(size_t paramblen,
114 struct CPRBX **preqCPRB,
115 struct CPRBX **prepCPRB)
118 size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen;
119 struct CPRBX *preqcblk, *prepcblk;
122 * allocate consecutive memory for request CPRB, request param
123 * block, reply CPRB and reply param block
125 cprbmem = kcalloc(2, cprbplusparamblen, GFP_KERNEL);
129 preqcblk = (struct CPRBX *) cprbmem;
130 prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen);
132 /* fill request cprb struct */
133 preqcblk->cprb_len = sizeof(struct CPRBX);
134 preqcblk->cprb_ver_id = 0x02;
135 memcpy(preqcblk->func_id, "T2", 2);
136 preqcblk->rpl_msgbl = cprbplusparamblen;
138 preqcblk->req_parmb =
139 ((u8 *) preqcblk) + sizeof(struct CPRBX);
140 preqcblk->rpl_parmb =
141 ((u8 *) prepcblk) + sizeof(struct CPRBX);
145 *preqCPRB = preqcblk;
146 *prepCPRB = prepcblk;
152 * Free the cprb memory allocated with the function above.
153 * If the scrub value is not zero, the memory is filled
154 * with zeros before freeing (useful if there was some
155 * clear key material in there).
157 static void free_cprbmem(void *mem, size_t paramblen, int scrub)
160 memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen));
165 * Helper function to prepare the xcrb struct
167 static inline void prep_xcrb(struct ica_xcRB *pxcrb,
169 struct CPRBX *preqcblk,
170 struct CPRBX *prepcblk)
172 memset(pxcrb, 0, sizeof(*pxcrb));
173 pxcrb->agent_ID = 0x4341; /* 'CA' */
174 pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr);
175 pxcrb->request_control_blk_length =
176 preqcblk->cprb_len + preqcblk->req_parml;
177 pxcrb->request_control_blk_addr = (void __user *) preqcblk;
178 pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl;
179 pxcrb->reply_control_blk_addr = (void __user *) prepcblk;
183 * Helper function which calls zcrypt_send_cprb with
184 * memory management segment adjusted to kernel space
185 * so that the copy_from_user called within this
186 * function do in fact copy from kernel space.
188 static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb)
191 mm_segment_t old_fs = get_fs();
194 rc = zcrypt_send_cprb(xcrb);
201 * Generate (random) AES secure key.
203 int pkey_genseckey(u16 cardnr, u16 domain,
204 u32 keytype, struct pkey_seckey *seckey)
209 struct CPRBX *preqcblk, *prepcblk;
210 struct ica_xcRB xcrb;
226 u8 data[SECKEYBLOBSIZE];
240 /* ... some more data ... */
245 /* get already prepared memory for 2 cprbs with param block each */
246 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
250 /* fill request cprb struct */
251 preqcblk->domain = domain;
253 /* fill request cprb param block with KG request */
254 preqparm = (struct kgreqparm *) preqcblk->req_parmb;
255 memcpy(preqparm->subfunc_code, "KG", 2);
256 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
257 preqparm->lv1.len = sizeof(struct lv1);
258 memcpy(preqparm->lv1.key_form, "OP ", 8);
260 case PKEY_KEYTYPE_AES_128:
262 memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8);
264 case PKEY_KEYTYPE_AES_192:
266 memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8);
268 case PKEY_KEYTYPE_AES_256:
270 memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8);
274 "%s unknown/unsupported keytype %d\n",
279 memcpy(preqparm->lv1.key_type1, "AESDATA ", 8);
280 preqparm->lv2.len = sizeof(struct lv2);
281 for (i = 0; i < 6; i++) {
282 preqparm->lv2.keyid[i].len = sizeof(struct keyid);
283 preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10);
285 preqcblk->req_parml = sizeof(struct kgreqparm);
287 /* fill xcrb struct */
288 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
290 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
291 rc = _zcrypt_send_cprb(&xcrb);
294 "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
295 __func__, (int) cardnr, (int) domain, rc);
299 /* check response returncode and reasoncode */
300 if (prepcblk->ccp_rtcode != 0) {
302 "%s secure key generate failure, card response %d/%d\n",
304 (int) prepcblk->ccp_rtcode,
305 (int) prepcblk->ccp_rscode);
310 /* process response cprb param block */
311 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
312 prepparm = (struct kgrepparm *) prepcblk->rpl_parmb;
314 /* check length of the returned secure key token */
315 seckeysize = prepparm->lv3.keyblock.toklen
316 - sizeof(prepparm->lv3.keyblock.toklen)
317 - sizeof(prepparm->lv3.keyblock.tokattr);
318 if (seckeysize != SECKEYBLOBSIZE) {
320 "%s secure token size mismatch %d != %d bytes\n",
321 __func__, seckeysize, SECKEYBLOBSIZE);
326 /* check secure key token */
327 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
333 /* copy the generated secure key token */
334 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
337 free_cprbmem(mem, PARMBSIZE, 0);
340 EXPORT_SYMBOL(pkey_genseckey);
343 * Generate an AES secure key with given key value.
345 int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype,
346 const struct pkey_clrkey *clrkey,
347 struct pkey_seckey *seckey)
349 int rc, keysize, seckeysize;
351 struct CPRBX *preqcblk, *prepcblk;
352 struct ica_xcRB xcrb;
366 u8 data[SECKEYBLOBSIZE];
381 /* ... some more data ... */
386 /* get already prepared memory for 2 cprbs with param block each */
387 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
391 /* fill request cprb struct */
392 preqcblk->domain = domain;
394 /* fill request cprb param block with CM request */
395 preqparm = (struct cmreqparm *) preqcblk->req_parmb;
396 memcpy(preqparm->subfunc_code, "CM", 2);
397 memcpy(preqparm->rule_array, "AES ", 8);
398 preqparm->rule_array_len =
399 sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
401 case PKEY_KEYTYPE_AES_128:
404 case PKEY_KEYTYPE_AES_192:
407 case PKEY_KEYTYPE_AES_256:
412 "%s unknown/unsupported keytype %d\n",
417 preqparm->lv1.len = sizeof(struct lv1) + keysize;
418 memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize);
419 plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize);
420 plv2->len = sizeof(struct lv2);
421 plv2->keyid.len = sizeof(struct keyid);
422 plv2->keyid.attr = 0x30;
423 preqcblk->req_parml = sizeof(struct cmreqparm) + keysize;
425 /* fill xcrb struct */
426 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
428 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
429 rc = _zcrypt_send_cprb(&xcrb);
432 "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
433 __func__, (int) cardnr, (int) domain, rc);
437 /* check response returncode and reasoncode */
438 if (prepcblk->ccp_rtcode != 0) {
440 "%s clear key import failure, card response %d/%d\n",
442 (int) prepcblk->ccp_rtcode,
443 (int) prepcblk->ccp_rscode);
448 /* process response cprb param block */
449 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
450 prepparm = (struct cmrepparm *) prepcblk->rpl_parmb;
452 /* check length of the returned secure key token */
453 seckeysize = prepparm->lv3.keyblock.toklen
454 - sizeof(prepparm->lv3.keyblock.toklen)
455 - sizeof(prepparm->lv3.keyblock.tokattr);
456 if (seckeysize != SECKEYBLOBSIZE) {
458 "%s secure token size mismatch %d != %d bytes\n",
459 __func__, seckeysize, SECKEYBLOBSIZE);
464 /* check secure key token */
465 rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
471 /* copy the generated secure key token */
472 memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
475 free_cprbmem(mem, PARMBSIZE, 1);
478 EXPORT_SYMBOL(pkey_clr2seckey);
481 * Derive a proteced key from the secure key blob.
483 int pkey_sec2protkey(u16 cardnr, u16 domain,
484 const struct pkey_seckey *seckey,
485 struct pkey_protkey *protkey)
489 struct CPRBX *preqcblk, *prepcblk;
490 struct ica_xcRB xcrb;
503 u8 token[0]; /* cca secure key token */
513 struct cpacfkeyblock {
514 u8 version; /* version of this struct */
520 u8 key[64]; /* the key (keylen bytes) */
525 u8 vp[32]; /* verification pattern */
530 /* get already prepared memory for 2 cprbs with param block each */
531 rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
535 /* fill request cprb struct */
536 preqcblk->domain = domain;
538 /* fill request cprb param block with USK request */
539 preqparm = (struct uskreqparm *) preqcblk->req_parmb;
540 memcpy(preqparm->subfunc_code, "US", 2);
541 preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
542 preqparm->lv1.len = sizeof(struct lv1);
543 preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len);
544 preqparm->lv1.attr_flags = 0x0001;
545 preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE;
546 preqparm->lv2.attr_len = sizeof(struct lv2)
547 - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE;
548 preqparm->lv2.attr_flags = 0x0000;
549 memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE);
550 preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE;
552 /* fill xcrb struct */
553 prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
555 /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
556 rc = _zcrypt_send_cprb(&xcrb);
559 "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
560 __func__, (int) cardnr, (int) domain, rc);
564 /* check response returncode and reasoncode */
565 if (prepcblk->ccp_rtcode != 0) {
567 "%s unwrap secure key failure, card response %d/%d\n",
569 (int) prepcblk->ccp_rtcode,
570 (int) prepcblk->ccp_rscode);
574 if (prepcblk->ccp_rscode != 0) {
576 "%s 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 "%s reply param keyblock version mismatch 0x%02x != 0x01\n",
590 __func__, (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("%s unknown/unsupported keytype %d\n",
608 __func__, 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("%s 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 memcpy(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 "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
722 __func__, (int) cardnr, (int) domain, rc);
726 /* check response returncode and reasoncode */
727 if (prepcblk->ccp_rtcode != 0) {
729 "%s unwrap secure key failure, card response %d/%d\n",
731 (int) prepcblk->ccp_rtcode,
732 (int) prepcblk->ccp_rscode);
737 /* process response cprb param block */
738 prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
739 prepparm = (struct fqrepparm *) prepcblk->rpl_parmb;
740 ptr = prepparm->lvdata;
742 /* check and possibly copy reply rule array */
743 len = *((u16 *) ptr);
744 if (len > sizeof(u16)) {
747 if (rarray && rarraylen && *rarraylen > 0) {
748 *rarraylen = (len > *rarraylen ? *rarraylen : len);
749 memcpy(rarray, ptr, *rarraylen);
753 /* check and possible copy reply var array */
754 len = *((u16 *) ptr);
755 if (len > sizeof(u16)) {
758 if (varray && varraylen && *varraylen > 0) {
759 *varraylen = (len > *varraylen ? *varraylen : len);
760 memcpy(varray, ptr, *varraylen);
766 free_cprbmem(mem, parmbsize, 0);
771 * Fetch the current and old mkvp values via
772 * query_crypto_facility from adapter.
774 static int fetch_mkvp(u16 cardnr, u16 domain, u64 mkvp[2])
778 u8 *rarray, *varray, *pg;
780 pg = (u8 *) __get_free_page(GFP_KERNEL);
784 varray = pg + PAGE_SIZE/2;
785 rlen = vlen = PAGE_SIZE/2;
787 rc = query_crypto_facility(cardnr, domain, "STATICSA",
788 rarray, &rlen, varray, &vlen);
789 if (rc == 0 && rlen > 8*8 && vlen > 184+8) {
790 if (rarray[8*8] == '2') {
791 /* current master key state is valid */
792 mkvp[0] = *((u64 *)(varray + 184));
793 mkvp[1] = *((u64 *)(varray + 172));
798 free_page((unsigned long) pg);
800 return found ? 0 : -ENOENT;
803 /* struct to hold cached mkvp info for each card/domain */
805 struct list_head list;
811 /* a list with mkvp_info entries */
812 static LIST_HEAD(mkvp_list);
813 static DEFINE_SPINLOCK(mkvp_list_lock);
815 static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 mkvp[2])
818 struct mkvp_info *ptr;
820 spin_lock_bh(&mkvp_list_lock);
821 list_for_each_entry(ptr, &mkvp_list, list) {
822 if (ptr->cardnr == cardnr &&
823 ptr->domain == domain) {
824 memcpy(mkvp, ptr->mkvp, 2 * sizeof(u64));
829 spin_unlock_bh(&mkvp_list_lock);
834 static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp[2])
837 struct mkvp_info *ptr;
839 spin_lock_bh(&mkvp_list_lock);
840 list_for_each_entry(ptr, &mkvp_list, list) {
841 if (ptr->cardnr == cardnr &&
842 ptr->domain == domain) {
843 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
849 ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
851 spin_unlock_bh(&mkvp_list_lock);
854 ptr->cardnr = cardnr;
855 ptr->domain = domain;
856 memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
857 list_add(&ptr->list, &mkvp_list);
859 spin_unlock_bh(&mkvp_list_lock);
862 static void mkvp_cache_scrub(u16 cardnr, u16 domain)
864 struct mkvp_info *ptr;
866 spin_lock_bh(&mkvp_list_lock);
867 list_for_each_entry(ptr, &mkvp_list, list) {
868 if (ptr->cardnr == cardnr &&
869 ptr->domain == domain) {
870 list_del(&ptr->list);
875 spin_unlock_bh(&mkvp_list_lock);
878 static void __exit mkvp_cache_free(void)
880 struct mkvp_info *ptr, *pnext;
882 spin_lock_bh(&mkvp_list_lock);
883 list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) {
884 list_del(&ptr->list);
887 spin_unlock_bh(&mkvp_list_lock);
891 * Search for a matching crypto card based on the Master Key
892 * Verification Pattern provided inside a secure key.
894 int pkey_findcard(const struct pkey_seckey *seckey,
895 u16 *pcardnr, u16 *pdomain, int verify)
897 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
898 struct zcrypt_device_status_ext *device_status;
903 /* mkvp must not be zero */
907 /* fetch status of all crypto cards */
908 device_status = kmalloc_array(MAX_ZDEV_ENTRIES_EXT,
909 sizeof(struct zcrypt_device_status_ext),
913 zcrypt_device_status_mask_ext(device_status);
915 /* walk through all crypto cards */
916 for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
917 card = AP_QID_CARD(device_status[i].qid);
918 dom = AP_QID_QUEUE(device_status[i].qid);
919 if (device_status[i].online &&
920 device_status[i].functions & 0x04) {
921 /* an enabled CCA Coprocessor card */
922 /* try cached mkvp */
923 if (mkvp_cache_fetch(card, dom, mkvp) == 0 &&
924 t->mkvp == mkvp[0]) {
927 /* verify: fetch mkvp from adapter */
928 if (fetch_mkvp(card, dom, mkvp) == 0) {
929 mkvp_cache_update(card, dom, mkvp);
930 if (t->mkvp == mkvp[0])
935 /* Card is offline and/or not a CCA card. */
936 /* del mkvp entry from cache if it exists */
937 mkvp_cache_scrub(card, dom);
940 if (i >= MAX_ZDEV_ENTRIES_EXT) {
941 /* nothing found, so this time without cache */
942 for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
943 if (!(device_status[i].online &&
944 device_status[i].functions & 0x04))
946 card = AP_QID_CARD(device_status[i].qid);
947 dom = AP_QID_QUEUE(device_status[i].qid);
948 /* fresh fetch mkvp from adapter */
949 if (fetch_mkvp(card, dom, mkvp) == 0) {
950 mkvp_cache_update(card, dom, mkvp);
951 if (t->mkvp == mkvp[0])
953 if (t->mkvp == mkvp[1] && oi < 0)
957 if (i >= MAX_ZDEV_ENTRIES_EXT && oi >= 0) {
958 /* old mkvp matched, use this card then */
959 card = AP_QID_CARD(device_status[oi].qid);
960 dom = AP_QID_QUEUE(device_status[oi].qid);
963 if (i < MAX_ZDEV_ENTRIES_EXT || oi >= 0) {
972 kfree(device_status);
975 EXPORT_SYMBOL(pkey_findcard);
978 * Find card and transform secure key into protected key.
980 int pkey_skey2pkey(const struct pkey_seckey *seckey,
981 struct pkey_protkey *protkey)
987 * The pkey_sec2protkey call may fail when a card has been
988 * addressed where the master key was changed after last fetch
989 * of the mkvp into the cache. So first try without verify then
990 * with verify enabled (thus refreshing the mkvp for each card).
992 for (verify = 0; verify < 2; verify++) {
993 rc = pkey_findcard(seckey, &cardnr, &domain, verify);
996 rc = pkey_sec2protkey(cardnr, domain, seckey, protkey);
1002 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
1006 EXPORT_SYMBOL(pkey_skey2pkey);
1009 * Verify key and give back some info about the key.
1011 int pkey_verifykey(const struct pkey_seckey *seckey,
1012 u16 *pcardnr, u16 *pdomain,
1013 u16 *pkeysize, u32 *pattributes)
1015 struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
1020 /* check the secure key for valid AES secure key */
1021 rc = check_secaeskeytoken((u8 *) seckey, 0);
1025 *pattributes = PKEY_VERIFY_ATTR_AES;
1027 *pkeysize = t->bitsize;
1029 /* try to find a card which can handle this key */
1030 rc = pkey_findcard(seckey, &cardnr, &domain, 1);
1034 /* check mkvp for old mkvp match */
1035 rc = mkvp_cache_fetch(cardnr, domain, mkvp);
1038 if (t->mkvp == mkvp[1]) {
1039 DEBUG_DBG("%s secure key has old mkvp\n", __func__);
1041 *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
1050 DEBUG_DBG("%s rc=%d\n", __func__, rc);
1053 EXPORT_SYMBOL(pkey_verifykey);
1059 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1065 case PKEY_GENSECK: {
1066 struct pkey_genseck __user *ugs = (void __user *) arg;
1067 struct pkey_genseck kgs;
1069 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1071 rc = pkey_genseckey(kgs.cardnr, kgs.domain,
1072 kgs.keytype, &kgs.seckey);
1073 DEBUG_DBG("%s pkey_genseckey()=%d\n", __func__, rc);
1076 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1080 case PKEY_CLR2SECK: {
1081 struct pkey_clr2seck __user *ucs = (void __user *) arg;
1082 struct pkey_clr2seck kcs;
1084 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1086 rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1087 &kcs.clrkey, &kcs.seckey);
1088 DEBUG_DBG("%s pkey_clr2seckey()=%d\n", __func__, rc);
1091 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1093 memzero_explicit(&kcs, sizeof(kcs));
1096 case PKEY_SEC2PROTK: {
1097 struct pkey_sec2protk __user *usp = (void __user *) arg;
1098 struct pkey_sec2protk ksp;
1100 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1102 rc = pkey_sec2protkey(ksp.cardnr, ksp.domain,
1103 &ksp.seckey, &ksp.protkey);
1104 DEBUG_DBG("%s pkey_sec2protkey()=%d\n", __func__, rc);
1107 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1111 case PKEY_CLR2PROTK: {
1112 struct pkey_clr2protk __user *ucp = (void __user *) arg;
1113 struct pkey_clr2protk kcp;
1115 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1117 rc = pkey_clr2protkey(kcp.keytype,
1118 &kcp.clrkey, &kcp.protkey);
1119 DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1122 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1124 memzero_explicit(&kcp, sizeof(kcp));
1127 case PKEY_FINDCARD: {
1128 struct pkey_findcard __user *ufc = (void __user *) arg;
1129 struct pkey_findcard kfc;
1131 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1133 rc = pkey_findcard(&kfc.seckey,
1134 &kfc.cardnr, &kfc.domain, 1);
1135 DEBUG_DBG("%s pkey_findcard()=%d\n", __func__, rc);
1138 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1142 case PKEY_SKEY2PKEY: {
1143 struct pkey_skey2pkey __user *usp = (void __user *) arg;
1144 struct pkey_skey2pkey ksp;
1146 if (copy_from_user(&ksp, usp, sizeof(ksp)))
1148 rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
1149 DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1152 if (copy_to_user(usp, &ksp, sizeof(ksp)))
1156 case PKEY_VERIFYKEY: {
1157 struct pkey_verifykey __user *uvk = (void __user *) arg;
1158 struct pkey_verifykey kvk;
1160 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1162 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1163 &kvk.keysize, &kvk.attributes);
1164 DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1167 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1172 /* unknown/unsupported ioctl cmd */
1180 * Sysfs and file io operations
1182 static const struct file_operations pkey_fops = {
1183 .owner = THIS_MODULE,
1184 .open = nonseekable_open,
1185 .llseek = no_llseek,
1186 .unlocked_ioctl = pkey_unlocked_ioctl,
1189 static struct miscdevice pkey_dev = {
1191 .minor = MISC_DYNAMIC_MINOR,
1199 static int __init pkey_init(void)
1201 cpacf_mask_t pckmo_functions;
1203 /* check for pckmo instructions available */
1204 if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
1206 if (!cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_128_KEY) ||
1207 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_192_KEY) ||
1208 !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_256_KEY))
1213 return misc_register(&pkey_dev);
1219 static void __exit pkey_exit(void)
1221 misc_deregister(&pkey_dev);
1226 module_init(pkey_init);
1227 module_exit(pkey_exit);