1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 IBM Corporation
4 * Copyright (c) 2019-2021, Linaro Limited
6 * See Documentation/security/keys/trusted-encrypted.rst
9 #include <crypto/hash_info.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/parser.h>
13 #include <linux/string.h>
14 #include <linux/err.h>
15 #include <keys/trusted-type.h>
16 #include <linux/key-type.h>
17 #include <linux/crypto.h>
18 #include <crypto/hash.h>
19 #include <crypto/sha1.h>
20 #include <linux/tpm.h>
21 #include <linux/tpm_command.h>
23 #include <keys/trusted_tpm.h>
25 static const char hmac_alg[] = "hmac(sha1)";
26 static const char hash_alg[] = "sha1";
27 static struct tpm_chip *chip;
28 static struct tpm_digest *digests;
31 struct shash_desc shash;
35 static struct crypto_shash *hashalg;
36 static struct crypto_shash *hmacalg;
38 static struct sdesc *init_sdesc(struct crypto_shash *alg)
43 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
44 sdesc = kmalloc(size, GFP_KERNEL);
46 return ERR_PTR(-ENOMEM);
47 sdesc->shash.tfm = alg;
51 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
52 unsigned char *digest)
57 sdesc = init_sdesc(hashalg);
59 pr_info("can't alloc %s\n", hash_alg);
60 return PTR_ERR(sdesc);
63 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
64 kfree_sensitive(sdesc);
68 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
69 unsigned int keylen, ...)
77 sdesc = init_sdesc(hmacalg);
79 pr_info("can't alloc %s\n", hmac_alg);
80 return PTR_ERR(sdesc);
83 ret = crypto_shash_setkey(hmacalg, key, keylen);
86 ret = crypto_shash_init(&sdesc->shash);
90 va_start(argp, keylen);
92 dlen = va_arg(argp, unsigned int);
95 data = va_arg(argp, unsigned char *);
100 ret = crypto_shash_update(&sdesc->shash, data, dlen);
106 ret = crypto_shash_final(&sdesc->shash, digest);
108 kfree_sensitive(sdesc);
113 * calculate authorization info fields to send to TPM
115 int TSS_authhmac(unsigned char *digest, const unsigned char *key,
116 unsigned int keylen, unsigned char *h1,
117 unsigned char *h2, unsigned int h3, ...)
119 unsigned char paramdigest[SHA1_DIGEST_SIZE];
130 sdesc = init_sdesc(hashalg);
132 pr_info("can't alloc %s\n", hash_alg);
133 return PTR_ERR(sdesc);
137 ret = crypto_shash_init(&sdesc->shash);
142 dlen = va_arg(argp, unsigned int);
145 data = va_arg(argp, unsigned char *);
150 ret = crypto_shash_update(&sdesc->shash, data, dlen);
156 ret = crypto_shash_final(&sdesc->shash, paramdigest);
158 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
159 paramdigest, TPM_NONCE_SIZE, h1,
160 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
162 kfree_sensitive(sdesc);
165 EXPORT_SYMBOL_GPL(TSS_authhmac);
168 * verify the AUTH1_COMMAND (Seal) result from TPM
170 int TSS_checkhmac1(unsigned char *buffer,
171 const uint32_t command,
172 const unsigned char *ononce,
173 const unsigned char *key,
174 unsigned int keylen, ...)
180 unsigned char *enonce;
181 unsigned char *continueflag;
182 unsigned char *authdata;
183 unsigned char testhmac[SHA1_DIGEST_SIZE];
184 unsigned char paramdigest[SHA1_DIGEST_SIZE];
194 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
195 tag = LOAD16(buffer, 0);
197 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
198 if (tag == TPM_TAG_RSP_COMMAND)
200 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
202 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
203 continueflag = authdata - 1;
204 enonce = continueflag - TPM_NONCE_SIZE;
206 sdesc = init_sdesc(hashalg);
208 pr_info("can't alloc %s\n", hash_alg);
209 return PTR_ERR(sdesc);
211 ret = crypto_shash_init(&sdesc->shash);
214 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
218 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
222 va_start(argp, keylen);
224 dlen = va_arg(argp, unsigned int);
227 dpos = va_arg(argp, unsigned int);
228 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
234 ret = crypto_shash_final(&sdesc->shash, paramdigest);
238 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
239 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
240 1, continueflag, 0, 0);
244 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
247 kfree_sensitive(sdesc);
250 EXPORT_SYMBOL_GPL(TSS_checkhmac1);
253 * verify the AUTH2_COMMAND (unseal) result from TPM
255 static int TSS_checkhmac2(unsigned char *buffer,
256 const uint32_t command,
257 const unsigned char *ononce,
258 const unsigned char *key1,
259 unsigned int keylen1,
260 const unsigned char *key2,
261 unsigned int keylen2, ...)
267 unsigned char *enonce1;
268 unsigned char *continueflag1;
269 unsigned char *authdata1;
270 unsigned char *enonce2;
271 unsigned char *continueflag2;
272 unsigned char *authdata2;
273 unsigned char testhmac1[SHA1_DIGEST_SIZE];
274 unsigned char testhmac2[SHA1_DIGEST_SIZE];
275 unsigned char paramdigest[SHA1_DIGEST_SIZE];
282 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
283 tag = LOAD16(buffer, 0);
285 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
287 if (tag == TPM_TAG_RSP_COMMAND)
289 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
291 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
292 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
293 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
294 continueflag1 = authdata1 - 1;
295 continueflag2 = authdata2 - 1;
296 enonce1 = continueflag1 - TPM_NONCE_SIZE;
297 enonce2 = continueflag2 - TPM_NONCE_SIZE;
299 sdesc = init_sdesc(hashalg);
301 pr_info("can't alloc %s\n", hash_alg);
302 return PTR_ERR(sdesc);
304 ret = crypto_shash_init(&sdesc->shash);
307 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
311 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
316 va_start(argp, keylen2);
318 dlen = va_arg(argp, unsigned int);
321 dpos = va_arg(argp, unsigned int);
322 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
328 ret = crypto_shash_final(&sdesc->shash, paramdigest);
332 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
333 paramdigest, TPM_NONCE_SIZE, enonce1,
334 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
337 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
341 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
342 paramdigest, TPM_NONCE_SIZE, enonce2,
343 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
346 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
349 kfree_sensitive(sdesc);
354 * For key specific tpm requests, we will generate and send our
355 * own TPM command packets using the drivers send function.
357 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
365 rc = tpm_send(chip, cmd, buflen);
368 /* Can't return positive return codes values to keyctl */
372 EXPORT_SYMBOL_GPL(trusted_tpm_send);
375 * Lock a trusted key, by extending a selected PCR.
377 * Prevents a trusted key that is sealed to PCRs from being accessed.
378 * This uses the tpm driver's extend function.
380 static int pcrlock(const int pcrnum)
382 if (!capable(CAP_SYS_ADMIN))
385 return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0;
389 * Create an object specific authorisation protocol (OSAP) session
391 static int osap(struct tpm_buf *tb, struct osapsess *s,
392 const unsigned char *key, uint16_t type, uint32_t handle)
394 unsigned char enonce[TPM_NONCE_SIZE];
395 unsigned char ononce[TPM_NONCE_SIZE];
398 ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
402 if (ret != TPM_NONCE_SIZE)
405 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP);
406 tpm_buf_append_u16(tb, type);
407 tpm_buf_append_u32(tb, handle);
408 tpm_buf_append(tb, ononce, TPM_NONCE_SIZE);
410 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
414 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
415 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
417 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
418 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
419 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
420 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
424 * Create an object independent authorisation protocol (oiap) session
426 int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
433 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OIAP);
434 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
438 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
439 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
443 EXPORT_SYMBOL_GPL(oiap);
446 unsigned char encauth[SHA1_DIGEST_SIZE];
447 unsigned char pubauth[SHA1_DIGEST_SIZE];
448 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
449 unsigned char xorhash[SHA1_DIGEST_SIZE];
450 unsigned char nonceodd[TPM_NONCE_SIZE];
454 * Have the TPM seal(encrypt) the trusted key, possibly based on
455 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
457 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
458 uint32_t keyhandle, const unsigned char *keyauth,
459 const unsigned char *data, uint32_t datalen,
460 unsigned char *blob, uint32_t *bloblen,
461 const unsigned char *blobauth,
462 const unsigned char *pcrinfo, uint32_t pcrinfosize)
464 struct osapsess sess;
465 struct tpm_digests *td;
476 /* alloc some work space for all the hashes */
477 td = kmalloc(sizeof *td, GFP_KERNEL);
481 /* get session for sealing key */
482 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
487 /* calculate encrypted authorization value */
488 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
489 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
490 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
494 ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
498 if (ret != TPM_NONCE_SIZE) {
503 ordinal = htonl(TPM_ORD_SEAL);
504 datsize = htonl(datalen);
505 pcrsize = htonl(pcrinfosize);
508 /* encrypt data authorization key */
509 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
510 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
512 /* calculate authorization HMAC value */
513 if (pcrinfosize == 0) {
514 /* no pcr info specified */
515 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
516 sess.enonce, td->nonceodd, cont,
517 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
518 td->encauth, sizeof(uint32_t), &pcrsize,
519 sizeof(uint32_t), &datsize, datalen, data, 0,
522 /* pcr info specified */
523 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
524 sess.enonce, td->nonceodd, cont,
525 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
526 td->encauth, sizeof(uint32_t), &pcrsize,
527 pcrinfosize, pcrinfo, sizeof(uint32_t),
528 &datsize, datalen, data, 0, 0);
533 /* build and send the TPM request packet */
534 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SEAL);
535 tpm_buf_append_u32(tb, keyhandle);
536 tpm_buf_append(tb, td->encauth, SHA1_DIGEST_SIZE);
537 tpm_buf_append_u32(tb, pcrinfosize);
538 tpm_buf_append(tb, pcrinfo, pcrinfosize);
539 tpm_buf_append_u32(tb, datalen);
540 tpm_buf_append(tb, data, datalen);
541 tpm_buf_append_u32(tb, sess.handle);
542 tpm_buf_append(tb, td->nonceodd, TPM_NONCE_SIZE);
543 tpm_buf_append_u8(tb, cont);
544 tpm_buf_append(tb, td->pubauth, SHA1_DIGEST_SIZE);
546 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
550 /* calculate the size of the returned Blob */
551 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
552 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
553 sizeof(uint32_t) + sealinfosize);
554 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
555 sizeof(uint32_t) + encdatasize;
557 /* check the HMAC in the response */
558 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
559 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
562 /* copy the returned blob to caller */
564 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
565 *bloblen = storedsize;
573 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
575 static int tpm_unseal(struct tpm_buf *tb,
576 uint32_t keyhandle, const unsigned char *keyauth,
577 const unsigned char *blob, int bloblen,
578 const unsigned char *blobauth,
579 unsigned char *data, unsigned int *datalen)
581 unsigned char nonceodd[TPM_NONCE_SIZE];
582 unsigned char enonce1[TPM_NONCE_SIZE];
583 unsigned char enonce2[TPM_NONCE_SIZE];
584 unsigned char authdata1[SHA1_DIGEST_SIZE];
585 unsigned char authdata2[SHA1_DIGEST_SIZE];
586 uint32_t authhandle1 = 0;
587 uint32_t authhandle2 = 0;
588 unsigned char cont = 0;
592 /* sessions for unsealing key and data */
593 ret = oiap(tb, &authhandle1, enonce1);
595 pr_info("oiap failed (%d)\n", ret);
598 ret = oiap(tb, &authhandle2, enonce2);
600 pr_info("oiap failed (%d)\n", ret);
604 ordinal = htonl(TPM_ORD_UNSEAL);
605 ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
609 if (ret != TPM_NONCE_SIZE) {
610 pr_info("tpm_get_random failed (%d)\n", ret);
613 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
614 enonce1, nonceodd, cont, sizeof(uint32_t),
615 &ordinal, bloblen, blob, 0, 0);
618 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
619 enonce2, nonceodd, cont, sizeof(uint32_t),
620 &ordinal, bloblen, blob, 0, 0);
624 /* build and send TPM request packet */
625 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH2_COMMAND, TPM_ORD_UNSEAL);
626 tpm_buf_append_u32(tb, keyhandle);
627 tpm_buf_append(tb, blob, bloblen);
628 tpm_buf_append_u32(tb, authhandle1);
629 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
630 tpm_buf_append_u8(tb, cont);
631 tpm_buf_append(tb, authdata1, SHA1_DIGEST_SIZE);
632 tpm_buf_append_u32(tb, authhandle2);
633 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
634 tpm_buf_append_u8(tb, cont);
635 tpm_buf_append(tb, authdata2, SHA1_DIGEST_SIZE);
637 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
639 pr_info("authhmac failed (%d)\n", ret);
643 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
644 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
645 keyauth, SHA1_DIGEST_SIZE,
646 blobauth, SHA1_DIGEST_SIZE,
647 sizeof(uint32_t), TPM_DATA_OFFSET,
648 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
651 pr_info("TSS_checkhmac2 failed (%d)\n", ret);
654 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
659 * Have the TPM seal(encrypt) the symmetric key
661 static int key_seal(struct trusted_key_payload *p,
662 struct trusted_key_options *o)
667 ret = tpm_buf_init(&tb, 0, 0);
671 /* include migratable flag at end of sealed key */
672 p->key[p->key_len] = p->migratable;
674 ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
675 p->key, p->key_len + 1, p->blob, &p->blob_len,
676 o->blobauth, o->pcrinfo, o->pcrinfo_len);
678 pr_info("srkseal failed (%d)\n", ret);
680 tpm_buf_destroy(&tb);
685 * Have the TPM unseal(decrypt) the symmetric key
687 static int key_unseal(struct trusted_key_payload *p,
688 struct trusted_key_options *o)
693 ret = tpm_buf_init(&tb, 0, 0);
697 ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
698 o->blobauth, p->key, &p->key_len);
700 pr_info("srkunseal failed (%d)\n", ret);
702 /* pull migratable flag out of sealed key */
703 p->migratable = p->key[--p->key_len];
705 tpm_buf_destroy(&tb);
711 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
712 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
718 static const match_table_t key_tokens = {
719 {Opt_keyhandle, "keyhandle=%s"},
720 {Opt_keyauth, "keyauth=%s"},
721 {Opt_blobauth, "blobauth=%s"},
722 {Opt_pcrinfo, "pcrinfo=%s"},
723 {Opt_pcrlock, "pcrlock=%s"},
724 {Opt_migratable, "migratable=%s"},
725 {Opt_hash, "hash=%s"},
726 {Opt_policydigest, "policydigest=%s"},
727 {Opt_policyhandle, "policyhandle=%s"},
731 /* can have zero or more token= options */
732 static int getoptions(char *c, struct trusted_key_payload *pay,
733 struct trusted_key_options *opt)
735 substring_t args[MAX_OPT_ARGS];
739 unsigned long handle;
741 unsigned long token_mask = 0;
742 unsigned int digest_len;
746 tpm2 = tpm_is_tpm2(chip);
750 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
755 while ((p = strsep(&c, " \t"))) {
756 if (*p == '\0' || *p == ' ' || *p == '\t')
758 token = match_token(p, key_tokens, args);
759 if (test_and_set_bit(token, &token_mask))
764 opt->pcrinfo_len = strlen(args[0].from) / 2;
765 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
767 res = hex2bin(opt->pcrinfo, args[0].from,
773 res = kstrtoul(args[0].from, 16, &handle);
776 opt->keytype = SEAL_keytype;
777 opt->keyhandle = handle;
780 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
782 res = hex2bin(opt->keyauth, args[0].from,
789 * TPM 1.2 authorizations are sha1 hashes passed in as
790 * hex strings. TPM 2.0 authorizations are simple
791 * passwords (although it can take a hash as well)
793 opt->blobauth_len = strlen(args[0].from);
795 if (opt->blobauth_len == 2 * TPM_DIGEST_SIZE) {
796 res = hex2bin(opt->blobauth, args[0].from,
801 opt->blobauth_len = TPM_DIGEST_SIZE;
805 if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) {
806 memcpy(opt->blobauth, args[0].from,
816 if (*args[0].from == '0')
818 else if (*args[0].from != '1')
822 res = kstrtoul(args[0].from, 10, &lock);
828 if (test_bit(Opt_policydigest, &token_mask))
830 for (i = 0; i < HASH_ALGO__LAST; i++) {
831 if (!strcmp(args[0].from, hash_algo_name[i])) {
836 if (i == HASH_ALGO__LAST)
838 if (!tpm2 && i != HASH_ALGO_SHA1) {
839 pr_info("TPM 1.x only supports SHA-1.\n");
843 case Opt_policydigest:
844 digest_len = hash_digest_size[opt->hash];
845 if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
847 res = hex2bin(opt->policydigest, args[0].from,
851 opt->policydigest_len = digest_len;
853 case Opt_policyhandle:
856 res = kstrtoul(args[0].from, 16, &handle);
859 opt->policyhandle = handle;
868 static struct trusted_key_options *trusted_options_alloc(void)
870 struct trusted_key_options *options;
873 tpm2 = tpm_is_tpm2(chip);
877 options = kzalloc(sizeof *options, GFP_KERNEL);
879 /* set any non-zero defaults */
880 options->keytype = SRK_keytype;
883 options->keyhandle = SRKHANDLE;
888 static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
890 struct trusted_key_options *options = NULL;
894 tpm2 = tpm_is_tpm2(chip);
898 options = trusted_options_alloc();
902 ret = getoptions(datablob, p, options);
905 dump_options(options);
907 if (!options->keyhandle && !tpm2) {
913 ret = tpm2_seal_trusted(chip, p, options);
915 ret = key_seal(p, options);
917 pr_info("key_seal failed (%d)\n", ret);
921 if (options->pcrlock) {
922 ret = pcrlock(options->pcrlock);
924 pr_info("pcrlock failed (%d)\n", ret);
929 kfree_sensitive(options);
933 static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
935 struct trusted_key_options *options = NULL;
939 tpm2 = tpm_is_tpm2(chip);
943 options = trusted_options_alloc();
947 ret = getoptions(datablob, p, options);
950 dump_options(options);
952 if (!options->keyhandle && !tpm2) {
958 ret = tpm2_unseal_trusted(chip, p, options);
960 ret = key_unseal(p, options);
962 pr_info("key_unseal failed (%d)\n", ret);
964 if (options->pcrlock) {
965 ret = pcrlock(options->pcrlock);
967 pr_info("pcrlock failed (%d)\n", ret);
972 kfree_sensitive(options);
976 static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
978 return tpm_get_random(chip, key, key_len);
981 static void trusted_shash_release(void)
984 crypto_free_shash(hashalg);
986 crypto_free_shash(hmacalg);
989 static int __init trusted_shash_alloc(void)
993 hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
994 if (IS_ERR(hmacalg)) {
995 pr_info("could not allocate crypto %s\n",
997 return PTR_ERR(hmacalg);
1000 hashalg = crypto_alloc_shash(hash_alg, 0, 0);
1001 if (IS_ERR(hashalg)) {
1002 pr_info("could not allocate crypto %s\n",
1004 ret = PTR_ERR(hashalg);
1011 crypto_free_shash(hmacalg);
1015 static int __init init_digests(void)
1019 digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests),
1024 for (i = 0; i < chip->nr_allocated_banks; i++)
1025 digests[i].alg_id = chip->allocated_banks[i].alg_id;
1030 static int __init trusted_tpm_init(void)
1034 chip = tpm_default_chip();
1038 ret = init_digests();
1041 ret = trusted_shash_alloc();
1044 ret = register_key_type(&key_type_trusted);
1049 trusted_shash_release();
1053 put_device(&chip->dev);
1057 static void trusted_tpm_exit(void)
1060 put_device(&chip->dev);
1062 trusted_shash_release();
1063 unregister_key_type(&key_type_trusted);
1067 struct trusted_key_ops trusted_key_tpm_ops = {
1068 .migratable = 1, /* migratable by default */
1069 .init = trusted_tpm_init,
1070 .seal = trusted_tpm_seal,
1071 .unseal = trusted_tpm_unseal,
1072 .get_random = trusted_tpm_get_random,
1073 .exit = trusted_tpm_exit,