1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 IBM Corporation
6 * David Safford <safford@us.ibm.com>
8 * See Documentation/security/keys/trusted-encrypted.rst
11 #include <crypto/hash_info.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/parser.h>
17 #include <linux/string.h>
18 #include <linux/err.h>
19 #include <keys/user-type.h>
20 #include <keys/trusted-type.h>
21 #include <linux/key-type.h>
22 #include <linux/rcupdate.h>
23 #include <linux/crypto.h>
24 #include <crypto/hash.h>
25 #include <crypto/sha1.h>
26 #include <linux/capability.h>
27 #include <linux/tpm.h>
28 #include <linux/tpm_command.h>
30 #include <keys/trusted_tpm.h>
32 static const char hmac_alg
[] = "hmac(sha1)";
33 static const char hash_alg
[] = "sha1";
34 static struct tpm_chip
*chip
;
35 static struct tpm_digest
*digests
;
38 struct shash_desc shash
;
42 static struct crypto_shash
*hashalg
;
43 static struct crypto_shash
*hmacalg
;
45 static struct sdesc
*init_sdesc(struct crypto_shash
*alg
)
50 size
= sizeof(struct shash_desc
) + crypto_shash_descsize(alg
);
51 sdesc
= kmalloc(size
, GFP_KERNEL
);
53 return ERR_PTR(-ENOMEM
);
54 sdesc
->shash
.tfm
= alg
;
58 static int TSS_sha1(const unsigned char *data
, unsigned int datalen
,
59 unsigned char *digest
)
64 sdesc
= init_sdesc(hashalg
);
66 pr_info("trusted_key: can't alloc %s\n", hash_alg
);
67 return PTR_ERR(sdesc
);
70 ret
= crypto_shash_digest(&sdesc
->shash
, data
, datalen
, digest
);
71 kfree_sensitive(sdesc
);
75 static int TSS_rawhmac(unsigned char *digest
, const unsigned char *key
,
76 unsigned int keylen
, ...)
84 sdesc
= init_sdesc(hmacalg
);
86 pr_info("trusted_key: can't alloc %s\n", hmac_alg
);
87 return PTR_ERR(sdesc
);
90 ret
= crypto_shash_setkey(hmacalg
, key
, keylen
);
93 ret
= crypto_shash_init(&sdesc
->shash
);
97 va_start(argp
, keylen
);
99 dlen
= va_arg(argp
, unsigned int);
102 data
= va_arg(argp
, unsigned char *);
107 ret
= crypto_shash_update(&sdesc
->shash
, data
, dlen
);
113 ret
= crypto_shash_final(&sdesc
->shash
, digest
);
115 kfree_sensitive(sdesc
);
120 * calculate authorization info fields to send to TPM
122 int TSS_authhmac(unsigned char *digest
, const unsigned char *key
,
123 unsigned int keylen
, unsigned char *h1
,
124 unsigned char *h2
, unsigned int h3
, ...)
126 unsigned char paramdigest
[SHA1_DIGEST_SIZE
];
137 sdesc
= init_sdesc(hashalg
);
139 pr_info("trusted_key: can't alloc %s\n", hash_alg
);
140 return PTR_ERR(sdesc
);
144 ret
= crypto_shash_init(&sdesc
->shash
);
149 dlen
= va_arg(argp
, unsigned int);
152 data
= va_arg(argp
, unsigned char *);
157 ret
= crypto_shash_update(&sdesc
->shash
, data
, dlen
);
163 ret
= crypto_shash_final(&sdesc
->shash
, paramdigest
);
165 ret
= TSS_rawhmac(digest
, key
, keylen
, SHA1_DIGEST_SIZE
,
166 paramdigest
, TPM_NONCE_SIZE
, h1
,
167 TPM_NONCE_SIZE
, h2
, 1, &c
, 0, 0);
169 kfree_sensitive(sdesc
);
172 EXPORT_SYMBOL_GPL(TSS_authhmac
);
175 * verify the AUTH1_COMMAND (Seal) result from TPM
177 int TSS_checkhmac1(unsigned char *buffer
,
178 const uint32_t command
,
179 const unsigned char *ononce
,
180 const unsigned char *key
,
181 unsigned int keylen
, ...)
187 unsigned char *enonce
;
188 unsigned char *continueflag
;
189 unsigned char *authdata
;
190 unsigned char testhmac
[SHA1_DIGEST_SIZE
];
191 unsigned char paramdigest
[SHA1_DIGEST_SIZE
];
201 bufsize
= LOAD32(buffer
, TPM_SIZE_OFFSET
);
202 tag
= LOAD16(buffer
, 0);
204 result
= LOAD32N(buffer
, TPM_RETURN_OFFSET
);
205 if (tag
== TPM_TAG_RSP_COMMAND
)
207 if (tag
!= TPM_TAG_RSP_AUTH1_COMMAND
)
209 authdata
= buffer
+ bufsize
- SHA1_DIGEST_SIZE
;
210 continueflag
= authdata
- 1;
211 enonce
= continueflag
- TPM_NONCE_SIZE
;
213 sdesc
= init_sdesc(hashalg
);
215 pr_info("trusted_key: can't alloc %s\n", hash_alg
);
216 return PTR_ERR(sdesc
);
218 ret
= crypto_shash_init(&sdesc
->shash
);
221 ret
= crypto_shash_update(&sdesc
->shash
, (const u8
*)&result
,
225 ret
= crypto_shash_update(&sdesc
->shash
, (const u8
*)&ordinal
,
229 va_start(argp
, keylen
);
231 dlen
= va_arg(argp
, unsigned int);
234 dpos
= va_arg(argp
, unsigned int);
235 ret
= crypto_shash_update(&sdesc
->shash
, buffer
+ dpos
, dlen
);
241 ret
= crypto_shash_final(&sdesc
->shash
, paramdigest
);
245 ret
= TSS_rawhmac(testhmac
, key
, keylen
, SHA1_DIGEST_SIZE
, paramdigest
,
246 TPM_NONCE_SIZE
, enonce
, TPM_NONCE_SIZE
, ononce
,
247 1, continueflag
, 0, 0);
251 if (memcmp(testhmac
, authdata
, SHA1_DIGEST_SIZE
))
254 kfree_sensitive(sdesc
);
257 EXPORT_SYMBOL_GPL(TSS_checkhmac1
);
260 * verify the AUTH2_COMMAND (unseal) result from TPM
262 static int TSS_checkhmac2(unsigned char *buffer
,
263 const uint32_t command
,
264 const unsigned char *ononce
,
265 const unsigned char *key1
,
266 unsigned int keylen1
,
267 const unsigned char *key2
,
268 unsigned int keylen2
, ...)
274 unsigned char *enonce1
;
275 unsigned char *continueflag1
;
276 unsigned char *authdata1
;
277 unsigned char *enonce2
;
278 unsigned char *continueflag2
;
279 unsigned char *authdata2
;
280 unsigned char testhmac1
[SHA1_DIGEST_SIZE
];
281 unsigned char testhmac2
[SHA1_DIGEST_SIZE
];
282 unsigned char paramdigest
[SHA1_DIGEST_SIZE
];
289 bufsize
= LOAD32(buffer
, TPM_SIZE_OFFSET
);
290 tag
= LOAD16(buffer
, 0);
292 result
= LOAD32N(buffer
, TPM_RETURN_OFFSET
);
294 if (tag
== TPM_TAG_RSP_COMMAND
)
296 if (tag
!= TPM_TAG_RSP_AUTH2_COMMAND
)
298 authdata1
= buffer
+ bufsize
- (SHA1_DIGEST_SIZE
+ 1
299 + SHA1_DIGEST_SIZE
+ SHA1_DIGEST_SIZE
);
300 authdata2
= buffer
+ bufsize
- (SHA1_DIGEST_SIZE
);
301 continueflag1
= authdata1
- 1;
302 continueflag2
= authdata2
- 1;
303 enonce1
= continueflag1
- TPM_NONCE_SIZE
;
304 enonce2
= continueflag2
- TPM_NONCE_SIZE
;
306 sdesc
= init_sdesc(hashalg
);
308 pr_info("trusted_key: can't alloc %s\n", hash_alg
);
309 return PTR_ERR(sdesc
);
311 ret
= crypto_shash_init(&sdesc
->shash
);
314 ret
= crypto_shash_update(&sdesc
->shash
, (const u8
*)&result
,
318 ret
= crypto_shash_update(&sdesc
->shash
, (const u8
*)&ordinal
,
323 va_start(argp
, keylen2
);
325 dlen
= va_arg(argp
, unsigned int);
328 dpos
= va_arg(argp
, unsigned int);
329 ret
= crypto_shash_update(&sdesc
->shash
, buffer
+ dpos
, dlen
);
335 ret
= crypto_shash_final(&sdesc
->shash
, paramdigest
);
339 ret
= TSS_rawhmac(testhmac1
, key1
, keylen1
, SHA1_DIGEST_SIZE
,
340 paramdigest
, TPM_NONCE_SIZE
, enonce1
,
341 TPM_NONCE_SIZE
, ononce
, 1, continueflag1
, 0, 0);
344 if (memcmp(testhmac1
, authdata1
, SHA1_DIGEST_SIZE
)) {
348 ret
= TSS_rawhmac(testhmac2
, key2
, keylen2
, SHA1_DIGEST_SIZE
,
349 paramdigest
, TPM_NONCE_SIZE
, enonce2
,
350 TPM_NONCE_SIZE
, ononce
, 1, continueflag2
, 0, 0);
353 if (memcmp(testhmac2
, authdata2
, SHA1_DIGEST_SIZE
))
356 kfree_sensitive(sdesc
);
361 * For key specific tpm requests, we will generate and send our
362 * own TPM command packets using the drivers send function.
364 int trusted_tpm_send(unsigned char *cmd
, size_t buflen
)
372 rc
= tpm_send(chip
, cmd
, buflen
);
375 /* Can't return positive return codes values to keyctl */
379 EXPORT_SYMBOL_GPL(trusted_tpm_send
);
382 * Lock a trusted key, by extending a selected PCR.
384 * Prevents a trusted key that is sealed to PCRs from being accessed.
385 * This uses the tpm driver's extend function.
387 static int pcrlock(const int pcrnum
)
389 if (!capable(CAP_SYS_ADMIN
))
392 return tpm_pcr_extend(chip
, pcrnum
, digests
) ? -EINVAL
: 0;
396 * Create an object specific authorisation protocol (OSAP) session
398 static int osap(struct tpm_buf
*tb
, struct osapsess
*s
,
399 const unsigned char *key
, uint16_t type
, uint32_t handle
)
401 unsigned char enonce
[TPM_NONCE_SIZE
];
402 unsigned char ononce
[TPM_NONCE_SIZE
];
405 ret
= tpm_get_random(chip
, ononce
, TPM_NONCE_SIZE
);
406 if (ret
!= TPM_NONCE_SIZE
)
409 tpm_buf_reset(tb
, TPM_TAG_RQU_COMMAND
, TPM_ORD_OSAP
);
410 tpm_buf_append_u16(tb
, type
);
411 tpm_buf_append_u32(tb
, handle
);
412 tpm_buf_append(tb
, ononce
, TPM_NONCE_SIZE
);
414 ret
= trusted_tpm_send(tb
->data
, MAX_BUF_SIZE
);
418 s
->handle
= LOAD32(tb
->data
, TPM_DATA_OFFSET
);
419 memcpy(s
->enonce
, &(tb
->data
[TPM_DATA_OFFSET
+ sizeof(uint32_t)]),
421 memcpy(enonce
, &(tb
->data
[TPM_DATA_OFFSET
+ sizeof(uint32_t) +
422 TPM_NONCE_SIZE
]), TPM_NONCE_SIZE
);
423 return TSS_rawhmac(s
->secret
, key
, SHA1_DIGEST_SIZE
, TPM_NONCE_SIZE
,
424 enonce
, TPM_NONCE_SIZE
, ononce
, 0, 0);
428 * Create an object independent authorisation protocol (oiap) session
430 int oiap(struct tpm_buf
*tb
, uint32_t *handle
, unsigned char *nonce
)
437 tpm_buf_reset(tb
, TPM_TAG_RQU_COMMAND
, TPM_ORD_OIAP
);
438 ret
= trusted_tpm_send(tb
->data
, MAX_BUF_SIZE
);
442 *handle
= LOAD32(tb
->data
, TPM_DATA_OFFSET
);
443 memcpy(nonce
, &tb
->data
[TPM_DATA_OFFSET
+ sizeof(uint32_t)],
447 EXPORT_SYMBOL_GPL(oiap
);
450 unsigned char encauth
[SHA1_DIGEST_SIZE
];
451 unsigned char pubauth
[SHA1_DIGEST_SIZE
];
452 unsigned char xorwork
[SHA1_DIGEST_SIZE
* 2];
453 unsigned char xorhash
[SHA1_DIGEST_SIZE
];
454 unsigned char nonceodd
[TPM_NONCE_SIZE
];
458 * Have the TPM seal(encrypt) the trusted key, possibly based on
459 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
461 static int tpm_seal(struct tpm_buf
*tb
, uint16_t keytype
,
462 uint32_t keyhandle
, const unsigned char *keyauth
,
463 const unsigned char *data
, uint32_t datalen
,
464 unsigned char *blob
, uint32_t *bloblen
,
465 const unsigned char *blobauth
,
466 const unsigned char *pcrinfo
, uint32_t pcrinfosize
)
468 struct osapsess sess
;
469 struct tpm_digests
*td
;
480 /* alloc some work space for all the hashes */
481 td
= kmalloc(sizeof *td
, GFP_KERNEL
);
485 /* get session for sealing key */
486 ret
= osap(tb
, &sess
, keyauth
, keytype
, keyhandle
);
491 /* calculate encrypted authorization value */
492 memcpy(td
->xorwork
, sess
.secret
, SHA1_DIGEST_SIZE
);
493 memcpy(td
->xorwork
+ SHA1_DIGEST_SIZE
, sess
.enonce
, SHA1_DIGEST_SIZE
);
494 ret
= TSS_sha1(td
->xorwork
, SHA1_DIGEST_SIZE
* 2, td
->xorhash
);
498 ret
= tpm_get_random(chip
, td
->nonceodd
, TPM_NONCE_SIZE
);
499 if (ret
!= TPM_NONCE_SIZE
)
501 ordinal
= htonl(TPM_ORD_SEAL
);
502 datsize
= htonl(datalen
);
503 pcrsize
= htonl(pcrinfosize
);
506 /* encrypt data authorization key */
507 for (i
= 0; i
< SHA1_DIGEST_SIZE
; ++i
)
508 td
->encauth
[i
] = td
->xorhash
[i
] ^ blobauth
[i
];
510 /* calculate authorization HMAC value */
511 if (pcrinfosize
== 0) {
512 /* no pcr info specified */
513 ret
= TSS_authhmac(td
->pubauth
, sess
.secret
, SHA1_DIGEST_SIZE
,
514 sess
.enonce
, td
->nonceodd
, cont
,
515 sizeof(uint32_t), &ordinal
, SHA1_DIGEST_SIZE
,
516 td
->encauth
, sizeof(uint32_t), &pcrsize
,
517 sizeof(uint32_t), &datsize
, datalen
, data
, 0,
520 /* pcr info specified */
521 ret
= TSS_authhmac(td
->pubauth
, sess
.secret
, SHA1_DIGEST_SIZE
,
522 sess
.enonce
, td
->nonceodd
, cont
,
523 sizeof(uint32_t), &ordinal
, SHA1_DIGEST_SIZE
,
524 td
->encauth
, sizeof(uint32_t), &pcrsize
,
525 pcrinfosize
, pcrinfo
, sizeof(uint32_t),
526 &datsize
, datalen
, data
, 0, 0);
531 /* build and send the TPM request packet */
532 tpm_buf_reset(tb
, TPM_TAG_RQU_AUTH1_COMMAND
, TPM_ORD_SEAL
);
533 tpm_buf_append_u32(tb
, keyhandle
);
534 tpm_buf_append(tb
, td
->encauth
, SHA1_DIGEST_SIZE
);
535 tpm_buf_append_u32(tb
, pcrinfosize
);
536 tpm_buf_append(tb
, pcrinfo
, pcrinfosize
);
537 tpm_buf_append_u32(tb
, datalen
);
538 tpm_buf_append(tb
, data
, datalen
);
539 tpm_buf_append_u32(tb
, sess
.handle
);
540 tpm_buf_append(tb
, td
->nonceodd
, TPM_NONCE_SIZE
);
541 tpm_buf_append_u8(tb
, cont
);
542 tpm_buf_append(tb
, td
->pubauth
, SHA1_DIGEST_SIZE
);
544 ret
= trusted_tpm_send(tb
->data
, MAX_BUF_SIZE
);
548 /* calculate the size of the returned Blob */
549 sealinfosize
= LOAD32(tb
->data
, TPM_DATA_OFFSET
+ sizeof(uint32_t));
550 encdatasize
= LOAD32(tb
->data
, TPM_DATA_OFFSET
+ sizeof(uint32_t) +
551 sizeof(uint32_t) + sealinfosize
);
552 storedsize
= sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize
+
553 sizeof(uint32_t) + encdatasize
;
555 /* check the HMAC in the response */
556 ret
= TSS_checkhmac1(tb
->data
, ordinal
, td
->nonceodd
, sess
.secret
,
557 SHA1_DIGEST_SIZE
, storedsize
, TPM_DATA_OFFSET
, 0,
560 /* copy the returned blob to caller */
562 memcpy(blob
, tb
->data
+ TPM_DATA_OFFSET
, storedsize
);
563 *bloblen
= storedsize
;
571 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
573 static int tpm_unseal(struct tpm_buf
*tb
,
574 uint32_t keyhandle
, const unsigned char *keyauth
,
575 const unsigned char *blob
, int bloblen
,
576 const unsigned char *blobauth
,
577 unsigned char *data
, unsigned int *datalen
)
579 unsigned char nonceodd
[TPM_NONCE_SIZE
];
580 unsigned char enonce1
[TPM_NONCE_SIZE
];
581 unsigned char enonce2
[TPM_NONCE_SIZE
];
582 unsigned char authdata1
[SHA1_DIGEST_SIZE
];
583 unsigned char authdata2
[SHA1_DIGEST_SIZE
];
584 uint32_t authhandle1
= 0;
585 uint32_t authhandle2
= 0;
586 unsigned char cont
= 0;
590 /* sessions for unsealing key and data */
591 ret
= oiap(tb
, &authhandle1
, enonce1
);
593 pr_info("trusted_key: oiap failed (%d)\n", ret
);
596 ret
= oiap(tb
, &authhandle2
, enonce2
);
598 pr_info("trusted_key: oiap failed (%d)\n", ret
);
602 ordinal
= htonl(TPM_ORD_UNSEAL
);
603 ret
= tpm_get_random(chip
, nonceodd
, TPM_NONCE_SIZE
);
604 if (ret
!= TPM_NONCE_SIZE
) {
605 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret
);
608 ret
= TSS_authhmac(authdata1
, keyauth
, TPM_NONCE_SIZE
,
609 enonce1
, nonceodd
, cont
, sizeof(uint32_t),
610 &ordinal
, bloblen
, blob
, 0, 0);
613 ret
= TSS_authhmac(authdata2
, blobauth
, TPM_NONCE_SIZE
,
614 enonce2
, nonceodd
, cont
, sizeof(uint32_t),
615 &ordinal
, bloblen
, blob
, 0, 0);
619 /* build and send TPM request packet */
620 tpm_buf_reset(tb
, TPM_TAG_RQU_AUTH2_COMMAND
, TPM_ORD_UNSEAL
);
621 tpm_buf_append_u32(tb
, keyhandle
);
622 tpm_buf_append(tb
, blob
, bloblen
);
623 tpm_buf_append_u32(tb
, authhandle1
);
624 tpm_buf_append(tb
, nonceodd
, TPM_NONCE_SIZE
);
625 tpm_buf_append_u8(tb
, cont
);
626 tpm_buf_append(tb
, authdata1
, SHA1_DIGEST_SIZE
);
627 tpm_buf_append_u32(tb
, authhandle2
);
628 tpm_buf_append(tb
, nonceodd
, TPM_NONCE_SIZE
);
629 tpm_buf_append_u8(tb
, cont
);
630 tpm_buf_append(tb
, authdata2
, SHA1_DIGEST_SIZE
);
632 ret
= trusted_tpm_send(tb
->data
, MAX_BUF_SIZE
);
634 pr_info("trusted_key: authhmac failed (%d)\n", ret
);
638 *datalen
= LOAD32(tb
->data
, TPM_DATA_OFFSET
);
639 ret
= TSS_checkhmac2(tb
->data
, ordinal
, nonceodd
,
640 keyauth
, SHA1_DIGEST_SIZE
,
641 blobauth
, SHA1_DIGEST_SIZE
,
642 sizeof(uint32_t), TPM_DATA_OFFSET
,
643 *datalen
, TPM_DATA_OFFSET
+ sizeof(uint32_t), 0,
646 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret
);
649 memcpy(data
, tb
->data
+ TPM_DATA_OFFSET
+ sizeof(uint32_t), *datalen
);
654 * Have the TPM seal(encrypt) the symmetric key
656 static int key_seal(struct trusted_key_payload
*p
,
657 struct trusted_key_options
*o
)
662 ret
= tpm_buf_init(&tb
, 0, 0);
666 /* include migratable flag at end of sealed key */
667 p
->key
[p
->key_len
] = p
->migratable
;
669 ret
= tpm_seal(&tb
, o
->keytype
, o
->keyhandle
, o
->keyauth
,
670 p
->key
, p
->key_len
+ 1, p
->blob
, &p
->blob_len
,
671 o
->blobauth
, o
->pcrinfo
, o
->pcrinfo_len
);
673 pr_info("trusted_key: srkseal failed (%d)\n", ret
);
675 tpm_buf_destroy(&tb
);
680 * Have the TPM unseal(decrypt) the symmetric key
682 static int key_unseal(struct trusted_key_payload
*p
,
683 struct trusted_key_options
*o
)
688 ret
= tpm_buf_init(&tb
, 0, 0);
692 ret
= tpm_unseal(&tb
, o
->keyhandle
, o
->keyauth
, p
->blob
, p
->blob_len
,
693 o
->blobauth
, p
->key
, &p
->key_len
);
695 pr_info("trusted_key: srkunseal failed (%d)\n", ret
);
697 /* pull migratable flag out of sealed key */
698 p
->migratable
= p
->key
[--p
->key_len
];
700 tpm_buf_destroy(&tb
);
706 Opt_new
, Opt_load
, Opt_update
,
707 Opt_keyhandle
, Opt_keyauth
, Opt_blobauth
,
708 Opt_pcrinfo
, Opt_pcrlock
, Opt_migratable
,
714 static const match_table_t key_tokens
= {
717 {Opt_update
, "update"},
718 {Opt_keyhandle
, "keyhandle=%s"},
719 {Opt_keyauth
, "keyauth=%s"},
720 {Opt_blobauth
, "blobauth=%s"},
721 {Opt_pcrinfo
, "pcrinfo=%s"},
722 {Opt_pcrlock
, "pcrlock=%s"},
723 {Opt_migratable
, "migratable=%s"},
724 {Opt_hash
, "hash=%s"},
725 {Opt_policydigest
, "policydigest=%s"},
726 {Opt_policyhandle
, "policyhandle=%s"},
730 /* can have zero or more token= options */
731 static int getoptions(char *c
, struct trusted_key_payload
*pay
,
732 struct trusted_key_options
*opt
)
734 substring_t args
[MAX_OPT_ARGS
];
738 unsigned long handle
;
740 unsigned long token_mask
= 0;
741 unsigned int digest_len
;
745 tpm2
= tpm_is_tpm2(chip
);
749 opt
->hash
= tpm2
? HASH_ALGO_SHA256
: HASH_ALGO_SHA1
;
751 while ((p
= strsep(&c
, " \t"))) {
752 if (*p
== '\0' || *p
== ' ' || *p
== '\t')
754 token
= match_token(p
, key_tokens
, args
);
755 if (test_and_set_bit(token
, &token_mask
))
760 opt
->pcrinfo_len
= strlen(args
[0].from
) / 2;
761 if (opt
->pcrinfo_len
> MAX_PCRINFO_SIZE
)
763 res
= hex2bin(opt
->pcrinfo
, args
[0].from
,
769 res
= kstrtoul(args
[0].from
, 16, &handle
);
772 opt
->keytype
= SEAL_keytype
;
773 opt
->keyhandle
= handle
;
776 if (strlen(args
[0].from
) != 2 * SHA1_DIGEST_SIZE
)
778 res
= hex2bin(opt
->keyauth
, args
[0].from
,
784 if (strlen(args
[0].from
) != 2 * SHA1_DIGEST_SIZE
)
786 res
= hex2bin(opt
->blobauth
, args
[0].from
,
792 if (*args
[0].from
== '0')
798 res
= kstrtoul(args
[0].from
, 10, &lock
);
804 if (test_bit(Opt_policydigest
, &token_mask
))
806 for (i
= 0; i
< HASH_ALGO__LAST
; i
++) {
807 if (!strcmp(args
[0].from
, hash_algo_name
[i
])) {
812 if (i
== HASH_ALGO__LAST
)
814 if (!tpm2
&& i
!= HASH_ALGO_SHA1
) {
815 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
819 case Opt_policydigest
:
820 digest_len
= hash_digest_size
[opt
->hash
];
821 if (!tpm2
|| strlen(args
[0].from
) != (2 * digest_len
))
823 res
= hex2bin(opt
->policydigest
, args
[0].from
,
827 opt
->policydigest_len
= digest_len
;
829 case Opt_policyhandle
:
832 res
= kstrtoul(args
[0].from
, 16, &handle
);
835 opt
->policyhandle
= handle
;
845 * datablob_parse - parse the keyctl data and fill in the
846 * payload and options structures
848 * On success returns 0, otherwise -EINVAL.
850 static int datablob_parse(char *datablob
, struct trusted_key_payload
*p
,
851 struct trusted_key_options
*o
)
853 substring_t args
[MAX_OPT_ARGS
];
860 c
= strsep(&datablob
, " \t");
863 key_cmd
= match_token(c
, key_tokens
, args
);
866 /* first argument is key size */
867 c
= strsep(&datablob
, " \t");
870 ret
= kstrtol(c
, 10, &keylen
);
871 if (ret
< 0 || keylen
< MIN_KEY_SIZE
|| keylen
> MAX_KEY_SIZE
)
874 ret
= getoptions(datablob
, p
, o
);
880 /* first argument is sealed blob */
881 c
= strsep(&datablob
, " \t");
884 p
->blob_len
= strlen(c
) / 2;
885 if (p
->blob_len
> MAX_BLOB_SIZE
)
887 ret
= hex2bin(p
->blob
, c
, p
->blob_len
);
890 ret
= getoptions(datablob
, p
, o
);
896 /* all arguments are options */
897 ret
= getoptions(datablob
, p
, o
);
909 static struct trusted_key_options
*trusted_options_alloc(void)
911 struct trusted_key_options
*options
;
914 tpm2
= tpm_is_tpm2(chip
);
918 options
= kzalloc(sizeof *options
, GFP_KERNEL
);
920 /* set any non-zero defaults */
921 options
->keytype
= SRK_keytype
;
924 options
->keyhandle
= SRKHANDLE
;
929 static struct trusted_key_payload
*trusted_payload_alloc(struct key
*key
)
931 struct trusted_key_payload
*p
= NULL
;
934 ret
= key_payload_reserve(key
, sizeof *p
);
937 p
= kzalloc(sizeof *p
, GFP_KERNEL
);
939 p
->migratable
= 1; /* migratable by default */
944 * trusted_instantiate - create a new trusted key
946 * Unseal an existing trusted blob or, for a new key, get a
947 * random key, then seal and create a trusted key-type key,
948 * adding it to the specified keyring.
950 * On success, return 0. Otherwise return errno.
952 static int trusted_instantiate(struct key
*key
,
953 struct key_preparsed_payload
*prep
)
955 struct trusted_key_payload
*payload
= NULL
;
956 struct trusted_key_options
*options
= NULL
;
957 size_t datalen
= prep
->datalen
;
964 tpm2
= tpm_is_tpm2(chip
);
968 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
971 datablob
= kmalloc(datalen
+ 1, GFP_KERNEL
);
974 memcpy(datablob
, prep
->data
, datalen
);
975 datablob
[datalen
] = '\0';
977 options
= trusted_options_alloc();
982 payload
= trusted_payload_alloc(key
);
988 key_cmd
= datablob_parse(datablob
, payload
, options
);
994 if (!options
->keyhandle
) {
999 dump_payload(payload
);
1000 dump_options(options
);
1005 ret
= tpm2_unseal_trusted(chip
, payload
, options
);
1007 ret
= key_unseal(payload
, options
);
1008 dump_payload(payload
);
1009 dump_options(options
);
1011 pr_info("trusted_key: key_unseal failed (%d)\n", ret
);
1014 key_len
= payload
->key_len
;
1015 ret
= tpm_get_random(chip
, payload
->key
, key_len
);
1016 if (ret
!= key_len
) {
1017 pr_info("trusted_key: key_create failed (%d)\n", ret
);
1021 ret
= tpm2_seal_trusted(chip
, payload
, options
);
1023 ret
= key_seal(payload
, options
);
1025 pr_info("trusted_key: key_seal failed (%d)\n", ret
);
1031 if (!ret
&& options
->pcrlock
)
1032 ret
= pcrlock(options
->pcrlock
);
1034 kfree_sensitive(datablob
);
1035 kfree_sensitive(options
);
1037 rcu_assign_keypointer(key
, payload
);
1039 kfree_sensitive(payload
);
1043 static void trusted_rcu_free(struct rcu_head
*rcu
)
1045 struct trusted_key_payload
*p
;
1047 p
= container_of(rcu
, struct trusted_key_payload
, rcu
);
1052 * trusted_update - reseal an existing key with new PCR values
1054 static int trusted_update(struct key
*key
, struct key_preparsed_payload
*prep
)
1056 struct trusted_key_payload
*p
;
1057 struct trusted_key_payload
*new_p
;
1058 struct trusted_key_options
*new_o
;
1059 size_t datalen
= prep
->datalen
;
1063 if (key_is_negative(key
))
1065 p
= key
->payload
.data
[0];
1068 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
1071 datablob
= kmalloc(datalen
+ 1, GFP_KERNEL
);
1074 new_o
= trusted_options_alloc();
1079 new_p
= trusted_payload_alloc(key
);
1085 memcpy(datablob
, prep
->data
, datalen
);
1086 datablob
[datalen
] = '\0';
1087 ret
= datablob_parse(datablob
, new_p
, new_o
);
1088 if (ret
!= Opt_update
) {
1090 kfree_sensitive(new_p
);
1094 if (!new_o
->keyhandle
) {
1096 kfree_sensitive(new_p
);
1100 /* copy old key values, and reseal with new pcrs */
1101 new_p
->migratable
= p
->migratable
;
1102 new_p
->key_len
= p
->key_len
;
1103 memcpy(new_p
->key
, p
->key
, p
->key_len
);
1105 dump_payload(new_p
);
1107 ret
= key_seal(new_p
, new_o
);
1109 pr_info("trusted_key: key_seal failed (%d)\n", ret
);
1110 kfree_sensitive(new_p
);
1113 if (new_o
->pcrlock
) {
1114 ret
= pcrlock(new_o
->pcrlock
);
1116 pr_info("trusted_key: pcrlock failed (%d)\n", ret
);
1117 kfree_sensitive(new_p
);
1121 rcu_assign_keypointer(key
, new_p
);
1122 call_rcu(&p
->rcu
, trusted_rcu_free
);
1124 kfree_sensitive(datablob
);
1125 kfree_sensitive(new_o
);
1130 * trusted_read - copy the sealed blob data to userspace in hex.
1131 * On success, return to userspace the trusted key datablob size.
1133 static long trusted_read(const struct key
*key
, char *buffer
,
1136 const struct trusted_key_payload
*p
;
1140 p
= dereference_key_locked(key
);
1144 if (buffer
&& buflen
>= 2 * p
->blob_len
) {
1146 for (i
= 0; i
< p
->blob_len
; i
++)
1147 bufp
= hex_byte_pack(bufp
, p
->blob
[i
]);
1149 return 2 * p
->blob_len
;
1153 * trusted_destroy - clear and free the key's payload
1155 static void trusted_destroy(struct key
*key
)
1157 kfree_sensitive(key
->payload
.data
[0]);
1160 struct key_type key_type_trusted
= {
1162 .instantiate
= trusted_instantiate
,
1163 .update
= trusted_update
,
1164 .destroy
= trusted_destroy
,
1165 .describe
= user_describe
,
1166 .read
= trusted_read
,
1169 EXPORT_SYMBOL_GPL(key_type_trusted
);
1171 static void trusted_shash_release(void)
1174 crypto_free_shash(hashalg
);
1176 crypto_free_shash(hmacalg
);
1179 static int __init
trusted_shash_alloc(void)
1183 hmacalg
= crypto_alloc_shash(hmac_alg
, 0, 0);
1184 if (IS_ERR(hmacalg
)) {
1185 pr_info("trusted_key: could not allocate crypto %s\n",
1187 return PTR_ERR(hmacalg
);
1190 hashalg
= crypto_alloc_shash(hash_alg
, 0, 0);
1191 if (IS_ERR(hashalg
)) {
1192 pr_info("trusted_key: could not allocate crypto %s\n",
1194 ret
= PTR_ERR(hashalg
);
1201 crypto_free_shash(hmacalg
);
1205 static int __init
init_digests(void)
1209 digests
= kcalloc(chip
->nr_allocated_banks
, sizeof(*digests
),
1214 for (i
= 0; i
< chip
->nr_allocated_banks
; i
++)
1215 digests
[i
].alg_id
= chip
->allocated_banks
[i
].alg_id
;
1220 static int __init
init_trusted(void)
1224 /* encrypted_keys.ko depends on successful load of this module even if
1227 chip
= tpm_default_chip();
1231 ret
= init_digests();
1234 ret
= trusted_shash_alloc();
1237 ret
= register_key_type(&key_type_trusted
);
1242 trusted_shash_release();
1246 put_device(&chip
->dev
);
1250 static void __exit
cleanup_trusted(void)
1253 put_device(&chip
->dev
);
1255 trusted_shash_release();
1256 unregister_key_type(&key_type_trusted
);
1260 late_initcall(init_trusted
);
1261 module_exit(cleanup_trusted
);
1263 MODULE_LICENSE("GPL");