2 * Copyright (C) 2010 IBM Corporation
5 * David Safford <safford@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
11 * See Documentation/security/keys/trusted-encrypted.rst
14 #include <crypto/hash_info.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/parser.h>
20 #include <linux/string.h>
21 #include <linux/err.h>
22 #include <keys/user-type.h>
23 #include <keys/trusted-type.h>
24 #include <linux/key-type.h>
25 #include <linux/rcupdate.h>
26 #include <linux/crypto.h>
27 #include <crypto/hash.h>
28 #include <crypto/sha.h>
29 #include <linux/capability.h>
30 #include <linux/tpm.h>
31 #include <linux/tpm_command.h>
33 #include <keys/trusted.h>
35 static const char hmac_alg[] = "hmac(sha1)";
36 static const char hash_alg[] = "sha1";
37 static struct tpm_chip *chip;
38 static struct tpm_digest *digests;
41 struct shash_desc shash;
45 static struct crypto_shash *hashalg;
46 static struct crypto_shash *hmacalg;
48 static struct sdesc *init_sdesc(struct crypto_shash *alg)
53 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
54 sdesc = kmalloc(size, GFP_KERNEL);
56 return ERR_PTR(-ENOMEM);
57 sdesc->shash.tfm = alg;
61 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
62 unsigned char *digest)
67 sdesc = init_sdesc(hashalg);
69 pr_info("trusted_key: can't alloc %s\n", hash_alg);
70 return PTR_ERR(sdesc);
73 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
78 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
79 unsigned int keylen, ...)
87 sdesc = init_sdesc(hmacalg);
89 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
90 return PTR_ERR(sdesc);
93 ret = crypto_shash_setkey(hmacalg, key, keylen);
96 ret = crypto_shash_init(&sdesc->shash);
100 va_start(argp, keylen);
102 dlen = va_arg(argp, unsigned int);
105 data = va_arg(argp, unsigned char *);
110 ret = crypto_shash_update(&sdesc->shash, data, dlen);
116 ret = crypto_shash_final(&sdesc->shash, digest);
123 * calculate authorization info fields to send to TPM
125 int TSS_authhmac(unsigned char *digest, const unsigned char *key,
126 unsigned int keylen, unsigned char *h1,
127 unsigned char *h2, unsigned int h3, ...)
129 unsigned char paramdigest[SHA1_DIGEST_SIZE];
140 sdesc = init_sdesc(hashalg);
142 pr_info("trusted_key: can't alloc %s\n", hash_alg);
143 return PTR_ERR(sdesc);
147 ret = crypto_shash_init(&sdesc->shash);
152 dlen = va_arg(argp, unsigned int);
155 data = va_arg(argp, unsigned char *);
160 ret = crypto_shash_update(&sdesc->shash, data, dlen);
166 ret = crypto_shash_final(&sdesc->shash, paramdigest);
168 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
169 paramdigest, TPM_NONCE_SIZE, h1,
170 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
175 EXPORT_SYMBOL_GPL(TSS_authhmac);
178 * verify the AUTH1_COMMAND (Seal) result from TPM
180 int TSS_checkhmac1(unsigned char *buffer,
181 const uint32_t command,
182 const unsigned char *ononce,
183 const unsigned char *key,
184 unsigned int keylen, ...)
190 unsigned char *enonce;
191 unsigned char *continueflag;
192 unsigned char *authdata;
193 unsigned char testhmac[SHA1_DIGEST_SIZE];
194 unsigned char paramdigest[SHA1_DIGEST_SIZE];
204 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
205 tag = LOAD16(buffer, 0);
207 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
208 if (tag == TPM_TAG_RSP_COMMAND)
210 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
212 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
213 continueflag = authdata - 1;
214 enonce = continueflag - TPM_NONCE_SIZE;
216 sdesc = init_sdesc(hashalg);
218 pr_info("trusted_key: can't alloc %s\n", hash_alg);
219 return PTR_ERR(sdesc);
221 ret = crypto_shash_init(&sdesc->shash);
224 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
228 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
232 va_start(argp, keylen);
234 dlen = va_arg(argp, unsigned int);
237 dpos = va_arg(argp, unsigned int);
238 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
244 ret = crypto_shash_final(&sdesc->shash, paramdigest);
248 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
249 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
250 1, continueflag, 0, 0);
254 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
260 EXPORT_SYMBOL_GPL(TSS_checkhmac1);
263 * verify the AUTH2_COMMAND (unseal) result from TPM
265 static int TSS_checkhmac2(unsigned char *buffer,
266 const uint32_t command,
267 const unsigned char *ononce,
268 const unsigned char *key1,
269 unsigned int keylen1,
270 const unsigned char *key2,
271 unsigned int keylen2, ...)
277 unsigned char *enonce1;
278 unsigned char *continueflag1;
279 unsigned char *authdata1;
280 unsigned char *enonce2;
281 unsigned char *continueflag2;
282 unsigned char *authdata2;
283 unsigned char testhmac1[SHA1_DIGEST_SIZE];
284 unsigned char testhmac2[SHA1_DIGEST_SIZE];
285 unsigned char paramdigest[SHA1_DIGEST_SIZE];
292 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
293 tag = LOAD16(buffer, 0);
295 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
297 if (tag == TPM_TAG_RSP_COMMAND)
299 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
301 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
302 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
303 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
304 continueflag1 = authdata1 - 1;
305 continueflag2 = authdata2 - 1;
306 enonce1 = continueflag1 - TPM_NONCE_SIZE;
307 enonce2 = continueflag2 - TPM_NONCE_SIZE;
309 sdesc = init_sdesc(hashalg);
311 pr_info("trusted_key: can't alloc %s\n", hash_alg);
312 return PTR_ERR(sdesc);
314 ret = crypto_shash_init(&sdesc->shash);
317 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
321 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
326 va_start(argp, keylen2);
328 dlen = va_arg(argp, unsigned int);
331 dpos = va_arg(argp, unsigned int);
332 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
338 ret = crypto_shash_final(&sdesc->shash, paramdigest);
342 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
343 paramdigest, TPM_NONCE_SIZE, enonce1,
344 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
347 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
351 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
352 paramdigest, TPM_NONCE_SIZE, enonce2,
353 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
356 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
364 * For key specific tpm requests, we will generate and send our
365 * own TPM command packets using the drivers send function.
367 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
375 rc = tpm_send(chip, cmd, buflen);
378 /* Can't return positive return codes values to keyctl */
382 EXPORT_SYMBOL_GPL(trusted_tpm_send);
385 * Lock a trusted key, by extending a selected PCR.
387 * Prevents a trusted key that is sealed to PCRs from being accessed.
388 * This uses the tpm driver's extend function.
390 static int pcrlock(const int pcrnum)
392 if (!capable(CAP_SYS_ADMIN))
395 return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0;
399 * Create an object specific authorisation protocol (OSAP) session
401 static int osap(struct tpm_buf *tb, struct osapsess *s,
402 const unsigned char *key, uint16_t type, uint32_t handle)
404 unsigned char enonce[TPM_NONCE_SIZE];
405 unsigned char ononce[TPM_NONCE_SIZE];
408 ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
409 if (ret != TPM_NONCE_SIZE)
413 store16(tb, TPM_TAG_RQU_COMMAND);
414 store32(tb, TPM_OSAP_SIZE);
415 store32(tb, TPM_ORD_OSAP);
418 storebytes(tb, ononce, TPM_NONCE_SIZE);
420 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
424 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
425 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
427 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
428 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
429 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
430 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
434 * Create an object independent authorisation protocol (oiap) session
436 int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
444 store16(tb, TPM_TAG_RQU_COMMAND);
445 store32(tb, TPM_OIAP_SIZE);
446 store32(tb, TPM_ORD_OIAP);
447 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
451 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
452 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
456 EXPORT_SYMBOL_GPL(oiap);
459 unsigned char encauth[SHA1_DIGEST_SIZE];
460 unsigned char pubauth[SHA1_DIGEST_SIZE];
461 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
462 unsigned char xorhash[SHA1_DIGEST_SIZE];
463 unsigned char nonceodd[TPM_NONCE_SIZE];
467 * Have the TPM seal(encrypt) the trusted key, possibly based on
468 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
470 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
471 uint32_t keyhandle, const unsigned char *keyauth,
472 const unsigned char *data, uint32_t datalen,
473 unsigned char *blob, uint32_t *bloblen,
474 const unsigned char *blobauth,
475 const unsigned char *pcrinfo, uint32_t pcrinfosize)
477 struct osapsess sess;
478 struct tpm_digests *td;
489 /* alloc some work space for all the hashes */
490 td = kmalloc(sizeof *td, GFP_KERNEL);
494 /* get session for sealing key */
495 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
500 /* calculate encrypted authorization value */
501 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
502 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
503 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
507 ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
508 if (ret != TPM_NONCE_SIZE)
510 ordinal = htonl(TPM_ORD_SEAL);
511 datsize = htonl(datalen);
512 pcrsize = htonl(pcrinfosize);
515 /* encrypt data authorization key */
516 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
517 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
519 /* calculate authorization HMAC value */
520 if (pcrinfosize == 0) {
521 /* no pcr info specified */
522 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
523 sess.enonce, td->nonceodd, cont,
524 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
525 td->encauth, sizeof(uint32_t), &pcrsize,
526 sizeof(uint32_t), &datsize, datalen, data, 0,
529 /* pcr info specified */
530 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
531 sess.enonce, td->nonceodd, cont,
532 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
533 td->encauth, sizeof(uint32_t), &pcrsize,
534 pcrinfosize, pcrinfo, sizeof(uint32_t),
535 &datsize, datalen, data, 0, 0);
540 /* build and send the TPM request packet */
542 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
543 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
544 store32(tb, TPM_ORD_SEAL);
545 store32(tb, keyhandle);
546 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
547 store32(tb, pcrinfosize);
548 storebytes(tb, pcrinfo, pcrinfosize);
549 store32(tb, datalen);
550 storebytes(tb, data, datalen);
551 store32(tb, sess.handle);
552 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
554 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
556 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
560 /* calculate the size of the returned Blob */
561 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
562 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
563 sizeof(uint32_t) + sealinfosize);
564 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
565 sizeof(uint32_t) + encdatasize;
567 /* check the HMAC in the response */
568 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
569 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
572 /* copy the returned blob to caller */
574 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
575 *bloblen = storedsize;
583 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
585 static int tpm_unseal(struct tpm_buf *tb,
586 uint32_t keyhandle, const unsigned char *keyauth,
587 const unsigned char *blob, int bloblen,
588 const unsigned char *blobauth,
589 unsigned char *data, unsigned int *datalen)
591 unsigned char nonceodd[TPM_NONCE_SIZE];
592 unsigned char enonce1[TPM_NONCE_SIZE];
593 unsigned char enonce2[TPM_NONCE_SIZE];
594 unsigned char authdata1[SHA1_DIGEST_SIZE];
595 unsigned char authdata2[SHA1_DIGEST_SIZE];
596 uint32_t authhandle1 = 0;
597 uint32_t authhandle2 = 0;
598 unsigned char cont = 0;
603 /* sessions for unsealing key and data */
604 ret = oiap(tb, &authhandle1, enonce1);
606 pr_info("trusted_key: oiap failed (%d)\n", ret);
609 ret = oiap(tb, &authhandle2, enonce2);
611 pr_info("trusted_key: oiap failed (%d)\n", ret);
615 ordinal = htonl(TPM_ORD_UNSEAL);
616 keyhndl = htonl(SRKHANDLE);
617 ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
618 if (ret != TPM_NONCE_SIZE) {
619 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
622 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
623 enonce1, nonceodd, cont, sizeof(uint32_t),
624 &ordinal, bloblen, blob, 0, 0);
627 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
628 enonce2, nonceodd, cont, sizeof(uint32_t),
629 &ordinal, bloblen, blob, 0, 0);
633 /* build and send TPM request packet */
635 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
636 store32(tb, TPM_UNSEAL_SIZE + bloblen);
637 store32(tb, TPM_ORD_UNSEAL);
638 store32(tb, keyhandle);
639 storebytes(tb, blob, bloblen);
640 store32(tb, authhandle1);
641 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
643 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
644 store32(tb, authhandle2);
645 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
647 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
649 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
651 pr_info("trusted_key: authhmac failed (%d)\n", ret);
655 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
656 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
657 keyauth, SHA1_DIGEST_SIZE,
658 blobauth, SHA1_DIGEST_SIZE,
659 sizeof(uint32_t), TPM_DATA_OFFSET,
660 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
663 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
666 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
671 * Have the TPM seal(encrypt) the symmetric key
673 static int key_seal(struct trusted_key_payload *p,
674 struct trusted_key_options *o)
679 tb = kzalloc(sizeof *tb, GFP_KERNEL);
683 /* include migratable flag at end of sealed key */
684 p->key[p->key_len] = p->migratable;
686 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
687 p->key, p->key_len + 1, p->blob, &p->blob_len,
688 o->blobauth, o->pcrinfo, o->pcrinfo_len);
690 pr_info("trusted_key: srkseal failed (%d)\n", ret);
697 * Have the TPM unseal(decrypt) the symmetric key
699 static int key_unseal(struct trusted_key_payload *p,
700 struct trusted_key_options *o)
705 tb = kzalloc(sizeof *tb, GFP_KERNEL);
709 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
710 o->blobauth, p->key, &p->key_len);
712 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
714 /* pull migratable flag out of sealed key */
715 p->migratable = p->key[--p->key_len];
723 Opt_new, Opt_load, Opt_update,
724 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
725 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
731 static const match_table_t key_tokens = {
734 {Opt_update, "update"},
735 {Opt_keyhandle, "keyhandle=%s"},
736 {Opt_keyauth, "keyauth=%s"},
737 {Opt_blobauth, "blobauth=%s"},
738 {Opt_pcrinfo, "pcrinfo=%s"},
739 {Opt_pcrlock, "pcrlock=%s"},
740 {Opt_migratable, "migratable=%s"},
741 {Opt_hash, "hash=%s"},
742 {Opt_policydigest, "policydigest=%s"},
743 {Opt_policyhandle, "policyhandle=%s"},
747 /* can have zero or more token= options */
748 static int getoptions(char *c, struct trusted_key_payload *pay,
749 struct trusted_key_options *opt)
751 substring_t args[MAX_OPT_ARGS];
755 unsigned long handle;
757 unsigned long token_mask = 0;
758 unsigned int digest_len;
762 tpm2 = tpm_is_tpm2(chip);
766 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
768 while ((p = strsep(&c, " \t"))) {
769 if (*p == '\0' || *p == ' ' || *p == '\t')
771 token = match_token(p, key_tokens, args);
772 if (test_and_set_bit(token, &token_mask))
777 opt->pcrinfo_len = strlen(args[0].from) / 2;
778 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
780 res = hex2bin(opt->pcrinfo, args[0].from,
786 res = kstrtoul(args[0].from, 16, &handle);
789 opt->keytype = SEAL_keytype;
790 opt->keyhandle = handle;
793 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
795 res = hex2bin(opt->keyauth, args[0].from,
801 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
803 res = hex2bin(opt->blobauth, args[0].from,
809 if (*args[0].from == '0')
815 res = kstrtoul(args[0].from, 10, &lock);
821 if (test_bit(Opt_policydigest, &token_mask))
823 for (i = 0; i < HASH_ALGO__LAST; i++) {
824 if (!strcmp(args[0].from, hash_algo_name[i])) {
829 if (i == HASH_ALGO__LAST)
831 if (!tpm2 && i != HASH_ALGO_SHA1) {
832 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
836 case Opt_policydigest:
837 digest_len = hash_digest_size[opt->hash];
838 if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
840 res = hex2bin(opt->policydigest, args[0].from,
844 opt->policydigest_len = digest_len;
846 case Opt_policyhandle:
849 res = kstrtoul(args[0].from, 16, &handle);
852 opt->policyhandle = handle;
862 * datablob_parse - parse the keyctl data and fill in the
863 * payload and options structures
865 * On success returns 0, otherwise -EINVAL.
867 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
868 struct trusted_key_options *o)
870 substring_t args[MAX_OPT_ARGS];
877 c = strsep(&datablob, " \t");
880 key_cmd = match_token(c, key_tokens, args);
883 /* first argument is key size */
884 c = strsep(&datablob, " \t");
887 ret = kstrtol(c, 10, &keylen);
888 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
891 ret = getoptions(datablob, p, o);
897 /* first argument is sealed blob */
898 c = strsep(&datablob, " \t");
901 p->blob_len = strlen(c) / 2;
902 if (p->blob_len > MAX_BLOB_SIZE)
904 ret = hex2bin(p->blob, c, p->blob_len);
907 ret = getoptions(datablob, p, o);
913 /* all arguments are options */
914 ret = getoptions(datablob, p, o);
926 static struct trusted_key_options *trusted_options_alloc(void)
928 struct trusted_key_options *options;
931 tpm2 = tpm_is_tpm2(chip);
935 options = kzalloc(sizeof *options, GFP_KERNEL);
937 /* set any non-zero defaults */
938 options->keytype = SRK_keytype;
941 options->keyhandle = SRKHANDLE;
946 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
948 struct trusted_key_payload *p = NULL;
951 ret = key_payload_reserve(key, sizeof *p);
954 p = kzalloc(sizeof *p, GFP_KERNEL);
956 p->migratable = 1; /* migratable by default */
961 * trusted_instantiate - create a new trusted key
963 * Unseal an existing trusted blob or, for a new key, get a
964 * random key, then seal and create a trusted key-type key,
965 * adding it to the specified keyring.
967 * On success, return 0. Otherwise return errno.
969 static int trusted_instantiate(struct key *key,
970 struct key_preparsed_payload *prep)
972 struct trusted_key_payload *payload = NULL;
973 struct trusted_key_options *options = NULL;
974 size_t datalen = prep->datalen;
981 tpm2 = tpm_is_tpm2(chip);
985 if (datalen <= 0 || datalen > 32767 || !prep->data)
988 datablob = kmalloc(datalen + 1, GFP_KERNEL);
991 memcpy(datablob, prep->data, datalen);
992 datablob[datalen] = '\0';
994 options = trusted_options_alloc();
999 payload = trusted_payload_alloc(key);
1005 key_cmd = datablob_parse(datablob, payload, options);
1011 if (!options->keyhandle) {
1016 dump_payload(payload);
1017 dump_options(options);
1022 ret = tpm_unseal_trusted(chip, payload, options);
1024 ret = key_unseal(payload, options);
1025 dump_payload(payload);
1026 dump_options(options);
1028 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
1031 key_len = payload->key_len;
1032 ret = tpm_get_random(chip, payload->key, key_len);
1033 if (ret != key_len) {
1034 pr_info("trusted_key: key_create failed (%d)\n", ret);
1038 ret = tpm_seal_trusted(chip, payload, options);
1040 ret = key_seal(payload, options);
1042 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1048 if (!ret && options->pcrlock)
1049 ret = pcrlock(options->pcrlock);
1054 rcu_assign_keypointer(key, payload);
1060 static void trusted_rcu_free(struct rcu_head *rcu)
1062 struct trusted_key_payload *p;
1064 p = container_of(rcu, struct trusted_key_payload, rcu);
1069 * trusted_update - reseal an existing key with new PCR values
1071 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
1073 struct trusted_key_payload *p;
1074 struct trusted_key_payload *new_p;
1075 struct trusted_key_options *new_o;
1076 size_t datalen = prep->datalen;
1080 if (key_is_negative(key))
1082 p = key->payload.data[0];
1085 if (datalen <= 0 || datalen > 32767 || !prep->data)
1088 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1091 new_o = trusted_options_alloc();
1096 new_p = trusted_payload_alloc(key);
1102 memcpy(datablob, prep->data, datalen);
1103 datablob[datalen] = '\0';
1104 ret = datablob_parse(datablob, new_p, new_o);
1105 if (ret != Opt_update) {
1111 if (!new_o->keyhandle) {
1117 /* copy old key values, and reseal with new pcrs */
1118 new_p->migratable = p->migratable;
1119 new_p->key_len = p->key_len;
1120 memcpy(new_p->key, p->key, p->key_len);
1122 dump_payload(new_p);
1124 ret = key_seal(new_p, new_o);
1126 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1130 if (new_o->pcrlock) {
1131 ret = pcrlock(new_o->pcrlock);
1133 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1138 rcu_assign_keypointer(key, new_p);
1139 call_rcu(&p->rcu, trusted_rcu_free);
1147 * trusted_read - copy the sealed blob data to userspace in hex.
1148 * On success, return to userspace the trusted key datablob size.
1150 static long trusted_read(const struct key *key, char __user *buffer,
1153 const struct trusted_key_payload *p;
1158 p = dereference_key_locked(key);
1162 if (buffer && buflen >= 2 * p->blob_len) {
1163 ascii_buf = kmalloc_array(2, p->blob_len, GFP_KERNEL);
1168 for (i = 0; i < p->blob_len; i++)
1169 bufp = hex_byte_pack(bufp, p->blob[i]);
1170 if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
1176 return 2 * p->blob_len;
1180 * trusted_destroy - clear and free the key's payload
1182 static void trusted_destroy(struct key *key)
1184 kzfree(key->payload.data[0]);
1187 struct key_type key_type_trusted = {
1189 .instantiate = trusted_instantiate,
1190 .update = trusted_update,
1191 .destroy = trusted_destroy,
1192 .describe = user_describe,
1193 .read = trusted_read,
1196 EXPORT_SYMBOL_GPL(key_type_trusted);
1198 static void trusted_shash_release(void)
1201 crypto_free_shash(hashalg);
1203 crypto_free_shash(hmacalg);
1206 static int __init trusted_shash_alloc(void)
1210 hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
1211 if (IS_ERR(hmacalg)) {
1212 pr_info("trusted_key: could not allocate crypto %s\n",
1214 return PTR_ERR(hmacalg);
1217 hashalg = crypto_alloc_shash(hash_alg, 0, 0);
1218 if (IS_ERR(hashalg)) {
1219 pr_info("trusted_key: could not allocate crypto %s\n",
1221 ret = PTR_ERR(hashalg);
1228 crypto_free_shash(hmacalg);
1232 static int __init init_digests(void)
1234 u8 digest[TPM_MAX_DIGEST_SIZE];
1238 ret = tpm_get_random(chip, digest, TPM_MAX_DIGEST_SIZE);
1241 if (ret < TPM_MAX_DIGEST_SIZE)
1244 digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests),
1249 for (i = 0; i < chip->nr_allocated_banks; i++)
1250 memcpy(digests[i].digest, digest, TPM_MAX_DIGEST_SIZE);
1255 static int __init init_trusted(void)
1259 /* encrypted_keys.ko depends on successful load of this module even if
1262 chip = tpm_default_chip();
1266 ret = init_digests();
1269 ret = trusted_shash_alloc();
1272 ret = register_key_type(&key_type_trusted);
1277 trusted_shash_release();
1281 put_device(&chip->dev);
1285 static void __exit cleanup_trusted(void)
1288 put_device(&chip->dev);
1290 trusted_shash_release();
1291 unregister_key_type(&key_type_trusted);
1295 late_initcall(init_trusted);
1296 module_exit(cleanup_trusted);
1298 MODULE_LICENSE("GPL");