Skip to contentSkip to conversation

Streamline TPM2 HMAC sessions

PATCH Superseded Jarkko Sakkinen 1 participant 10 messages Dec 9, 2025 – Dec 10, 2025

Subsystems (4)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

v2 cover Streamline TPM2 HMAC sessions

Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.

This patch set addresses two major issues in the feature:

1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.

Jarkko Sakkinen (5):
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm-buf: Remove tpm_buf_append_handle

 drivers/char/tpm/tpm-buf.c                |  25 ----
 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 130 ++++++---------------
 include/linux/tpm.h                       |  49 +++++---
 security/keys/trusted-keys/trusted_tpm2.c | 134 +++++++++++++---------
 6 files changed, 155 insertions(+), 194 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH v2 1/5 KEYS: trusted: Remove dead branch from tpm2_unseal_cmd

TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.

Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.

---
 security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +1 -9
index 9074ae1a5896..27424e1a4a63 100644
@@ -450,9 +450,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   struct trusted_key_options *options,
 			   u32 blob_handle)
 {
-	struct tpm_header *head;
 	u16 data_len;
-	int offset;
 	u8 *data;
 	int rc;
 
@@ -488,14 +486,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		tpm_buf_append_u16(buf, options->blobauth_len);
 		tpm_buf_append(buf, options->blobauth, options->blobauth_len);
 
-		if (tpm2_chip_auth(chip)) {
+		if (tpm2_chip_auth(chip))
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT, NULL, 0);
-		} else  {
-			offset = buf->handles * 4 + TPM_HEADER_SIZE;
-			head = (struct tpm_header *)buf->data;
-			if (tpm_buf_length(buf) == offset)
-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
-		}
 	}
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH v2 2/5 tpm2-sessions: Define TPM2_NAME_MAX_SIZE

In reply to Jarkko Sakkinen

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.

---
v2:
- Rename TPM2_NAME_SIZE as TPM2_NULL_NAME_SIZE.

