Skip to contentSkip to conversation

Asymmetric TPM2 key type

RFC PATCH In review Jarkko Sakkinen 1 participant 7 messages May 19, 2024

Subsystems (8)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

v2 cover Asymmetric TPM2 key type

## Overview

Introduce tpm2_key_rsa implementing asymmetric TPM RSA key.

I submit this first as RFC as I could not execute the keyctl padd in the following sequence (returns EBADF):

tpm2_createprimary --hierarchy o -G rsa2048 -c owner.txt tpm2_evictcontrol -c owner.txt 0x81000001
tpm2_getcap handles-persistent
openssl genrsa -out private.pem 2048
tpm2_import -C 0x81000001 -G rsa -i private.pem -u key.pub -r key.priv tpm2_encodeobject -C 0x81000001 -u key.pub -r key.priv -o key.priv.pem openssl asn1parse -inform pem -in key.priv.pem -noout -out key.priv.der key_serial=cat key.priv.der | keyctl padd asymmetric tpm @u

This is derived work from James Prestwood’s earlier work from 2020:
https://lore.kernel.org/all/[email protected]/

## Change Log

v2:
Sorry for rush update. I noticed that not all changes were in the commits. This version just fixes those compilation errors.

James Prestwood (1):
keys: asymmetric: ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE

Jarkko Sakkinen (4):
crypto: rsa-pkcs1pad: export rsa1_asn_lookup()
tpm: export tpm2_load_context()
KEYS: trusted: Do not use WARN when encode fails
KEYS: trusted: Migrate tpm2_key_{encode,decode}() to TPM driver

 crypto/asymmetric_keys/Kconfig                |  13 +
 crypto/asymmetric_keys/Makefile               |   1 +
 crypto/asymmetric_keys/tpm2_key_rsa.c         | 655 ++++++++++++++++++
 crypto/rsa-pkcs1pad.c                         |  16 +-
 drivers/char/tpm/Kconfig                      |   2 +
 drivers/char/tpm/Makefile                     |   5 +
 drivers/char/tpm/tpm.h                        |   2 -
 drivers/char/tpm/tpm2-cmd.c                   |  77 ++
 drivers/char/tpm/tpm2-space.c                 |  61 --
 drivers/char/tpm/tpm2_key.c                   | 192 +++++
 .../char/tpm}/tpm2key.asn1                    |   0
 include/crypto/rsa-pkcs1pad.h                 |  20 +
 include/crypto/tpm2_key.h                     |  36 +
 include/linux/tpm.h                           |   3 +
 security/keys/trusted-keys/Makefile           |   2 -
 security/keys/trusted-keys/trusted_tpm2.c     | 183 +----
 16 files changed, 1032 insertions(+), 236 deletions(-)
 create mode 100644 crypto/asymmetric_keys/tpm2_key_rsa.c
 create mode 100644 drivers/char/tpm/tpm2_key.c
 rename {security/keys/trusted-keys => drivers/char/tpm}/tpm2key.asn1 (100%)
 create mode 100644 include/crypto/rsa-pkcs1pad.h
 create mode 100644 include/crypto/tpm2_key.h
Signature · 1 line
2.45.1
author maintainer
#1 lore raw

RFC PATCH v2 1/5 crypto: rsa-pkcs1pad: export rsa1_asn_lookup()

ASN.1 template is required for TPM2 asymmetric keys, as it needs to be piggy-packed with the input data before applying TPM2_RSA_Decrypt. This patch prepares crypto subsystem for the addition of those keys.

---
 crypto/rsa-pkcs1pad.c         | 16 ++++++++++------
 include/crypto/rsa-pkcs1pad.h | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)
 create mode 100644 include/crypto/rsa-pkcs1pad.h
crypto/rsa-pkcs1pad.c +10 -6
index cd501195f34a..00b6c14f861c 100644
@@ -7,6 +7,7 @@
 
 #include <crypto/algapi.h>
 #include <crypto/akcipher.h>
