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;
58 sdesc->shash.flags = 0x0;
62 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
63 unsigned char *digest)
68 sdesc = init_sdesc(hashalg);
70 pr_info("trusted_key: can't alloc %s\n", hash_alg);
71 return PTR_ERR(sdesc);
74 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
79 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
80 unsigned int keylen, ...)
88 sdesc = init_sdesc(hmacalg);
90 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
91 return PTR_ERR(sdesc);
94 ret = crypto_shash_setkey(hmacalg, key, keylen);
97 ret = crypto_shash_init(&sdesc->shash);
101 va_start(argp, keylen);
103 dlen = va_arg(argp, unsigned int);
106 data = va_arg(argp, unsigned char *);
111 ret = crypto_shash_update(&sdesc->shash, data, dlen);
117 ret = crypto_shash_final(&sdesc->shash, digest);
124 * calculate authorization info fields to send to TPM
126 int TSS_authhmac(unsigned char *digest, const unsigned char *key,
127 unsigned int keylen, unsigned char *h1,
128 unsigned char *h2, unsigned int h3, ...)
130 unsigned char paramdigest[SHA1_DIGEST_SIZE];
141 sdesc = init_sdesc(hashalg);
143 pr_info("trusted_key: can't alloc %s\n", hash_alg);
144 return PTR_ERR(sdesc);
148 ret = crypto_shash_init(&sdesc->shash);
153 dlen = va_arg(argp, unsigned int);
156 data = va_arg(argp, unsigned char *);
161 ret = crypto_shash_update(&sdesc->shash, data, dlen);
167 ret = crypto_shash_final(&sdesc->shash, paramdigest);
169 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
170 paramdigest, TPM_NONCE_SIZE, h1,
171 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
176 EXPORT_SYMBOL_GPL(TSS_authhmac);
179 * verify the AUTH1_COMMAND (Seal) result from TPM
181 int TSS_checkhmac1(unsigned char *buffer,
182 const uint32_t command,
183 const unsigned char *ononce,
184 const unsigned char *key,
185 unsigned int keylen, ...)
191 unsigned char *enonce;
192 unsigned char *continueflag;
193 unsigned char *authdata;
194 unsigned char testhmac[SHA1_DIGEST_SIZE];
195 unsigned char paramdigest[SHA1_DIGEST_SIZE];
205 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
206 tag = LOAD16(buffer, 0);
208 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
209 if (tag == TPM_TAG_RSP_COMMAND)
211 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
213 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
214 continueflag = authdata - 1;
215 enonce = continueflag - TPM_NONCE_SIZE;
217 sdesc = init_sdesc(hashalg);
219 pr_info("trusted_key: can't alloc %s\n", hash_alg);
220 return PTR_ERR(sdesc);
222 ret = crypto_shash_init(&sdesc->shash);
225 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
229 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
233 va_start(argp, keylen);
235 dlen = va_arg(argp, unsigned int);
238 dpos = va_arg(argp, unsigned int);
239 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
245 ret = crypto_shash_final(&sdesc->shash, paramdigest);
249 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
250 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
251 1, continueflag, 0, 0);
255 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
261 EXPORT_SYMBOL_GPL(TSS_checkhmac1);
264 * verify the AUTH2_COMMAND (unseal) result from TPM
266 static int TSS_checkhmac2(unsigned char *buffer,
267 const uint32_t command,
268 const unsigned char *ononce,
269 const unsigned char *key1,
270 unsigned int keylen1,
271 const unsigned char *key2,
272 unsigned int keylen2, ...)
278 unsigned char *enonce1;
279 unsigned char *continueflag1;
280 unsigned char *authdata1;
281 unsigned char *enonce2;
282 unsigned char *continueflag2;
283 unsigned char *authdata2;
284 unsigned char testhmac1[SHA1_DIGEST_SIZE];
285 unsigned char testhmac2[SHA1_DIGEST_SIZE];
286 unsigned char paramdigest[SHA1_DIGEST_SIZE];
293 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
294 tag = LOAD16(buffer, 0);
296 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
298 if (tag == TPM_TAG_RSP_COMMAND)
300 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
302 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
303 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
304 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
305 continueflag1 = authdata1 - 1;
306 continueflag2 = authdata2 - 1;
307 enonce1 = continueflag1 - TPM_NONCE_SIZE;
308 enonce2 = continueflag2 - TPM_NONCE_SIZE;
310 sdesc = init_sdesc(hashalg);
312 pr_info("trusted_key: can't alloc %s\n", hash_alg);
313 return PTR_ERR(sdesc);
315 ret = crypto_shash_init(&sdesc->shash);
318 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
322 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
327 va_start(argp, keylen2);
329 dlen = va_arg(argp, unsigned int);
332 dpos = va_arg(argp, unsigned int);
333 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
339 ret = crypto_shash_final(&sdesc->shash, paramdigest);
343 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
344 paramdigest, TPM_NONCE_SIZE, enonce1,
345 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
348 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
352 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
353 paramdigest, TPM_NONCE_SIZE, enonce2,
354 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
357 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
365 * For key specific tpm requests, we will generate and send our
366 * own TPM command packets using the drivers send function.
368 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
376 rc = tpm_send(chip, cmd, buflen);
379 /* Can't return positive return codes values to keyctl */
383 EXPORT_SYMBOL_GPL(trusted_tpm_send);
386 * Lock a trusted key, by extending a selected PCR.
388 * Prevents a trusted key that is sealed to PCRs from being accessed.
389 * This uses the tpm driver's extend function.
391 static int pcrlock(const int pcrnum)
393 if (!capable(CAP_SYS_ADMIN))
396 return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0;
400 * Create an object specific authorisation protocol (OSAP) session
402 static int osap(struct tpm_buf *tb, struct osapsess *s,
403 const unsigned char *key, uint16_t type, uint32_t handle)
405 unsigned char enonce[TPM_NONCE_SIZE];
406 unsigned char ononce[TPM_NONCE_SIZE];
409 ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
410 if (ret != TPM_NONCE_SIZE)
414 store16(tb, TPM_TAG_RQU_COMMAND);
415 store32(tb, TPM_OSAP_SIZE);
416 store32(tb, TPM_ORD_OSAP);
419 storebytes(tb, ononce, TPM_NONCE_SIZE);
421 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
425 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
426 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
428 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
429 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
430 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
431 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
435 * Create an object independent authorisation protocol (oiap) session
437 int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
445 store16(tb, TPM_TAG_RQU_COMMAND);
446 store32(tb, TPM_OIAP_SIZE);
447 store32(tb, TPM_ORD_OIAP);
448 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
452 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
453 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
457 EXPORT_SYMBOL_GPL(oiap);
460 unsigned char encauth[SHA1_DIGEST_SIZE];
461 unsigned char pubauth[SHA1_DIGEST_SIZE];
462 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
463 unsigned char xorhash[SHA1_DIGEST_SIZE];
464 unsigned char nonceodd[TPM_NONCE_SIZE];
468 * Have the TPM seal(encrypt) the trusted key, possibly based on
469 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
471 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
472 uint32_t keyhandle, const unsigned char *keyauth,
473 const unsigned char *data, uint32_t datalen,
474 unsigned char *blob, uint32_t *bloblen,
475 const unsigned char *blobauth,
476 const unsigned char *pcrinfo, uint32_t pcrinfosize)
478 struct osapsess sess;
479 struct tpm_digests *td;
490 /* alloc some work space for all the hashes */
491 td = kmalloc(sizeof *td, GFP_KERNEL);
495 /* get session for sealing key */
496 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
501 /* calculate encrypted authorization value */
502 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
503 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
504 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
508 ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
509 if (ret != TPM_NONCE_SIZE)
511 ordinal = htonl(TPM_ORD_SEAL);
512 datsize = htonl(datalen);
513 pcrsize = htonl(pcrinfosize);
516 /* encrypt data authorization key */
517 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
518 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
520 /* calculate authorization HMAC value */
521 if (pcrinfosize == 0) {
522 /* no 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 sizeof(uint32_t), &datsize, datalen, data, 0,
530 /* pcr info specified */
531 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
532 sess.enonce, td->nonceodd, cont,
533 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
534 td->encauth, sizeof(uint32_t), &pcrsize,
535 pcrinfosize, pcrinfo, sizeof(uint32_t),
536 &datsize, datalen, data, 0, 0);
541 /* build and send the TPM request packet */
543 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
544 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
545 store32(tb, TPM_ORD_SEAL);
546 store32(tb, keyhandle);
547 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
548 store32(tb, pcrinfosize);
549 storebytes(tb, pcrinfo, pcrinfosize);
550 store32(tb, datalen);
551 storebytes(tb, data, datalen);
552 store32(tb, sess.handle);
553 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
555 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
557 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
561 /* calculate the size of the returned Blob */
562 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
563 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
564 sizeof(uint32_t) + sealinfosize);
565 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
566 sizeof(uint32_t) + encdatasize;
568 /* check the HMAC in the response */
569 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
570 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
573 /* copy the returned blob to caller */
575 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
576 *bloblen = storedsize;
584 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
586 static int tpm_unseal(struct tpm_buf *tb,
587 uint32_t keyhandle, const unsigned char *keyauth,
588 const unsigned char *blob, int bloblen,
589 const unsigned char *blobauth,
590 unsigned char *data, unsigned int *datalen)
592 unsigned char nonceodd[TPM_NONCE_SIZE];
593 unsigned char enonce1[TPM_NONCE_SIZE];
594 unsigned char enonce2[TPM_NONCE_SIZE];
595 unsigned char authdata1[SHA1_DIGEST_SIZE];
596 unsigned char authdata2[SHA1_DIGEST_SIZE];
597 uint32_t authhandle1 = 0;
598 uint32_t authhandle2 = 0;
599 unsigned char cont = 0;
604 /* sessions for unsealing key and data */
605 ret = oiap(tb, &authhandle1, enonce1);
607 pr_info("trusted_key: oiap failed (%d)\n", ret);
610 ret = oiap(tb, &authhandle2, enonce2);
612 pr_info("trusted_key: oiap failed (%d)\n", ret);
616 ordinal = htonl(TPM_ORD_UNSEAL);
617 keyhndl = htonl(SRKHANDLE);
618 ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
619 if (ret != TPM_NONCE_SIZE) {
620 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
623 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
624 enonce1, nonceodd, cont, sizeof(uint32_t),
625 &ordinal, bloblen, blob, 0, 0);
628 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
629 enonce2, nonceodd, cont, sizeof(uint32_t),
630 &ordinal, bloblen, blob, 0, 0);
634 /* build and send TPM request packet */
636 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
637 store32(tb, TPM_UNSEAL_SIZE + bloblen);
638 store32(tb, TPM_ORD_UNSEAL);
639 store32(tb, keyhandle);
640 storebytes(tb, blob, bloblen);
641 store32(tb, authhandle1);
642 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
644 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
645 store32(tb, authhandle2);
646 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
648 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
650 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
652 pr_info("trusted_key: authhmac failed (%d)\n", ret);
656 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
657 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
658 keyauth, SHA1_DIGEST_SIZE,
659 blobauth, SHA1_DIGEST_SIZE,
660 sizeof(uint32_t), TPM_DATA_OFFSET,
661 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
664 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
667 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
672 * Have the TPM seal(encrypt) the symmetric key
674 static int key_seal(struct trusted_key_payload *p,
675 struct trusted_key_options *o)
680 tb = kzalloc(sizeof *tb, GFP_KERNEL);
684 /* include migratable flag at end of sealed key */
685 p->key[p->key_len] = p->migratable;
687 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
688 p->key, p->key_len + 1, p->blob, &p->blob_len,
689 o->blobauth, o->pcrinfo, o->pcrinfo_len);
691 pr_info("trusted_key: srkseal failed (%d)\n", ret);
698 * Have the TPM unseal(decrypt) the symmetric key
700 static int key_unseal(struct trusted_key_payload *p,
701 struct trusted_key_options *o)
706 tb = kzalloc(sizeof *tb, GFP_KERNEL);
710 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
711 o->blobauth, p->key, &p->key_len);
713 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
715 /* pull migratable flag out of sealed key */
716 p->migratable = p->key[--p->key_len];
724 Opt_new, Opt_load, Opt_update,
725 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
726 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
732 static const match_table_t key_tokens = {
735 {Opt_update, "update"},
736 {Opt_keyhandle, "keyhandle=%s"},
737 {Opt_keyauth, "keyauth=%s"},
738 {Opt_blobauth, "blobauth=%s"},
739 {Opt_pcrinfo, "pcrinfo=%s"},
740 {Opt_pcrlock, "pcrlock=%s"},
741 {Opt_migratable, "migratable=%s"},
742 {Opt_hash, "hash=%s"},
743 {Opt_policydigest, "policydigest=%s"},
744 {Opt_policyhandle, "policyhandle=%s"},
748 /* can have zero or more token= options */
749 static int getoptions(char *c, struct trusted_key_payload *pay,
750 struct trusted_key_options *opt)
752 substring_t args[MAX_OPT_ARGS];
756 unsigned long handle;
758 unsigned long token_mask = 0;
759 unsigned int digest_len;
763 tpm2 = tpm_is_tpm2(chip);
767 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
769 while ((p = strsep(&c, " \t"))) {
770 if (*p == '\0' || *p == ' ' || *p == '\t')
772 token = match_token(p, key_tokens, args);
773 if (test_and_set_bit(token, &token_mask))
778 opt->pcrinfo_len = strlen(args[0].from) / 2;
779 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
781 res = hex2bin(opt->pcrinfo, args[0].from,
787 res = kstrtoul(args[0].from, 16, &handle);
790 opt->keytype = SEAL_keytype;
791 opt->keyhandle = handle;
794 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
796 res = hex2bin(opt->keyauth, args[0].from,
802 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
804 res = hex2bin(opt->blobauth, args[0].from,
810 if (*args[0].from == '0')
816 res = kstrtoul(args[0].from, 10, &lock);
822 if (test_bit(Opt_policydigest, &token_mask))
824 for (i = 0; i < HASH_ALGO__LAST; i++) {
825 if (!strcmp(args[0].from, hash_algo_name[i])) {
830 if (i == HASH_ALGO__LAST)
832 if (!tpm2 && i != HASH_ALGO_SHA1) {
833 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
837 case Opt_policydigest:
838 digest_len = hash_digest_size[opt->hash];
839 if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
841 res = hex2bin(opt->policydigest, args[0].from,
845 opt->policydigest_len = digest_len;
847 case Opt_policyhandle:
850 res = kstrtoul(args[0].from, 16, &handle);
853 opt->policyhandle = handle;
863 * datablob_parse - parse the keyctl data and fill in the
864 * payload and options structures
866 * On success returns 0, otherwise -EINVAL.
868 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
869 struct trusted_key_options *o)
871 substring_t args[MAX_OPT_ARGS];
878 c = strsep(&datablob, " \t");
881 key_cmd = match_token(c, key_tokens, args);
884 /* first argument is key size */
885 c = strsep(&datablob, " \t");
888 ret = kstrtol(c, 10, &keylen);
889 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
892 ret = getoptions(datablob, p, o);
898 /* first argument is sealed blob */
899 c = strsep(&datablob, " \t");
902 p->blob_len = strlen(c) / 2;
903 if (p->blob_len > MAX_BLOB_SIZE)
905 ret = hex2bin(p->blob, c, p->blob_len);
908 ret = getoptions(datablob, p, o);
914 /* all arguments are options */
915 ret = getoptions(datablob, p, o);
927 static struct trusted_key_options *trusted_options_alloc(void)
929 struct trusted_key_options *options;
932 tpm2 = tpm_is_tpm2(chip);
936 options = kzalloc(sizeof *options, GFP_KERNEL);
938 /* set any non-zero defaults */
939 options->keytype = SRK_keytype;
942 options->keyhandle = SRKHANDLE;
947 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
949 struct trusted_key_payload *p = NULL;
952 ret = key_payload_reserve(key, sizeof *p);
955 p = kzalloc(sizeof *p, GFP_KERNEL);
957 p->migratable = 1; /* migratable by default */
962 * trusted_instantiate - create a new trusted key
964 * Unseal an existing trusted blob or, for a new key, get a
965 * random key, then seal and create a trusted key-type key,
966 * adding it to the specified keyring.
968 * On success, return 0. Otherwise return errno.
970 static int trusted_instantiate(struct key *key,
971 struct key_preparsed_payload *prep)
973 struct trusted_key_payload *payload = NULL;
974 struct trusted_key_options *options = NULL;
975 size_t datalen = prep->datalen;
982 tpm2 = tpm_is_tpm2(chip);
986 if (datalen <= 0 || datalen > 32767 || !prep->data)
989 datablob = kmalloc(datalen + 1, GFP_KERNEL);
992 memcpy(datablob, prep->data, datalen);
993 datablob[datalen] = '\0';
995 options = trusted_options_alloc();
1000 payload = trusted_payload_alloc(key);
1006 key_cmd = datablob_parse(datablob, payload, options);
1012 if (!options->keyhandle) {
1017 dump_payload(payload);
1018 dump_options(options);
1023 ret = tpm_unseal_trusted(chip, payload, options);
1025 ret = key_unseal(payload, options);
1026 dump_payload(payload);
1027 dump_options(options);
1029 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
1032 key_len = payload->key_len;
1033 ret = tpm_get_random(chip, payload->key, key_len);
1034 if (ret != key_len) {
1035 pr_info("trusted_key: key_create failed (%d)\n", ret);
1039 ret = tpm_seal_trusted(chip, payload, options);
1041 ret = key_seal(payload, options);
1043 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1049 if (!ret && options->pcrlock)
1050 ret = pcrlock(options->pcrlock);
1055 rcu_assign_keypointer(key, payload);
1061 static void trusted_rcu_free(struct rcu_head *rcu)
1063 struct trusted_key_payload *p;
1065 p = container_of(rcu, struct trusted_key_payload, rcu);
1070 * trusted_update - reseal an existing key with new PCR values
1072 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
1074 struct trusted_key_payload *p;
1075 struct trusted_key_payload *new_p;
1076 struct trusted_key_options *new_o;
1077 size_t datalen = prep->datalen;
1081 if (key_is_negative(key))
1083 p = key->payload.data[0];
1086 if (datalen <= 0 || datalen > 32767 || !prep->data)
1089 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1092 new_o = trusted_options_alloc();
1097 new_p = trusted_payload_alloc(key);
1103 memcpy(datablob, prep->data, datalen);
1104 datablob[datalen] = '\0';
1105 ret = datablob_parse(datablob, new_p, new_o);
1106 if (ret != Opt_update) {
1112 if (!new_o->keyhandle) {
1118 /* copy old key values, and reseal with new pcrs */
1119 new_p->migratable = p->migratable;
1120 new_p->key_len = p->key_len;
1121 memcpy(new_p->key, p->key, p->key_len);
1123 dump_payload(new_p);
1125 ret = key_seal(new_p, new_o);
1127 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1131 if (new_o->pcrlock) {
1132 ret = pcrlock(new_o->pcrlock);
1134 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1139 rcu_assign_keypointer(key, new_p);
1140 call_rcu(&p->rcu, trusted_rcu_free);
1148 * trusted_read - copy the sealed blob data to userspace in hex.
1149 * On success, return to userspace the trusted key datablob size.
1151 static long trusted_read(const struct key *key, char __user *buffer,
1154 const struct trusted_key_payload *p;
1159 p = dereference_key_locked(key);
1163 if (buffer && buflen >= 2 * p->blob_len) {
1164 ascii_buf = kmalloc_array(2, p->blob_len, GFP_KERNEL);
1169 for (i = 0; i < p->blob_len; i++)
1170 bufp = hex_byte_pack(bufp, p->blob[i]);
1171 if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
1177 return 2 * p->blob_len;
1181 * trusted_destroy - clear and free the key's payload
1183 static void trusted_destroy(struct key *key)
1185 kzfree(key->payload.data[0]);
1188 struct key_type key_type_trusted = {
1190 .instantiate = trusted_instantiate,
1191 .update = trusted_update,
1192 .destroy = trusted_destroy,
1193 .describe = user_describe,
1194 .read = trusted_read,
1197 EXPORT_SYMBOL_GPL(key_type_trusted);
1199 static void trusted_shash_release(void)
1202 crypto_free_shash(hashalg);
1204 crypto_free_shash(hmacalg);
1207 static int __init trusted_shash_alloc(void)
1211 hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
1212 if (IS_ERR(hmacalg)) {
1213 pr_info("trusted_key: could not allocate crypto %s\n",
1215 return PTR_ERR(hmacalg);
1218 hashalg = crypto_alloc_shash(hash_alg, 0, 0);
1219 if (IS_ERR(hashalg)) {
1220 pr_info("trusted_key: could not allocate crypto %s\n",
1222 ret = PTR_ERR(hashalg);
1229 crypto_free_shash(hmacalg);
1233 static int __init init_digests(void)
1235 u8 digest[TPM_MAX_DIGEST_SIZE];
1239 ret = tpm_get_random(chip, digest, TPM_MAX_DIGEST_SIZE);
1242 if (ret < TPM_MAX_DIGEST_SIZE)
1245 digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests),
1250 for (i = 0; i < chip->nr_allocated_banks; i++)
1251 memcpy(digests[i].digest, digest, TPM_MAX_DIGEST_SIZE);
1256 static int __init init_trusted(void)
1260 /* encrypted_keys.ko depends on successful load of this module even if
1263 chip = tpm_default_chip();
1267 ret = init_digests();
1270 ret = trusted_shash_alloc();
1273 ret = register_key_type(&key_type_trusted);
1278 trusted_shash_release();
1282 put_device(&chip->dev);
1286 static void __exit cleanup_trusted(void)
1289 put_device(&chip->dev);
1291 trusted_shash_release();
1292 unregister_key_type(&key_type_trusted);
1296 late_initcall(init_trusted);
1297 module_exit(cleanup_trusted);
1299 MODULE_LICENSE("GPL");