^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2010 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * David Safford <safford@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * See Documentation/security/keys/trusted-encrypted.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/hash_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <keys/trusted-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/key-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/tpm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/tpm_command.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <keys/trusted_tpm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static const char hmac_alg[] = "hmac(sha1)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static const char hash_alg[] = "sha1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct tpm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct tpm_digest *digests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct sdesc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct shash_desc shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) char ctx[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static struct crypto_shash *hashalg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct crypto_shash *hmacalg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct sdesc *init_sdesc(struct crypto_shash *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sdesc = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!sdesc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) sdesc->shash.tfm = alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int TSS_sha1(const unsigned char *data, unsigned int datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned char *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) sdesc = init_sdesc(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (IS_ERR(sdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pr_info("trusted_key: can't alloc %s\n", hash_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return PTR_ERR(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kfree_sensitive(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned int keylen, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) va_list argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned int dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sdesc = init_sdesc(hmacalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (IS_ERR(sdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pr_info("trusted_key: can't alloc %s\n", hmac_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return PTR_ERR(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = crypto_shash_setkey(hmacalg, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = crypto_shash_init(&sdesc->shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) va_start(argp, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dlen = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (dlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) data = va_arg(argp, unsigned char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = crypto_shash_update(&sdesc->shash, data, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) va_end(argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = crypto_shash_final(&sdesc->shash, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree_sensitive(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * calculate authorization info fields to send to TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int TSS_authhmac(unsigned char *digest, const unsigned char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned int keylen, unsigned char *h1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned char *h2, unsigned int h3, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned char paramdigest[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unsigned int dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) va_list argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) sdesc = init_sdesc(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (IS_ERR(sdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pr_info("trusted_key: can't alloc %s\n", hash_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return PTR_ERR(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) c = !!h3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = crypto_shash_init(&sdesc->shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) va_start(argp, h3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dlen = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (dlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) data = va_arg(argp, unsigned char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret = crypto_shash_update(&sdesc->shash, data, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) va_end(argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = crypto_shash_final(&sdesc->shash, paramdigest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) paramdigest, TPM_NONCE_SIZE, h1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) kfree_sensitive(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) EXPORT_SYMBOL_GPL(TSS_authhmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * verify the AUTH1_COMMAND (Seal) result from TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int TSS_checkhmac1(unsigned char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) const uint32_t command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) const unsigned char *ononce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) const unsigned char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) unsigned int keylen, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) uint32_t bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) uint16_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) uint32_t ordinal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) uint32_t result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned char *enonce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned char *continueflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned char *authdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned char testhmac[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) unsigned char paramdigest[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned int dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned int dpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) va_list argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) tag = LOAD16(buffer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ordinal = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) result = LOAD32N(buffer, TPM_RETURN_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (tag == TPM_TAG_RSP_COMMAND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) continueflag = authdata - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) enonce = continueflag - TPM_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) sdesc = init_sdesc(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (IS_ERR(sdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) pr_info("trusted_key: can't alloc %s\n", hash_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return PTR_ERR(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = crypto_shash_init(&sdesc->shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) sizeof result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) sizeof ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) va_start(argp, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dlen = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (dlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dpos = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) va_end(argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ret = crypto_shash_final(&sdesc->shash, paramdigest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 1, continueflag, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) kfree_sensitive(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) EXPORT_SYMBOL_GPL(TSS_checkhmac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * verify the AUTH2_COMMAND (unseal) result from TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static int TSS_checkhmac2(unsigned char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) const uint32_t command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) const unsigned char *ononce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) const unsigned char *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) unsigned int keylen1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) const unsigned char *key2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned int keylen2, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) uint32_t bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) uint16_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) uint32_t ordinal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) uint32_t result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) unsigned char *enonce1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unsigned char *continueflag1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) unsigned char *authdata1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) unsigned char *enonce2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unsigned char *continueflag2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) unsigned char *authdata2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) unsigned char testhmac1[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned char testhmac2[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unsigned char paramdigest[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct sdesc *sdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned int dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) unsigned int dpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) va_list argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) tag = LOAD16(buffer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ordinal = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) result = LOAD32N(buffer, TPM_RETURN_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (tag == TPM_TAG_RSP_COMMAND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) continueflag1 = authdata1 - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) continueflag2 = authdata2 - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) enonce1 = continueflag1 - TPM_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) enonce2 = continueflag2 - TPM_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sdesc = init_sdesc(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (IS_ERR(sdesc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) pr_info("trusted_key: can't alloc %s\n", hash_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return PTR_ERR(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) ret = crypto_shash_init(&sdesc->shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) sizeof result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) sizeof ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) va_start(argp, keylen2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) dlen = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (dlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) dpos = va_arg(argp, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) va_end(argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ret = crypto_shash_final(&sdesc->shash, paramdigest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) paramdigest, TPM_NONCE_SIZE, enonce1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) paramdigest, TPM_NONCE_SIZE, enonce2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) kfree_sensitive(sdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * For key specific tpm requests, we will generate and send our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * own TPM command packets using the drivers send function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int trusted_tpm_send(unsigned char *cmd, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dump_tpm_buf(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) rc = tpm_send(chip, cmd, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) dump_tpm_buf(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Can't return positive return codes values to keyctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) rc = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) EXPORT_SYMBOL_GPL(trusted_tpm_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * Lock a trusted key, by extending a selected PCR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * Prevents a trusted key that is sealed to PCRs from being accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * This uses the tpm driver's extend function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static int pcrlock(const int pcrnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * Create an object specific authorisation protocol (OSAP) session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static int osap(struct tpm_buf *tb, struct osapsess *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) const unsigned char *key, uint16_t type, uint32_t handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) unsigned char enonce[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) unsigned char ononce[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (ret != TPM_NONCE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) tpm_buf_append_u16(tb, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) tpm_buf_append_u32(tb, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) tpm_buf_append(tb, ononce, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) enonce, TPM_NONCE_SIZE, ononce, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * Create an object independent authorisation protocol (oiap) session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OIAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) EXPORT_SYMBOL_GPL(oiap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct tpm_digests {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) unsigned char encauth[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) unsigned char pubauth[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) unsigned char xorhash[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) unsigned char nonceodd[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Have the TPM seal(encrypt) the trusted key, possibly based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) uint32_t keyhandle, const unsigned char *keyauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) const unsigned char *data, uint32_t datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) unsigned char *blob, uint32_t *bloblen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) const unsigned char *blobauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) const unsigned char *pcrinfo, uint32_t pcrinfosize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct osapsess sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct tpm_digests *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) unsigned char cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) uint32_t ordinal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) uint32_t pcrsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) uint32_t datsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) int sealinfosize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int encdatasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int storedsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /* alloc some work space for all the hashes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) td = kmalloc(sizeof *td, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /* get session for sealing key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) ret = osap(tb, &sess, keyauth, keytype, keyhandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) dump_sess(&sess);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /* calculate encrypted authorization value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (ret != TPM_NONCE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ordinal = htonl(TPM_ORD_SEAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) datsize = htonl(datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) pcrsize = htonl(pcrinfosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) cont = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) /* encrypt data authorization key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) td->encauth[i] = td->xorhash[i] ^ blobauth[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* calculate authorization HMAC value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (pcrinfosize == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /* no pcr info specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) sess.enonce, td->nonceodd, cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) td->encauth, sizeof(uint32_t), &pcrsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) sizeof(uint32_t), &datsize, datalen, data, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* pcr info specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) sess.enonce, td->nonceodd, cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) td->encauth, sizeof(uint32_t), &pcrsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) pcrinfosize, pcrinfo, sizeof(uint32_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) &datsize, datalen, data, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* build and send the TPM request packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SEAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) tpm_buf_append_u32(tb, keyhandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) tpm_buf_append(tb, td->encauth, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) tpm_buf_append_u32(tb, pcrinfosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) tpm_buf_append(tb, pcrinfo, pcrinfosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) tpm_buf_append_u32(tb, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) tpm_buf_append(tb, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) tpm_buf_append_u32(tb, sess.handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) tpm_buf_append(tb, td->nonceodd, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) tpm_buf_append_u8(tb, cont);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) tpm_buf_append(tb, td->pubauth, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /* calculate the size of the returned Blob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) sizeof(uint32_t) + sealinfosize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) sizeof(uint32_t) + encdatasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /* check the HMAC in the response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* copy the returned blob to caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) *bloblen = storedsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) kfree_sensitive(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static int tpm_unseal(struct tpm_buf *tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) uint32_t keyhandle, const unsigned char *keyauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) const unsigned char *blob, int bloblen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) const unsigned char *blobauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) unsigned char *data, unsigned int *datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) unsigned char nonceodd[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) unsigned char enonce1[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) unsigned char enonce2[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) unsigned char authdata1[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) unsigned char authdata2[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) uint32_t authhandle1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) uint32_t authhandle2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) unsigned char cont = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) uint32_t ordinal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /* sessions for unsealing key and data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ret = oiap(tb, &authhandle1, enonce1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) pr_info("trusted_key: oiap failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) ret = oiap(tb, &authhandle2, enonce2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) pr_info("trusted_key: oiap failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ordinal = htonl(TPM_ORD_UNSEAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (ret != TPM_NONCE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) enonce1, nonceodd, cont, sizeof(uint32_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) &ordinal, bloblen, blob, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) enonce2, nonceodd, cont, sizeof(uint32_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) &ordinal, bloblen, blob, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /* build and send TPM request packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) tpm_buf_reset(tb, TPM_TAG_RQU_AUTH2_COMMAND, TPM_ORD_UNSEAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) tpm_buf_append_u32(tb, keyhandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) tpm_buf_append(tb, blob, bloblen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) tpm_buf_append_u32(tb, authhandle1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) tpm_buf_append_u8(tb, cont);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) tpm_buf_append(tb, authdata1, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) tpm_buf_append_u32(tb, authhandle2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) tpm_buf_append_u8(tb, cont);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) tpm_buf_append(tb, authdata2, SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) pr_info("trusted_key: authhmac failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) keyauth, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) blobauth, SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) sizeof(uint32_t), TPM_DATA_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Have the TPM seal(encrypt) the symmetric key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static int key_seal(struct trusted_key_payload *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct trusted_key_options *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct tpm_buf tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) ret = tpm_buf_init(&tb, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /* include migratable flag at end of sealed key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) p->key[p->key_len] = p->migratable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) p->key, p->key_len + 1, p->blob, &p->blob_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) o->blobauth, o->pcrinfo, o->pcrinfo_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) pr_info("trusted_key: srkseal failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) tpm_buf_destroy(&tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * Have the TPM unseal(decrypt) the symmetric key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int key_unseal(struct trusted_key_payload *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) struct trusted_key_options *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct tpm_buf tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) ret = tpm_buf_init(&tb, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) o->blobauth, p->key, &p->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pr_info("trusted_key: srkunseal failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /* pull migratable flag out of sealed key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) p->migratable = p->key[--p->key_len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) tpm_buf_destroy(&tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) Opt_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) Opt_new, Opt_load, Opt_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) Opt_keyhandle, Opt_keyauth, Opt_blobauth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) Opt_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) Opt_policydigest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) Opt_policyhandle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) static const match_table_t key_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) {Opt_new, "new"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {Opt_load, "load"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) {Opt_update, "update"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) {Opt_keyhandle, "keyhandle=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {Opt_keyauth, "keyauth=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {Opt_blobauth, "blobauth=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {Opt_pcrinfo, "pcrinfo=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {Opt_pcrlock, "pcrlock=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {Opt_migratable, "migratable=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {Opt_hash, "hash=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) {Opt_policydigest, "policydigest=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {Opt_policyhandle, "policyhandle=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {Opt_err, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /* can have zero or more token= options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) static int getoptions(char *c, struct trusted_key_payload *pay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) struct trusted_key_options *opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) char *p = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) unsigned long handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) unsigned long lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) unsigned long token_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) unsigned int digest_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) int tpm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) tpm2 = tpm_is_tpm2(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (tpm2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return tpm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) while ((p = strsep(&c, " \t"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (*p == '\0' || *p == ' ' || *p == '\t')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) token = match_token(p, key_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (test_and_set_bit(token, &token_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) case Opt_pcrinfo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) opt->pcrinfo_len = strlen(args[0].from) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) res = hex2bin(opt->pcrinfo, args[0].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) opt->pcrinfo_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) case Opt_keyhandle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) res = kstrtoul(args[0].from, 16, &handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) opt->keytype = SEAL_keytype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) opt->keyhandle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) case Opt_keyauth:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) res = hex2bin(opt->keyauth, args[0].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) SHA1_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) case Opt_blobauth:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * TPM 1.2 authorizations are sha1 hashes passed in as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * hex strings. TPM 2.0 authorizations are simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * passwords (although it can take a hash as well)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) opt->blobauth_len = strlen(args[0].from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (opt->blobauth_len == 2 * TPM_DIGEST_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) res = hex2bin(opt->blobauth, args[0].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) TPM_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) opt->blobauth_len = TPM_DIGEST_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) memcpy(opt->blobauth, args[0].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) opt->blobauth_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) case Opt_migratable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (*args[0].from == '0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) pay->migratable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) else if (*args[0].from != '1')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) case Opt_pcrlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) res = kstrtoul(args[0].from, 10, &lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) opt->pcrlock = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) case Opt_hash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (test_bit(Opt_policydigest, &token_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) for (i = 0; i < HASH_ALGO__LAST; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (!strcmp(args[0].from, hash_algo_name[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) opt->hash = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (i == HASH_ALGO__LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (!tpm2 && i != HASH_ALGO_SHA1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) case Opt_policydigest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) digest_len = hash_digest_size[opt->hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) res = hex2bin(opt->policydigest, args[0].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) digest_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) opt->policydigest_len = digest_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) case Opt_policyhandle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (!tpm2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) res = kstrtoul(args[0].from, 16, &handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) opt->policyhandle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * datablob_parse - parse the keyctl data and fill in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) * payload and options structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) * On success returns 0, otherwise -EINVAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) static int datablob_parse(char *datablob, struct trusted_key_payload *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) struct trusted_key_options *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) long keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) int key_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) char *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) /* main command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) c = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) key_cmd = match_token(c, key_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) switch (key_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) case Opt_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) /* first argument is key size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) c = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) ret = kstrtol(c, 10, &keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) p->key_len = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ret = getoptions(datablob, p, o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ret = Opt_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) case Opt_load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) /* first argument is sealed blob */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) c = strsep(&datablob, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) p->blob_len = strlen(c) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) if (p->blob_len > MAX_BLOB_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) ret = hex2bin(p->blob, c, p->blob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) ret = getoptions(datablob, p, o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) ret = Opt_load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) case Opt_update:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /* all arguments are options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) ret = getoptions(datablob, p, o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) ret = Opt_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) case Opt_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static struct trusted_key_options *trusted_options_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) struct trusted_key_options *options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) int tpm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) tpm2 = tpm_is_tpm2(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) if (tpm2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) options = kzalloc(sizeof *options, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) /* set any non-zero defaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) options->keytype = SRK_keytype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) if (!tpm2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) options->keyhandle = SRKHANDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) return options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct trusted_key_payload *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) ret = key_payload_reserve(key, sizeof *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) p = kzalloc(sizeof *p, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) p->migratable = 1; /* migratable by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * trusted_instantiate - create a new trusted key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * Unseal an existing trusted blob or, for a new key, get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * random key, then seal and create a trusted key-type key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * adding it to the specified keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * On success, return 0. Otherwise return errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) static int trusted_instantiate(struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct trusted_key_payload *payload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct trusted_key_options *options = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) size_t datalen = prep->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) char *datablob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) int key_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) size_t key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) int tpm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) tpm2 = tpm_is_tpm2(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (tpm2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return tpm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (datalen <= 0 || datalen > 32767 || !prep->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) datablob = kmalloc(datalen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (!datablob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) memcpy(datablob, prep->data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) datablob[datalen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) options = trusted_options_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (!options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) payload = trusted_payload_alloc(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (!payload) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) key_cmd = datablob_parse(datablob, payload, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (key_cmd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) ret = key_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (!options->keyhandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) dump_payload(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) dump_options(options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) switch (key_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) case Opt_load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (tpm2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) ret = tpm2_unseal_trusted(chip, payload, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) ret = key_unseal(payload, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) dump_payload(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) dump_options(options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) pr_info("trusted_key: key_unseal failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) case Opt_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) key_len = payload->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) ret = tpm_get_random(chip, payload->key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (ret != key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) pr_info("trusted_key: key_create failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (tpm2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) ret = tpm2_seal_trusted(chip, payload, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) ret = key_seal(payload, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) pr_info("trusted_key: key_seal failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (!ret && options->pcrlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) ret = pcrlock(options->pcrlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) kfree_sensitive(datablob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) kfree_sensitive(options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) rcu_assign_keypointer(key, payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) kfree_sensitive(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static void trusted_rcu_free(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct trusted_key_payload *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) p = container_of(rcu, struct trusted_key_payload, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) kfree_sensitive(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * trusted_update - reseal an existing key with new PCR values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) struct trusted_key_payload *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) struct trusted_key_payload *new_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) struct trusted_key_options *new_o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) size_t datalen = prep->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) char *datablob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) if (key_is_negative(key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) p = key->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) if (!p->migratable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (datalen <= 0 || datalen > 32767 || !prep->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) datablob = kmalloc(datalen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) if (!datablob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) new_o = trusted_options_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (!new_o) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) new_p = trusted_payload_alloc(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (!new_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) memcpy(datablob, prep->data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) datablob[datalen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) ret = datablob_parse(datablob, new_p, new_o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (ret != Opt_update) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) kfree_sensitive(new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (!new_o->keyhandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) kfree_sensitive(new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) /* copy old key values, and reseal with new pcrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) new_p->migratable = p->migratable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) new_p->key_len = p->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) memcpy(new_p->key, p->key, p->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) dump_payload(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) dump_payload(new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) ret = key_seal(new_p, new_o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) pr_info("trusted_key: key_seal failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) kfree_sensitive(new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (new_o->pcrlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ret = pcrlock(new_o->pcrlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) pr_info("trusted_key: pcrlock failed (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) kfree_sensitive(new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) rcu_assign_keypointer(key, new_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) call_rcu(&p->rcu, trusted_rcu_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) kfree_sensitive(datablob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) kfree_sensitive(new_o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * trusted_read - copy the sealed blob data to userspace in hex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * On success, return to userspace the trusted key datablob size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static long trusted_read(const struct key *key, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) const struct trusted_key_payload *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) char *bufp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) p = dereference_key_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (buffer && buflen >= 2 * p->blob_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) bufp = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) for (i = 0; i < p->blob_len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) bufp = hex_byte_pack(bufp, p->blob[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return 2 * p->blob_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) * trusted_destroy - clear and free the key's payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) static void trusted_destroy(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) kfree_sensitive(key->payload.data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct key_type key_type_trusted = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) .name = "trusted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) .instantiate = trusted_instantiate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) .update = trusted_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) .destroy = trusted_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) .describe = user_describe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) .read = trusted_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) EXPORT_SYMBOL_GPL(key_type_trusted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static void trusted_shash_release(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (hashalg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) crypto_free_shash(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) if (hmacalg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) crypto_free_shash(hmacalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static int __init trusted_shash_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (IS_ERR(hmacalg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) pr_info("trusted_key: could not allocate crypto %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) hmac_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return PTR_ERR(hmacalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) hashalg = crypto_alloc_shash(hash_alg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) if (IS_ERR(hashalg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) pr_info("trusted_key: could not allocate crypto %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) hash_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) ret = PTR_ERR(hashalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) goto hashalg_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) hashalg_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) crypto_free_shash(hmacalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static int __init init_digests(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (!digests)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) for (i = 0; i < chip->nr_allocated_banks; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) digests[i].alg_id = chip->allocated_banks[i].alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static int __init init_trusted(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) /* encrypted_keys.ko depends on successful load of this module even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * TPM is not used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) chip = tpm_default_chip();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) ret = init_digests();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) ret = trusted_shash_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) ret = register_key_type(&key_type_trusted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) goto err_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) err_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) trusted_shash_release();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) kfree(digests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static void __exit cleanup_trusted(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) if (chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) kfree(digests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) trusted_shash_release();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) unregister_key_type(&key_type_trusted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) late_initcall(init_trusted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) module_exit(cleanup_trusted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) MODULE_LICENSE("GPL");