+#include <crypto/rsa-pkcs1pad.h>
 #include <crypto/internal/akcipher.h>
 #include <crypto/internal/rsa.h>
 #include <linux/err.h>
@@ -79,11 +80,7 @@ static const u8 rsa_digest_info_sha3_512[] = {
 	0x05, 0x00, 0x04, 0x40
 };
 
-static const struct rsa_asn1_template {
-	const char	*name;
-	const u8	*data;
-	size_t		size;
-} rsa_asn1_templates[] = {
+static const struct rsa_asn1_template rsa_asn1_templates[] = {
 #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
 	_(md5),
 	_(sha1),
@@ -101,7 +98,13 @@ static const struct rsa_asn1_template {
 	{ NULL }
 };
 
-static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
+/**
+ * rsa_lookup_asn1() - Lookup the ASN.1 digest info given the hash
+ * name:	hash algorithm name
+ *
+ * Returns the ASN.1 digest info on success, and NULL on failure.
+ */
+const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
 {
 	const struct rsa_asn1_template *p;
 
@@ -110,6 +113,7 @@ static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
 			return p;
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(rsa_lookup_asn1);
 
 struct pkcs1pad_ctx {
 	struct crypto_akcipher *child;
include/crypto/rsa-pkcs1pad.h new file +20 -0
new file mode 100644
index 000000000000..32c7453ff644
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * RSA padding templates.
+ */
+
+#ifndef _CRYPTO_RSA_PKCS1PAD_H
+#define _CRYPTO_RSA_PKCS1PAD_H
+
+/*
+ * Hash algorithm name to ASN.1 template mapping.
+ */
+struct rsa_asn1_template {
+	const char *name;
+	const u8 *data;
+	size_t size;
+};
+
+const struct rsa_asn1_template *rsa_lookup_asn1(const char *name);
+
+#endif /* _CRYPTO_RSA_PKCS1PAD_H */
Signature · 1 line
2.45.1
author maintainer
#2 lore raw

RFC PATCH v2 2/5 tpm: export tpm2_load_context()

In reply to Jarkko Sakkinen
---
 drivers/char/tpm/tpm.h        |  2 -
 drivers/char/tpm/tpm2-cmd.c   | 77 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm2-space.c | 61 ---------------------------
 include/linux/tpm.h           |  2 +
 4 files changed, 79 insertions(+), 63 deletions(-)
drivers/char/tpm/tpm.h +0 -2
index 6b8b9956ba69..c9c67fe84f33 100644
@@ -314,8 +314,6 @@ int tpm_devs_add(struct tpm_chip *chip);
 void tpm_devs_remove(struct tpm_chip *chip);
 int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
 		      unsigned int buf_size, unsigned int *offset);
-int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
-		      unsigned int *offset, u32 *handle);
 
 void tpm_bios_log_setup(struct tpm_chip *chip);
 void tpm_bios_log_teardown(struct tpm_chip *chip);
drivers/char/tpm/tpm2-cmd.c +77 -0

83 lines not on this page · raw message

drivers/char/tpm/tpm2-space.c +0 -61

73 lines not on this page · raw message

include/linux/tpm.h +2 -0
index c17e4efbb2e5..2f25ca07127b 100644
@@ -466,6 +466,8 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
 extern struct tpm_chip *tpm_default_chip(void);
 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
+int tpm2_load_context(struct tpm_chip *chip, const u8 *buf,
+		      unsigned int *offset, u32 *handle);
 
 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
 {
Signature · 1 line
2.45.1
author maintainer
#3 lore raw

RFC PATCH v2 3/5 KEYS: trusted: Do not use WARN when encode fails

In reply to Jarkko Sakkinen

When asn1_encode_sequence() fails, WARN is not the correct solution.

1. asn1_encode_sequence() is not an internal function (located in lib/asn1_encode.c).
2. Location is known, which makes the stack trace useless.
3. Results a crash if panic_on_warn is set.

It is also noteworthy that the use of WARN is undocumented, and it should be avoided unless there is a carefully considered rationale to use it.

Replace WARN with pr_err, and print the return value instead, which is only useful piece of information.

Cc [email protected] # v5.13+
Fixes f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
---
 security/keys/trusted-keys/trusted_tpm2.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +6 -2
index dfeec06301ce..dbdd6a318b8b 100644
@@ -38,6 +38,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 	u8 *end_work = scratch + SCRATCH_SIZE;
 	u8 *priv, *pub;
 	u16 priv_len, pub_len;
+	int ret;
 
 	priv_len = get_unaligned_be16(src) + 2;
 	priv = src;
@@ -79,8 +80,11 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 	work1 = payload->blob;
 	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
 				     scratch, work - scratch);
-	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
-		return PTR_ERR(work1);
+	if (IS_ERR(work1)) {
+		ret = PTR_ERR(work1);
+		pr_err("ASN.1 encode error %d\n", ret);
+		return ret;
+	}
 
 	return work1 - payload->blob;
 }
Signature · 1 line
2.45.1
author maintainer
#4 lore raw

RFC PATCH v2 4/5 KEYS: trusted: Migrate tpm2_key_{encode,decode}() to TPM driver

In reply to Jarkko Sakkinen

Migrate tpm2_key_{encode,decode}() to TPM driver and export the symbols to make them callable from trusted keys.

---
 drivers/char/tpm/Kconfig                      |   5 +
 drivers/char/tpm/Makefile                     |   5 +
 drivers/char/tpm/tpm2_key.c                   | 181 +++++++++++++++++
 .../char/tpm}/tpm2key.asn1                    |   0
 include/crypto/tpm2_key.h                     |  33 ++++
 security/keys/trusted-keys/Makefile           |   2 -
 security/keys/trusted-keys/trusted_tpm2.c     | 187 ++----------------
 7 files changed, 242 insertions(+), 171 deletions(-)
 create mode 100644 drivers/char/tpm/tpm2_key.c
 rename {security/keys/trusted-keys => drivers/char/tpm}/tpm2key.asn1 (100%)
 create mode 100644 include/crypto/tpm2_key.h
