1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright IBM Corp. 2024
5 * Pkey base: debug feature, defines and structs
6 * common to all pkey code.
12 #include <linux/types.h>
13 #include <asm/debug.h>
20 extern debug_info_t
*pkey_dbf_info
;
22 #define PKEY_DBF_INFO(...) debug_sprintf_event(pkey_dbf_info, 5, ##__VA_ARGS__)
23 #define PKEY_DBF_WARN(...) debug_sprintf_event(pkey_dbf_info, 4, ##__VA_ARGS__)
24 #define PKEY_DBF_ERR(...) debug_sprintf_event(pkey_dbf_info, 3, ##__VA_ARGS__)
27 * common defines and common structs
30 #define KEYBLOBBUFSIZE 8192 /* key buffer size used for internal processing */
31 #define MINKEYBLOBBUFSIZE (sizeof(struct keytoken_header))
32 #define PROTKEYBLOBBUFSIZE 256 /* protected key buffer size used internal */
33 #define MAXAPQNSINLIST 64 /* max 64 apqns within a apqn list */
34 #define AES_WK_VP_SIZE 32 /* Size of WK VP block appended to a prot key */
36 /* inside view of a generic protected key token */
38 u8 type
; /* 0x00 for PAES specific key tokens */
40 u8 version
; /* should be 0x01 for protected key token */
42 u32 keytype
; /* key type, one of the PKEY_KEYTYPE values */
43 u32 len
; /* bytes actually stored in protkey[] */
44 u8 protkey
[]; /* the protected key blob */
47 /* inside view of a protected AES key token */
48 struct protaeskeytoken
{
49 u8 type
; /* 0x00 for PAES specific key tokens */
51 u8 version
; /* should be 0x01 for protected key token */
53 u32 keytype
; /* key type, one of the PKEY_KEYTYPE values */
54 u32 len
; /* bytes actually stored in protkey[] */
55 u8 protkey
[MAXPROTKEYSIZE
]; /* the protected key blob */
58 /* inside view of a clear key token (type 0x00 version 0x02) */
59 struct clearkeytoken
{
60 u8 type
; /* 0x00 for PAES specific key tokens */
62 u8 version
; /* 0x02 for clear key token */
64 u32 keytype
; /* key type, one of the PKEY_KEYTYPE_* values */
65 u32 len
; /* bytes actually stored in clearkey[] */
66 u8 clearkey
[]; /* clear key value */
69 /* helper function which translates the PKEY_KEYTYPE_AES_* to their keysize */
70 static inline u32
pkey_keytype_aes_to_size(u32 keytype
)
73 case PKEY_KEYTYPE_AES_128
:
75 case PKEY_KEYTYPE_AES_192
:
77 case PKEY_KEYTYPE_AES_256
:
84 /* helper function which translates AES key bit size into PKEY_KEYTYPE_AES_* */
85 static inline u32
pkey_aes_bitsize_to_keytype(u32 keybitsize
)
89 return PKEY_KEYTYPE_AES_128
;
91 return PKEY_KEYTYPE_AES_192
;
93 return PKEY_KEYTYPE_AES_256
;
100 * helper function which translates the PKEY_KEYTYPE_*
101 * to the protected key size minus the WK VP length
103 static inline u32
pkey_keytype_to_size(u32 keytype
)
106 case PKEY_KEYTYPE_AES_128
:
108 case PKEY_KEYTYPE_AES_192
:
110 case PKEY_KEYTYPE_AES_256
:
112 case PKEY_KEYTYPE_ECC_P256
:
114 case PKEY_KEYTYPE_ECC_P384
:
116 case PKEY_KEYTYPE_ECC_P521
:
118 case PKEY_KEYTYPE_ECC_ED25519
:
120 case PKEY_KEYTYPE_ECC_ED448
:
122 case PKEY_KEYTYPE_AES_XTS_128
:
124 case PKEY_KEYTYPE_AES_XTS_256
:
126 case PKEY_KEYTYPE_HMAC_512
:
128 case PKEY_KEYTYPE_HMAC_1024
:
138 int __init
pkey_api_init(void);
139 void __exit
pkey_api_exit(void);
145 extern const struct attribute_group
*pkey_attr_groups
[];
148 * pkey handler registry
151 struct pkey_handler
{
152 struct module
*module
;
155 * is_supported_key() and is_supported_keytype() are called
156 * within an rcu_read_lock() scope and thus must not sleep!
158 bool (*is_supported_key
)(const u8
*key
, u32 keylen
);
159 bool (*is_supported_keytype
)(enum pkey_key_type
);
160 int (*key_to_protkey
)(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
161 const u8
*key
, u32 keylen
,
162 u8
*protkey
, u32
*protkeylen
, u32
*protkeytype
);
163 int (*slowpath_key_to_protkey
)(const struct pkey_apqn
*apqns
,
165 const u8
*key
, u32 keylen
,
166 u8
*protkey
, u32
*protkeylen
,
168 int (*gen_key
)(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
169 u32 keytype
, u32 keysubtype
,
170 u32 keybitsize
, u32 flags
,
171 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
);
172 int (*clr_to_key
)(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
173 u32 keytype
, u32 keysubtype
,
174 u32 keybitsize
, u32 flags
,
175 const u8
*clrkey
, u32 clrkeylen
,
176 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
);
177 int (*verify_key
)(const u8
*key
, u32 keylen
,
179 u32
*keytype
, u32
*keybitsize
, u32
*flags
);
180 int (*apqns_for_key
)(const u8
*key
, u32 keylen
, u32 flags
,
181 struct pkey_apqn
*apqns
, size_t *nr_apqns
);
182 int (*apqns_for_keytype
)(enum pkey_key_type ktype
,
183 u8 cur_mkvp
[32], u8 alt_mkvp
[32], u32 flags
,
184 struct pkey_apqn
*apqns
, size_t *nr_apqns
);
185 /* used internal by pkey base */
186 struct list_head list
;
189 int pkey_handler_register(struct pkey_handler
*handler
);
190 int pkey_handler_unregister(struct pkey_handler
*handler
);
193 * invocation function for the registered pkey handlers
196 const struct pkey_handler
*pkey_handler_get_keybased(const u8
*key
, u32 keylen
);
197 const struct pkey_handler
*pkey_handler_get_keytypebased(enum pkey_key_type kt
);
198 void pkey_handler_put(const struct pkey_handler
*handler
);
200 int pkey_handler_key_to_protkey(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
201 const u8
*key
, u32 keylen
,
202 u8
*protkey
, u32
*protkeylen
, u32
*protkeytype
);
203 int pkey_handler_slowpath_key_to_protkey(const struct pkey_apqn
*apqns
,
205 const u8
*key
, u32 keylen
,
206 u8
*protkey
, u32
*protkeylen
,
208 int pkey_handler_gen_key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
209 u32 keytype
, u32 keysubtype
,
210 u32 keybitsize
, u32 flags
,
211 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
);
212 int pkey_handler_clr_to_key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
213 u32 keytype
, u32 keysubtype
,
214 u32 keybitsize
, u32 flags
,
215 const u8
*clrkey
, u32 clrkeylen
,
216 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
);
217 int pkey_handler_verify_key(const u8
*key
, u32 keylen
,
219 u32
*keytype
, u32
*keybitsize
, u32
*flags
);
220 int pkey_handler_apqns_for_key(const u8
*key
, u32 keylen
, u32 flags
,
221 struct pkey_apqn
*apqns
, size_t *nr_apqns
);
222 int pkey_handler_apqns_for_keytype(enum pkey_key_type ktype
,
223 u8 cur_mkvp
[32], u8 alt_mkvp
[32], u32 flags
,
224 struct pkey_apqn
*apqns
, size_t *nr_apqns
);
227 * Unconditional try to load all handler modules
229 void pkey_handler_request_modules(void);
231 #endif /* _PKEY_BASE_H_ */