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";
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 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);
171 EXPORT_SYMBOL_GPL(TSS_authhmac);
174 * verify the AUTH1_COMMAND (Seal) result from TPM
176 int TSS_checkhmac1(unsigned char *buffer,
177 const uint32_t command,
178 const unsigned char *ononce,
179 const unsigned char *key,
180 unsigned int keylen, ...)
186 unsigned char *enonce;
187 unsigned char *continueflag;
188 unsigned char *authdata;
189 unsigned char testhmac[SHA1_DIGEST_SIZE];
190 unsigned char paramdigest[SHA1_DIGEST_SIZE];
197 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
198 tag = LOAD16(buffer, 0);
200 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
201 if (tag == TPM_TAG_RSP_COMMAND)
203 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
205 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
206 continueflag = authdata - 1;
207 enonce = continueflag - TPM_NONCE_SIZE;
209 sdesc = init_sdesc(hashalg);
211 pr_info("trusted_key: can't alloc %s\n", hash_alg);
212 return PTR_ERR(sdesc);
214 ret = crypto_shash_init(&sdesc->shash);
217 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
221 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
225 va_start(argp, keylen);
227 dlen = va_arg(argp, unsigned int);
230 dpos = va_arg(argp, unsigned int);
231 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
237 ret = crypto_shash_final(&sdesc->shash, paramdigest);
241 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
242 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
243 1, continueflag, 0, 0);
247 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
253 EXPORT_SYMBOL_GPL(TSS_checkhmac1);
256 * verify the AUTH2_COMMAND (unseal) result from TPM
258 static int TSS_checkhmac2(unsigned char *buffer,
259 const uint32_t command,
260 const unsigned char *ononce,
261 const unsigned char *key1,
262 unsigned int keylen1,
263 const unsigned char *key2,
264 unsigned int keylen2, ...)
270 unsigned char *enonce1;
271 unsigned char *continueflag1;
272 unsigned char *authdata1;
273 unsigned char *enonce2;
274 unsigned char *continueflag2;
275 unsigned char *authdata2;
276 unsigned char testhmac1[SHA1_DIGEST_SIZE];
277 unsigned char testhmac2[SHA1_DIGEST_SIZE];
278 unsigned char paramdigest[SHA1_DIGEST_SIZE];
285 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
286 tag = LOAD16(buffer, 0);
288 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
290 if (tag == TPM_TAG_RSP_COMMAND)
292 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
294 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
295 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
296 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
297 continueflag1 = authdata1 - 1;
298 continueflag2 = authdata2 - 1;
299 enonce1 = continueflag1 - TPM_NONCE_SIZE;
300 enonce2 = continueflag2 - TPM_NONCE_SIZE;
302 sdesc = init_sdesc(hashalg);
304 pr_info("trusted_key: can't alloc %s\n", hash_alg);
305 return PTR_ERR(sdesc);
307 ret = crypto_shash_init(&sdesc->shash);
310 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
314 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
319 va_start(argp, keylen2);
321 dlen = va_arg(argp, unsigned int);
324 dpos = va_arg(argp, unsigned int);
325 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
331 ret = crypto_shash_final(&sdesc->shash, paramdigest);
335 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
336 paramdigest, TPM_NONCE_SIZE, enonce1,
337 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
340 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
344 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
345 paramdigest, TPM_NONCE_SIZE, enonce2,
346 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
349 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
357 * For key specific tpm requests, we will generate and send our
358 * own TPM command packets using the drivers send function.
360 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
365 rc = tpm_send(NULL, 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 unsigned char hash[SHA1_DIGEST_SIZE];
385 if (!capable(CAP_SYS_ADMIN))
387 ret = tpm_get_random(NULL, hash, SHA1_DIGEST_SIZE);
388 if (ret != SHA1_DIGEST_SIZE)
390 return tpm_pcr_extend(NULL, pcrnum, hash) ? -EINVAL : 0;
394 * Create an object specific authorisation protocol (OSAP) session
396 static int osap(struct tpm_buf *tb, struct osapsess *s,
397 const unsigned char *key, uint16_t type, uint32_t handle)
399 unsigned char enonce[TPM_NONCE_SIZE];
400 unsigned char ononce[TPM_NONCE_SIZE];
403 ret = tpm_get_random(NULL, ononce, TPM_NONCE_SIZE);
404 if (ret != TPM_NONCE_SIZE)
408 store16(tb, TPM_TAG_RQU_COMMAND);
409 store32(tb, TPM_OSAP_SIZE);
410 store32(tb, TPM_ORD_OSAP);
413 storebytes(tb, ononce, TPM_NONCE_SIZE);
415 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
419 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
420 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
422 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
423 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
424 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
425 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
429 * Create an object independent authorisation protocol (oiap) session
431 int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
436 store16(tb, TPM_TAG_RQU_COMMAND);
437 store32(tb, TPM_OIAP_SIZE);
438 store32(tb, TPM_ORD_OIAP);
439 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
443 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
444 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
448 EXPORT_SYMBOL_GPL(oiap);
451 unsigned char encauth[SHA1_DIGEST_SIZE];
452 unsigned char pubauth[SHA1_DIGEST_SIZE];
453 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
454 unsigned char xorhash[SHA1_DIGEST_SIZE];
455 unsigned char nonceodd[TPM_NONCE_SIZE];
459 * Have the TPM seal(encrypt) the trusted key, possibly based on
460 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
462 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
463 uint32_t keyhandle, const unsigned char *keyauth,
464 const unsigned char *data, uint32_t datalen,
465 unsigned char *blob, uint32_t *bloblen,
466 const unsigned char *blobauth,
467 const unsigned char *pcrinfo, uint32_t pcrinfosize)
469 struct osapsess sess;
470 struct tpm_digests *td;
481 /* alloc some work space for all the hashes */
482 td = kmalloc(sizeof *td, GFP_KERNEL);
486 /* get session for sealing key */
487 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
492 /* calculate encrypted authorization value */
493 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
494 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
495 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
499 ret = tpm_get_random(NULL, td->nonceodd, TPM_NONCE_SIZE);
500 if (ret != TPM_NONCE_SIZE)
502 ordinal = htonl(TPM_ORD_SEAL);
503 datsize = htonl(datalen);
504 pcrsize = htonl(pcrinfosize);
507 /* encrypt data authorization key */
508 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
509 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
511 /* calculate authorization HMAC value */
512 if (pcrinfosize == 0) {
513 /* no pcr info specified */
514 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
515 sess.enonce, td->nonceodd, cont,
516 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
517 td->encauth, sizeof(uint32_t), &pcrsize,
518 sizeof(uint32_t), &datsize, datalen, data, 0,
521 /* 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 pcrinfosize, pcrinfo, sizeof(uint32_t),
527 &datsize, datalen, data, 0, 0);
532 /* build and send the TPM request packet */
534 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
535 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
536 store32(tb, TPM_ORD_SEAL);
537 store32(tb, keyhandle);
538 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
539 store32(tb, pcrinfosize);
540 storebytes(tb, pcrinfo, pcrinfosize);
541 store32(tb, datalen);
542 storebytes(tb, data, datalen);
543 store32(tb, sess.handle);
544 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
546 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
548 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
552 /* calculate the size of the returned Blob */
553 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
554 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
555 sizeof(uint32_t) + sealinfosize);
556 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
557 sizeof(uint32_t) + encdatasize;
559 /* check the HMAC in the response */
560 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
561 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
564 /* copy the returned blob to caller */
566 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
567 *bloblen = storedsize;
575 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
577 static int tpm_unseal(struct tpm_buf *tb,
578 uint32_t keyhandle, const unsigned char *keyauth,
579 const unsigned char *blob, int bloblen,
580 const unsigned char *blobauth,
581 unsigned char *data, unsigned int *datalen)
583 unsigned char nonceodd[TPM_NONCE_SIZE];
584 unsigned char enonce1[TPM_NONCE_SIZE];
585 unsigned char enonce2[TPM_NONCE_SIZE];
586 unsigned char authdata1[SHA1_DIGEST_SIZE];
587 unsigned char authdata2[SHA1_DIGEST_SIZE];
588 uint32_t authhandle1 = 0;
589 uint32_t authhandle2 = 0;
590 unsigned char cont = 0;
595 /* sessions for unsealing key and data */
596 ret = oiap(tb, &authhandle1, enonce1);
598 pr_info("trusted_key: oiap failed (%d)\n", ret);
601 ret = oiap(tb, &authhandle2, enonce2);
603 pr_info("trusted_key: oiap failed (%d)\n", ret);
607 ordinal = htonl(TPM_ORD_UNSEAL);
608 keyhndl = htonl(SRKHANDLE);
609 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
610 if (ret != TPM_NONCE_SIZE) {
611 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
614 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
615 enonce1, nonceodd, cont, sizeof(uint32_t),
616 &ordinal, bloblen, blob, 0, 0);
619 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
620 enonce2, nonceodd, cont, sizeof(uint32_t),
621 &ordinal, bloblen, blob, 0, 0);
625 /* build and send TPM request packet */
627 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
628 store32(tb, TPM_UNSEAL_SIZE + bloblen);
629 store32(tb, TPM_ORD_UNSEAL);
630 store32(tb, keyhandle);
631 storebytes(tb, blob, bloblen);
632 store32(tb, authhandle1);
633 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
635 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
636 store32(tb, authhandle2);
637 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
639 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
641 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
643 pr_info("trusted_key: authhmac failed (%d)\n", ret);
647 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
648 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
649 keyauth, SHA1_DIGEST_SIZE,
650 blobauth, SHA1_DIGEST_SIZE,
651 sizeof(uint32_t), TPM_DATA_OFFSET,
652 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
655 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
658 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
663 * Have the TPM seal(encrypt) the symmetric key
665 static int key_seal(struct trusted_key_payload *p,
666 struct trusted_key_options *o)
671 tb = kzalloc(sizeof *tb, GFP_KERNEL);
675 /* include migratable flag at end of sealed key */
676 p->key[p->key_len] = p->migratable;
678 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
679 p->key, p->key_len + 1, p->blob, &p->blob_len,
680 o->blobauth, o->pcrinfo, o->pcrinfo_len);
682 pr_info("trusted_key: srkseal failed (%d)\n", ret);
689 * Have the TPM unseal(decrypt) the symmetric key
691 static int key_unseal(struct trusted_key_payload *p,
692 struct trusted_key_options *o)
697 tb = kzalloc(sizeof *tb, GFP_KERNEL);
701 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
702 o->blobauth, p->key, &p->key_len);
704 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
706 /* pull migratable flag out of sealed key */
707 p->migratable = p->key[--p->key_len];
715 Opt_new, Opt_load, Opt_update,
716 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
717 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
723 static const match_table_t key_tokens = {
726 {Opt_update, "update"},
727 {Opt_keyhandle, "keyhandle=%s"},
728 {Opt_keyauth, "keyauth=%s"},
729 {Opt_blobauth, "blobauth=%s"},
730 {Opt_pcrinfo, "pcrinfo=%s"},
731 {Opt_pcrlock, "pcrlock=%s"},
732 {Opt_migratable, "migratable=%s"},
733 {Opt_hash, "hash=%s"},
734 {Opt_policydigest, "policydigest=%s"},
735 {Opt_policyhandle, "policyhandle=%s"},
739 /* can have zero or more token= options */
740 static int getoptions(char *c, struct trusted_key_payload *pay,
741 struct trusted_key_options *opt)
743 substring_t args[MAX_OPT_ARGS];
747 unsigned long handle;
749 unsigned long token_mask = 0;
750 unsigned int digest_len;
754 tpm2 = tpm_is_tpm2(NULL);
758 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
760 while ((p = strsep(&c, " \t"))) {
761 if (*p == '\0' || *p == ' ' || *p == '\t')
763 token = match_token(p, key_tokens, args);
764 if (test_and_set_bit(token, &token_mask))
769 opt->pcrinfo_len = strlen(args[0].from) / 2;
770 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
772 res = hex2bin(opt->pcrinfo, args[0].from,
778 res = kstrtoul(args[0].from, 16, &handle);
781 opt->keytype = SEAL_keytype;
782 opt->keyhandle = handle;
785 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
787 res = hex2bin(opt->keyauth, args[0].from,
793 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
795 res = hex2bin(opt->blobauth, args[0].from,
801 if (*args[0].from == '0')
807 res = kstrtoul(args[0].from, 10, &lock);
813 if (test_bit(Opt_policydigest, &token_mask))
815 for (i = 0; i < HASH_ALGO__LAST; i++) {
816 if (!strcmp(args[0].from, hash_algo_name[i])) {
821 if (i == HASH_ALGO__LAST)
823 if (!tpm2 && i != HASH_ALGO_SHA1) {
824 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
828 case Opt_policydigest:
829 digest_len = hash_digest_size[opt->hash];
830 if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
832 res = hex2bin(opt->policydigest, args[0].from,
836 opt->policydigest_len = digest_len;
838 case Opt_policyhandle:
841 res = kstrtoul(args[0].from, 16, &handle);
844 opt->policyhandle = handle;
854 * datablob_parse - parse the keyctl data and fill in the
855 * payload and options structures
857 * On success returns 0, otherwise -EINVAL.
859 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
860 struct trusted_key_options *o)
862 substring_t args[MAX_OPT_ARGS];
869 c = strsep(&datablob, " \t");
872 key_cmd = match_token(c, key_tokens, args);
875 /* first argument is key size */
876 c = strsep(&datablob, " \t");
879 ret = kstrtol(c, 10, &keylen);
880 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
883 ret = getoptions(datablob, p, o);
889 /* first argument is sealed blob */
890 c = strsep(&datablob, " \t");
893 p->blob_len = strlen(c) / 2;
894 if (p->blob_len > MAX_BLOB_SIZE)
896 ret = hex2bin(p->blob, c, p->blob_len);
899 ret = getoptions(datablob, p, o);
905 /* all arguments are options */
906 ret = getoptions(datablob, p, o);
918 static struct trusted_key_options *trusted_options_alloc(void)
920 struct trusted_key_options *options;
923 tpm2 = tpm_is_tpm2(NULL);
927 options = kzalloc(sizeof *options, GFP_KERNEL);
929 /* set any non-zero defaults */
930 options->keytype = SRK_keytype;
933 options->keyhandle = SRKHANDLE;
938 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
940 struct trusted_key_payload *p = NULL;
943 ret = key_payload_reserve(key, sizeof *p);
946 p = kzalloc(sizeof *p, GFP_KERNEL);
948 p->migratable = 1; /* migratable by default */
953 * trusted_instantiate - create a new trusted key
955 * Unseal an existing trusted blob or, for a new key, get a
956 * random key, then seal and create a trusted key-type key,
957 * adding it to the specified keyring.
959 * On success, return 0. Otherwise return errno.
961 static int trusted_instantiate(struct key *key,
962 struct key_preparsed_payload *prep)
964 struct trusted_key_payload *payload = NULL;
965 struct trusted_key_options *options = NULL;
966 size_t datalen = prep->datalen;
973 tpm2 = tpm_is_tpm2(NULL);
977 if (datalen <= 0 || datalen > 32767 || !prep->data)
980 datablob = kmalloc(datalen + 1, GFP_KERNEL);
983 memcpy(datablob, prep->data, datalen);
984 datablob[datalen] = '\0';
986 options = trusted_options_alloc();
991 payload = trusted_payload_alloc(key);
997 key_cmd = datablob_parse(datablob, payload, options);
1003 if (!options->keyhandle) {
1008 dump_payload(payload);
1009 dump_options(options);
1014 ret = tpm_unseal_trusted(NULL, payload, options);
1016 ret = key_unseal(payload, options);
1017 dump_payload(payload);
1018 dump_options(options);
1020 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
1023 key_len = payload->key_len;
1024 ret = tpm_get_random(NULL, payload->key, key_len);
1025 if (ret != key_len) {
1026 pr_info("trusted_key: key_create failed (%d)\n", ret);
1030 ret = tpm_seal_trusted(NULL, payload, options);
1032 ret = key_seal(payload, options);
1034 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1040 if (!ret && options->pcrlock)
1041 ret = pcrlock(options->pcrlock);
1046 rcu_assign_keypointer(key, payload);
1052 static void trusted_rcu_free(struct rcu_head *rcu)
1054 struct trusted_key_payload *p;
1056 p = container_of(rcu, struct trusted_key_payload, rcu);
1061 * trusted_update - reseal an existing key with new PCR values
1063 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
1065 struct trusted_key_payload *p;
1066 struct trusted_key_payload *new_p;
1067 struct trusted_key_options *new_o;
1068 size_t datalen = prep->datalen;
1072 if (key_is_negative(key))
1074 p = key->payload.data[0];
1077 if (datalen <= 0 || datalen > 32767 || !prep->data)
1080 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1083 new_o = trusted_options_alloc();
1088 new_p = trusted_payload_alloc(key);
1094 memcpy(datablob, prep->data, datalen);
1095 datablob[datalen] = '\0';
1096 ret = datablob_parse(datablob, new_p, new_o);
1097 if (ret != Opt_update) {
1103 if (!new_o->keyhandle) {
1109 /* copy old key values, and reseal with new pcrs */
1110 new_p->migratable = p->migratable;
1111 new_p->key_len = p->key_len;
1112 memcpy(new_p->key, p->key, p->key_len);
1114 dump_payload(new_p);
1116 ret = key_seal(new_p, new_o);
1118 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1122 if (new_o->pcrlock) {
1123 ret = pcrlock(new_o->pcrlock);
1125 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1130 rcu_assign_keypointer(key, new_p);
1131 call_rcu(&p->rcu, trusted_rcu_free);
1139 * trusted_read - copy the sealed blob data to userspace in hex.
1140 * On success, return to userspace the trusted key datablob size.
1142 static long trusted_read(const struct key *key, char __user *buffer,
1145 const struct trusted_key_payload *p;
1150 p = dereference_key_locked(key);
1154 if (buffer && buflen >= 2 * p->blob_len) {
1155 ascii_buf = kmalloc_array(2, p->blob_len, GFP_KERNEL);
1160 for (i = 0; i < p->blob_len; i++)
1161 bufp = hex_byte_pack(bufp, p->blob[i]);
1162 if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
1168 return 2 * p->blob_len;
1172 * trusted_destroy - clear and free the key's payload
1174 static void trusted_destroy(struct key *key)
1176 kzfree(key->payload.data[0]);
1179 struct key_type key_type_trusted = {
1181 .instantiate = trusted_instantiate,
1182 .update = trusted_update,
1183 .destroy = trusted_destroy,
1184 .describe = user_describe,
1185 .read = trusted_read,
1188 EXPORT_SYMBOL_GPL(key_type_trusted);
1190 static void trusted_shash_release(void)
1193 crypto_free_shash(hashalg);
1195 crypto_free_shash(hmacalg);
1198 static int __init trusted_shash_alloc(void)
1202 hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
1203 if (IS_ERR(hmacalg)) {
1204 pr_info("trusted_key: could not allocate crypto %s\n",
1206 return PTR_ERR(hmacalg);
1209 hashalg = crypto_alloc_shash(hash_alg, 0, 0);
1210 if (IS_ERR(hashalg)) {
1211 pr_info("trusted_key: could not allocate crypto %s\n",
1213 ret = PTR_ERR(hashalg);
1220 crypto_free_shash(hmacalg);
1224 static int __init init_trusted(void)
1228 ret = trusted_shash_alloc();
1231 ret = register_key_type(&key_type_trusted);
1233 trusted_shash_release();
1237 static void __exit cleanup_trusted(void)
1239 trusted_shash_release();
1240 unregister_key_type(&key_type_trusted);
1243 late_initcall(init_trusted);
1244 module_exit(cleanup_trusted);
1246 MODULE_LICENSE("GPL");