drivers/char/tpm/Kconfig +5 -0
index db41301e63f2..d5d06cc96932 100644
@@ -3,10 +3,15 @@
 # TPM device configuration
 #
 
+config TCG_TPM2_KEY
+	bool
+
 menuconfig TCG_TPM
 	tristate "TPM Hardware Support"
 	depends on HAS_IOMEM
 	imply SECURITYFS
+	select ASN1
+	select ASN1_ENCODER
 	select CRYPTO
 	select CRYPTO_HASH_INFO
 	help
drivers/char/tpm/Makefile +5 -0
index 4c695b0388f3..071437058ef6 100644
@@ -17,6 +17,11 @@ tpm-y += eventlog/tpm1.o
 tpm-y += eventlog/tpm2.o
 tpm-y += tpm-buf.o
 
+# TPM2 Asymmetric Key
+$(obj)/trusted_tpm2.o: $(obj)/tpm2key.asn1.h
+tpm-y += tpm2key.asn1.o
+tpm-y += tpm2_key.o
+
 tpm-$(CONFIG_TCG_TPM2_HMAC) += tpm2-sessions.o
 tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
 tpm-$(CONFIG_EFI) += eventlog/efi.o
drivers/char/tpm/tpm2_key.c new file +181 -0

181 lines not on this page · raw message

