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, const 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 const 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);
111 ret = crypto_shash_final(&sdesc->shash, digest);
118 * calculate authorization info fields to send to TPM
120 static uint32_t TSS_authhmac(unsigned char *digest, const unsigned char *key,
121 const unsigned int keylen, unsigned char *h1,
122 unsigned char *h2, unsigned char h3, ...)
124 unsigned char paramdigest[SHA1_DIGEST_SIZE];
132 sdesc = init_sdesc(hashalg);
134 pr_info("trusted_key: can't alloc %s\n", hash_alg);
135 return PTR_ERR(sdesc);
139 ret = crypto_shash_init(&sdesc->shash);
144 dlen = va_arg(argp, unsigned int);
147 data = va_arg(argp, unsigned char *);
148 ret = crypto_shash_update(&sdesc->shash, data, dlen);
153 ret = crypto_shash_final(&sdesc->shash, paramdigest);
155 TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
156 paramdigest, TPM_NONCE_SIZE, h1,
157 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
164 * verify the AUTH1_COMMAND (Seal) result from TPM
166 static uint32_t TSS_checkhmac1(unsigned char *buffer,
167 const uint32_t command,
168 const unsigned char *ononce,
169 const unsigned char *key,
170 const unsigned int keylen, ...)
176 unsigned char *enonce;
177 unsigned char *continueflag;
178 unsigned char *authdata;
179 unsigned char testhmac[SHA1_DIGEST_SIZE];
180 unsigned char paramdigest[SHA1_DIGEST_SIZE];
187 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
188 tag = LOAD16(buffer, 0);
190 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
191 if (tag == TPM_TAG_RSP_COMMAND)
193 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
195 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
196 continueflag = authdata - 1;
197 enonce = continueflag - TPM_NONCE_SIZE;
199 sdesc = init_sdesc(hashalg);
201 pr_info("trusted_key: can't alloc %s\n", hash_alg);
202 return PTR_ERR(sdesc);
204 ret = crypto_shash_init(&sdesc->shash);
207 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
211 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
215 va_start(argp, keylen);
217 dlen = va_arg(argp, unsigned int);
220 dpos = va_arg(argp, unsigned int);
221 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
226 ret = crypto_shash_final(&sdesc->shash, paramdigest);
229 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
230 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
231 1, continueflag, 0, 0);
234 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
242 * verify the AUTH2_COMMAND (unseal) result from TPM
244 static uint32_t TSS_checkhmac2(unsigned char *buffer,
245 const uint32_t command,
246 const unsigned char *ononce,
247 const unsigned char *key1,
248 const unsigned int keylen1,
249 const unsigned char *key2,
250 const unsigned int keylen2, ...)
256 unsigned char *enonce1;
257 unsigned char *continueflag1;
258 unsigned char *authdata1;
259 unsigned char *enonce2;
260 unsigned char *continueflag2;
261 unsigned char *authdata2;
262 unsigned char testhmac1[SHA1_DIGEST_SIZE];
263 unsigned char testhmac2[SHA1_DIGEST_SIZE];
264 unsigned char paramdigest[SHA1_DIGEST_SIZE];
271 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
272 tag = LOAD16(buffer, 0);
274 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
276 if (tag == TPM_TAG_RSP_COMMAND)
278 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
280 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
281 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
282 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
283 continueflag1 = authdata1 - 1;
284 continueflag2 = authdata2 - 1;
285 enonce1 = continueflag1 - TPM_NONCE_SIZE;
286 enonce2 = continueflag2 - TPM_NONCE_SIZE;
288 sdesc = init_sdesc(hashalg);
290 pr_info("trusted_key: can't alloc %s\n", hash_alg);
291 return PTR_ERR(sdesc);
293 ret = crypto_shash_init(&sdesc->shash);
296 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
300 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
305 va_start(argp, keylen2);
307 dlen = va_arg(argp, unsigned int);
310 dpos = va_arg(argp, unsigned int);
311 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
315 ret = crypto_shash_final(&sdesc->shash, paramdigest);
319 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
320 paramdigest, TPM_NONCE_SIZE, enonce1,
321 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
322 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
326 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
327 paramdigest, TPM_NONCE_SIZE, enonce2,
328 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
329 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
337 * For key specific tpm requests, we will generate and send our
338 * own TPM command packets using the drivers send function.
340 static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
346 rc = tpm_send(chip_num, cmd, buflen);
349 /* Can't return positive return codes values to keyctl */
355 * get a random value from TPM
357 static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len)
362 store16(tb, TPM_TAG_RQU_COMMAND);
363 store32(tb, TPM_GETRANDOM_SIZE);
364 store32(tb, TPM_ORD_GETRANDOM);
366 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data);
367 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
372 static int my_get_random(unsigned char *buf, int len)
377 tb = kzalloc(sizeof *tb, GFP_KERNEL);
380 ret = tpm_get_random(tb, buf, len);
387 * Lock a trusted key, by extending a selected PCR.
389 * Prevents a trusted key that is sealed to PCRs from being accessed.
390 * This uses the tpm driver's extend function.
392 static int pcrlock(const int pcrnum)
394 unsigned char hash[SHA1_DIGEST_SIZE];
396 if (!capable(CAP_SYS_ADMIN))
398 my_get_random(hash, SHA1_DIGEST_SIZE);
399 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
403 * Create an object specific authorisation protocol (OSAP) session
405 static int osap(struct tpm_buf *tb, struct osapsess *s,
406 const unsigned char *key, const uint16_t type,
407 const uint32_t handle)
409 unsigned char enonce[TPM_NONCE_SIZE];
410 unsigned char ononce[TPM_NONCE_SIZE];
413 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
418 store16(tb, TPM_TAG_RQU_COMMAND);
419 store32(tb, TPM_OSAP_SIZE);
420 store32(tb, TPM_ORD_OSAP);
423 storebytes(tb, ononce, TPM_NONCE_SIZE);
425 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
429 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
430 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
432 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
433 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
434 ret = TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
435 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
440 * Create an object independent authorisation protocol (oiap) session
442 static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
447 store16(tb, TPM_TAG_RQU_COMMAND);
448 store32(tb, TPM_OIAP_SIZE);
449 store32(tb, TPM_ORD_OIAP);
450 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
454 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
455 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
461 unsigned char encauth[SHA1_DIGEST_SIZE];
462 unsigned char pubauth[SHA1_DIGEST_SIZE];
463 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
464 unsigned char xorhash[SHA1_DIGEST_SIZE];
465 unsigned char nonceodd[TPM_NONCE_SIZE];
469 * Have the TPM seal(encrypt) the trusted key, possibly based on
470 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
472 static int tpm_seal(struct tpm_buf *tb, const uint16_t keytype,
473 const uint32_t keyhandle, const unsigned char *keyauth,
474 const unsigned char *data, const uint32_t datalen,
475 unsigned char *blob, uint32_t *bloblen,
476 const unsigned char *blobauth,
477 const unsigned char *pcrinfo, const uint32_t pcrinfosize)
479 struct osapsess sess;
480 struct tpm_digests *td;
491 /* alloc some work space for all the hashes */
492 td = kmalloc(sizeof *td, GFP_KERNEL);
496 /* get session for sealing key */
497 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
502 /* calculate encrypted authorization value */
503 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
504 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
505 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
509 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
512 ordinal = htonl(TPM_ORD_SEAL);
513 datsize = htonl(datalen);
514 pcrsize = htonl(pcrinfosize);
517 /* encrypt data authorization key */
518 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
519 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
521 /* calculate authorization HMAC value */
522 if (pcrinfosize == 0) {
523 /* no pcr info specified */
524 TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
525 sess.enonce, td->nonceodd, cont, sizeof(uint32_t),
526 &ordinal, SHA1_DIGEST_SIZE, td->encauth,
527 sizeof(uint32_t), &pcrsize, sizeof(uint32_t),
528 &datsize, datalen, data, 0, 0);
530 /* pcr info specified */
531 TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
532 sess.enonce, td->nonceodd, cont, sizeof(uint32_t),
533 &ordinal, SHA1_DIGEST_SIZE, td->encauth,
534 sizeof(uint32_t), &pcrsize, pcrinfosize,
535 pcrinfo, sizeof(uint32_t), &datsize, datalen,
539 /* build and send the TPM request packet */
541 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
542 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
543 store32(tb, TPM_ORD_SEAL);
544 store32(tb, keyhandle);
545 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
546 store32(tb, pcrinfosize);
547 storebytes(tb, pcrinfo, pcrinfosize);
548 store32(tb, datalen);
549 storebytes(tb, data, datalen);
550 store32(tb, sess.handle);
551 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
553 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
555 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
559 /* calculate the size of the returned Blob */
560 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
561 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
562 sizeof(uint32_t) + sealinfosize);
563 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
564 sizeof(uint32_t) + encdatasize;
566 /* check the HMAC in the response */
567 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
568 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
571 /* copy the returned blob to caller */
572 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
573 *bloblen = storedsize;
578 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
580 static int tpm_unseal(struct tpm_buf *tb,
581 const uint32_t keyhandle, const unsigned char *keyauth,
582 const unsigned char *blob, const int bloblen,
583 const unsigned char *blobauth,
584 unsigned char *data, unsigned int *datalen)
586 unsigned char nonceodd[TPM_NONCE_SIZE];
587 unsigned char enonce1[TPM_NONCE_SIZE];
588 unsigned char enonce2[TPM_NONCE_SIZE];
589 unsigned char authdata1[SHA1_DIGEST_SIZE];
590 unsigned char authdata2[SHA1_DIGEST_SIZE];
591 uint32_t authhandle1 = 0;
592 uint32_t authhandle2 = 0;
593 unsigned char cont = 0;
598 /* sessions for unsealing key and data */
599 ret = oiap(tb, &authhandle1, enonce1);
601 pr_info("trusted_key: oiap failed (%d)\n", ret);
604 ret = oiap(tb, &authhandle2, enonce2);
606 pr_info("trusted_key: oiap failed (%d)\n", ret);
610 ordinal = htonl(TPM_ORD_UNSEAL);
611 keyhndl = htonl(SRKHANDLE);
612 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
614 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
617 TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
618 enonce1, nonceodd, cont, sizeof(uint32_t),
619 &ordinal, bloblen, blob, 0, 0);
620 TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
621 enonce2, nonceodd, cont, sizeof(uint32_t),
622 &ordinal, bloblen, blob, 0, 0);
624 /* build and send TPM request packet */
626 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
627 store32(tb, TPM_UNSEAL_SIZE + bloblen);
628 store32(tb, TPM_ORD_UNSEAL);
629 store32(tb, keyhandle);
630 storebytes(tb, blob, bloblen);
631 store32(tb, authhandle1);
632 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
634 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
635 store32(tb, authhandle2);
636 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
638 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
640 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
642 pr_info("trusted_key: authhmac failed (%d)\n", ret);
646 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
647 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
648 keyauth, SHA1_DIGEST_SIZE,
649 blobauth, SHA1_DIGEST_SIZE,
650 sizeof(uint32_t), TPM_DATA_OFFSET,
651 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
654 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
655 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
660 * Have the TPM seal(encrypt) the symmetric key
662 static int key_seal(struct trusted_key_payload *p,
663 struct trusted_key_options *o)
668 tb = kzalloc(sizeof *tb, GFP_KERNEL);
672 /* include migratable flag at end of sealed key */
673 p->key[p->key_len] = p->migratable;
675 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
676 p->key, p->key_len + 1, p->blob, &p->blob_len,
677 o->blobauth, o->pcrinfo, o->pcrinfo_len);
679 pr_info("trusted_key: srkseal failed (%d)\n", ret);
686 * Have the TPM unseal(decrypt) the symmetric key
688 static int key_unseal(struct trusted_key_payload *p,
689 struct trusted_key_options *o)
694 tb = kzalloc(sizeof *tb, GFP_KERNEL);
698 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
699 o->blobauth, p->key, &p->key_len);
700 /* pull migratable flag out of sealed key */
701 p->migratable = p->key[--p->key_len];
704 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
712 Opt_new, Opt_load, Opt_update,
713 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
714 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
717 static const match_table_t key_tokens = {
720 {Opt_update, "update"},
721 {Opt_keyhandle, "keyhandle=%s"},
722 {Opt_keyauth, "keyauth=%s"},
723 {Opt_blobauth, "blobauth=%s"},
724 {Opt_pcrinfo, "pcrinfo=%s"},
725 {Opt_pcrlock, "pcrlock=%s"},
726 {Opt_migratable, "migratable=%s"},
730 /* can have zero or more token= options */
731 static int getoptions(char *c, struct trusted_key_payload *pay,
732 struct trusted_key_options *opt)
734 substring_t args[MAX_OPT_ARGS];
738 unsigned long handle;
741 while ((p = strsep(&c, " \t"))) {
742 if (*p == '\0' || *p == ' ' || *p == '\t')
744 token = match_token(p, key_tokens, args);
748 opt->pcrinfo_len = strlen(args[0].from) / 2;
749 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
751 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
754 res = strict_strtoul(args[0].from, 16, &handle);
757 opt->keytype = SEAL_keytype;
758 opt->keyhandle = handle;
761 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
763 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
766 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
768 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
771 if (*args[0].from == '0')
777 res = strict_strtoul(args[0].from, 10, &lock);
790 * datablob_parse - parse the keyctl data and fill in the
791 * payload and options structures
793 * On success returns 0, otherwise -EINVAL.
795 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
796 struct trusted_key_options *o)
798 substring_t args[MAX_OPT_ARGS];
805 c = strsep(&datablob, " \t");
808 key_cmd = match_token(c, key_tokens, args);
811 /* first argument is key size */
812 c = strsep(&datablob, " \t");
815 ret = strict_strtol(c, 10, &keylen);
816 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
819 ret = getoptions(datablob, p, o);
825 /* first argument is sealed blob */
826 c = strsep(&datablob, " \t");
829 p->blob_len = strlen(c) / 2;
830 if (p->blob_len > MAX_BLOB_SIZE)
832 hex2bin(p->blob, c, p->blob_len);
833 ret = getoptions(datablob, p, o);
839 /* all arguments are options */
840 ret = getoptions(datablob, p, o);
852 static struct trusted_key_options *trusted_options_alloc(void)
854 struct trusted_key_options *options;
856 options = kzalloc(sizeof *options, GFP_KERNEL);
860 /* set any non-zero defaults */
861 options->keytype = SRK_keytype;
862 options->keyhandle = SRKHANDLE;
866 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
868 struct trusted_key_payload *p = NULL;
871 ret = key_payload_reserve(key, sizeof *p);
874 p = kzalloc(sizeof *p, GFP_KERNEL);
876 /* migratable by default */
882 * trusted_instantiate - create a new trusted key
884 * Unseal an existing trusted blob or, for a new key, get a
885 * random key, then seal and create a trusted key-type key,
886 * adding it to the specified keyring.
888 * On success, return 0. Otherwise return errno.
890 static int trusted_instantiate(struct key *key, const void *data,
891 const size_t datalen)
893 struct trusted_key_payload *payload = NULL;
894 struct trusted_key_options *options = NULL;
899 if (datalen <= 0 || datalen > 32767 || !data)
902 datablob = kmalloc(datalen + 1, GFP_KERNEL);
905 memcpy(datablob, data, datalen);
906 datablob[datalen] = '\0';
908 options = trusted_options_alloc();
913 payload = trusted_payload_alloc(key);
919 key_cmd = datablob_parse(datablob, payload, options);
925 dump_payload(payload);
926 dump_options(options);
930 ret = key_unseal(payload, options);
931 dump_payload(payload);
932 dump_options(options);
934 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
937 ret = my_get_random(payload->key, payload->key_len);
939 pr_info("trusted_key: key_create failed (%d)\n", ret);
942 ret = key_seal(payload, options);
944 pr_info("trusted_key: key_seal failed (%d)\n", ret);
950 if (!ret && options->pcrlock)
951 ret = pcrlock(options->pcrlock);
956 rcu_assign_pointer(key->payload.data, payload);
962 static void trusted_rcu_free(struct rcu_head *rcu)
964 struct trusted_key_payload *p;
966 p = container_of(rcu, struct trusted_key_payload, rcu);
967 memset(p->key, 0, p->key_len);
972 * trusted_update - reseal an existing key with new PCR values
974 static int trusted_update(struct key *key, const void *data,
975 const size_t datalen)
977 struct trusted_key_payload *p = key->payload.data;
978 struct trusted_key_payload *new_p;
979 struct trusted_key_options *new_o;
985 if (datalen <= 0 || datalen > 32767 || !data)
988 datablob = kmalloc(datalen + 1, GFP_KERNEL);
991 new_o = trusted_options_alloc();
996 new_p = trusted_payload_alloc(key);
1002 memcpy(datablob, data, datalen);
1003 datablob[datalen] = '\0';
1004 ret = datablob_parse(datablob, new_p, new_o);
1005 if (ret != Opt_update) {
1009 /* copy old key values, and reseal with new pcrs */
1010 new_p->migratable = p->migratable;
1011 new_p->key_len = p->key_len;
1012 memcpy(new_p->key, p->key, p->key_len);
1014 dump_payload(new_p);
1016 ret = key_seal(new_p, new_o);
1018 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1022 if (new_o->pcrlock) {
1023 ret = pcrlock(new_o->pcrlock);
1025 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1030 rcu_assign_pointer(key->payload.data, new_p);
1031 call_rcu(&p->rcu, trusted_rcu_free);
1039 * trusted_read - copy the sealed blob data to userspace in hex.
1040 * On success, return to userspace the trusted key datablob size.
1042 static long trusted_read(const struct key *key, char __user *buffer,
1045 struct trusted_key_payload *p;
1050 p = rcu_dereference_protected(key->payload.data,
1051 rwsem_is_locked(&((struct key *)key)->sem));
1054 if (!buffer || buflen <= 0)
1055 return 2 * p->blob_len;
1056 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1061 for (i = 0; i < p->blob_len; i++)
1062 bufp = pack_hex_byte(bufp, p->blob[i]);
1063 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1068 return 2 * p->blob_len;
1072 * trusted_destroy - before freeing the key, clear the decrypted data
1074 static void trusted_destroy(struct key *key)
1076 struct trusted_key_payload *p = key->payload.data;
1080 memset(p->key, 0, p->key_len);
1081 kfree(key->payload.data);
1084 struct key_type key_type_trusted = {
1086 .instantiate = trusted_instantiate,
1087 .update = trusted_update,
1088 .match = user_match,
1089 .destroy = trusted_destroy,
1090 .describe = user_describe,
1091 .read = trusted_read,
1094 EXPORT_SYMBOL_GPL(key_type_trusted);
1096 static void trusted_shash_release(void)
1099 crypto_free_shash(hashalg);
1101 crypto_free_shash(hmacalg);
1104 static int __init trusted_shash_alloc(void)
1108 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1109 if (IS_ERR(hmacalg)) {
1110 pr_info("trusted_key: could not allocate crypto %s\n",
1112 return PTR_ERR(hmacalg);
1115 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1116 if (IS_ERR(hashalg)) {
1117 pr_info("trusted_key: could not allocate crypto %s\n",
1119 ret = PTR_ERR(hashalg);
1126 crypto_free_shash(hmacalg);
1130 static int __init init_trusted(void)
1134 ret = trusted_shash_alloc();
1137 ret = register_key_type(&key_type_trusted);
1139 trusted_shash_release();
1143 static void __exit cleanup_trusted(void)
1145 trusted_shash_release();
1146 unregister_key_type(&key_type_trusted);
1149 late_initcall(init_trusted);
1150 module_exit(cleanup_trusted);
1152 MODULE_LICENSE("GPL");