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/keys-trusted-encrypted.txt
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/parser.h>
19 #include <linux/string.h>
20 #include <linux/err.h>
21 #include <keys/user-type.h>
22 #include <keys/trusted-type.h>
23 #include <linux/key-type.h>
24 #include <linux/rcupdate.h>
25 #include <linux/crypto.h>
26 #include <crypto/hash.h>
27 #include <crypto/sha.h>
28 #include <linux/capability.h>
29 #include <linux/tpm.h>
30 #include <linux/tpm_command.h>
32 #include "trusted_defined.h"
34 static const char hmac_alg[] = "hmac(sha1)";
35 static const char hash_alg[] = "sha1";
38 struct shash_desc shash;
42 static struct crypto_shash *hashalg;
43 static struct crypto_shash *hmacalg;
45 static struct sdesc *init_sdesc(struct crypto_shash *alg)
50 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
51 sdesc = kmalloc(size, GFP_KERNEL);
53 return ERR_PTR(-ENOMEM);
54 sdesc->shash.tfm = alg;
55 sdesc->shash.flags = 0x0;
59 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
60 unsigned char *digest)
65 sdesc = init_sdesc(hashalg);
67 pr_info("trusted_key: can't alloc %s\n", hash_alg);
68 return PTR_ERR(sdesc);
71 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
76 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
77 unsigned int keylen, ...)
85 sdesc = init_sdesc(hmacalg);
87 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
88 return PTR_ERR(sdesc);
91 ret = crypto_shash_setkey(hmacalg, key, keylen);
94 ret = crypto_shash_init(&sdesc->shash);
98 va_start(argp, keylen);
100 dlen = va_arg(argp, unsigned int);
103 data = va_arg(argp, unsigned char *);
106 ret = crypto_shash_update(&sdesc->shash, data, dlen);
112 ret = crypto_shash_final(&sdesc->shash, digest);
119 * calculate authorization info fields to send to TPM
121 static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
122 unsigned int keylen, unsigned char *h1,
123 unsigned char *h2, unsigned char h3, ...)
125 unsigned char paramdigest[SHA1_DIGEST_SIZE];
133 sdesc = init_sdesc(hashalg);
135 pr_info("trusted_key: can't alloc %s\n", hash_alg);
136 return PTR_ERR(sdesc);
140 ret = crypto_shash_init(&sdesc->shash);
145 dlen = va_arg(argp, unsigned int);
148 data = va_arg(argp, unsigned char *);
149 ret = crypto_shash_update(&sdesc->shash, data, dlen);
156 ret = crypto_shash_final(&sdesc->shash, paramdigest);
158 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
159 paramdigest, TPM_NONCE_SIZE, h1,
160 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
167 * verify the AUTH1_COMMAND (Seal) result from TPM
169 static int TSS_checkhmac1(unsigned char *buffer,
170 const uint32_t command,
171 const unsigned char *ononce,
172 const unsigned char *key,
173 unsigned int keylen, ...)
179 unsigned char *enonce;
180 unsigned char *continueflag;
181 unsigned char *authdata;
182 unsigned char testhmac[SHA1_DIGEST_SIZE];
183 unsigned char paramdigest[SHA1_DIGEST_SIZE];
190 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
191 tag = LOAD16(buffer, 0);
193 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
194 if (tag == TPM_TAG_RSP_COMMAND)
196 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
198 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
199 continueflag = authdata - 1;
200 enonce = continueflag - TPM_NONCE_SIZE;
202 sdesc = init_sdesc(hashalg);
204 pr_info("trusted_key: can't alloc %s\n", hash_alg);
205 return PTR_ERR(sdesc);
207 ret = crypto_shash_init(&sdesc->shash);
210 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
214 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
218 va_start(argp, keylen);
220 dlen = va_arg(argp, unsigned int);
223 dpos = va_arg(argp, unsigned int);
224 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
231 ret = crypto_shash_final(&sdesc->shash, paramdigest);
235 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
236 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
237 1, continueflag, 0, 0);
241 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
249 * verify the AUTH2_COMMAND (unseal) result from TPM
251 static int TSS_checkhmac2(unsigned char *buffer,
252 const uint32_t command,
253 const unsigned char *ononce,
254 const unsigned char *key1,
255 unsigned int keylen1,
256 const unsigned char *key2,
257 unsigned int keylen2, ...)
263 unsigned char *enonce1;
264 unsigned char *continueflag1;
265 unsigned char *authdata1;
266 unsigned char *enonce2;
267 unsigned char *continueflag2;
268 unsigned char *authdata2;
269 unsigned char testhmac1[SHA1_DIGEST_SIZE];
270 unsigned char testhmac2[SHA1_DIGEST_SIZE];
271 unsigned char paramdigest[SHA1_DIGEST_SIZE];
278 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
279 tag = LOAD16(buffer, 0);
281 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
283 if (tag == TPM_TAG_RSP_COMMAND)
285 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
287 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
288 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
289 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
290 continueflag1 = authdata1 - 1;
291 continueflag2 = authdata2 - 1;
292 enonce1 = continueflag1 - TPM_NONCE_SIZE;
293 enonce2 = continueflag2 - TPM_NONCE_SIZE;
295 sdesc = init_sdesc(hashalg);
297 pr_info("trusted_key: can't alloc %s\n", hash_alg);
298 return PTR_ERR(sdesc);
300 ret = crypto_shash_init(&sdesc->shash);
303 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
307 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
312 va_start(argp, keylen2);
314 dlen = va_arg(argp, unsigned int);
317 dpos = va_arg(argp, unsigned int);
318 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
325 ret = crypto_shash_final(&sdesc->shash, paramdigest);
329 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
330 paramdigest, TPM_NONCE_SIZE, enonce1,
331 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
334 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
338 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
339 paramdigest, TPM_NONCE_SIZE, enonce2,
340 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
343 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
351 * For key specific tpm requests, we will generate and send our
352 * own TPM command packets using the drivers send function.
354 static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
360 rc = tpm_send(chip_num, cmd, buflen);
363 /* Can't return positive return codes values to keyctl */
369 * get a random value from TPM
371 static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len)
376 store16(tb, TPM_TAG_RQU_COMMAND);
377 store32(tb, TPM_GETRANDOM_SIZE);
378 store32(tb, TPM_ORD_GETRANDOM);
380 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data);
382 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
386 static int my_get_random(unsigned char *buf, int len)
391 tb = kmalloc(sizeof *tb, GFP_KERNEL);
394 ret = tpm_get_random(tb, buf, len);
401 * Lock a trusted key, by extending a selected PCR.
403 * Prevents a trusted key that is sealed to PCRs from being accessed.
404 * This uses the tpm driver's extend function.
406 static int pcrlock(const int pcrnum)
408 unsigned char hash[SHA1_DIGEST_SIZE];
411 if (!capable(CAP_SYS_ADMIN))
413 ret = my_get_random(hash, SHA1_DIGEST_SIZE);
416 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
420 * Create an object specific authorisation protocol (OSAP) session
422 static int osap(struct tpm_buf *tb, struct osapsess *s,
423 const unsigned char *key, uint16_t type, uint32_t handle)
425 unsigned char enonce[TPM_NONCE_SIZE];
426 unsigned char ononce[TPM_NONCE_SIZE];
429 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
434 store16(tb, TPM_TAG_RQU_COMMAND);
435 store32(tb, TPM_OSAP_SIZE);
436 store32(tb, TPM_ORD_OSAP);
439 storebytes(tb, ononce, TPM_NONCE_SIZE);
441 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
445 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
446 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
448 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
449 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
450 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
451 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
455 * Create an object independent authorisation protocol (oiap) session
457 static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
462 store16(tb, TPM_TAG_RQU_COMMAND);
463 store32(tb, TPM_OIAP_SIZE);
464 store32(tb, TPM_ORD_OIAP);
465 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
469 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
470 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
476 unsigned char encauth[SHA1_DIGEST_SIZE];
477 unsigned char pubauth[SHA1_DIGEST_SIZE];
478 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
479 unsigned char xorhash[SHA1_DIGEST_SIZE];
480 unsigned char nonceodd[TPM_NONCE_SIZE];
484 * Have the TPM seal(encrypt) the trusted key, possibly based on
485 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
487 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
488 uint32_t keyhandle, const unsigned char *keyauth,
489 const unsigned char *data, uint32_t datalen,
490 unsigned char *blob, uint32_t *bloblen,
491 const unsigned char *blobauth,
492 const unsigned char *pcrinfo, uint32_t pcrinfosize)
494 struct osapsess sess;
495 struct tpm_digests *td;
506 /* alloc some work space for all the hashes */
507 td = kmalloc(sizeof *td, GFP_KERNEL);
511 /* get session for sealing key */
512 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
517 /* calculate encrypted authorization value */
518 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
519 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
520 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
524 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
527 ordinal = htonl(TPM_ORD_SEAL);
528 datsize = htonl(datalen);
529 pcrsize = htonl(pcrinfosize);
532 /* encrypt data authorization key */
533 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
534 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
536 /* calculate authorization HMAC value */
537 if (pcrinfosize == 0) {
538 /* no pcr info specified */
539 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
540 sess.enonce, td->nonceodd, cont,
541 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
542 td->encauth, sizeof(uint32_t), &pcrsize,
543 sizeof(uint32_t), &datsize, datalen, data, 0,
546 /* pcr info specified */
547 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
548 sess.enonce, td->nonceodd, cont,
549 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
550 td->encauth, sizeof(uint32_t), &pcrsize,
551 pcrinfosize, pcrinfo, sizeof(uint32_t),
552 &datsize, datalen, data, 0, 0);
557 /* build and send the TPM request packet */
559 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
560 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
561 store32(tb, TPM_ORD_SEAL);
562 store32(tb, keyhandle);
563 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
564 store32(tb, pcrinfosize);
565 storebytes(tb, pcrinfo, pcrinfosize);
566 store32(tb, datalen);
567 storebytes(tb, data, datalen);
568 store32(tb, sess.handle);
569 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
571 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
573 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
577 /* calculate the size of the returned Blob */
578 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
579 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
580 sizeof(uint32_t) + sealinfosize);
581 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
582 sizeof(uint32_t) + encdatasize;
584 /* check the HMAC in the response */
585 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
586 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
589 /* copy the returned blob to caller */
591 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
592 *bloblen = storedsize;
600 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
602 static int tpm_unseal(struct tpm_buf *tb,
603 uint32_t keyhandle, const unsigned char *keyauth,
604 const unsigned char *blob, int bloblen,
605 const unsigned char *blobauth,
606 unsigned char *data, unsigned int *datalen)
608 unsigned char nonceodd[TPM_NONCE_SIZE];
609 unsigned char enonce1[TPM_NONCE_SIZE];
610 unsigned char enonce2[TPM_NONCE_SIZE];
611 unsigned char authdata1[SHA1_DIGEST_SIZE];
612 unsigned char authdata2[SHA1_DIGEST_SIZE];
613 uint32_t authhandle1 = 0;
614 uint32_t authhandle2 = 0;
615 unsigned char cont = 0;
620 /* sessions for unsealing key and data */
621 ret = oiap(tb, &authhandle1, enonce1);
623 pr_info("trusted_key: oiap failed (%d)\n", ret);
626 ret = oiap(tb, &authhandle2, enonce2);
628 pr_info("trusted_key: oiap failed (%d)\n", ret);
632 ordinal = htonl(TPM_ORD_UNSEAL);
633 keyhndl = htonl(SRKHANDLE);
634 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
636 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
639 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
640 enonce1, nonceodd, cont, sizeof(uint32_t),
641 &ordinal, bloblen, blob, 0, 0);
644 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
645 enonce2, nonceodd, cont, sizeof(uint32_t),
646 &ordinal, bloblen, blob, 0, 0);
650 /* build and send TPM request packet */
652 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
653 store32(tb, TPM_UNSEAL_SIZE + bloblen);
654 store32(tb, TPM_ORD_UNSEAL);
655 store32(tb, keyhandle);
656 storebytes(tb, blob, bloblen);
657 store32(tb, authhandle1);
658 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
660 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
661 store32(tb, authhandle2);
662 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
664 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
666 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
668 pr_info("trusted_key: authhmac failed (%d)\n", ret);
672 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
673 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
674 keyauth, SHA1_DIGEST_SIZE,
675 blobauth, SHA1_DIGEST_SIZE,
676 sizeof(uint32_t), TPM_DATA_OFFSET,
677 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
680 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
683 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
688 * Have the TPM seal(encrypt) the symmetric key
690 static int key_seal(struct trusted_key_payload *p,
691 struct trusted_key_options *o)
696 tb = kzalloc(sizeof *tb, GFP_KERNEL);
700 /* include migratable flag at end of sealed key */
701 p->key[p->key_len] = p->migratable;
703 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
704 p->key, p->key_len + 1, p->blob, &p->blob_len,
705 o->blobauth, o->pcrinfo, o->pcrinfo_len);
707 pr_info("trusted_key: srkseal failed (%d)\n", ret);
714 * Have the TPM unseal(decrypt) the symmetric key
716 static int key_unseal(struct trusted_key_payload *p,
717 struct trusted_key_options *o)
722 tb = kzalloc(sizeof *tb, GFP_KERNEL);
726 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
727 o->blobauth, p->key, &p->key_len);
729 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
731 /* pull migratable flag out of sealed key */
732 p->migratable = p->key[--p->key_len];
740 Opt_new, Opt_load, Opt_update,
741 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
742 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
745 static const match_table_t key_tokens = {
748 {Opt_update, "update"},
749 {Opt_keyhandle, "keyhandle=%s"},
750 {Opt_keyauth, "keyauth=%s"},
751 {Opt_blobauth, "blobauth=%s"},
752 {Opt_pcrinfo, "pcrinfo=%s"},
753 {Opt_pcrlock, "pcrlock=%s"},
754 {Opt_migratable, "migratable=%s"},
758 /* can have zero or more token= options */
759 static int getoptions(char *c, struct trusted_key_payload *pay,
760 struct trusted_key_options *opt)
762 substring_t args[MAX_OPT_ARGS];
766 unsigned long handle;
769 while ((p = strsep(&c, " \t"))) {
770 if (*p == '\0' || *p == ' ' || *p == '\t')
772 token = match_token(p, key_tokens, args);
776 opt->pcrinfo_len = strlen(args[0].from) / 2;
777 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
779 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
782 res = strict_strtoul(args[0].from, 16, &handle);
785 opt->keytype = SEAL_keytype;
786 opt->keyhandle = handle;
789 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
791 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
794 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
796 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
799 if (*args[0].from == '0')
805 res = strict_strtoul(args[0].from, 10, &lock);
818 * datablob_parse - parse the keyctl data and fill in the
819 * payload and options structures
821 * On success returns 0, otherwise -EINVAL.
823 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
824 struct trusted_key_options *o)
826 substring_t args[MAX_OPT_ARGS];
833 c = strsep(&datablob, " \t");
836 key_cmd = match_token(c, key_tokens, args);
839 /* first argument is key size */
840 c = strsep(&datablob, " \t");
843 ret = strict_strtol(c, 10, &keylen);
844 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
847 ret = getoptions(datablob, p, o);
853 /* first argument is sealed blob */
854 c = strsep(&datablob, " \t");
857 p->blob_len = strlen(c) / 2;
858 if (p->blob_len > MAX_BLOB_SIZE)
860 hex2bin(p->blob, c, p->blob_len);
861 ret = getoptions(datablob, p, o);
867 /* all arguments are options */
868 ret = getoptions(datablob, p, o);
880 static struct trusted_key_options *trusted_options_alloc(void)
882 struct trusted_key_options *options;
884 options = kzalloc(sizeof *options, GFP_KERNEL);
886 /* set any non-zero defaults */
887 options->keytype = SRK_keytype;
888 options->keyhandle = SRKHANDLE;
893 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
895 struct trusted_key_payload *p = NULL;
898 ret = key_payload_reserve(key, sizeof *p);
901 p = kzalloc(sizeof *p, GFP_KERNEL);
903 p->migratable = 1; /* migratable by default */
908 * trusted_instantiate - create a new trusted key
910 * Unseal an existing trusted blob or, for a new key, get a
911 * random key, then seal and create a trusted key-type key,
912 * adding it to the specified keyring.
914 * On success, return 0. Otherwise return errno.
916 static int trusted_instantiate(struct key *key, const void *data,
919 struct trusted_key_payload *payload = NULL;
920 struct trusted_key_options *options = NULL;
925 if (datalen <= 0 || datalen > 32767 || !data)
928 datablob = kmalloc(datalen + 1, GFP_KERNEL);
931 memcpy(datablob, data, datalen);
932 datablob[datalen] = '\0';
934 options = trusted_options_alloc();
939 payload = trusted_payload_alloc(key);
945 key_cmd = datablob_parse(datablob, payload, options);
951 dump_payload(payload);
952 dump_options(options);
956 ret = key_unseal(payload, options);
957 dump_payload(payload);
958 dump_options(options);
960 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
963 ret = my_get_random(payload->key, payload->key_len);
965 pr_info("trusted_key: key_create failed (%d)\n", ret);
968 ret = key_seal(payload, options);
970 pr_info("trusted_key: key_seal failed (%d)\n", ret);
976 if (!ret && options->pcrlock)
977 ret = pcrlock(options->pcrlock);
982 rcu_assign_pointer(key->payload.data, payload);
988 static void trusted_rcu_free(struct rcu_head *rcu)
990 struct trusted_key_payload *p;
992 p = container_of(rcu, struct trusted_key_payload, rcu);
993 memset(p->key, 0, p->key_len);
998 * trusted_update - reseal an existing key with new PCR values
1000 static int trusted_update(struct key *key, const void *data, size_t datalen)
1002 struct trusted_key_payload *p = key->payload.data;
1003 struct trusted_key_payload *new_p;
1004 struct trusted_key_options *new_o;
1010 if (datalen <= 0 || datalen > 32767 || !data)
1013 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1016 new_o = trusted_options_alloc();
1021 new_p = trusted_payload_alloc(key);
1027 memcpy(datablob, data, datalen);
1028 datablob[datalen] = '\0';
1029 ret = datablob_parse(datablob, new_p, new_o);
1030 if (ret != Opt_update) {
1034 /* copy old key values, and reseal with new pcrs */
1035 new_p->migratable = p->migratable;
1036 new_p->key_len = p->key_len;
1037 memcpy(new_p->key, p->key, p->key_len);
1039 dump_payload(new_p);
1041 ret = key_seal(new_p, new_o);
1043 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1047 if (new_o->pcrlock) {
1048 ret = pcrlock(new_o->pcrlock);
1050 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1055 rcu_assign_pointer(key->payload.data, new_p);
1056 call_rcu(&p->rcu, trusted_rcu_free);
1064 * trusted_read - copy the sealed blob data to userspace in hex.
1065 * On success, return to userspace the trusted key datablob size.
1067 static long trusted_read(const struct key *key, char __user *buffer,
1070 struct trusted_key_payload *p;
1075 p = rcu_dereference_protected(key->payload.data,
1076 rwsem_is_locked(&((struct key *)key)->sem));
1079 if (!buffer || buflen <= 0)
1080 return 2 * p->blob_len;
1081 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1086 for (i = 0; i < p->blob_len; i++)
1087 bufp = pack_hex_byte(bufp, p->blob[i]);
1088 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1093 return 2 * p->blob_len;
1097 * trusted_destroy - before freeing the key, clear the decrypted data
1099 static void trusted_destroy(struct key *key)
1101 struct trusted_key_payload *p = key->payload.data;
1105 memset(p->key, 0, p->key_len);
1106 kfree(key->payload.data);
1109 struct key_type key_type_trusted = {
1111 .instantiate = trusted_instantiate,
1112 .update = trusted_update,
1113 .match = user_match,
1114 .destroy = trusted_destroy,
1115 .describe = user_describe,
1116 .read = trusted_read,
1119 EXPORT_SYMBOL_GPL(key_type_trusted);
1121 static void trusted_shash_release(void)
1124 crypto_free_shash(hashalg);
1126 crypto_free_shash(hmacalg);
1129 static int __init trusted_shash_alloc(void)
1133 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1134 if (IS_ERR(hmacalg)) {
1135 pr_info("trusted_key: could not allocate crypto %s\n",
1137 return PTR_ERR(hmacalg);
1140 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1141 if (IS_ERR(hashalg)) {
1142 pr_info("trusted_key: could not allocate crypto %s\n",
1144 ret = PTR_ERR(hashalg);
1151 crypto_free_shash(hmacalg);
1155 static int __init init_trusted(void)
1159 ret = trusted_shash_alloc();
1162 ret = register_key_type(&key_type_trusted);
1164 trusted_shash_release();
1168 static void __exit cleanup_trusted(void)
1170 trusted_shash_release();
1171 unregister_key_type(&key_type_trusted);
1174 late_initcall(init_trusted);
1175 module_exit(cleanup_trusted);
1177 MODULE_LICENSE("GPL");