drivers/char/tpm/tpm2key.asn1 renamed from security/keys/trusted-keys/tpm2key.asn1 +0 -0
similarity index 100%
rename from security/keys/trusted-keys/tpm2key.asn1
rename to drivers/char/tpm/tpm2key.asn1
include/crypto/tpm2_key.h new file +33 -0
new file mode 100644
index 000000000000..e5d3330afef5
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_TPM2_KEY_H__
+#define __LINUX_TPM2_KEY_H__
+
+#include <linux/slab.h>
+
+/*
+ * TPM2 ASN.1 key
+ */
+struct tpm2_key {
+	u32 parent;
+	const u8 *pub;
+	u32 pub_len;
+	const u8 *priv;
+	u32 priv_len;
+};
+
+int tpm2_key_encode(u8 *blob, u32 blob_auth_len, u32 key_handle, u8 *src);
+int tpm2_key_decode(const u8 *src, u32 src_len, struct tpm2_key *key,
+		    u32 max_key_len);
+
+/**
+ * tpm2_key_free() - Release TPM2 asymmetric key resources and reset values
+ * @key:	TPM2 asymmetric key.
+ */
+static inline void tpm2_key_destroy(struct tpm2_key *key)
+{
+	kfree(key->priv);
+	kfree(key->pub);
+	memset(key, 0, sizeof(*key));
+}
+
+#endif /* __LINUX_TPM2_KEY_H__ */
security/keys/trusted-keys/Makefile +0 -2
index f0f3b27f688b..2674d5c10fc9 100644
@@ -7,9 +7,7 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
 trusted-y += trusted_core.o
 trusted-$(CONFIG_TRUSTED_KEYS_TPM) += trusted_tpm1.o
 
-$(obj)/trusted_tpm2.o: $(obj)/tpm2key.asn1.h
 trusted-$(CONFIG_TRUSTED_KEYS_TPM) += trusted_tpm2.o
-trusted-$(CONFIG_TRUSTED_KEYS_TPM) += tpm2key.asn1.o
 
 trusted-$(CONFIG_TRUSTED_KEYS_TEE) += trusted_tee.o
 
security/keys/trusted-keys/trusted_tpm2.c +18 -169

225 lines not on this page · raw message

Signature · 1 line
2.45.1
author maintainer
#5 lore raw

RFC PATCH v2 5/5 keys: asymmetric: ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE

In reply to Jarkko Sakkinen

From: James Prestwood <[email protected]>

Based on earlier work by James Prestwood.

Add ASN.1 compatible asymmetric TPM2 RSA key subtype:

1. Signing and decryption (with the private key) is handled by TPM2_RSA_Decrypt.
2. Encryption (with the public key) is handled by the kernel RSA implementation.

---
 crypto/asymmetric_keys/Kconfig        |  13 +
 crypto/asymmetric_keys/Makefile       |   1 +
 crypto/asymmetric_keys/tpm2_key_rsa.c | 655 ++++++++++++++++++++++++++
 drivers/char/tpm/Kconfig              |   3 -
 drivers/char/tpm/tpm2_key.c           |  11 +
 include/crypto/tpm2_key.h             |   3 +
 include/linux/tpm.h                   |   1 +
 7 files changed, 684 insertions(+), 3 deletions(-)
 create mode 100644 crypto/asymmetric_keys/tpm2_key_rsa.c
crypto/asymmetric_keys/Kconfig +13 -0
index e1345b8f39f1..f83defe562a1 100644
@@ -23,6 +23,19 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	  appropriate hash algorithms (such as SHA-1) must be available.
 	  ENOPKG will be reported if the requisite algorithm is unavailable.
 
+config ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE
+	tristate "Asymmetric TPM2 RSA crypto algorithm subtype"
+	depends on TCG_TPM
+	select TCG_TPM2_HMAC
+	select CRYPTO_RSA
+	select CRYPTO_SHA256
+	select CRYPTO_HASH_INFO
+	help
+	  This option provides support for asymmetric TPM2 key type handling.
+	  If signature generation and/or verification are to be used,
+	  appropriate hash algorithms (such as SHA-256) must be available.
+	  ENOPKG will be reported if the requisite algorithm is unavailable.
+
 config X509_CERTIFICATE_PARSER
 	tristate "X.509 certificate parser"
 	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
crypto/asymmetric_keys/Makefile +1 -0
index bc65d3b98dcb..c6da84607824 100644
@@ -11,6 +11,7 @@ asymmetric_keys-y := \
 	signature.o
 
 obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+obj-$(CONFIG_ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE) += tpm2_key_rsa.o
 
 #
 # X.509 Certificate handling
