Skip to contentSkip to conversation

Optimize tpm2_read_public() calls

PATCH In review Jarkko Sakkinen 1 participant 5 messages Dec 8, 2025

Subsystems (4)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

v7 cover Optimize tpm2_read_public() calls

The main goal is fairly straight-forwrd here.

The aim of these patches is optimize the number of tpm2_read_public() calls to the bare minimum.

## About dropping 'parentName' attribute for ASN.1 keys from the patch set

I wrote this section as a remainder as I have facts fresh in my mind so that I can return them as soon as there is working group for the ASN.1 specification. We really need to have this in the spec.

I dropped [1] given that [2] is landing shortly to IETF draft process, according to James Bottomley [3]. We will return to [1] as soon as draft process is open for comments. Still, that attribute is super important, and here is why.

This will cause a overhead as tpm2_unseal_trusted needs to do an unnecessary (from pure technical perspective) TPM2_ReadPublic command to acquire TPM name of the parent. This is obviously known at the time of creation of a key but the information is not stored anywhere by the key format.

It also aligns badly with TCG specifications as Table 6 of architecture spec explicitly defines a reference (or name) for transient keys, persistent keys and NV indexes to be TPM_ALG_ID concatenated together with the hash of TPMT_PUBLIC. I.e. the file format is using exactly the opposite what should be use as reference for keys than what it should use.

Other benefits are of course auto-discovery of parent for a key file, which is nasty to do without the name pre-stored.

[1] https://lore.kernel.org/linux-integrity/[email protected]/
[2] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.txt
[3] https://lore.kernel.org/linux-integrity/89d90617ba9b7a5eff1d5fad6bb9773033d3c18c.camel@HansenPartnership.com/

Jarkko Sakkinen (3):
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES

 drivers/char/tpm/tpm2-cmd.c               |   3 +-
 drivers/char/tpm/tpm2-sessions.c          | 116 ++++++--------------
 include/linux/tpm.h                       |  19 +++-
 security/keys/trusted-keys/trusted_tpm2.c | 124 ++++++++++++++--------
 4 files changed, 133 insertions(+), 129 deletions(-)
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH v7 1/3 tpm2-sessions: Define TPM2_NAME_MAX_SIZE

This is somewhat cosmetic change but it does serve a purpose on tracking the value set for the maximum length of TPM names, and to clearly states what components it is composed of. It also anchors the value so that when buffers are declared for this particular purpose, the same value is used for the capacity.

---
v7:
- A new patch.

---
 drivers/char/tpm/tpm2-sessions.c | 2 +-
 include/linux/tpm.h              | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm2-sessions.c +1 -1
index 79f27a46bd7f..a0c88fb1804c 100644
@@ -137,7 +137,7 @@ struct tpm2_auth {
 	 * we must compute and remember
 	 */
 	u32 name_h[AUTH_MAX_NAMES];
-	u8 name[AUTH_MAX_NAMES][2 + SHA512_DIGEST_SIZE];
+	u8 name[AUTH_MAX_NAMES][TPM2_MAX_NAME_SIZE];
 };
 
 #ifdef CONFIG_TCG_TPM2_HMAC
include/linux/tpm.h +9 -0
index 42e2a091f43d..b4b5bd586501 100644
@@ -28,7 +28,16 @@
 #define TPM_DIGEST_SIZE		20	/* Max TPM v1.2 PCR size */
 #define TPM_BUFSIZE		4096
 
+/*
+ * SHA-512 is, as of today, the largest digest in the TCG algorithm repository.
+ */
 #define TPM2_MAX_DIGEST_SIZE	SHA512_DIGEST_SIZE
+
+/*
+ * A TPM name digest i.e., TPMT_HA, is a concatenation of TPM_ALG_ID of the
+ * name algorithm and hash of TPMT_PUBLIC.
+ */
+#define TPM2_MAX_NAME_SIZE	(TPM2_MAX_DIGEST_SIZE + 2)
 #define TPM2_MAX_PCR_BANKS	8
 
 struct tpm_chip;
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH v7 2/3 KEYS: trusted: Re-orchestrate tpm2_read_public() calls

