From: Jarkko Sakkinen <jarkko@kernel.org>
To: linux-integrity@vger.kernel.org
Cc: Ross Philipson <ross.philipson@oracle.com>,
Jarkko Sakkinen <jarkko@kernel.org>,
"David S . Miller" <davem@davemloft.net>,
Herbert Xu <herbert@gondor.apana.org.au>,
Peter Huewe <peterhuewe@gmx.de>, Jason Gunthorpe <jgg@ziepe.ca>,
David Howells <dhowells@redhat.com>,
Paul Moore <paul@paul-moore.com>,
James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
James Bottomley <James.Bottomley@HansenPartnership.com>,
Mimi Zohar <zohar@linux.ibm.com>,
linux-kernel@vger.kernel.org (open list),
keyrings@vger.kernel.org (open list:KEYS/KEYRINGS),
linux-security-module@vger.kernel.org (open list:SECURITY
SUBSYSTEM)
Subject: [PATCH v6 07/11] tpm: Send only one at most TPM2_GetRandom command
Date: Sun, 14 Dec 2025 17:38:04 +0200 [thread overview]
Message-ID: <20251214153808.73831-8-jarkko@kernel.org> (raw)
In-Reply-To: <20251214153808.73831-1-jarkko@kernel.org>
hwrng framework does not have a requirement that the all bytes requested
need to be provided. By enforcing such a requirement internally, TPM driver
can cause unpredictability in latency, as a single tpm_get_random() call
can result multiple TPM commands.
Especially, when TCG_TPM2_HMAC is enabled, extra roundtrips could have
significant effect to the system latency.
Add a wait-parameter to enforce the old behavior and set it to true only at
the call sites for TPM 1.2 keys. At the call sites of hwrng, set @wait to
false.
Cc: David S. Miller <davem@davemloft.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
v4:
- Fixed grammar mistakes.
---
drivers/char/tpm/tpm-chip.c | 2 +-
drivers/char/tpm/tpm-interface.c | 11 +++++++++--
include/linux/tpm.h | 2 +-
security/keys/trusted-keys/trusted_tpm1.c | 8 ++++----
4 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 082b910ddf0d..8fca4373e2df 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -494,7 +494,7 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
- return tpm_get_random(chip, data, max);
+ return tpm_get_random(chip, data, max, false);
}
static bool tpm_is_hwrng_enabled(struct tpm_chip *chip)
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index d157be738612..0a79ed3696b7 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -614,9 +614,11 @@ static int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
* @chip: A &tpm_chip instance. Whenset to %NULL, the default chip is used.
* @out: Destination buffer for the acquired random bytes.
* @max: The maximum number of bytes to write to @out.
+ * @wait: Set to true when all of the @max bytes need to be acquired.
*
* Iterates pulling more bytes from TPM up until all of the @max bytes have been
- * received.
+ * received, when @wait it sets true. Otherwise, the queries for @max bytes from
+ * TPM exactly once, and returns the bytes that were received.
*
* Returns the number of random bytes read on success.
* Returns -EINVAL when @out is NULL, or @max is not between zero and
@@ -624,7 +626,7 @@ static int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
* Returns tpm_transmit_cmd() error codes when the TPM command results an
* error.
*/
-int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
+int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait)
{
u32 num_bytes = max;
u8 *out_ptr = out;
@@ -657,6 +659,11 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
if (rc < 0)
goto err;
+ if (!wait) {
+ total = rc;
+ break;
+ }
+
out_ptr += rc;
total += rc;
num_bytes -= rc;
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 72610f1aa402..d710a3aea2ff 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -487,7 +487,7 @@ extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
struct tpm_digest *digest);
extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
struct tpm_digest *digests);
-extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
+int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait);
extern struct tpm_chip *tpm_default_chip(void);
void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
int tpm2_find_hash_alg(unsigned int crypto_id);
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 636acb66a4f6..5b5f7a029bc3 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -361,7 +361,7 @@ static int osap(struct tpm_buf *tb, struct osapsess *s,
unsigned char ononce[TPM_NONCE_SIZE];
int ret;
- ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
+ ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE, true);
if (ret < 0)
return ret;
@@ -454,7 +454,7 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
- ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
+ ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE, true);
if (ret < 0)
goto out;
@@ -565,7 +565,7 @@ static int tpm_unseal(struct tpm_buf *tb,
}
ordinal = htonl(TPM_ORD_UNSEAL);
- ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
+ ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE, true);
if (ret < 0)
return ret;
@@ -938,7 +938,7 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
{
- return tpm_get_random(chip, key, key_len);
+ return tpm_get_random(chip, key, key_len, true);
}
static int __init init_digests(void)
--
2.39.5
next prev parent reply other threads:[~2025-12-14 15:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-14 15:37 [PATCH v6 00/11] Streamline TPM2 HMAC sessions Jarkko Sakkinen
2025-12-14 15:37 ` [PATCH v6 01/11] tpm2-sessions: Define TPM2_NAME_MAX_SIZE Jarkko Sakkinen
2025-12-14 15:37 ` [PATCH v6 02/11] KEYS: trusted: Open code tpm2_buf_append() Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 03/11] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 04/11] KEYS: trusted: Re-orchestrate tpm2_read_public() calls Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 05/11] tpm2-sessions: Remove AUTH_MAX_NAMES Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 06/11] tpm: Orchestrate TPM commands in tpm_get_random() Jarkko Sakkinen
2025-12-14 15:38 ` Jarkko Sakkinen [this message]
2025-12-14 15:38 ` [PATCH v6 08/11] tpm: In tpm_get_random() replace 'retries' with a zero check Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 09/11] tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 10/11] tpm-buf: Implement managed allocations Jarkko Sakkinen
2025-12-14 15:38 ` [PATCH v6 11/11] tpm-buf: Remove tpm_buf_append_handle Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251214153808.73831-8-jarkko@kernel.org \
--to=jarkko@kernel.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=herbert@gondor.apana.org.au \
--cc=jgg@ziepe.ca \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=peterhuewe@gmx.de \
--cc=ross.philipson@oracle.com \
--cc=serge@hallyn.com \
--cc=zohar@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).