xref: /OK3568_Linux_fs/kernel/drivers/s390/crypto/zcrypt_ep11misc.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+ */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Copyright IBM Corp. 2019
4*4882a593Smuzhiyun  *  Author(s): Harald Freudenberger <freude@linux.ibm.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Collection of EP11 misc functions used by zcrypt and pkey
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _ZCRYPT_EP11MISC_H_
10*4882a593Smuzhiyun #define _ZCRYPT_EP11MISC_H_
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <asm/zcrypt.h>
13*4882a593Smuzhiyun #include <asm/pkey.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define EP11_API_V 4  /* highest known and supported EP11 API version */
16*4882a593Smuzhiyun #define EP11_STRUCT_MAGIC 0x1234
17*4882a593Smuzhiyun #define EP11_BLOB_PKEY_EXTRACTABLE 0x00200000
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Internal used values for the version field of the key header.
21*4882a593Smuzhiyun  * Should match to the enum pkey_key_type in pkey.h.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun #define TOKVER_EP11_AES  0x03  /* EP11 AES key blob (old style) */
24*4882a593Smuzhiyun #define TOKVER_EP11_AES_WITH_HEADER 0x06 /* EP11 AES key blob with header */
25*4882a593Smuzhiyun #define TOKVER_EP11_ECC_WITH_HEADER 0x07 /* EP11 ECC key blob with header */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* inside view of an EP11 secure key blob */
28*4882a593Smuzhiyun struct ep11keyblob {
29*4882a593Smuzhiyun 	union {
30*4882a593Smuzhiyun 		u8 session[32];
31*4882a593Smuzhiyun 		/* only used for PKEY_TYPE_EP11: */
32*4882a593Smuzhiyun 		struct {
33*4882a593Smuzhiyun 			u8  type;      /* 0x00 (TOKTYPE_NON_CCA) */
34*4882a593Smuzhiyun 			u8  res0;      /* unused */
35*4882a593Smuzhiyun 			u16 len;       /* total length in bytes of this blob */
36*4882a593Smuzhiyun 			u8  version;   /* 0x03 (TOKVER_EP11_AES) */
37*4882a593Smuzhiyun 			u8  res1;      /* unused */
38*4882a593Smuzhiyun 			u16 keybitlen; /* clear key bit len, 0 for unknown */
39*4882a593Smuzhiyun 		} head;
40*4882a593Smuzhiyun 	};
41*4882a593Smuzhiyun 	u8  wkvp[16];  /* wrapping key verification pattern */
42*4882a593Smuzhiyun 	u64 attr;      /* boolean key attributes */
43*4882a593Smuzhiyun 	u64 mode;      /* mode bits */
44*4882a593Smuzhiyun 	u16 version;   /* 0x1234, EP11_STRUCT_MAGIC */
45*4882a593Smuzhiyun 	u8  iv[14];
46*4882a593Smuzhiyun 	u8  encrypted_key_data[144];
47*4882a593Smuzhiyun 	u8  mac[32];
48*4882a593Smuzhiyun } __packed;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* check ep11 key magic to find out if this is an ep11 key blob */
is_ep11_keyblob(const u8 * key)51*4882a593Smuzhiyun static inline bool is_ep11_keyblob(const u8 *key)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct ep11keyblob *kb = (struct ep11keyblob *) key;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	return (kb->version == EP11_STRUCT_MAGIC);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * Simple check if the key blob is a valid EP11 AES key blob with header.
60*4882a593Smuzhiyun  * If checkcpacfexport is enabled, the key is also checked for the
61*4882a593Smuzhiyun  * attributes needed to export this key for CPACF use.
62*4882a593Smuzhiyun  * Returns 0 on success or errno value on failure.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun int ep11_check_aes_key_with_hdr(debug_info_t *dbg, int dbflvl,
65*4882a593Smuzhiyun 				const u8 *key, size_t keylen, int checkcpacfexp);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun  * Simple check if the key blob is a valid EP11 ECC key blob with header.
69*4882a593Smuzhiyun  * If checkcpacfexport is enabled, the key is also checked for the
70*4882a593Smuzhiyun  * attributes needed to export this key for CPACF use.
71*4882a593Smuzhiyun  * Returns 0 on success or errno value on failure.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun int ep11_check_ecc_key_with_hdr(debug_info_t *dbg, int dbflvl,
74*4882a593Smuzhiyun 				const u8 *key, size_t keylen, int checkcpacfexp);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * Simple check if the key blob is a valid EP11 AES key blob with
78*4882a593Smuzhiyun  * the header in the session field (old style EP11 AES key).
79*4882a593Smuzhiyun  * If checkcpacfexport is enabled, the key is also checked for the
80*4882a593Smuzhiyun  * attributes needed to export this key for CPACF use.
81*4882a593Smuzhiyun  * Returns 0 on success or errno value on failure.
82*4882a593Smuzhiyun  */
83*4882a593Smuzhiyun int ep11_check_aes_key(debug_info_t *dbg, int dbflvl,
84*4882a593Smuzhiyun 		       const u8 *key, size_t keylen, int checkcpacfexp);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* EP11 card info struct */
87*4882a593Smuzhiyun struct ep11_card_info {
88*4882a593Smuzhiyun 	u32  API_ord_nr;    /* API ordinal number */
89*4882a593Smuzhiyun 	u16  FW_version;    /* Firmware major and minor version */
90*4882a593Smuzhiyun 	char serial[16];    /* serial number string (16 ascii, no 0x00 !) */
91*4882a593Smuzhiyun 	u64  op_mode;	    /* card operational mode(s) */
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* EP11 domain info struct */
95*4882a593Smuzhiyun struct ep11_domain_info {
96*4882a593Smuzhiyun 	char cur_wk_state;  /* '0' invalid, '1' valid */
97*4882a593Smuzhiyun 	char new_wk_state;  /* '0' empty, '1' uncommitted, '2' committed */
98*4882a593Smuzhiyun 	u8   cur_wkvp[32];  /* current wrapping key verification pattern */
99*4882a593Smuzhiyun 	u8   new_wkvp[32];  /* new wrapping key verification pattern */
100*4882a593Smuzhiyun 	u64  op_mode;	    /* domain operational mode(s) */
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun  * Provide information about an EP11 card.
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * Provide information about a domain within an EP11 card.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun  * Generate (random) EP11 AES secure key.
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
117*4882a593Smuzhiyun 		   u8 *keybuf, size_t *keybufsize);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  * Generate EP11 AES secure key with given clear key value.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun int ep11_clr2keyblob(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags,
123*4882a593Smuzhiyun 		     const u8 *clrkey, u8 *keybuf, size_t *keybufsize);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Build a list of ep11 apqns meeting the following constrains:
127*4882a593Smuzhiyun  * - apqn is online and is in fact an EP11 apqn
128*4882a593Smuzhiyun  * - if cardnr is not FFFF only apqns with this cardnr
129*4882a593Smuzhiyun  * - if domain is not FFFF only apqns with this domainnr
130*4882a593Smuzhiyun  * - if minhwtype > 0 only apqns with hwtype >= minhwtype
131*4882a593Smuzhiyun  * - if minapi > 0 only apqns with API_ord_nr >= minapi
132*4882a593Smuzhiyun  * - if wkvp != NULL only apqns where the wkvp (EP11_WKVPLEN bytes) matches
133*4882a593Smuzhiyun  *   to the first EP11_WKVPLEN bytes of the wkvp of the current wrapping
134*4882a593Smuzhiyun  *   key for this domain. When a wkvp is given there will aways be a re-fetch
135*4882a593Smuzhiyun  *   of the domain info for the potential apqn - so this triggers an request
136*4882a593Smuzhiyun  *   reply to each apqn eligible.
137*4882a593Smuzhiyun  * The array of apqn entries is allocated with kmalloc and returned in *apqns;
138*4882a593Smuzhiyun  * the number of apqns stored into the list is returned in *nr_apqns. One apqn
139*4882a593Smuzhiyun  * entry is simple a 32 bit value with 16 bit cardnr and 16 bit domain nr and
140*4882a593Smuzhiyun  * may be casted to struct pkey_apqn. The return value is either 0 for success
141*4882a593Smuzhiyun  * or a negative errno value. If no apqn meeting the criterias is found,
142*4882a593Smuzhiyun  * -ENODEV is returned.
143*4882a593Smuzhiyun  */
144*4882a593Smuzhiyun int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
145*4882a593Smuzhiyun 		   int minhwtype, int minapi, const u8 *wkvp);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun  * Derive proteced key from EP11 key blob (AES and ECC keys).
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun int ep11_kblob2protkey(u16 card, u16 dom, const u8 *key, size_t keylen,
151*4882a593Smuzhiyun 		       u8 *protkey, u32 *protkeylen, u32 *protkeytype);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun void zcrypt_ep11misc_exit(void);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun #endif /* _ZCRYPT_EP11MISC_H_ */
156