In reply to Jarkko Sakkinen

tpm2_load_cmd() and tpm2_unseal_cmd() use the same parent, and calls to tpm_buf_append_name() cause the exact same TPM2_ReadPublic command to be sent to the chip, causing unnecessary traffic.

1. Export tpm2_read_public in order to make it callable from 'trusted_tpm2'.
2. Re-orchestrate tpm2_seal_trusted() and tpm2_unseal_trusted() in order to halve the name resolutions required:
2a. Move tpm2_read_public() calls into trusted_tpm2.
2b. Pass TPM name to tpm_buf_append_name().
2c. Rework tpm_buf_append_name() to use the pre-resolved name.

---
v7:
- No changes of any of the versions so far.

---
 drivers/char/tpm/tpm2-cmd.c               |   3 +-
 drivers/char/tpm/tpm2-sessions.c          |  95 +++++------------
 include/linux/tpm.h                       |  10 +-
 security/keys/trusted-keys/trusted_tpm2.c | 124 ++++++++++++++--------
 4 files changed, 118 insertions(+), 114 deletions(-)
drivers/char/tpm/tpm2-cmd.c +2 -1
index 2682f5ec09cb..5b04e74b6377 100644
@@ -199,7 +199,8 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
 
 	if (!disable_pcr_integrity) {
-		rc = tpm_buf_append_name(chip, buf, pcr_idx, NULL);
+		rc = tpm_buf_append_name(chip, buf, pcr_idx, (u8 *)&pcr_idx,
+					 sizeof(u32));
 		if (rc)
 			return rc;
 		tpm_buf_append_hmac_session(chip, buf, 0, NULL, 0);
drivers/char/tpm/tpm2-sessions.c +28 -67

153 lines not on this page · raw message

include/linux/tpm.h +9 -1
index b4b5bd586501..db716841973f 100644
@@ -539,7 +539,7 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip)
 }
 
 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
-			u32 handle, u8 *name);
+			u32 handle, u8 *name, u16 name_size);
 void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,
 				 u8 attributes, u8 *passphrase,
 				 int passphraselen);
@@ -553,6 +553,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);
 int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 				int rc);
 void tpm2_end_auth_session(struct tpm_chip *chip);
+int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name);
 #else
 #include <linux/unaligned.h>
 
@@ -576,6 +577,13 @@ static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip,
 {
 	return rc;
 }
+
+static inline int tpm2_read_public(struct tpm_chip *chip, u32 handle,
+				   void *name)
+{
+	memcpy(name, &handle, sizeof(u32));
+	return sizeof(u32);
+}
 #endif	/* CONFIG_TCG_TPM2_HMAC */
 
 #endif
security/keys/trusted-keys/trusted_tpm2.c +79 -45

211 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH v7 3/3 tpm2-sessions: Remove AUTH_MAX_NAMES

In reply to Jarkko Sakkinen

In all of the call sites only one session is ever append. Thus, reduce AUTH_MAX_NAMES, which leads into removing constant completely.

---
v7:
- A new patch.

---
 drivers/char/tpm/tpm2-sessions.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)
drivers/char/tpm/tpm2-sessions.c +11 -20
index 0816a91134fc..62a200ae72d7 100644
@@ -72,9 +72,6 @@
 #include <crypto/sha2.h>
 #include <crypto/utils.h>
 
-/* maximum number of names the TPM must remember for authorization */
-#define AUTH_MAX_NAMES	3
-
 #define AES_KEY_BYTES	AES_KEYSIZE_128
 #define AES_KEY_BITS	(AES_KEY_BYTES*8)
 
@@ -136,8 +133,8 @@ struct tpm2_auth {
 	 * handle, but they are part of the session by name, which
 	 * we must compute and remember
 	 */
-	u8 name[AUTH_MAX_NAMES][TPM2_MAX_NAME_SIZE];
-	u16 name_size_tbl[AUTH_MAX_NAMES];
+	u8 name[TPM2_MAX_NAME_SIZE];
+	u16 name_size;
 };
 
 #ifdef CONFIG_TCG_TPM2_HMAC
