1 // SPDX-License-Identifier: GPL-2.0
3 * pkey ep11 specific code
5 * Copyright IBM Corp. 2024
8 #define KMSG_COMPONENT "pkey"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/cpufeature.h>
15 #include "zcrypt_ccamisc.h"
16 #include "zcrypt_ep11misc.h"
17 #include "pkey_base.h"
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("IBM Corporation");
21 MODULE_DESCRIPTION("s390 protected key EP11 handler");
23 #if IS_MODULE(CONFIG_PKEY_EP11)
24 static struct ap_device_id pkey_ep11_card_ids
[] = {
25 { .dev_type
= AP_DEVICE_TYPE_CEX4
},
26 { .dev_type
= AP_DEVICE_TYPE_CEX5
},
27 { .dev_type
= AP_DEVICE_TYPE_CEX6
},
28 { .dev_type
= AP_DEVICE_TYPE_CEX7
},
29 { .dev_type
= AP_DEVICE_TYPE_CEX8
},
30 { /* end of list */ },
32 MODULE_DEVICE_TABLE(ap
, pkey_ep11_card_ids
);
36 * Check key blob for known and supported EP11 key.
38 static bool is_ep11_key(const u8
*key
, u32 keylen
)
40 struct keytoken_header
*hdr
= (struct keytoken_header
*)key
;
42 if (keylen
< sizeof(*hdr
))
47 switch (hdr
->version
) {
49 case TOKVER_EP11_AES_WITH_HEADER
:
50 case TOKVER_EP11_ECC_WITH_HEADER
:
60 static bool is_ep11_keytype(enum pkey_key_type key_type
)
64 case PKEY_TYPE_EP11_AES
:
65 case PKEY_TYPE_EP11_ECC
:
72 static int ep11_apqns4key(const u8
*key
, u32 keylen
, u32 flags
,
73 struct pkey_apqn
*apqns
, size_t *nr_apqns
)
75 struct keytoken_header
*hdr
= (struct keytoken_header
*)key
;
76 u32 _nr_apqns
, *_apqns
= NULL
;
80 flags
= PKEY_FLAGS_MATCH_CUR_MKVP
;
82 if (keylen
< sizeof(struct keytoken_header
) || flags
== 0)
85 zcrypt_wait_api_operational();
87 if (hdr
->type
== TOKTYPE_NON_CCA
&&
88 (hdr
->version
== TOKVER_EP11_AES_WITH_HEADER
||
89 hdr
->version
== TOKVER_EP11_ECC_WITH_HEADER
) &&
90 is_ep11_keyblob(key
+ sizeof(struct ep11kblob_header
))) {
91 struct ep11keyblob
*kb
= (struct ep11keyblob
*)
92 (key
+ sizeof(struct ep11kblob_header
));
93 int minhwtype
= 0, api
= 0;
95 if (flags
!= PKEY_FLAGS_MATCH_CUR_MKVP
)
97 if (kb
->attr
& EP11_BLOB_PKEY_EXTRACTABLE
) {
98 minhwtype
= ZCRYPT_CEX7
;
99 api
= ap_is_se_guest() ? EP11_API_V6
: EP11_API_V4
;
101 rc
= ep11_findcard2(&_apqns
, &_nr_apqns
, 0xFFFF, 0xFFFF,
102 minhwtype
, api
, kb
->wkvp
);
106 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
107 hdr
->version
== TOKVER_EP11_AES
&&
108 is_ep11_keyblob(key
)) {
109 struct ep11keyblob
*kb
= (struct ep11keyblob
*)key
;
110 int minhwtype
= 0, api
= 0;
112 if (flags
!= PKEY_FLAGS_MATCH_CUR_MKVP
)
114 if (kb
->attr
& EP11_BLOB_PKEY_EXTRACTABLE
) {
115 minhwtype
= ZCRYPT_CEX7
;
116 api
= ap_is_se_guest() ? EP11_API_V6
: EP11_API_V4
;
118 rc
= ep11_findcard2(&_apqns
, &_nr_apqns
, 0xFFFF, 0xFFFF,
119 minhwtype
, api
, kb
->wkvp
);
124 PKEY_DBF_ERR("%s unknown/unsupported blob type %d version %d\n",
125 __func__
, hdr
->type
, hdr
->version
);
130 if (*nr_apqns
< _nr_apqns
)
133 memcpy(apqns
, _apqns
, _nr_apqns
* sizeof(u32
));
135 *nr_apqns
= _nr_apqns
;
139 pr_debug("rc=%d\n", rc
);
143 static int ep11_apqns4type(enum pkey_key_type ktype
,
144 u8 cur_mkvp
[32], u8 alt_mkvp
[32], u32 flags
,
145 struct pkey_apqn
*apqns
, size_t *nr_apqns
)
147 u32 _nr_apqns
, *_apqns
= NULL
;
150 zcrypt_wait_api_operational();
152 if (ktype
== PKEY_TYPE_EP11
||
153 ktype
== PKEY_TYPE_EP11_AES
||
154 ktype
== PKEY_TYPE_EP11_ECC
) {
158 if (flags
& PKEY_FLAGS_MATCH_CUR_MKVP
)
160 api
= ap_is_se_guest() ? EP11_API_V6
: EP11_API_V4
;
161 rc
= ep11_findcard2(&_apqns
, &_nr_apqns
, 0xFFFF, 0xFFFF,
162 ZCRYPT_CEX7
, api
, wkvp
);
167 PKEY_DBF_ERR("%s unknown/unsupported key type %d\n",
168 __func__
, (int)ktype
);
173 if (*nr_apqns
< _nr_apqns
)
176 memcpy(apqns
, _apqns
, _nr_apqns
* sizeof(u32
));
178 *nr_apqns
= _nr_apqns
;
182 pr_debug("rc=%d\n", rc
);
186 static int ep11_key2protkey(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
187 const u8
*key
, u32 keylen
,
188 u8
*protkey
, u32
*protkeylen
, u32
*protkeytype
)
190 struct keytoken_header
*hdr
= (struct keytoken_header
*)key
;
191 struct pkey_apqn
*local_apqns
= NULL
;
194 if (keylen
< sizeof(*hdr
))
197 if (hdr
->type
== TOKTYPE_NON_CCA
&&
198 hdr
->version
== TOKVER_EP11_AES_WITH_HEADER
&&
199 is_ep11_keyblob(key
+ sizeof(struct ep11kblob_header
))) {
200 /* EP11 AES key blob with header */
201 if (ep11_check_aes_key_with_hdr(pkey_dbf_info
,
204 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
205 hdr
->version
== TOKVER_EP11_ECC_WITH_HEADER
&&
206 is_ep11_keyblob(key
+ sizeof(struct ep11kblob_header
))) {
207 /* EP11 ECC key blob with header */
208 if (ep11_check_ecc_key_with_hdr(pkey_dbf_info
,
211 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
212 hdr
->version
== TOKVER_EP11_AES
&&
213 is_ep11_keyblob(key
)) {
214 /* EP11 AES key blob with header in session field */
215 if (ep11_check_aes_key(pkey_dbf_info
, 3, key
, keylen
, 1))
218 PKEY_DBF_ERR("%s unknown/unsupported blob type %d version %d\n",
219 __func__
, hdr
->type
, hdr
->version
);
223 zcrypt_wait_api_operational();
225 if (!apqns
|| (nr_apqns
== 1 &&
226 apqns
[0].card
== 0xFFFF && apqns
[0].domain
== 0xFFFF)) {
227 nr_apqns
= MAXAPQNSINLIST
;
228 local_apqns
= kmalloc_array(nr_apqns
, sizeof(struct pkey_apqn
),
232 rc
= ep11_apqns4key(key
, keylen
, 0, local_apqns
, &nr_apqns
);
238 for (rc
= -ENODEV
, i
= 0; rc
&& i
< nr_apqns
; i
++) {
239 if (hdr
->type
== TOKTYPE_NON_CCA
&&
240 hdr
->version
== TOKVER_EP11_AES_WITH_HEADER
&&
241 is_ep11_keyblob(key
+ sizeof(struct ep11kblob_header
))) {
242 rc
= ep11_kblob2protkey(apqns
[i
].card
, apqns
[i
].domain
,
243 key
, hdr
->len
, protkey
,
244 protkeylen
, protkeytype
);
245 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
246 hdr
->version
== TOKVER_EP11_ECC_WITH_HEADER
&&
247 is_ep11_keyblob(key
+ sizeof(struct ep11kblob_header
))) {
248 rc
= ep11_kblob2protkey(apqns
[i
].card
, apqns
[i
].domain
,
249 key
, hdr
->len
, protkey
,
250 protkeylen
, protkeytype
);
251 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
252 hdr
->version
== TOKVER_EP11_AES
&&
253 is_ep11_keyblob(key
)) {
254 rc
= ep11_kblob2protkey(apqns
[i
].card
, apqns
[i
].domain
,
255 key
, hdr
->len
, protkey
,
256 protkeylen
, protkeytype
);
265 pr_debug("rc=%d\n", rc
);
270 * Generate EP11 secure key.
271 * As of now only EP11 AES secure keys are supported.
272 * keytype is one of the PKEY_KEYTYPE_* constants,
273 * subtype may be PKEY_TYPE_EP11 or PKEY_TYPE_EP11_AES
274 * or 0 (results in subtype PKEY_TYPE_EP11_AES),
275 * keybitsize is the bit size of the key (may be 0 for
276 * keytype PKEY_KEYTYPE_AES_*).
278 static int ep11_gen_key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
279 u32 keytype
, u32 subtype
,
280 u32 keybitsize
, u32 flags
,
281 u8
*keybuf
, u32
*keybuflen
, u32
*_keyinfo
)
283 struct pkey_apqn
*local_apqns
= NULL
;
286 /* check keytype, subtype, keybitsize */
288 case PKEY_KEYTYPE_AES_128
:
289 case PKEY_KEYTYPE_AES_192
:
290 case PKEY_KEYTYPE_AES_256
:
291 len
= pkey_keytype_aes_to_size(keytype
);
292 if (keybitsize
&& keybitsize
!= 8 * len
) {
293 PKEY_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
294 __func__
, keybitsize
);
297 keybitsize
= 8 * len
;
300 case PKEY_TYPE_EP11_AES
:
303 PKEY_DBF_ERR("%s unknown/unsupported subtype %d\n",
309 PKEY_DBF_ERR("%s unknown/unsupported keytype %d\n",
314 zcrypt_wait_api_operational();
316 if (!apqns
|| (nr_apqns
== 1 &&
317 apqns
[0].card
== 0xFFFF && apqns
[0].domain
== 0xFFFF)) {
318 nr_apqns
= MAXAPQNSINLIST
;
319 local_apqns
= kmalloc_array(nr_apqns
, sizeof(struct pkey_apqn
),
323 rc
= ep11_apqns4type(subtype
, NULL
, NULL
, 0,
324 local_apqns
, &nr_apqns
);
330 for (rc
= -ENODEV
, i
= 0; rc
&& i
< nr_apqns
; i
++) {
331 rc
= ep11_genaeskey(apqns
[i
].card
, apqns
[i
].domain
,
333 keybuf
, keybuflen
, subtype
);
338 pr_debug("rc=%d\n", rc
);
343 * Generate EP11 secure key with given clear key value.
344 * As of now only EP11 AES secure keys are supported.
345 * keytype is one of the PKEY_KEYTYPE_* constants,
346 * subtype may be PKEY_TYPE_EP11 or PKEY_TYPE_EP11_AES
347 * or 0 (assumes PKEY_TYPE_EP11_AES then).
348 * keybitsize is the bit size of the key (may be 0 for
349 * keytype PKEY_KEYTYPE_AES_*).
351 static int ep11_clr2key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
352 u32 keytype
, u32 subtype
,
353 u32 keybitsize
, u32 flags
,
354 const u8
*clrkey
, u32 clrkeylen
,
355 u8
*keybuf
, u32
*keybuflen
, u32
*_keyinfo
)
357 struct pkey_apqn
*local_apqns
= NULL
;
360 /* check keytype, subtype, clrkeylen, keybitsize */
362 case PKEY_KEYTYPE_AES_128
:
363 case PKEY_KEYTYPE_AES_192
:
364 case PKEY_KEYTYPE_AES_256
:
365 len
= pkey_keytype_aes_to_size(keytype
);
366 if (keybitsize
&& keybitsize
!= 8 * len
) {
367 PKEY_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
368 __func__
, keybitsize
);
371 keybitsize
= 8 * len
;
372 if (clrkeylen
!= len
) {
373 PKEY_DBF_ERR("%s invalid clear key len %d != %d\n",
374 __func__
, clrkeylen
, len
);
379 case PKEY_TYPE_EP11_AES
:
382 PKEY_DBF_ERR("%s unknown/unsupported subtype %d\n",
388 PKEY_DBF_ERR("%s unknown/unsupported keytype %d\n",
393 zcrypt_wait_api_operational();
395 if (!apqns
|| (nr_apqns
== 1 &&
396 apqns
[0].card
== 0xFFFF && apqns
[0].domain
== 0xFFFF)) {
397 nr_apqns
= MAXAPQNSINLIST
;
398 local_apqns
= kmalloc_array(nr_apqns
, sizeof(struct pkey_apqn
),
402 rc
= ep11_apqns4type(subtype
, NULL
, NULL
, 0,
403 local_apqns
, &nr_apqns
);
409 for (rc
= -ENODEV
, i
= 0; rc
&& i
< nr_apqns
; i
++) {
410 rc
= ep11_clr2keyblob(apqns
[i
].card
, apqns
[i
].domain
,
411 keybitsize
, flags
, clrkey
,
412 keybuf
, keybuflen
, subtype
);
417 pr_debug("rc=%d\n", rc
);
421 static int ep11_verifykey(const u8
*key
, u32 keylen
,
423 u32
*keytype
, u32
*keybitsize
, u32
*flags
)
425 struct keytoken_header
*hdr
= (struct keytoken_header
*)key
;
426 u32 nr_apqns
, *apqns
= NULL
;
429 if (keylen
< sizeof(*hdr
))
432 zcrypt_wait_api_operational();
434 if (hdr
->type
== TOKTYPE_NON_CCA
&&
435 hdr
->version
== TOKVER_EP11_AES
) {
436 struct ep11keyblob
*kb
= (struct ep11keyblob
*)key
;
439 rc
= ep11_check_aes_key(pkey_dbf_info
, 3, key
, keylen
, 1);
442 *keytype
= PKEY_TYPE_EP11
;
443 *keybitsize
= kb
->head
.bitlen
;
445 api
= ap_is_se_guest() ? EP11_API_V6
: EP11_API_V4
;
446 rc
= ep11_findcard2(&apqns
, &nr_apqns
, *card
, *dom
,
448 ep11_kb_wkvp(key
, keylen
));
452 *flags
= PKEY_FLAGS_MATCH_CUR_MKVP
;
454 *card
= ((struct pkey_apqn
*)apqns
)->card
;
455 *dom
= ((struct pkey_apqn
*)apqns
)->domain
;
457 } else if (hdr
->type
== TOKTYPE_NON_CCA
&&
458 hdr
->version
== TOKVER_EP11_AES_WITH_HEADER
) {
459 struct ep11kblob_header
*kh
= (struct ep11kblob_header
*)key
;
462 rc
= ep11_check_aes_key_with_hdr(pkey_dbf_info
,
466 *keytype
= PKEY_TYPE_EP11_AES
;
467 *keybitsize
= kh
->bitlen
;
469 api
= ap_is_se_guest() ? EP11_API_V6
: EP11_API_V4
;
470 rc
= ep11_findcard2(&apqns
, &nr_apqns
, *card
, *dom
,
472 ep11_kb_wkvp(key
, keylen
));
476 *flags
= PKEY_FLAGS_MATCH_CUR_MKVP
;
478 *card
= ((struct pkey_apqn
*)apqns
)->card
;
479 *dom
= ((struct pkey_apqn
*)apqns
)->domain
;
482 /* unknown/unsupported key blob */
488 pr_debug("rc=%d\n", rc
);
493 * This function provides an alternate but usually slow way
494 * to convert a 'clear key token' with AES key material into
495 * a protected key. That is done via an intermediate step
496 * which creates an EP11 AES secure key first and then derives
497 * the protected key from this secure key.
499 static int ep11_slowpath_key2protkey(const struct pkey_apqn
*apqns
,
501 const u8
*key
, u32 keylen
,
502 u8
*protkey
, u32
*protkeylen
,
505 const struct keytoken_header
*hdr
= (const struct keytoken_header
*)key
;
506 const struct clearkeytoken
*t
= (const struct clearkeytoken
*)key
;
507 u32 tmplen
, keysize
= 0;
511 if (keylen
< sizeof(*hdr
))
514 if (hdr
->type
== TOKTYPE_NON_CCA
&&
515 hdr
->version
== TOKVER_CLEAR_KEY
)
516 keysize
= pkey_keytype_aes_to_size(t
->keytype
);
517 if (!keysize
|| t
->len
!= keysize
)
520 /* alloc tmp key buffer */
521 tmpbuf
= kmalloc(MAXEP11AESKEYBLOBSIZE
, GFP_ATOMIC
);
525 /* try two times in case of failure */
526 for (i
= 0, rc
= -ENODEV
; i
< 2 && rc
; i
++) {
527 tmplen
= MAXEP11AESKEYBLOBSIZE
;
528 rc
= ep11_clr2key(NULL
, 0, t
->keytype
, PKEY_TYPE_EP11
,
529 8 * keysize
, 0, t
->clearkey
, t
->len
,
530 tmpbuf
, &tmplen
, NULL
);
531 pr_debug("ep11_clr2key()=%d\n", rc
);
534 rc
= ep11_key2protkey(NULL
, 0, tmpbuf
, tmplen
,
535 protkey
, protkeylen
, protkeytype
);
536 pr_debug("ep11_key2protkey()=%d\n", rc
);
540 pr_debug("rc=%d\n", rc
);
544 static struct pkey_handler ep11_handler
= {
545 .module
= THIS_MODULE
,
546 .name
= "PKEY EP11 handler",
547 .is_supported_key
= is_ep11_key
,
548 .is_supported_keytype
= is_ep11_keytype
,
549 .key_to_protkey
= ep11_key2protkey
,
550 .slowpath_key_to_protkey
= ep11_slowpath_key2protkey
,
551 .gen_key
= ep11_gen_key
,
552 .clr_to_key
= ep11_clr2key
,
553 .verify_key
= ep11_verifykey
,
554 .apqns_for_key
= ep11_apqns4key
,
555 .apqns_for_keytype
= ep11_apqns4type
,
561 static int __init
pkey_ep11_init(void)
563 /* register this module as pkey handler for all the ep11 stuff */
564 return pkey_handler_register(&ep11_handler
);
570 static void __exit
pkey_ep11_exit(void)
572 /* unregister this module as pkey handler */
573 pkey_handler_unregister(&ep11_handler
);
576 module_init(pkey_ep11_init
);
577 module_exit(pkey_ep11_exit
);