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>
35 static const char hmac_alg[] = "hmac(sha1)";
36 static const char hash_alg[] = "sha1";
39 struct shash_desc shash;
43 static struct crypto_shash *hashalg;
44 static struct crypto_shash *hmacalg;
46 static struct sdesc *init_sdesc(struct crypto_shash *alg)
51 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
52 sdesc = kmalloc(size, GFP_KERNEL);
54 return ERR_PTR(-ENOMEM);
55 sdesc->shash.tfm = alg;
56 sdesc->shash.flags = 0x0;
60 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
61 unsigned char *digest)
66 sdesc = init_sdesc(hashalg);
68 pr_info("trusted_key: can't alloc %s\n", hash_alg);
69 return PTR_ERR(sdesc);
72 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
77 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
78 unsigned int keylen, ...)
86 sdesc = init_sdesc(hmacalg);
88 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
89 return PTR_ERR(sdesc);
92 ret = crypto_shash_setkey(hmacalg, key, keylen);
95 ret = crypto_shash_init(&sdesc->shash);
99 va_start(argp, keylen);
101 dlen = va_arg(argp, unsigned int);
104 data = va_arg(argp, unsigned char *);
109 ret = crypto_shash_update(&sdesc->shash, data, dlen);
115 ret = crypto_shash_final(&sdesc->shash, digest);
122 * calculate authorization info fields to send to TPM
124 static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
125 unsigned int keylen, unsigned char *h1,
126 unsigned char *h2, unsigned char h3, ...)
128 unsigned char paramdigest[SHA1_DIGEST_SIZE];
136 sdesc = init_sdesc(hashalg);
138 pr_info("trusted_key: can't alloc %s\n", hash_alg);
139 return PTR_ERR(sdesc);
143 ret = crypto_shash_init(&sdesc->shash);
148 dlen = va_arg(argp, unsigned int);
151 data = va_arg(argp, unsigned char *);
156 ret = crypto_shash_update(&sdesc->shash, data, dlen);
162 ret = crypto_shash_final(&sdesc->shash, paramdigest);
164 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
165 paramdigest, TPM_NONCE_SIZE, h1,
166 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
173 * verify the AUTH1_COMMAND (Seal) result from TPM
175 static int TSS_checkhmac1(unsigned char *buffer,
176 const uint32_t command,
177 const unsigned char *ononce,
178 const unsigned char *key,
179 unsigned int keylen, ...)
185 unsigned char *enonce;
186 unsigned char *continueflag;
187 unsigned char *authdata;
188 unsigned char testhmac[SHA1_DIGEST_SIZE];
189 unsigned char paramdigest[SHA1_DIGEST_SIZE];
196 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
197 tag = LOAD16(buffer, 0);
199 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
200 if (tag == TPM_TAG_RSP_COMMAND)
202 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
204 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
205 continueflag = authdata - 1;
206 enonce = continueflag - TPM_NONCE_SIZE;
208 sdesc = init_sdesc(hashalg);
210 pr_info("trusted_key: can't alloc %s\n", hash_alg);
211 return PTR_ERR(sdesc);
213 ret = crypto_shash_init(&sdesc->shash);
216 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
220 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
224 va_start(argp, keylen);
226 dlen = va_arg(argp, unsigned int);
229 dpos = va_arg(argp, unsigned int);
230 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
236 ret = crypto_shash_final(&sdesc->shash, paramdigest);
240 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
241 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
242 1, continueflag, 0, 0);
246 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
254 * verify the AUTH2_COMMAND (unseal) result from TPM
256 static int TSS_checkhmac2(unsigned char *buffer,
257 const uint32_t command,
258 const unsigned char *ononce,
259 const unsigned char *key1,
260 unsigned int keylen1,
261 const unsigned char *key2,
262 unsigned int keylen2, ...)
268 unsigned char *enonce1;
269 unsigned char *continueflag1;
270 unsigned char *authdata1;
271 unsigned char *enonce2;
272 unsigned char *continueflag2;
273 unsigned char *authdata2;
274 unsigned char testhmac1[SHA1_DIGEST_SIZE];
275 unsigned char testhmac2[SHA1_DIGEST_SIZE];
276 unsigned char paramdigest[SHA1_DIGEST_SIZE];
283 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
284 tag = LOAD16(buffer, 0);
286 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
288 if (tag == TPM_TAG_RSP_COMMAND)
290 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
292 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
293 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
294 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
295 continueflag1 = authdata1 - 1;
296 continueflag2 = authdata2 - 1;
297 enonce1 = continueflag1 - TPM_NONCE_SIZE;
298 enonce2 = continueflag2 - TPM_NONCE_SIZE;
300 sdesc = init_sdesc(hashalg);
302 pr_info("trusted_key: can't alloc %s\n", hash_alg);
303 return PTR_ERR(sdesc);
305 ret = crypto_shash_init(&sdesc->shash);
308 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
312 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
317 va_start(argp, keylen2);
319 dlen = va_arg(argp, unsigned int);
322 dpos = va_arg(argp, unsigned int);
323 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
329 ret = crypto_shash_final(&sdesc->shash, paramdigest);
333 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
334 paramdigest, TPM_NONCE_SIZE, enonce1,
335 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
338 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
342 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
343 paramdigest, TPM_NONCE_SIZE, enonce2,
344 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
347 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
355 * For key specific tpm requests, we will generate and send our
356 * own TPM command packets using the drivers send function.
358 static int trusted_tpm_send(unsigned char *cmd, size_t buflen)
363 rc = tpm_send(NULL, cmd, buflen);
366 /* Can't return positive return codes values to keyctl */
372 * Lock a trusted key, by extending a selected PCR.
374 * Prevents a trusted key that is sealed to PCRs from being accessed.
375 * This uses the tpm driver's extend function.
377 static int pcrlock(const int pcrnum)
379 unsigned char hash[SHA1_DIGEST_SIZE];
382 if (!capable(CAP_SYS_ADMIN))
384 ret = tpm_get_random(NULL, hash, SHA1_DIGEST_SIZE);
385 if (ret != SHA1_DIGEST_SIZE)
387 return tpm_pcr_extend(NULL, pcrnum, hash) ? -EINVAL : 0;
391 * Create an object specific authorisation protocol (OSAP) session
393 static int osap(struct tpm_buf *tb, struct osapsess *s,
394 const unsigned char *key, uint16_t type, uint32_t handle)
396 unsigned char enonce[TPM_NONCE_SIZE];
397 unsigned char ononce[TPM_NONCE_SIZE];
400 ret = tpm_get_random(NULL, ononce, TPM_NONCE_SIZE);
401 if (ret != TPM_NONCE_SIZE)
405 store16(tb, TPM_TAG_RQU_COMMAND);
406 store32(tb, TPM_OSAP_SIZE);
407 store32(tb, TPM_ORD_OSAP);
410 storebytes(tb, ononce, TPM_NONCE_SIZE);
412 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
416 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
417 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
419 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
420 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
421 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
422 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
426 * Create an object independent authorisation protocol (oiap) session
428 static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
433 store16(tb, TPM_TAG_RQU_COMMAND);
434 store32(tb, TPM_OIAP_SIZE);
435 store32(tb, TPM_ORD_OIAP);
436 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
440 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
441 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
447 unsigned char encauth[SHA1_DIGEST_SIZE];
448 unsigned char pubauth[SHA1_DIGEST_SIZE];
449 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
450 unsigned char xorhash[SHA1_DIGEST_SIZE];
451 unsigned char nonceodd[TPM_NONCE_SIZE];
455 * Have the TPM seal(encrypt) the trusted key, possibly based on
456 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
458 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
459 uint32_t keyhandle, const unsigned char *keyauth,
460 const unsigned char *data, uint32_t datalen,
461 unsigned char *blob, uint32_t *bloblen,
462 const unsigned char *blobauth,
463 const unsigned char *pcrinfo, uint32_t pcrinfosize)
465 struct osapsess sess;
466 struct tpm_digests *td;
477 /* alloc some work space for all the hashes */
478 td = kmalloc(sizeof *td, GFP_KERNEL);
482 /* get session for sealing key */
483 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
488 /* calculate encrypted authorization value */
489 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
490 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
491 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
495 ret = tpm_get_random(NULL, td->nonceodd, TPM_NONCE_SIZE);
496 if (ret != TPM_NONCE_SIZE)
498 ordinal = htonl(TPM_ORD_SEAL);
499 datsize = htonl(datalen);
500 pcrsize = htonl(pcrinfosize);
503 /* encrypt data authorization key */
504 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
505 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
507 /* calculate authorization HMAC value */
508 if (pcrinfosize == 0) {
509 /* no pcr info specified */
510 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
511 sess.enonce, td->nonceodd, cont,
512 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
513 td->encauth, sizeof(uint32_t), &pcrsize,
514 sizeof(uint32_t), &datsize, datalen, data, 0,
517 /* pcr info specified */
518 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
519 sess.enonce, td->nonceodd, cont,
520 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
521 td->encauth, sizeof(uint32_t), &pcrsize,
522 pcrinfosize, pcrinfo, sizeof(uint32_t),
523 &datsize, datalen, data, 0, 0);
528 /* build and send the TPM request packet */
530 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
531 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
532 store32(tb, TPM_ORD_SEAL);
533 store32(tb, keyhandle);
534 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
535 store32(tb, pcrinfosize);
536 storebytes(tb, pcrinfo, pcrinfosize);
537 store32(tb, datalen);
538 storebytes(tb, data, datalen);
539 store32(tb, sess.handle);
540 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
542 storebytes(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;
591 /* sessions for unsealing key and data */
592 ret = oiap(tb, &authhandle1, enonce1);
594 pr_info("trusted_key: oiap failed (%d)\n", ret);
597 ret = oiap(tb, &authhandle2, enonce2);
599 pr_info("trusted_key: oiap failed (%d)\n", ret);
603 ordinal = htonl(TPM_ORD_UNSEAL);
604 keyhndl = htonl(SRKHANDLE);
605 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
606 if (ret != TPM_NONCE_SIZE) {
607 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
610 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
611 enonce1, nonceodd, cont, sizeof(uint32_t),
612 &ordinal, bloblen, blob, 0, 0);
615 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
616 enonce2, nonceodd, cont, sizeof(uint32_t),
617 &ordinal, bloblen, blob, 0, 0);
621 /* build and send TPM request packet */
623 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
624 store32(tb, TPM_UNSEAL_SIZE + bloblen);
625 store32(tb, TPM_ORD_UNSEAL);
626 store32(tb, keyhandle);
627 storebytes(tb, blob, bloblen);
628 store32(tb, authhandle1);
629 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
631 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
632 store32(tb, authhandle2);
633 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
635 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
637 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
639 pr_info("trusted_key: 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("trusted_key: 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 tb = kzalloc(sizeof *tb, GFP_KERNEL);
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("trusted_key: srkseal failed (%d)\n", ret);
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 tb = kzalloc(sizeof *tb, GFP_KERNEL);
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("trusted_key: srkunseal failed (%d)\n", ret);
702 /* pull migratable flag out of sealed key */
703 p->migratable = p->key[--p->key_len];
711 Opt_new, Opt_load, Opt_update,
712 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
713 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
719 static const match_table_t key_tokens = {
722 {Opt_update, "update"},
723 {Opt_keyhandle, "keyhandle=%s"},
724 {Opt_keyauth, "keyauth=%s"},
725 {Opt_blobauth, "blobauth=%s"},
726 {Opt_pcrinfo, "pcrinfo=%s"},
727 {Opt_pcrlock, "pcrlock=%s"},
728 {Opt_migratable, "migratable=%s"},
729 {Opt_hash, "hash=%s"},
730 {Opt_policydigest, "policydigest=%s"},
731 {Opt_policyhandle, "policyhandle=%s"},
735 /* can have zero or more token= options */
736 static int getoptions(char *c, struct trusted_key_payload *pay,
737 struct trusted_key_options *opt)
739 substring_t args[MAX_OPT_ARGS];
743 unsigned long handle;
745 unsigned long token_mask = 0;
746 unsigned int digest_len;
750 tpm2 = tpm_is_tpm2(NULL);
754 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
756 while ((p = strsep(&c, " \t"))) {
757 if (*p == '\0' || *p == ' ' || *p == '\t')
759 token = match_token(p, key_tokens, args);
760 if (test_and_set_bit(token, &token_mask))
765 opt->pcrinfo_len = strlen(args[0].from) / 2;
766 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
768 res = hex2bin(opt->pcrinfo, args[0].from,
774 res = kstrtoul(args[0].from, 16, &handle);
777 opt->keytype = SEAL_keytype;
778 opt->keyhandle = handle;
781 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
783 res = hex2bin(opt->keyauth, args[0].from,
789 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
791 res = hex2bin(opt->blobauth, args[0].from,
797 if (*args[0].from == '0')
803 res = kstrtoul(args[0].from, 10, &lock);
809 if (test_bit(Opt_policydigest, &token_mask))
811 for (i = 0; i < HASH_ALGO__LAST; i++) {
812 if (!strcmp(args[0].from, hash_algo_name[i])) {
817 if (i == HASH_ALGO__LAST)
819 if (!tpm2 && i != HASH_ALGO_SHA1) {
820 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
824 case Opt_policydigest:
825 digest_len = hash_digest_size[opt->hash];
826 if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
828 res = hex2bin(opt->policydigest, args[0].from,
832 opt->policydigest_len = digest_len;
834 case Opt_policyhandle:
837 res = kstrtoul(args[0].from, 16, &handle);
840 opt->policyhandle = handle;
850 * datablob_parse - parse the keyctl data and fill in the
851 * payload and options structures
853 * On success returns 0, otherwise -EINVAL.
855 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
856 struct trusted_key_options *o)
858 substring_t args[MAX_OPT_ARGS];
865 c = strsep(&datablob, " \t");
868 key_cmd = match_token(c, key_tokens, args);
871 /* first argument is key size */
872 c = strsep(&datablob, " \t");
875 ret = kstrtol(c, 10, &keylen);
876 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
879 ret = getoptions(datablob, p, o);
885 /* first argument is sealed blob */
886 c = strsep(&datablob, " \t");
889 p->blob_len = strlen(c) / 2;
890 if (p->blob_len > MAX_BLOB_SIZE)
892 ret = hex2bin(p->blob, c, p->blob_len);
895 ret = getoptions(datablob, p, o);
901 /* all arguments are options */
902 ret = getoptions(datablob, p, o);
914 static struct trusted_key_options *trusted_options_alloc(void)
916 struct trusted_key_options *options;
919 tpm2 = tpm_is_tpm2(NULL);
923 options = kzalloc(sizeof *options, GFP_KERNEL);
925 /* set any non-zero defaults */
926 options->keytype = SRK_keytype;
929 options->keyhandle = SRKHANDLE;
934 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
936 struct trusted_key_payload *p = NULL;
939 ret = key_payload_reserve(key, sizeof *p);
942 p = kzalloc(sizeof *p, GFP_KERNEL);
944 p->migratable = 1; /* migratable by default */
949 * trusted_instantiate - create a new trusted key
951 * Unseal an existing trusted blob or, for a new key, get a
952 * random key, then seal and create a trusted key-type key,
953 * adding it to the specified keyring.
955 * On success, return 0. Otherwise return errno.
957 static int trusted_instantiate(struct key *key,
958 struct key_preparsed_payload *prep)
960 struct trusted_key_payload *payload = NULL;
961 struct trusted_key_options *options = NULL;
962 size_t datalen = prep->datalen;
969 tpm2 = tpm_is_tpm2(NULL);
973 if (datalen <= 0 || datalen > 32767 || !prep->data)
976 datablob = kmalloc(datalen + 1, GFP_KERNEL);
979 memcpy(datablob, prep->data, datalen);
980 datablob[datalen] = '\0';
982 options = trusted_options_alloc();
987 payload = trusted_payload_alloc(key);
993 key_cmd = datablob_parse(datablob, payload, options);
999 if (!options->keyhandle) {
1004 dump_payload(payload);
1005 dump_options(options);
1010 ret = tpm_unseal_trusted(NULL, payload, options);
1012 ret = key_unseal(payload, options);
1013 dump_payload(payload);
1014 dump_options(options);
1016 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
1019 key_len = payload->key_len;
1020 ret = tpm_get_random(NULL, payload->key, key_len);
1021 if (ret != key_len) {
1022 pr_info("trusted_key: key_create failed (%d)\n", ret);
1026 ret = tpm_seal_trusted(NULL, payload, options);
1028 ret = key_seal(payload, options);
1030 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1036 if (!ret && options->pcrlock)
1037 ret = pcrlock(options->pcrlock);
1042 rcu_assign_keypointer(key, payload);
1048 static void trusted_rcu_free(struct rcu_head *rcu)
1050 struct trusted_key_payload *p;
1052 p = container_of(rcu, struct trusted_key_payload, rcu);
1057 * trusted_update - reseal an existing key with new PCR values
1059 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
1061 struct trusted_key_payload *p;
1062 struct trusted_key_payload *new_p;
1063 struct trusted_key_options *new_o;
1064 size_t datalen = prep->datalen;
1068 if (key_is_negative(key))
1070 p = key->payload.data[0];
1073 if (datalen <= 0 || datalen > 32767 || !prep->data)
1076 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1079 new_o = trusted_options_alloc();
1084 new_p = trusted_payload_alloc(key);
1090 memcpy(datablob, prep->data, datalen);
1091 datablob[datalen] = '\0';
1092 ret = datablob_parse(datablob, new_p, new_o);
1093 if (ret != Opt_update) {
1099 if (!new_o->keyhandle) {
1105 /* copy old key values, and reseal with new pcrs */
1106 new_p->migratable = p->migratable;
1107 new_p->key_len = p->key_len;
1108 memcpy(new_p->key, p->key, p->key_len);
1110 dump_payload(new_p);
1112 ret = key_seal(new_p, new_o);
1114 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1118 if (new_o->pcrlock) {
1119 ret = pcrlock(new_o->pcrlock);
1121 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1126 rcu_assign_keypointer(key, new_p);
1127 call_rcu(&p->rcu, trusted_rcu_free);
1135 * trusted_read - copy the sealed blob data to userspace in hex.
1136 * On success, return to userspace the trusted key datablob size.
1138 static long trusted_read(const struct key *key, char __user *buffer,
1141 const struct trusted_key_payload *p;
1146 p = dereference_key_locked(key);
1150 if (buffer && buflen >= 2 * p->blob_len) {
1151 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1156 for (i = 0; i < p->blob_len; i++)
1157 bufp = hex_byte_pack(bufp, p->blob[i]);
1158 if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
1164 return 2 * p->blob_len;
1168 * trusted_destroy - clear and free the key's payload
1170 static void trusted_destroy(struct key *key)
1172 kzfree(key->payload.data[0]);
1175 struct key_type key_type_trusted = {
1177 .instantiate = trusted_instantiate,
1178 .update = trusted_update,
1179 .destroy = trusted_destroy,
1180 .describe = user_describe,
1181 .read = trusted_read,
1184 EXPORT_SYMBOL_GPL(key_type_trusted);
1186 static void trusted_shash_release(void)
1189 crypto_free_shash(hashalg);
1191 crypto_free_shash(hmacalg);
1194 static int __init trusted_shash_alloc(void)
1198 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1199 if (IS_ERR(hmacalg)) {
1200 pr_info("trusted_key: could not allocate crypto %s\n",
1202 return PTR_ERR(hmacalg);
1205 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1206 if (IS_ERR(hashalg)) {
1207 pr_info("trusted_key: could not allocate crypto %s\n",
1209 ret = PTR_ERR(hashalg);
1216 crypto_free_shash(hmacalg);
1220 static int __init init_trusted(void)
1224 ret = trusted_shash_alloc();
1227 ret = register_key_type(&key_type_trusted);
1229 trusted_shash_release();
1233 static void __exit cleanup_trusted(void)
1235 trusted_shash_release();
1236 unregister_key_type(&key_type_trusted);
1239 late_initcall(init_trusted);
1240 module_exit(cleanup_trusted);
1242 MODULE_LICENSE("GPL");