1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "ASYM-TPM: "fmt
3 #include <linux/slab.h>
4 #include <linux/module.h>
5 #include <linux/export.h>
6 #include <linux/kernel.h>
7 #include <linux/seq_file.h>
8 #include <linux/scatterlist.h>
10 #include <linux/tpm_command.h>
11 #include <crypto/akcipher.h>
12 #include <crypto/hash.h>
13 #include <crypto/sha.h>
14 #include <asm/unaligned.h>
15 #include <keys/asymmetric-subtype.h>
16 #include <keys/trusted_tpm.h>
17 #include <crypto/asym_tpm_subtype.h>
18 #include <crypto/public_key.h>
20 #define TPM_ORD_FLUSHSPECIFIC 186
21 #define TPM_ORD_LOADKEY2 65
22 #define TPM_ORD_UNBIND 30
23 #define TPM_ORD_SIGN 60
25 #define TPM_RT_KEY 0x00000001
28 * Load a TPM key from the blob provided by userspace
30 static int tpm_loadkey2(struct tpm_buf *tb,
31 uint32_t keyhandle, unsigned char *keyauth,
32 const unsigned char *keyblob, int keybloblen,
35 unsigned char nonceodd[TPM_NONCE_SIZE];
36 unsigned char enonce[TPM_NONCE_SIZE];
37 unsigned char authdata[SHA1_DIGEST_SIZE];
38 uint32_t authhandle = 0;
39 unsigned char cont = 0;
43 ordinal = htonl(TPM_ORD_LOADKEY2);
45 /* session for loading the key */
46 ret = oiap(tb, &authhandle, enonce);
48 pr_info("oiap failed (%d)\n", ret);
52 /* generate odd nonce */
53 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
55 pr_info("tpm_get_random failed (%d)\n", ret);
59 /* calculate authorization HMAC value */
60 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
61 nonceodd, cont, sizeof(uint32_t), &ordinal,
62 keybloblen, keyblob, 0, 0);
66 /* build the request buffer */
67 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_LOADKEY2);
68 tpm_buf_append_u32(tb, keyhandle);
69 tpm_buf_append(tb, keyblob, keybloblen);
70 tpm_buf_append_u32(tb, authhandle);
71 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
72 tpm_buf_append_u8(tb, cont);
73 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
75 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
77 pr_info("authhmac failed (%d)\n", ret);
81 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd, keyauth,
82 SHA1_DIGEST_SIZE, 0, 0);
84 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
88 *newhandle = LOAD32(tb->data, TPM_DATA_OFFSET);
93 * Execute the FlushSpecific TPM command
95 static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
97 tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_FLUSHSPECIFIC);
98 tpm_buf_append_u32(tb, handle);
99 tpm_buf_append_u32(tb, TPM_RT_KEY);
101 return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
105 * Decrypt a blob provided by userspace using a specific key handle.
106 * The handle is a well known handle or previously loaded by e.g. LoadKey2
108 static int tpm_unbind(struct tpm_buf *tb,
109 uint32_t keyhandle, unsigned char *keyauth,
110 const unsigned char *blob, uint32_t bloblen,
111 void *out, uint32_t outlen)
113 unsigned char nonceodd[TPM_NONCE_SIZE];
114 unsigned char enonce[TPM_NONCE_SIZE];
115 unsigned char authdata[SHA1_DIGEST_SIZE];
116 uint32_t authhandle = 0;
117 unsigned char cont = 0;
122 ordinal = htonl(TPM_ORD_UNBIND);
123 datalen = htonl(bloblen);
125 /* session for loading the key */
126 ret = oiap(tb, &authhandle, enonce);
128 pr_info("oiap failed (%d)\n", ret);
132 /* generate odd nonce */
133 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
135 pr_info("tpm_get_random failed (%d)\n", ret);
139 /* calculate authorization HMAC value */
140 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
141 nonceodd, cont, sizeof(uint32_t), &ordinal,
142 sizeof(uint32_t), &datalen,
143 bloblen, blob, 0, 0);
147 /* build the request buffer */
148 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_UNBIND);
149 tpm_buf_append_u32(tb, keyhandle);
150 tpm_buf_append_u32(tb, bloblen);
151 tpm_buf_append(tb, blob, bloblen);
152 tpm_buf_append_u32(tb, authhandle);
153 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
154 tpm_buf_append_u8(tb, cont);
155 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
157 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
159 pr_info("authhmac failed (%d)\n", ret);
163 datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
165 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
166 keyauth, SHA1_DIGEST_SIZE,
167 sizeof(uint32_t), TPM_DATA_OFFSET,
168 datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
171 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
175 memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
176 min(outlen, datalen));
182 * Sign a blob provided by userspace (that has had the hash function applied)
183 * using a specific key handle. The handle is assumed to have been previously
184 * loaded by e.g. LoadKey2.
186 * Note that the key signature scheme of the used key should be set to
187 * TPM_SS_RSASSAPKCS1v15_DER. This allows the hashed input to be of any size
188 * up to key_length_in_bytes - 11 and not be limited to size 20 like the
189 * TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
191 static int tpm_sign(struct tpm_buf *tb,
192 uint32_t keyhandle, unsigned char *keyauth,
193 const unsigned char *blob, uint32_t bloblen,
194 void *out, uint32_t outlen)
196 unsigned char nonceodd[TPM_NONCE_SIZE];
197 unsigned char enonce[TPM_NONCE_SIZE];
198 unsigned char authdata[SHA1_DIGEST_SIZE];
199 uint32_t authhandle = 0;
200 unsigned char cont = 0;
205 ordinal = htonl(TPM_ORD_SIGN);
206 datalen = htonl(bloblen);
208 /* session for loading the key */
209 ret = oiap(tb, &authhandle, enonce);
211 pr_info("oiap failed (%d)\n", ret);
215 /* generate odd nonce */
216 ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
218 pr_info("tpm_get_random failed (%d)\n", ret);
222 /* calculate authorization HMAC value */
223 ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
224 nonceodd, cont, sizeof(uint32_t), &ordinal,
225 sizeof(uint32_t), &datalen,
226 bloblen, blob, 0, 0);
230 /* build the request buffer */
231 tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SIGN);
232 tpm_buf_append_u32(tb, keyhandle);
233 tpm_buf_append_u32(tb, bloblen);
234 tpm_buf_append(tb, blob, bloblen);
235 tpm_buf_append_u32(tb, authhandle);
236 tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
237 tpm_buf_append_u8(tb, cont);
238 tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
240 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
242 pr_info("authhmac failed (%d)\n", ret);
246 datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
248 ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
249 keyauth, SHA1_DIGEST_SIZE,
250 sizeof(uint32_t), TPM_DATA_OFFSET,
251 datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
254 pr_info("TSS_checkhmac1 failed (%d)\n", ret);
258 memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
259 min(datalen, outlen));
264 /* Room to fit two u32 zeros for algo id and parameters length. */
265 #define SETKEY_PARAMS_SIZE (sizeof(u32) * 2)
268 * Maximum buffer size for the BER/DER encoded public key. The public key
269 * is of the form SEQUENCE { INTEGER n, INTEGER e } where n is a maximum 2048
270 * bit key and e is usually 65537
271 * The encoding overhead is:
272 * - max 4 bytes for SEQUENCE
273 * - max 4 bytes for INTEGER n type/length
275 * - max 2 bytes for INTEGER e type/length
277 * - 4+4 of zeros for set_pub_key parameters (SETKEY_PARAMS_SIZE)
279 #define PUB_KEY_BUF_SIZE (4 + 4 + 257 + 2 + 3 + SETKEY_PARAMS_SIZE)
282 * Provide a part of a description of the key for /proc/keys.
284 static void asym_tpm_describe(const struct key *asymmetric_key,
287 struct tpm_key *tk = asymmetric_key->payload.data[asym_crypto];
292 seq_printf(m, "TPM1.2/Blob");
295 static void asym_tpm_destroy(void *payload0, void *payload3)
297 struct tpm_key *tk = payload0;
308 /* How many bytes will it take to encode the length */
309 static inline uint32_t definite_length(uint32_t len)
318 static inline uint8_t *encode_tag_length(uint8_t *buf, uint8_t tag,
335 put_unaligned_be16(len, buf + 1);
339 static uint32_t derive_pub_key(const void *pub_key, uint32_t len, uint8_t *buf)
342 uint32_t n_len = definite_length(len) + 1 + len + 1;
343 uint32_t e_len = definite_length(3) + 1 + 3;
344 uint8_t e[3] = { 0x01, 0x00, 0x01 };
347 cur = encode_tag_length(cur, 0x30, n_len + e_len);
349 cur = encode_tag_length(cur, 0x02, len + 1);
351 memcpy(cur + 1, pub_key, len);
353 cur = encode_tag_length(cur, 0x02, sizeof(e));
354 memcpy(cur, e, sizeof(e));
356 /* Zero parameters to satisfy set_pub_key ABI. */
357 memzero_explicit(cur, SETKEY_PARAMS_SIZE);
363 * Determine the crypto algorithm name.
365 static int determine_akcipher(const char *encoding, const char *hash_algo,
366 char alg_name[CRYPTO_MAX_ALG_NAME])
368 if (strcmp(encoding, "pkcs1") == 0) {
370 strcpy(alg_name, "pkcs1pad(rsa)");
374 if (snprintf(alg_name, CRYPTO_MAX_ALG_NAME, "pkcs1pad(rsa,%s)",
375 hash_algo) >= CRYPTO_MAX_ALG_NAME)
381 if (strcmp(encoding, "raw") == 0) {
382 strcpy(alg_name, "rsa");
390 * Query information about a key.
392 static int tpm_key_query(const struct kernel_pkey_params *params,
393 struct kernel_pkey_query *info)
395 struct tpm_key *tk = params->key->payload.data[asym_crypto];
397 char alg_name[CRYPTO_MAX_ALG_NAME];
398 struct crypto_akcipher *tfm;
399 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
400 uint32_t der_pub_key_len;
403 /* TPM only works on private keys, public keys still done in software */
404 ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
408 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
412 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
415 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
419 len = crypto_akcipher_maxsize(tfm);
421 info->key_size = tk->key_len;
422 info->max_data_size = tk->key_len / 8;
423 info->max_sig_size = len;
424 info->max_enc_size = len;
425 info->max_dec_size = tk->key_len / 8;
427 info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT |
428 KEYCTL_SUPPORTS_DECRYPT |
429 KEYCTL_SUPPORTS_VERIFY |
430 KEYCTL_SUPPORTS_SIGN;
434 crypto_free_akcipher(tfm);
435 pr_devel("<==%s() = %d\n", __func__, ret);
440 * Encryption operation is performed with the public key. Hence it is done
443 static int tpm_key_encrypt(struct tpm_key *tk,
444 struct kernel_pkey_params *params,
445 const void *in, void *out)
447 char alg_name[CRYPTO_MAX_ALG_NAME];
448 struct crypto_akcipher *tfm;
449 struct akcipher_request *req;
450 struct crypto_wait cwait;
451 struct scatterlist in_sg, out_sg;
452 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
453 uint32_t der_pub_key_len;
456 pr_devel("==>%s()\n", __func__);
458 ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
462 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
466 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
469 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
474 req = akcipher_request_alloc(tfm, GFP_KERNEL);
478 sg_init_one(&in_sg, in, params->in_len);
479 sg_init_one(&out_sg, out, params->out_len);
480 akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
482 crypto_init_wait(&cwait);
483 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
484 CRYPTO_TFM_REQ_MAY_SLEEP,
485 crypto_req_done, &cwait);
487 ret = crypto_akcipher_encrypt(req);
488 ret = crypto_wait_req(ret, &cwait);
493 akcipher_request_free(req);
495 crypto_free_akcipher(tfm);
496 pr_devel("<==%s() = %d\n", __func__, ret);
501 * Decryption operation is performed with the private key in the TPM.
503 static int tpm_key_decrypt(struct tpm_key *tk,
504 struct kernel_pkey_params *params,
505 const void *in, void *out)
509 uint8_t srkauth[SHA1_DIGEST_SIZE];
510 uint8_t keyauth[SHA1_DIGEST_SIZE];
513 pr_devel("==>%s()\n", __func__);
515 if (params->hash_algo)
518 if (strcmp(params->encoding, "pkcs1"))
521 r = tpm_buf_init(&tb, 0, 0);
525 /* TODO: Handle a non-all zero SRK authorization */
526 memset(srkauth, 0, sizeof(srkauth));
528 r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
529 tk->blob, tk->blob_len, &keyhandle);
531 pr_devel("loadkey2 failed (%d)\n", r);
535 /* TODO: Handle a non-all zero key authorization */
536 memset(keyauth, 0, sizeof(keyauth));
538 r = tpm_unbind(&tb, keyhandle, keyauth,
539 in, params->in_len, out, params->out_len);
541 pr_devel("tpm_unbind failed (%d)\n", r);
543 if (tpm_flushspecific(&tb, keyhandle) < 0)
544 pr_devel("flushspecific failed (%d)\n", r);
547 tpm_buf_destroy(&tb);
548 pr_devel("<==%s() = %d\n", __func__, r);
553 * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
555 static const u8 digest_info_md5[] = {
556 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
557 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
558 0x05, 0x00, 0x04, 0x10
561 static const u8 digest_info_sha1[] = {
562 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
563 0x2b, 0x0e, 0x03, 0x02, 0x1a,
564 0x05, 0x00, 0x04, 0x14
567 static const u8 digest_info_rmd160[] = {
568 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
569 0x2b, 0x24, 0x03, 0x02, 0x01,
570 0x05, 0x00, 0x04, 0x14
573 static const u8 digest_info_sha224[] = {
574 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
575 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
576 0x05, 0x00, 0x04, 0x1c
579 static const u8 digest_info_sha256[] = {
580 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
581 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
582 0x05, 0x00, 0x04, 0x20
585 static const u8 digest_info_sha384[] = {
586 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
587 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
588 0x05, 0x00, 0x04, 0x30
591 static const u8 digest_info_sha512[] = {
592 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
593 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
594 0x05, 0x00, 0x04, 0x40
597 static const struct asn1_template {
601 } asn1_templates[] = {
602 #define _(X) { #X, digest_info_##X, sizeof(digest_info_##X) }
614 static const struct asn1_template *lookup_asn1(const char *name)
616 const struct asn1_template *p;
618 for (p = asn1_templates; p->name; p++)
619 if (strcmp(name, p->name) == 0)
625 * Sign operation is performed with the private key in the TPM.
627 static int tpm_key_sign(struct tpm_key *tk,
628 struct kernel_pkey_params *params,
629 const void *in, void *out)
633 uint8_t srkauth[SHA1_DIGEST_SIZE];
634 uint8_t keyauth[SHA1_DIGEST_SIZE];
635 void *asn1_wrapped = NULL;
636 uint32_t in_len = params->in_len;
639 pr_devel("==>%s()\n", __func__);
641 if (strcmp(params->encoding, "pkcs1"))
644 if (params->hash_algo) {
645 const struct asn1_template *asn1 =
646 lookup_asn1(params->hash_algo);
651 /* request enough space for the ASN.1 template + input hash */
652 asn1_wrapped = kzalloc(in_len + asn1->size, GFP_KERNEL);
656 /* Copy ASN.1 template, then the input */
657 memcpy(asn1_wrapped, asn1->data, asn1->size);
658 memcpy(asn1_wrapped + asn1->size, in, in_len);
661 in_len += asn1->size;
664 if (in_len > tk->key_len / 8 - 11) {
666 goto error_free_asn1_wrapped;
669 r = tpm_buf_init(&tb, 0, 0);
671 goto error_free_asn1_wrapped;
673 /* TODO: Handle a non-all zero SRK authorization */
674 memset(srkauth, 0, sizeof(srkauth));
676 r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
677 tk->blob, tk->blob_len, &keyhandle);
679 pr_devel("loadkey2 failed (%d)\n", r);
683 /* TODO: Handle a non-all zero key authorization */
684 memset(keyauth, 0, sizeof(keyauth));
686 r = tpm_sign(&tb, keyhandle, keyauth, in, in_len, out, params->out_len);
688 pr_devel("tpm_sign failed (%d)\n", r);
690 if (tpm_flushspecific(&tb, keyhandle) < 0)
691 pr_devel("flushspecific failed (%d)\n", r);
694 tpm_buf_destroy(&tb);
695 error_free_asn1_wrapped:
697 pr_devel("<==%s() = %d\n", __func__, r);
702 * Do encryption, decryption and signing ops.
704 static int tpm_key_eds_op(struct kernel_pkey_params *params,
705 const void *in, void *out)
707 struct tpm_key *tk = params->key->payload.data[asym_crypto];
708 int ret = -EOPNOTSUPP;
710 /* Perform the encryption calculation. */
711 switch (params->op) {
712 case kernel_pkey_encrypt:
713 ret = tpm_key_encrypt(tk, params, in, out);
715 case kernel_pkey_decrypt:
716 ret = tpm_key_decrypt(tk, params, in, out);
718 case kernel_pkey_sign:
719 ret = tpm_key_sign(tk, params, in, out);
729 * Verify a signature using a public key.
731 static int tpm_key_verify_signature(const struct key *key,
732 const struct public_key_signature *sig)
734 const struct tpm_key *tk = key->payload.data[asym_crypto];
735 struct crypto_wait cwait;
736 struct crypto_akcipher *tfm;
737 struct akcipher_request *req;
738 struct scatterlist src_sg[2];
739 char alg_name[CRYPTO_MAX_ALG_NAME];
740 uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
741 uint32_t der_pub_key_len;
744 pr_devel("==>%s()\n", __func__);
753 ret = determine_akcipher(sig->encoding, sig->hash_algo, alg_name);
757 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
761 der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
764 ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
769 req = akcipher_request_alloc(tfm, GFP_KERNEL);
773 sg_init_table(src_sg, 2);
774 sg_set_buf(&src_sg[0], sig->s, sig->s_size);
775 sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
776 akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
778 crypto_init_wait(&cwait);
779 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
780 CRYPTO_TFM_REQ_MAY_SLEEP,
781 crypto_req_done, &cwait);
782 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
784 akcipher_request_free(req);
786 crypto_free_akcipher(tfm);
787 pr_devel("<==%s() = %d\n", __func__, ret);
788 if (WARN_ON_ONCE(ret > 0))
794 * Parse enough information out of TPM_KEY structure:
795 * TPM_STRUCT_VER -> 4 bytes
796 * TPM_KEY_USAGE -> 2 bytes
797 * TPM_KEY_FLAGS -> 4 bytes
798 * TPM_AUTH_DATA_USAGE -> 1 byte
799 * TPM_KEY_PARMS -> variable
800 * UINT32 PCRInfoSize -> 4 bytes
801 * BYTE* -> PCRInfoSize bytes
803 * UINT32 encDataSize;
804 * BYTE* -> encDataSize;
807 * TPM_ALGORITHM_ID -> 4 bytes
808 * TPM_ENC_SCHEME -> 2 bytes
809 * TPM_SIG_SCHEME -> 2 bytes
810 * UINT32 parmSize -> 4 bytes
813 static int extract_key_parameters(struct tpm_key *tk)
815 const void *cur = tk->blob;
816 uint32_t len = tk->blob_len;
824 /* Ensure this is a legacy key */
825 if (get_unaligned_be16(cur + 4) != 0x0015)
828 /* Skip to TPM_KEY_PARMS */
835 /* Make sure this is an RSA key */
836 if (get_unaligned_be32(cur) != 0x00000001)
839 /* Make sure this is TPM_ES_RSAESPKCSv15 encoding scheme */
840 if (get_unaligned_be16(cur + 4) != 0x0002)
843 /* Make sure this is TPM_SS_RSASSAPKCS1v15_DER signature scheme */
844 if (get_unaligned_be16(cur + 6) != 0x0003)
847 sz = get_unaligned_be32(cur + 8);
851 /* Move to TPM_RSA_KEY_PARMS */
855 /* Grab the RSA key length */
856 key_len = get_unaligned_be32(cur);
868 /* Move just past TPM_KEY_PARMS */
875 sz = get_unaligned_be32(cur);
879 /* Move to TPM_STORE_PUBKEY */
883 /* Grab the size of the public key, it should jive with the key size */
884 sz = get_unaligned_be32(cur);
890 tk->key_len = key_len;
891 tk->pub_key = pub_key;
892 tk->pub_key_len = sz;
897 /* Given the blob, parse it and load it into the TPM */
898 struct tpm_key *tpm_key_create(const void *blob, uint32_t blob_len)
903 r = tpm_is_tpm2(NULL);
907 /* We don't support TPM2 yet */
914 tk = kzalloc(sizeof(struct tpm_key), GFP_KERNEL);
918 tk->blob = kmemdup(blob, blob_len, GFP_KERNEL);
922 tk->blob_len = blob_len;
924 r = extract_key_parameters(tk);
938 EXPORT_SYMBOL_GPL(tpm_key_create);
941 * TPM-based asymmetric key subtype
943 struct asymmetric_key_subtype asym_tpm_subtype = {
944 .owner = THIS_MODULE,
946 .name_len = sizeof("asym_tpm") - 1,
947 .describe = asym_tpm_describe,
948 .destroy = asym_tpm_destroy,
949 .query = tpm_key_query,
950 .eds_op = tpm_key_eds_op,
951 .verify_signature = tpm_key_verify_signature,
953 EXPORT_SYMBOL_GPL(asym_tpm_subtype);
955 MODULE_DESCRIPTION("TPM based asymmetric key subtype");
956 MODULE_AUTHOR("Intel Corporation");
957 MODULE_LICENSE("GPL v2");