1 // SPDX-License-Identifier: GPL-2.0
3 * pkey module sysfs related functions
5 * Copyright IBM Corp. 2024
8 #define KMSG_COMPONENT "pkey"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/sysfs.h>
13 #include "zcrypt_ccamisc.h"
14 #include "zcrypt_ep11misc.h"
16 #include "pkey_base.h"
19 * Wrapper around pkey_handler_gen_key() which deals with the
20 * ENODEV return code and then tries to enforce a pkey handler
23 static int sys_pkey_handler_gen_key(u32 keytype
, u32 keysubtype
,
24 u32 keybitsize
, u32 flags
,
25 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
)
29 rc
= pkey_handler_gen_key(NULL
, 0,
32 keybuf
, keybuflen
, keyinfo
);
34 pkey_handler_request_modules();
35 rc
= pkey_handler_gen_key(NULL
, 0,
38 keybuf
, keybuflen
, keyinfo
);
45 * Sysfs attribute read function for all protected key binary attributes.
46 * The implementation can not deal with partial reads, because a new random
47 * protected key blob is generated with each read. In case of partial reads
48 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
50 static ssize_t
pkey_protkey_aes_attr_read(u32 keytype
, bool is_xts
, char *buf
,
51 loff_t off
, size_t count
)
53 struct protaeskeytoken protkeytoken
;
54 struct pkey_protkey protkey
;
57 if (off
!= 0 || count
< sizeof(protkeytoken
))
60 if (count
< 2 * sizeof(protkeytoken
))
63 memset(&protkeytoken
, 0, sizeof(protkeytoken
));
64 protkeytoken
.type
= TOKTYPE_NON_CCA
;
65 protkeytoken
.version
= TOKVER_PROTECTED_KEY
;
66 protkeytoken
.keytype
= keytype
;
68 protkey
.len
= sizeof(protkey
.protkey
);
69 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_PROTKEY
, 0, 0,
70 protkey
.protkey
, &protkey
.len
,
75 protkeytoken
.len
= protkey
.len
;
76 memcpy(&protkeytoken
.protkey
, &protkey
.protkey
, protkey
.len
);
78 memcpy(buf
, &protkeytoken
, sizeof(protkeytoken
));
81 /* xts needs a second protected key, reuse protkey struct */
82 protkey
.len
= sizeof(protkey
.protkey
);
83 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_PROTKEY
, 0, 0,
84 protkey
.protkey
, &protkey
.len
,
89 protkeytoken
.len
= protkey
.len
;
90 memcpy(&protkeytoken
.protkey
, &protkey
.protkey
, protkey
.len
);
92 memcpy(buf
+ sizeof(protkeytoken
), &protkeytoken
,
93 sizeof(protkeytoken
));
95 return 2 * sizeof(protkeytoken
);
98 return sizeof(protkeytoken
);
102 * Sysfs attribute read function for the AES XTS prot key binary attributes.
103 * The implementation can not deal with partial reads, because a new random
104 * protected key blob is generated with each read. In case of partial reads
105 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
107 static ssize_t
pkey_protkey_aes_xts_attr_read(u32 keytype
, char *buf
,
108 loff_t off
, size_t count
)
110 struct protkeytoken
*t
= (struct protkeytoken
*)buf
;
111 u32 protlen
, prottype
;
115 case PKEY_KEYTYPE_AES_XTS_128
:
118 case PKEY_KEYTYPE_AES_XTS_256
:
125 if (off
!= 0 || count
< sizeof(*t
) + protlen
)
128 memset(t
, 0, sizeof(*t
) + protlen
);
129 t
->type
= TOKTYPE_NON_CCA
;
130 t
->version
= TOKVER_PROTECTED_KEY
;
131 t
->keytype
= keytype
;
133 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_PROTKEY
, 0, 0,
134 t
->protkey
, &protlen
, &prottype
);
140 return sizeof(*t
) + protlen
;
144 * Sysfs attribute read function for the HMAC prot key binary attributes.
145 * The implementation can not deal with partial reads, because a new random
146 * protected key blob is generated with each read. In case of partial reads
147 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
149 static ssize_t
pkey_protkey_hmac_attr_read(u32 keytype
, char *buf
,
150 loff_t off
, size_t count
)
152 struct protkeytoken
*t
= (struct protkeytoken
*)buf
;
153 u32 protlen
, prottype
;
157 case PKEY_KEYTYPE_HMAC_512
:
160 case PKEY_KEYTYPE_HMAC_1024
:
167 if (off
!= 0 || count
< sizeof(*t
) + protlen
)
170 memset(t
, 0, sizeof(*t
) + protlen
);
171 t
->type
= TOKTYPE_NON_CCA
;
172 t
->version
= TOKVER_PROTECTED_KEY
;
173 t
->keytype
= keytype
;
175 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_PROTKEY
, 0, 0,
176 t
->protkey
, &protlen
, &prottype
);
182 return sizeof(*t
) + protlen
;
185 static ssize_t
protkey_aes_128_read(struct file
*filp
,
186 struct kobject
*kobj
,
187 struct bin_attribute
*attr
,
188 char *buf
, loff_t off
,
191 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128
, false, buf
,
195 static ssize_t
protkey_aes_192_read(struct file
*filp
,
196 struct kobject
*kobj
,
197 struct bin_attribute
*attr
,
198 char *buf
, loff_t off
,
201 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192
, false, buf
,
205 static ssize_t
protkey_aes_256_read(struct file
*filp
,
206 struct kobject
*kobj
,
207 struct bin_attribute
*attr
,
208 char *buf
, loff_t off
,
211 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256
, false, buf
,
215 static ssize_t
protkey_aes_128_xts_read(struct file
*filp
,
216 struct kobject
*kobj
,
217 struct bin_attribute
*attr
,
218 char *buf
, loff_t off
,
221 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128
, true, buf
,
225 static ssize_t
protkey_aes_256_xts_read(struct file
*filp
,
226 struct kobject
*kobj
,
227 struct bin_attribute
*attr
,
228 char *buf
, loff_t off
,
231 return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256
, true, buf
,
235 static ssize_t
protkey_aes_xts_128_read(struct file
*filp
,
236 struct kobject
*kobj
,
237 struct bin_attribute
*attr
,
238 char *buf
, loff_t off
,
241 return pkey_protkey_aes_xts_attr_read(PKEY_KEYTYPE_AES_XTS_128
,
245 static ssize_t
protkey_aes_xts_256_read(struct file
*filp
,
246 struct kobject
*kobj
,
247 struct bin_attribute
*attr
,
248 char *buf
, loff_t off
,
251 return pkey_protkey_aes_xts_attr_read(PKEY_KEYTYPE_AES_XTS_256
,
255 static ssize_t
protkey_hmac_512_read(struct file
*filp
,
256 struct kobject
*kobj
,
257 struct bin_attribute
*attr
,
258 char *buf
, loff_t off
,
261 return pkey_protkey_hmac_attr_read(PKEY_KEYTYPE_HMAC_512
,
265 static ssize_t
protkey_hmac_1024_read(struct file
*filp
,
266 struct kobject
*kobj
,
267 struct bin_attribute
*attr
,
268 char *buf
, loff_t off
,
271 return pkey_protkey_hmac_attr_read(PKEY_KEYTYPE_HMAC_1024
,
275 static BIN_ATTR_RO(protkey_aes_128
, sizeof(struct protaeskeytoken
));
276 static BIN_ATTR_RO(protkey_aes_192
, sizeof(struct protaeskeytoken
));
277 static BIN_ATTR_RO(protkey_aes_256
, sizeof(struct protaeskeytoken
));
278 static BIN_ATTR_RO(protkey_aes_128_xts
, 2 * sizeof(struct protaeskeytoken
));
279 static BIN_ATTR_RO(protkey_aes_256_xts
, 2 * sizeof(struct protaeskeytoken
));
280 static BIN_ATTR_RO(protkey_aes_xts_128
, sizeof(struct protkeytoken
) + 64);
281 static BIN_ATTR_RO(protkey_aes_xts_256
, sizeof(struct protkeytoken
) + 96);
282 static BIN_ATTR_RO(protkey_hmac_512
, sizeof(struct protkeytoken
) + 96);
283 static BIN_ATTR_RO(protkey_hmac_1024
, sizeof(struct protkeytoken
) + 160);
285 static struct bin_attribute
*protkey_attrs
[] = {
286 &bin_attr_protkey_aes_128
,
287 &bin_attr_protkey_aes_192
,
288 &bin_attr_protkey_aes_256
,
289 &bin_attr_protkey_aes_128_xts
,
290 &bin_attr_protkey_aes_256_xts
,
291 &bin_attr_protkey_aes_xts_128
,
292 &bin_attr_protkey_aes_xts_256
,
293 &bin_attr_protkey_hmac_512
,
294 &bin_attr_protkey_hmac_1024
,
298 static struct attribute_group protkey_attr_group
= {
300 .bin_attrs
= protkey_attrs
,
304 * Sysfs attribute read function for all secure key ccadata binary attributes.
305 * The implementation can not deal with partial reads, because a new random
306 * protected key blob is generated with each read. In case of partial reads
307 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
309 static ssize_t
pkey_ccadata_aes_attr_read(u32 keytype
, bool is_xts
, char *buf
,
310 loff_t off
, size_t count
)
312 struct pkey_seckey
*seckey
= (struct pkey_seckey
*)buf
;
316 if (off
!= 0 || count
< sizeof(struct secaeskeytoken
))
319 if (count
< 2 * sizeof(struct secaeskeytoken
))
322 buflen
= sizeof(seckey
->seckey
);
323 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_CCA_DATA
, 0, 0,
324 seckey
->seckey
, &buflen
, NULL
);
330 buflen
= sizeof(seckey
->seckey
);
331 rc
= sys_pkey_handler_gen_key(keytype
, PKEY_TYPE_CCA_DATA
, 0, 0,
332 seckey
->seckey
, &buflen
, NULL
);
336 return 2 * sizeof(struct secaeskeytoken
);
339 return sizeof(struct secaeskeytoken
);
342 static ssize_t
ccadata_aes_128_read(struct file
*filp
,
343 struct kobject
*kobj
,
344 struct bin_attribute
*attr
,
345 char *buf
, loff_t off
,
348 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128
, false, buf
,
352 static ssize_t
ccadata_aes_192_read(struct file
*filp
,
353 struct kobject
*kobj
,
354 struct bin_attribute
*attr
,
355 char *buf
, loff_t off
,
358 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192
, false, buf
,
362 static ssize_t
ccadata_aes_256_read(struct file
*filp
,
363 struct kobject
*kobj
,
364 struct bin_attribute
*attr
,
365 char *buf
, loff_t off
,
368 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256
, false, buf
,
372 static ssize_t
ccadata_aes_128_xts_read(struct file
*filp
,
373 struct kobject
*kobj
,
374 struct bin_attribute
*attr
,
375 char *buf
, loff_t off
,
378 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128
, true, buf
,
382 static ssize_t
ccadata_aes_256_xts_read(struct file
*filp
,
383 struct kobject
*kobj
,
384 struct bin_attribute
*attr
,
385 char *buf
, loff_t off
,
388 return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256
, true, buf
,
392 static BIN_ATTR_RO(ccadata_aes_128
, sizeof(struct secaeskeytoken
));
393 static BIN_ATTR_RO(ccadata_aes_192
, sizeof(struct secaeskeytoken
));
394 static BIN_ATTR_RO(ccadata_aes_256
, sizeof(struct secaeskeytoken
));
395 static BIN_ATTR_RO(ccadata_aes_128_xts
, 2 * sizeof(struct secaeskeytoken
));
396 static BIN_ATTR_RO(ccadata_aes_256_xts
, 2 * sizeof(struct secaeskeytoken
));
398 static struct bin_attribute
*ccadata_attrs
[] = {
399 &bin_attr_ccadata_aes_128
,
400 &bin_attr_ccadata_aes_192
,
401 &bin_attr_ccadata_aes_256
,
402 &bin_attr_ccadata_aes_128_xts
,
403 &bin_attr_ccadata_aes_256_xts
,
407 static struct attribute_group ccadata_attr_group
= {
409 .bin_attrs
= ccadata_attrs
,
412 #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80)
415 * Sysfs attribute read function for all secure key ccacipher binary attributes.
416 * The implementation can not deal with partial reads, because a new random
417 * secure key blob is generated with each read. In case of partial reads
418 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
420 static ssize_t
pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits
,
421 bool is_xts
, char *buf
, loff_t off
,
424 u32 keysize
= CCACIPHERTOKENSIZE
;
427 if (off
!= 0 || count
< CCACIPHERTOKENSIZE
)
430 if (count
< 2 * CCACIPHERTOKENSIZE
)
433 memset(buf
, 0, is_xts
? 2 * keysize
: keysize
);
435 rc
= sys_pkey_handler_gen_key(pkey_aes_bitsize_to_keytype(keybits
),
436 PKEY_TYPE_CCA_CIPHER
, keybits
, 0,
437 buf
, &keysize
, NULL
);
442 keysize
= CCACIPHERTOKENSIZE
;
443 buf
+= CCACIPHERTOKENSIZE
;
444 rc
= sys_pkey_handler_gen_key(
445 pkey_aes_bitsize_to_keytype(keybits
),
446 PKEY_TYPE_CCA_CIPHER
, keybits
, 0,
447 buf
, &keysize
, NULL
);
450 return 2 * CCACIPHERTOKENSIZE
;
453 return CCACIPHERTOKENSIZE
;
456 static ssize_t
ccacipher_aes_128_read(struct file
*filp
,
457 struct kobject
*kobj
,
458 struct bin_attribute
*attr
,
459 char *buf
, loff_t off
,
462 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128
, false, buf
,
466 static ssize_t
ccacipher_aes_192_read(struct file
*filp
,
467 struct kobject
*kobj
,
468 struct bin_attribute
*attr
,
469 char *buf
, loff_t off
,
472 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192
, false, buf
,
476 static ssize_t
ccacipher_aes_256_read(struct file
*filp
,
477 struct kobject
*kobj
,
478 struct bin_attribute
*attr
,
479 char *buf
, loff_t off
,
482 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256
, false, buf
,
486 static ssize_t
ccacipher_aes_128_xts_read(struct file
*filp
,
487 struct kobject
*kobj
,
488 struct bin_attribute
*attr
,
489 char *buf
, loff_t off
,
492 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128
, true, buf
,
496 static ssize_t
ccacipher_aes_256_xts_read(struct file
*filp
,
497 struct kobject
*kobj
,
498 struct bin_attribute
*attr
,
499 char *buf
, loff_t off
,
502 return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256
, true, buf
,
506 static BIN_ATTR_RO(ccacipher_aes_128
, CCACIPHERTOKENSIZE
);
507 static BIN_ATTR_RO(ccacipher_aes_192
, CCACIPHERTOKENSIZE
);
508 static BIN_ATTR_RO(ccacipher_aes_256
, CCACIPHERTOKENSIZE
);
509 static BIN_ATTR_RO(ccacipher_aes_128_xts
, 2 * CCACIPHERTOKENSIZE
);
510 static BIN_ATTR_RO(ccacipher_aes_256_xts
, 2 * CCACIPHERTOKENSIZE
);
512 static struct bin_attribute
*ccacipher_attrs
[] = {
513 &bin_attr_ccacipher_aes_128
,
514 &bin_attr_ccacipher_aes_192
,
515 &bin_attr_ccacipher_aes_256
,
516 &bin_attr_ccacipher_aes_128_xts
,
517 &bin_attr_ccacipher_aes_256_xts
,
521 static struct attribute_group ccacipher_attr_group
= {
523 .bin_attrs
= ccacipher_attrs
,
527 * Sysfs attribute read function for all ep11 aes key binary attributes.
528 * The implementation can not deal with partial reads, because a new random
529 * secure key blob is generated with each read. In case of partial reads
530 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
531 * This function and the sysfs attributes using it provide EP11 key blobs
532 * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
535 static ssize_t
pkey_ep11_aes_attr_read(enum pkey_key_size keybits
,
536 bool is_xts
, char *buf
, loff_t off
,
539 u32 keysize
= MAXEP11AESKEYBLOBSIZE
;
542 if (off
!= 0 || count
< MAXEP11AESKEYBLOBSIZE
)
545 if (count
< 2 * MAXEP11AESKEYBLOBSIZE
)
548 memset(buf
, 0, is_xts
? 2 * keysize
: keysize
);
550 rc
= sys_pkey_handler_gen_key(pkey_aes_bitsize_to_keytype(keybits
),
551 PKEY_TYPE_EP11_AES
, keybits
, 0,
552 buf
, &keysize
, NULL
);
557 keysize
= MAXEP11AESKEYBLOBSIZE
;
558 buf
+= MAXEP11AESKEYBLOBSIZE
;
559 rc
= sys_pkey_handler_gen_key(
560 pkey_aes_bitsize_to_keytype(keybits
),
561 PKEY_TYPE_EP11_AES
, keybits
, 0,
562 buf
, &keysize
, NULL
);
565 return 2 * MAXEP11AESKEYBLOBSIZE
;
568 return MAXEP11AESKEYBLOBSIZE
;
571 static ssize_t
ep11_aes_128_read(struct file
*filp
,
572 struct kobject
*kobj
,
573 struct bin_attribute
*attr
,
574 char *buf
, loff_t off
,
577 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128
, false, buf
,
581 static ssize_t
ep11_aes_192_read(struct file
*filp
,
582 struct kobject
*kobj
,
583 struct bin_attribute
*attr
,
584 char *buf
, loff_t off
,
587 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192
, false, buf
,
591 static ssize_t
ep11_aes_256_read(struct file
*filp
,
592 struct kobject
*kobj
,
593 struct bin_attribute
*attr
,
594 char *buf
, loff_t off
,
597 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256
, false, buf
,
601 static ssize_t
ep11_aes_128_xts_read(struct file
*filp
,
602 struct kobject
*kobj
,
603 struct bin_attribute
*attr
,
604 char *buf
, loff_t off
,
607 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128
, true, buf
,
611 static ssize_t
ep11_aes_256_xts_read(struct file
*filp
,
612 struct kobject
*kobj
,
613 struct bin_attribute
*attr
,
614 char *buf
, loff_t off
,
617 return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256
, true, buf
,
621 static BIN_ATTR_RO(ep11_aes_128
, MAXEP11AESKEYBLOBSIZE
);
622 static BIN_ATTR_RO(ep11_aes_192
, MAXEP11AESKEYBLOBSIZE
);
623 static BIN_ATTR_RO(ep11_aes_256
, MAXEP11AESKEYBLOBSIZE
);
624 static BIN_ATTR_RO(ep11_aes_128_xts
, 2 * MAXEP11AESKEYBLOBSIZE
);
625 static BIN_ATTR_RO(ep11_aes_256_xts
, 2 * MAXEP11AESKEYBLOBSIZE
);
627 static struct bin_attribute
*ep11_attrs
[] = {
628 &bin_attr_ep11_aes_128
,
629 &bin_attr_ep11_aes_192
,
630 &bin_attr_ep11_aes_256
,
631 &bin_attr_ep11_aes_128_xts
,
632 &bin_attr_ep11_aes_256_xts
,
636 static struct attribute_group ep11_attr_group
= {
638 .bin_attrs
= ep11_attrs
,
641 const struct attribute_group
*pkey_attr_groups
[] = {
644 &ccacipher_attr_group
,