---
 drivers/char/tpm/tpm-sysfs.c     |  2 +-
 drivers/char/tpm/tpm2-sessions.c |  2 +-
 include/linux/tpm.h              | 37 +++++++++++++++++++++-----------
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-sysfs.c +1 -1
index f5dcadb1ab3c..0f321c307bc6 100644
@@ -313,7 +313,7 @@ static ssize_t null_name_show(struct device *dev, struct device_attribute *attr,
 			      char *buf)
 {
 	struct tpm_chip *chip = to_tpm_chip(dev);
-	int size = TPM2_NAME_SIZE;
+	int size = TPM2_NULL_NAME_SIZE;
 
 	bin2hex(buf, chip->null_key_name, size);
 	size *= 2;
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 +25 -12
index 42e2a091f43d..922a43ef23b5 100644
@@ -28,9 +28,33 @@
 #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)
+
+/*
+ * The maximum number of PCR banks.
+ */
 #define TPM2_MAX_PCR_BANKS	8
 
+/*
+ * fixed define for the size of a name.  This is actually HASHALG size
+ * plus 2, so 32 for SHA256
+ */
+#define TPM2_NULL_NAME_SIZE	34
+
+/*
+ * The maximum size for an object context
+ */
+#define TPM2_MAX_CONTEXT_SIZE	4096
+
 struct tpm_chip;
 struct trusted_key_payload;
 struct trusted_key_options;
@@ -140,17 +164,6 @@ struct tpm_chip_seqops {
 /* fixed define for the curve we use which is NIST_P256 */
 #define EC_PT_SZ	32
 
-/*
- * fixed define for the size of a name.  This is actually HASHALG size
- * plus 2, so 32 for SHA256
- */
-#define TPM2_NAME_SIZE	34
-
-/*
- * The maximum size for an object context
- */
-#define TPM2_MAX_CONTEXT_SIZE 4096
-
 struct tpm_chip {
 	struct device dev;
 	struct device devs;
@@ -212,7 +225,7 @@ struct tpm_chip {
 	/* saved context for NULL seed */
 	u8 null_key_context[TPM2_MAX_CONTEXT_SIZE];
 	 /* name of NULL seed */
-	u8 null_key_name[TPM2_NAME_SIZE];
+	u8 null_key_name[TPM2_NULL_NAME_SIZE];
 	u8 null_ec_key_x[EC_PT_SZ];
 	u8 null_ec_key_y[EC_PT_SZ];
 	struct tpm2_auth *auth;
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH v2 3/5 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.

---
 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 922a43ef23b5..9f684fc7ae04 100644
@@ -543,7 +543,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);
@@ -557,6 +557,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>
 
@@ -580,6 +581,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.52.0
author maintainer
#4 lore raw

PATCH v2 4/5 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.

---
 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.52.0
author maintainer
#5 lore raw

PATCH v2 5/5 tpm-buf: Remove tpm_buf_append_handle

In reply to Jarkko Sakkinen

Since the number of handles is fixed to a single handle, eliminate all uses of buf->handles and deduce values during compile-time.

v2:
- Streamline the code change and remove dead code.

---
 drivers/char/tpm/tpm-buf.c       | 25 -------------------------
 drivers/char/tpm/tpm2-cmd.c      |  6 ++----
 drivers/char/tpm/tpm2-sessions.c | 14 ++------------
 include/linux/tpm.h              |  2 --
 4 files changed, 4 insertions(+), 43 deletions(-)
drivers/char/tpm/tpm-buf.c +0 -25
index 73be8a87b472..752c69b8a4f5 100644
@@ -40,7 +40,6 @@ static void __tpm_buf_reset(struct tpm_buf *buf, u16 buf_size, u16 tag, u32 ordi
 	buf->flags = 0;
 	buf->length = sizeof(*head);
 	buf->capacity = buf_size - sizeof(*buf);
-	buf->handles = 0;
 	head->tag = cpu_to_be16(tag);
 	head->length = cpu_to_be32(sizeof(*head));
 	head->ordinal = cpu_to_be32(ordinal);
@@ -56,7 +55,6 @@ static void __tpm_buf_reset_sized(struct tpm_buf *buf, u16 buf_size)
 	buf->flags = TPM_BUF_TPM2B;
 	buf->length = 2;
 	buf->capacity = buf_size - sizeof(*buf);
-	buf->handles = 0;
 	buf->data[0] = 0;
 	buf->data[1] = 0;
 }
@@ -177,29 +175,6 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
 }
 EXPORT_SYMBOL_GPL(tpm_buf_append_u32);
 
-/**
- * tpm_buf_append_handle() - Add a handle
- * @buf:	&tpm_buf instance
- * @handle:	a TPM object handle
- *
- * Add a handle to the buffer, and increase the count tracking the number of
- * handles in the command buffer. Works only for command buffers.
- */
-void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle)
-{
-	if (buf->flags & TPM_BUF_INVALID)
-		return;
-
-	if (buf->flags & TPM_BUF_TPM2B) {
-		WARN(1, "tpm-buf: invalid type: TPM2B\n");
-		buf->flags |= TPM_BUF_INVALID;
-		return;
-	}
-
-	tpm_buf_append_u32(buf, handle);
-	buf->handles++;
-}
-
 /**
  * tpm_buf_read() - Read from a TPM buffer
  * @buf:	&tpm_buf instance
drivers/char/tpm/tpm2-cmd.c +2 -4
index 5b04e74b6377..dbcd46c5e09d 100644
@@ -205,7 +205,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 			return rc;
 		tpm_buf_append_hmac_session(chip, buf, 0, NULL, 0);
 	} else {
-		tpm_buf_append_handle(buf, pcr_idx);
+		tpm_buf_append_u32(buf, pcr_idx);
 		tpm_buf_append_auth(chip, buf, NULL, 0);
 	}
 
@@ -281,10 +281,8 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 						    TPM2_SA_CONTINUE_SESSION,
 						    NULL, 0);
 		} else  {
-			offset = buf->handles * 4 + TPM_HEADER_SIZE;
 			head = (struct tpm_header *)buf->data;
-			if (tpm_buf_length(buf) == offset)
-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
+			head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
 		}
 		tpm_buf_append_u16(buf, num_bytes);
 
drivers/char/tpm/tpm2-sessions.c +2 -12
index 62a200ae72d7..f2b8ca893e15 100644
@@ -261,7 +261,7 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 
 	if (!tpm2_chip_auth(chip)) {
-		tpm_buf_append_handle(buf, handle);
+		tpm_buf_append_u32(buf, handle);
 		return 0;
 	}
 
@@ -288,17 +288,7 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_name);
 void tpm_buf_append_auth(struct tpm_chip *chip, struct tpm_buf *buf,
 			 u8 *passphrase, int passphrase_len)
 {
-	/* offset tells us where the sessions area begins */
-	int offset = buf->handles * 4 + TPM_HEADER_SIZE;
-	u32 len = 9 + passphrase_len;
-
-	if (tpm_buf_length(buf) != offset) {
-		/* not the first session so update the existing length */
-		len += get_unaligned_be32(&buf->data[offset]);
-		put_unaligned_be32(len, &buf->data[offset]);
-	} else {
-		tpm_buf_append_u32(buf, len);
-	}
+	tpm_buf_append_u32(buf, 9 + passphrase_len);
 	/* auth handle */
 	tpm_buf_append_u32(buf, TPM2_RS_PW);
 	/* nonce */
include/linux/tpm.h +0 -2
index 9f684fc7ae04..e68995df8796 100644
@@ -395,7 +395,6 @@ enum tpm_buf_flags {
  */
 struct tpm_buf {
 	u8 flags;
-	u8 handles;
 	u16 length;
 	u16 capacity;
 	u8 data[];
@@ -441,7 +440,6 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);
 u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset);
 u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset);
 u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset);
