From: Matthew Garrett <matthewg@nvidia.com>
To: mjg59@srcf.ucam.org
Cc: keyrings@vger.kernel.org, James.Bottomley@HansenPartnership.com,
linux-integrity@vger.kernel.org, rafael@kernel.org,
linux-pm@vger.kernel.org, linux-efi@vger.kernel.org,
Matthew Garrett <matthewg@nvidia.com>
Subject: [PATCH 12/17] tpm: Add signing with the kernel signing key
Date: Thu, 8 Oct 2026 06:20:28 -0700 [thread overview]
Message-ID: <20261008132532.1155166-13-matthewg@nvidia.com> (raw)
In-Reply-To: <20261008132532.1155166-1-matthewg@nvidia.com>
Add tpm2_kernel_key_sign(), which signs a SHA-256 digest with a key
created by tpm2_kernel_key_create() using TPM2_Sign, and
tpm2_kernel_key_destroy(), which flushes the key and erases its auth
value.
Signed-off-by: Matthew Garrett <matthewg@nvidia.com>
---
drivers/char/tpm/tpm2-kernel-key.c | 98 ++++++++++++++++++++++++++++++
include/linux/tpm.h | 16 +++++
include/linux/tpm_command.h | 2 +
3 files changed, 116 insertions(+)
diff --git a/drivers/char/tpm/tpm2-kernel-key.c b/drivers/char/tpm/tpm2-kernel-key.c
index 27815525bc3f..89c11c78097a 100644
--- a/drivers/char/tpm/tpm2-kernel-key.c
+++ b/drivers/char/tpm/tpm2-kernel-key.c
@@ -402,3 +402,101 @@ int tpm2_kernel_key_create(struct tpm_chip *chip, struct tpm2_kernel_key *key,
return rc;
}
EXPORT_SYMBOL_GPL(tpm2_kernel_key_create);
+
+/**
+ * tpm2_kernel_key_sign() - sign a digest with a kernel signing key
+ * @chip: the TPM chip
+ * @key: a key created by tpm2_kernel_key_create()
+ * @digest: the SHA-256 digest to sign
+ * @r: filled with the R component of the ECDSA signature
+ * @s: filled with the S component of the ECDSA signature
+ *
+ * The key is authorized with its auth value through an HMAC session, so
+ * the auth value is never sent to the TPM in the clear. The caller must
+ * hold the chip's ops lock.
+ *
+ * Return:
+ * * 0 - OK
+ * * -errno - A system error
+ * * TPM_RC - A TPM error
+ */
+int tpm2_kernel_key_sign(struct tpm_chip *chip, struct tpm2_kernel_key *key,
+ const u8 digest[SHA256_DIGEST_SIZE],
+ u8 r[EC_PT_SZ], u8 s[EC_PT_SZ])
+{
+ struct tpm_buf *buf __free(kfree) = NULL;
+ struct tpm2_rsp rsp;
+ int rc;
+
+ rc = tpm2_start_auth_session(chip, false);
+ if (rc && rc != -EBUSY)
+ return rc;
+
+ buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+ if (!buf) {
+ tpm2_end_auth_session(chip);
+ return -ENOMEM;
+ }
+
+ tpm_buf_init(buf, TPM_BUFSIZE);
+ tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_SIGN);
+
+ rc = tpm_buf_append_name(chip, buf, key->handle, NULL);
+ if (rc)
+ return rc;
+
+ tpm_buf_append_hmac_session(chip, buf, TPM2_SA_CONTINUE_SESSION,
+ key->auth, sizeof(key->auth));
+
+ /* digest */
+ tpm_buf_append_u16(buf, SHA256_DIGEST_SIZE);
+ tpm_buf_append(buf, digest, SHA256_DIGEST_SIZE);
+ /* inScheme: use the key's scheme */
+ tpm_buf_append_u16(buf, TPM_ALG_NULL);
+ /* validation: a NULL ticket, as the key is not restricted */
+ tpm_buf_append_u16(buf, TPM2_ST_HASHCHECK);
+ tpm_buf_append_u32(buf, TPM2_RH_NULL);
+ tpm_buf_append_u16(buf, 0);
+
+ rc = tpm_buf_fill_hmac_session(chip, buf);
+ if (rc)
+ return rc;
+
+ rc = tpm_transmit_cmd(chip, buf, 0, "signing with kernel key");
+ rc = tpm_buf_check_hmac_response(chip, buf, rc);
+ if (rc) {
+ if (rc < 0)
+ tpm2_end_auth_session(chip);
+ return rc;
+ }
+
+ tpm2_rsp_init(&rsp, buf);
+ /* parameterSize */
+ tpm2_rsp_u32(&rsp);
+ if (tpm2_rsp_u16(&rsp) != TPM_ALG_ECDSA ||
+ tpm2_rsp_u16(&rsp) != TPM_ALG_SHA256)
+ return -EIO;
+ tpm2_rsp_ecc_param(&rsp, r);
+ tpm2_rsp_ecc_param(&rsp, s);
+
+ return rsp.err ? -EIO : 0;
+}
+EXPORT_SYMBOL_GPL(tpm2_kernel_key_sign);
+
+/**
+ * tpm2_kernel_key_destroy() - unload a kernel signing key
+ * @chip: the TPM chip
+ * @key: a key created by tpm2_kernel_key_create()
+ *
+ * Flushes the key from the TPM and erases its auth value. The caller must
+ * hold the chip's ops lock.
+ */
+void tpm2_kernel_key_destroy(struct tpm_chip *chip,
+ struct tpm2_kernel_key *key)
+{
+ if (key->handle)
+ tpm2_flush_context(chip, key->handle);
+
+ memzero_explicit(key, sizeof(*key));
+}
+EXPORT_SYMBOL_GPL(tpm2_kernel_key_destroy);
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 7ee553688906..505ea6f4bb46 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -388,6 +388,11 @@ struct tpm2_key_provenance {
#ifdef CONFIG_TCG_TPM2_KERNEL_KEY
int tpm2_kernel_key_create(struct tpm_chip *chip, struct tpm2_kernel_key *key,
struct tpm2_key_provenance *prov);
+int tpm2_kernel_key_sign(struct tpm_chip *chip, struct tpm2_kernel_key *key,
+ const u8 digest[SHA256_DIGEST_SIZE],
+ u8 r[EC_PT_SZ], u8 s[EC_PT_SZ]);
+void tpm2_kernel_key_destroy(struct tpm_chip *chip,
+ struct tpm2_kernel_key *key);
#else
static inline int tpm2_kernel_key_create(struct tpm_chip *chip,
struct tpm2_kernel_key *key,
@@ -395,6 +400,17 @@ static inline int tpm2_kernel_key_create(struct tpm_chip *chip,
{
return -EOPNOTSUPP;
}
+static inline int tpm2_kernel_key_sign(struct tpm_chip *chip,
+ struct tpm2_kernel_key *key,
+ const u8 digest[SHA256_DIGEST_SIZE],
+ u8 r[EC_PT_SZ], u8 s[EC_PT_SZ])
+{
+ return -EOPNOTSUPP;
+}
+static inline void tpm2_kernel_key_destroy(struct tpm_chip *chip,
+ struct tpm2_kernel_key *key)
+{
+}
#endif
#ifdef CONFIG_TCG_TPM2_HMAC
diff --git a/include/linux/tpm_command.h b/include/linux/tpm_command.h
index cef85f532b6c..9ff2f67c55c6 100644
--- a/include/linux/tpm_command.h
+++ b/include/linux/tpm_command.h
@@ -191,6 +191,7 @@ enum tpm2_structures {
TPM2_ST_SESSIONS = 0x8002,
TPM2_ST_ATTEST_SESSION_AUDIT = 0x8016,
TPM2_ST_CREATION = 0x8021,
+ TPM2_ST_HASHCHECK = 0x8024,
};
/* Indicates from what layer of the software stack the error comes from */
@@ -238,6 +239,7 @@ enum tpm2_command_codes {
TPM2_CC_CREATE = 0x0153,
TPM2_CC_LOAD = 0x0157,
TPM2_CC_SEQUENCE_UPDATE = 0x015C,
+ TPM2_CC_SIGN = 0x015D,
TPM2_CC_UNSEAL = 0x015E,
TPM2_CC_CONTEXT_LOAD = 0x0161,
TPM2_CC_CONTEXT_SAVE = 0x0162,
--
2.43.0
next prev parent reply other threads:[~2026-10-08 13:26 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-08 13:20 [RFC] Make hibernation work with lockdown Matthew Garrett
2026-10-08 13:20 ` [PATCH 01/17] tpm: Define a kernel-owned TPM NV index that can't be modified by userland Matthew Garrett
2026-10-08 13:41 ` Matthew Garrett
2026-10-08 16:24 ` Jarkko Sakkinen
2026-10-08 16:23 ` Jarkko Sakkinen
2026-10-09 8:33 ` Matthew Garrett
2026-10-08 17:06 ` Ilias Apalodimas
2026-10-08 13:20 ` [PATCH 02/17] efi: Add a mechanism to modify TPM state depending on kernel security features Matthew Garrett
2026-10-08 16:38 ` Jarkko Sakkinen
2026-10-08 13:20 ` [PATCH 03/17] tpm: Allow tpm2_start_auth_session() to start an audit session Matthew Garrett
2026-10-08 13:20 ` [PATCH 04/17] tpm: Log commands executed in " Matthew Garrett
2026-10-08 13:20 ` [PATCH 05/17] tpm: Add a kernel attestation key and signed audit digest retrieval Matthew Garrett
2026-10-08 16:45 ` James Bottomley
2026-10-09 8:29 ` Matthew Garrett
2026-10-08 13:20 ` [PATCH 06/17] tpm: Use TPM2_NV_ReadPublic to read NV index names Matthew Garrett
2026-10-08 13:20 ` [PATCH 07/17] tpm: Add in-kernel support for reading NV indices Matthew Garrett
2026-10-08 13:20 ` [PATCH 08/17] tpm: Add NV define, undefine and write helpers Matthew Garrett
2026-10-08 13:20 ` [PATCH 09/17] tpm: Provision the kernel NV index at registration Matthew Garrett
2026-10-08 13:20 ` [PATCH 10/17] tpm: Move the bounds-checked response reader to a header Matthew Garrett
2026-10-08 13:20 ` [PATCH 11/17] tpm: Add kernel signing key creation with audited provenance Matthew Garrett
2026-10-08 17:00 ` James Bottomley
2026-10-09 8:31 ` Matthew Garrett
2026-10-08 13:20 ` Matthew Garrett [this message]
2026-10-08 13:20 ` [PATCH 13/17] tpm: Add verification of kernel signing key provenance Matthew Garrett
2026-10-08 13:20 ` [PATCH 14/17] PM: hibernate: Add image digest and signature page infrastructure Matthew Garrett
2026-10-08 13:20 ` [PATCH 15/17] PM: hibernate: Sign and verify images with a kernel-generated TPM key Matthew Garrett
2026-10-08 13:20 ` [PATCH 16/17] PM: hibernate: Refuse to verify images with a virtual TPM Matthew Garrett
2026-10-08 13:20 ` [PATCH 17/17] PM: hibernate: Allow hibernation under lockdown with signed images Matthew Garrett
2026-10-08 16:53 ` Jarkko Sakkinen
2026-10-09 8:31 ` Matthew Garrett
2026-10-08 15:56 ` [RFC] Make hibernation work with lockdown 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=20261008132532.1155166-13-matthewg@nvidia.com \
--to=matthewg@nvidia.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=keyrings@vger.kernel.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mjg59@srcf.ucam.org \
--cc=rafael@kernel.org \
/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