crypto/asymmetric_keys/tpm2_key_rsa.c new file +655 -0

655 lines not on this page · raw message

drivers/char/tpm/Kconfig +0 -3
index d5d06cc96932..d0f71f51f2ae 100644
@@ -3,9 +3,6 @@
 # TPM device configuration
 #
 
-config TCG_TPM2_KEY
-	bool
-
 menuconfig TCG_TPM
 	tristate "TPM Hardware Support"
 	depends on HAS_IOMEM
drivers/char/tpm/tpm2_key.c +11 -0
index e09441efb0f0..0d125b53de0e 100644
@@ -175,6 +175,17 @@ int tpm2_key_decode(const u8 *src, u32 src_len, struct tpm2_key *key,
 		return -ENOMEM;
 	}
 
+	ctx.blob_len = ctx.priv_len + ctx.pub_len;
+	ctx.blob = kmalloc(ctx.priv_len + ctx.pub_len, GFP_KERNEL);
+	if (!ctx.blob) {
+		kfree(ctx.pub);
+		kfree(ctx.priv);
+		return -ENOMEM;
+	}
+
+	memcpy((void *)ctx.blob, ctx.priv, ctx.priv_len);
+	memcpy((void *)ctx.blob + ctx.priv_len, ctx.pub, ctx.pub_len);
+
 	memcpy(key, &ctx, sizeof(ctx));
 	return 0;
 }
include/crypto/tpm2_key.h +3 -0
index e5d3330afef5..7b5c5ec6bed5 100644
@@ -13,6 +13,8 @@ struct tpm2_key {
 	u32 pub_len;
 	const u8 *priv;
 	u32 priv_len;
+	const u8 *blob;
+	u32 blob_len;
 };
 
 int tpm2_key_encode(u8 *blob, u32 blob_auth_len, u32 key_handle, u8 *src);
@@ -27,6 +29,7 @@ static inline void tpm2_key_destroy(struct tpm2_key *key)
 {
 	kfree(key->priv);
 	kfree(key->pub);
+	kfree(key->blob);
 	memset(key, 0, sizeof(*key));
 }
 
include/linux/tpm.h +1 -0
index 2f25ca07127b..238814ee5c6f 100644
@@ -271,6 +271,7 @@ enum tpm2_command_codes {
 	TPM2_CC_NV_READ                 = 0x014E,
 	TPM2_CC_CREATE		        = 0x0153,
 	TPM2_CC_LOAD		        = 0x0157,
+	TPM2_CC_RSA_DECRYPT	        = 0x0159,
 	TPM2_CC_SEQUENCE_UPDATE         = 0x015C,
 	TPM2_CC_UNSEAL		        = 0x015E,
 	TPM2_CC_CONTEXT_LOAD	        = 0x0161,
Signature · 1 line
2.45.1
author maintainer
#6 lore raw
In reply to Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen

## Overview

Introduce tpm2_key_rsa implementing asymmetric TPM RSA key.

I submit this first as RFC as I could not execute the keyctl padd in the following sequence (returns EBADF):

Quoted 8 lines

tpm2_createprimary --hierarchy o -G rsa2048 -c owner.txt tpm2_evictcontrol -c owner.txt 0x81000001
tpm2_getcap handles-persistent
openssl genrsa -out private.pem 2048
tpm2_import -C 0x81000001 -G rsa -i private.pem -u key.pub -r key.priv tpm2_encodeobject -C 0x81000001 -u key.pub -r key.priv -o key.priv.pem openssl asn1parse -inform pem -in key.priv.pem -noout -out key.priv.der key_serial=cat key.priv.der | keyctl padd asymmetric tpm @u

After v2 changes it ends up to -EINVAL and:

OID is "2.23.133.10.1.3" which is not TPMSealedData

which makes total sense. James' old patch set has already TPMLoadableKey parsing PoC'd so I use that as the reference.

After the sequence above successfully completes keyctl public key ops are accesible by using $key_serial as the serial.

BR, Jarkko