@@ -254,11 +251,14 @@ EXPORT_SYMBOL_GPL(tpm2_read_public);
 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
 			u32 handle, u8 *name, u16 name_size)
 {
-#ifdef CONFIG_TCG_TPM2_HMAC
 	struct tpm2_auth *auth;
-	int slot;
 	int ret;
-#endif
+
+	if (tpm_buf_length(buf) != TPM_HEADER_SIZE) {
+		dev_err(&chip->dev, "too many handles\n");
+		ret = -EIO;
+		goto err;
+	}
 
 	if (!tpm2_chip_auth(chip)) {
 		tpm_buf_append_handle(buf, handle);
@@ -266,12 +266,6 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 
 #ifdef CONFIG_TCG_TPM2_HMAC
-	slot = (tpm_buf_length(buf) - TPM_HEADER_SIZE) / 4;
-	if (slot >= AUTH_MAX_NAMES) {
-		dev_err(&chip->dev, "too many handles\n");
-		ret = -EIO;
-		goto err;
-	}
 	auth = chip->auth;
 	if (auth->session != tpm_buf_length(buf)) {
 		dev_err(&chip->dev, "session state malformed");
@@ -280,16 +274,14 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 	tpm_buf_append_u32(buf, handle);
 	auth->session += 4;
-	memcpy(auth->name[slot], name, name_size);
-	auth->name_size_tbl[slot] = name_size;
+	memcpy(auth->name, name, name_size);
+	auth->name_size = name_size;
 #endif
 	return 0;
 
-#ifdef CONFIG_TCG_TPM2_HMAC
 err:
 	tpm2_end_auth_session(chip);
 	return ret;
-#endif
 }
 EXPORT_SYMBOL_GPL(tpm_buf_append_name);
 
@@ -658,8 +650,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 	/* ordinal is already BE */
 	sha256_update(&sctx, (u8 *)&head->ordinal, sizeof(head->ordinal));
 	/* add the handle names */
-	for (i = 0; i < handles; i++)
-		sha256_update(&sctx, auth->name[i], auth->name_size_tbl[i]);
+	sha256_update(&sctx, auth->name, auth->name_size);
 	if (offset_s != tpm_buf_length(buf))
 		sha256_update(&sctx, &buf->data[offset_s],
 			      tpm_buf_length(buf) - offset_s);
Signature · 1 line
2.39.5
author maintainer
#4 lore raw
In reply to Jarkko Sakkinen
Quoted 29 lines · Jarkko Sakkinen

The main goal is fairly straight-forwrd here.

The aim of these patches is optimize the number of tpm2_read_public() calls to the bare minimum.

## About dropping 'parentName' attribute for ASN.1 keys from the patch set

I wrote this section as a remainder as I have facts fresh in my mind so that I can return them as soon as there is working group for the ASN.1 specification. We really need to have this in the spec.

I dropped [1] given that [2] is landing shortly to IETF draft process, according to James Bottomley [3]. We will return to [1] as soon as draft process is open for comments. Still, that attribute is super important, and here is why.

This will cause a overhead as tpm2_unseal_trusted needs to do an unnecessary (from pure technical perspective) TPM2_ReadPublic command to acquire TPM name of the parent. This is obviously known at the time of creation of a key but the information is not stored anywhere by the key format.

It also aligns badly with TCG specifications as Table 6 of architecture spec explicitly defines a reference (or name) for transient keys, persistent keys and NV indexes to be TPM_ALG_ID concatenated together with the hash of TPMT_PUBLIC. I.e. the file format is using exactly the opposite what should be use as reference for keys than what it should use.

Quoted 2 lines

Other benefits are of course auto-discovery of parent for a key file, which is nasty to do without the name pre-stored.

Right and TPMT_HA is calculated everytime when a trusted key is created, and right after that the already calculated data is simply thrown into dumpster. And we are talking about spec compliant way to refer other keys here, and only 66 bytes of extra payload.

I don't get it. And neither do I get how anyone would want to fix this issue with TPM2_CreatePrimary interception.

BR, Jarkko