-void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle);
 
 /*
  * Check if TPM device is in the firmware upgrade mode.
Signature · 1 line
2.52.0
author maintainer
#6 lore raw
In reply to Jarkko Sakkinen
Quoted 19 lines · Jarkko Sakkinen

Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.

This patch set addresses two major issues in the feature:

1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.

Jarkko Sakkinen (5):
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm-buf: Remove tpm_buf_append_handle

Quoted 11 lines
 drivers/char/tpm/tpm-buf.c                |  25 ----
 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 130 ++++++---------------
 include/linux/tpm.h                       |  49 +++++---
 security/keys/trusted-keys/trusted_tpm2.c | 134 +++++++++++++---------
 6 files changed, 155 insertions(+), 194 deletions(-)
Signature · 1 line
2.52.0

For hwrng we can e.g., calculate factor by timing tpm2_get_random() with and without HMAC encryption. Then we can use this as frequency how often data is pulled.

The other angle to combine this is to maintain largeish FIFO of random bytes and fill this when it goes below a treshold.

Probably some combination of these will provide answer to performance and latency problem with hwrng when HMAC encryption is turned on:

1. The first amortizes the overall quota.
2. Second provides constant latency without major spikes.

BR, Jarkko

author maintainer
#7 lore raw
Quoted 36 lines · Jarkko Sakkinen
Quoted 19 lines · Jarkko Sakkinen

Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.

This patch set addresses two major issues in the feature:

1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.

Jarkko Sakkinen (5):
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm-buf: Remove tpm_buf_append_handle

Quoted 11 lines
 drivers/char/tpm/tpm-buf.c                |  25 ----
 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 130 ++++++---------------
 include/linux/tpm.h                       |  49 +++++---
 security/keys/trusted-keys/trusted_tpm2.c | 134 +++++++++++++---------
 6 files changed, 155 insertions(+), 194 deletions(-)
Signature · 1 line
2.52.0

For hwrng we can e.g., calculate factor by timing tpm2_get_random() with and without HMAC encryption. Then we can use this as frequency how often data is pulled.

Quoted 8 lines

The other angle to combine this is to maintain largeish FIFO of random bytes and fill this when it goes below a treshold.

Probably some combination of these will provide answer to performance and latency problem with hwrng when HMAC encryption is turned on:

1. The first amortizes the overall quota.
2. Second provides constant latency without major spikes.

Another perhaps more unorthodox idea:

1. Calculate factor as said above. Let's call it N.
2. Every Nth step seed a pseudo rng from TPM.
3. On steps not divisible by N, pull from pseud rng.

I'm not a cryptographer but would randomness suffer from this? Then the seeds that woud come from TPM are HMAC encrypted at least without major hits on performance.

Just enumerated this. Not sure about this idea yet by any means but I do like simplicy of it (and that also makes me concerned about missed details).

BR, Jarkko

author maintainer
#8 lore raw
Quoted 52 lines · Jarkko Sakkinen
Quoted 36 lines · Jarkko Sakkinen
Quoted 19 lines · Jarkko Sakkinen

Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.

This patch set addresses two major issues in the feature:

1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.

Jarkko Sakkinen (5):
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm-buf: Remove tpm_buf_append_handle

Quoted 11 lines
 drivers/char/tpm/tpm-buf.c                |  25 ----
 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 130 ++++++---------------
 include/linux/tpm.h                       |  49 +++++---
 security/keys/trusted-keys/trusted_tpm2.c | 134 +++++++++++++---------
 6 files changed, 155 insertions(+), 194 deletions(-)
Signature · 1 line
2.52.0

For hwrng we can e.g., calculate factor by timing tpm2_get_random() with and without HMAC encryption. Then we can use this as frequency how often data is pulled.

Quoted 8 lines

The other angle to combine this is to maintain largeish FIFO of random bytes and fill this when it goes below a treshold.

Probably some combination of these will provide answer to performance and latency problem with hwrng when HMAC encryption is turned on:

1. The first amortizes the overall quota.
2. Second provides constant latency without major spikes.

Another perhaps more unorthodox idea:

1. Calculate factor as said above. Let's call it N.
2. Every Nth step seed a pseudo rng from TPM.
3. On steps not divisible by N, pull from pseud rng.

Quoted 7 lines

I'm not a cryptographer but would randomness suffer from this? Then the seeds that woud come from TPM are HMAC encrypted at least without major hits on performance.

Just enumerated this. Not sure about this idea yet by any means but I do like simplicy of it (and that also makes me concerned about missed details).

Third option would be to have simply a kthread with a rate that fills a pool and hwrng callbacks serves from that pool. If there is no bytes, there is no bytes (which is fine as far as API is concerned).

This has optimal system latency properties I'd figure.

BR, Jarkko

author maintainer
#9 lore raw
Quoted 61 lines · Jarkko Sakkinen
Quoted 52 lines · Jarkko Sakkinen
Quoted 36 lines · Jarkko Sakkinen
Quoted 19 lines · Jarkko Sakkinen

Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.

This patch set addresses two major issues in the feature:

1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.

Jarkko Sakkinen (5):
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm-buf: Remove tpm_buf_append_handle

Quoted 11 lines
 drivers/char/tpm/tpm-buf.c                |  25 ----
 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 130 ++++++---------------
 include/linux/tpm.h                       |  49 +++++---
 security/keys/trusted-keys/trusted_tpm2.c | 134 +++++++++++++---------
 6 files changed, 155 insertions(+), 194 deletions(-)
Signature · 1 line
2.52.0

For hwrng we can e.g., calculate factor by timing tpm2_get_random() with and without HMAC encryption. Then we can use this as frequency how often data is pulled.

Quoted 8 lines

The other angle to combine this is to maintain largeish FIFO of random bytes and fill this when it goes below a treshold.

Probably some combination of these will provide answer to performance and latency problem with hwrng when HMAC encryption is turned on:

1. The first amortizes the overall quota.
2. Second provides constant latency without major spikes.

Another perhaps more unorthodox idea:

1. Calculate factor as said above. Let's call it N.
2. Every Nth step seed a pseudo rng from TPM.
3. On steps not divisible by N, pull from pseud rng.

Quoted 7 lines

I'm not a cryptographer but would randomness suffer from this? Then the seeds that woud come from TPM are HMAC encrypted at least without major hits on performance.

Just enumerated this. Not sure about this idea yet by any means but I do like simplicy of it (and that also makes me concerned about missed details).

Quoted 5 lines

Third option would be to have simply a kthread with a rate that fills a pool and hwrng callbacks serves from that pool. If there is no bytes, there is no bytes (which is fine as far as API is concerned).

This has optimal system latency properties I'd figure.

OK so the very first thing that should be done with hwrng is this: it should be opportunistic instead of being committing (to X number of bytes). Further optimizations should be considered only after changing this heuristics.

That guarantees that when hwrng's thread queries bytes from TPM driver it is guaranteed to cause always exactly one TPM2_GetRandom transaction.

How this can be done is to first re-orchestrate tpm_get_random() along the lines (out of top of my head, did not even compile this):

int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)

{
	u32 num_bytes = max;
	u8 *out_ptr = out;
	int retries = 5;
	int total = 0;
	int rc;

	if (!out || !max || max > TPM_MAX_RNG_DATA)
		return -EINVAL;

	if (!chip)
		return -ENODEV;

	rc = tpm_try_get_ops(chip);
	if (rc)
		return rc;

	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
		rc = tpm2_start_auth_session(chip);
		if (rc)
			return rc;
	}

	do {
		if (chip->flags & TPM_CHIP_FLAG_TPM2)
			rc = tpm2_get_random(chip, out_ptr, num_bytes);
		else
			rc = tpm1_get_random(chip, out_ptr, num_bytes);

		if (rc < 0)
			goto err;

		out_ptr += rc;
		total += rc;
		num_bytes -= rc;
	} while (retries-- && total < max);

	tpm_put_ops(chip);
	return total ? total : -EIO;

err:

	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		tpm2_end_auth_session(chip);

	tpm_put_ops(chip);
	return rc;
}

EXPORT_SYMBOL_GPL(tpm_get_random);

{tpm1,tpm2}_get_random() are changed to do exactly single transaction and on success they return how many bytes were read.

hwrng code should also use {tpm1,tpm2}_get_random() and use the requested size as a hint not as something that we commit into.

I think this will benefit kernel even without HMAC encryption because it objectively makes things a bit more deterministic.

BR, Jarkko