The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v5 00/17] KEYS: trusted: asymmetric keys
@ 2026-11-27  5:55 Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 01/17] KEYS: trusted: Validate public_len before reading key attributes Jarkko Sakkinen
                   ` (18 more replies)
  0 siblings, 19 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys
(OID_TPMLoadableKey, 2.23.133.10.1.3) and expose keyctl pkey-operations
directly on key_type_trusted. In addition there are some TPMKey
interoperability fixes.

I.e. this my old series with a twist: instead of introducing a separate
subtype for asymmetric keys, factor out pkey-operations into a separate
CONFIG_KEYCTL_PKEY-feature, and wire asymmetric operations to
key_type_trusted.

This work was originally derived from [3], is direct offspring of [2],
and folds in the prerequisite tpm2-sessions and tpm-buf cleanups from
[4].

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/20240520184727.22038-1-jarkko@kernel.org/
[3] https://lore.kernel.org/all/20200518172704.29608-1-prestwoj@gmail.com/
[4] https://lore.kernel.org/linux-integrity/20261116005105.1573016-1-jarkko@kernel.org/

CHANGES
=======

v5:
- tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*()
  patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
  Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(),
  and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with
  tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
- trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2"
  into a framework patch ("KEYS: trusted: Add asymmetric key
  operations", adding optional asym_* callbacks on struct
  trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend
  patch.
- trusted: Move "KEYS: trusted: Validate public_len before reading key
  attributes" to patch 01/17 and add the Fixes tag for e5fb5d2c5a03.

v4:
- KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
  subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software
  asymmetric keys, and switch TURRIS_SIGNING_KEY,
  TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from
  CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
- trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select
  CONFIG_KEYCTL_PKEY.
- trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into
  separate patches for public_len bounds checking, the private backend
  pointer in trusted_key_payload, and TPM2 blob loading.

v3:
- KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series
  and make it an off-by-default prompt instead of selecting it from
  CONFIG_ASYMMETRIC_KEY_TYPE.
- trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on
  CONFIG_KEYCTL_PKEY instead of selecting it.
- trusted: Use unsigned int instead of u16 for data_len and min_len in
  tpm2_unseal_cmd().
- asn1: The reason for not supporting implicit tags was that it follows
  what other functions do in asn1_encoder.c, and is that way coherent.

v2:
- lib/asn1_encoder: Remove unreachable !bytes_len branch in
  asn1_encode_integer_bytes().
- tpm: Bounds-check ECC coordinates, defer state updates until after
  validation, and verify subsequent TPM2_RH_NULL primaries against
  chip->null_key_name in tpm2_parse_create_primary().
- trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so
  tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
- trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt()
  and tpm2_asym_parse_signature().

VERSIONS
========

v4: https://lore.kernel.org/keyrings/20261126205021.2200819-1-jarkko@kernel.org/
v3: https://lore.kernel.org/keyrings/20261125180616.1854753-1-jarkko@kernel.org/
v2: https://lore.kernel.org/keyrings/20261124194043.1651679-1-jarkko@kernel.org/
v1: https://lore.kernel.org/keyrings/20261124120044.1481752-1-jarkko@kernel.org/

Cc: dm-devel@lists.linux.dev
Cc: keyrings@vger.kernel.org
Cc: linux-crypto@vger.kernel.org
Cc: linux-doc@vger.kernel.org
Cc: linux-integrity@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-security-module@vger.kernel.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: Alasdair Kergon <agk@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Benjamin Marzinski <bmarzins@redhat.com>
Cc: David Gstir <david@sigma-star.at>
Cc: David Howells <dhowells@redhat.com>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Ignat Korchagin <ignat@linux.win>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
Cc: James Morris <jmorris@namei.org>
Cc: James Prestwood <prestwoj@gmail.com>
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Jonathan McDowell <noodles@meta.com>
Cc: Lukas Wunner <lukas@wunner.de>
Cc: Marek Behún <kabel@kernel.org>
Cc: Mike Snitzer <snitzer@kernel.org>
Cc: Mikulas Patocka <mpatocka@redhat.com>
Cc: Mimi Zohar <zohar@linux.ibm.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: Peter Huewe <peterhuewe@gmx.de>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Sumit Garg <sumit.garg@kernel.org>

Jarkko Sakkinen (17):
  KEYS: trusted: Validate public_len before reading key attributes
  KEYS: Factor out KEYCTL_PKEY
  tpm2-sessions: Drop TPM2_NAME_SIZE
  KEYS: trusted: Open code tpm2_buf_append_auth()
  KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
  tpm2-sessions: Re-orchestrate tpm2_read_public() use
  tpm-buf: Make tpm_buf_read*() fallible
  KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE
  lib/asn1_encoder: Add asn1_encode_integer_bytes()
  tpm: Export tpm2_create_primary()
  tpm: Add persistent hierarchies to tpm2_create_primary()
  KEYS: trusted: Extend TPMKey support
  KEYS: trusted: Reject zero-length keys in symmetric consumers
  KEYS: trusted: Add private backend data to trusted_key_payload
  KEYS: trusted: Factor out TPM2 blob loading
  KEYS: trusted: Add asymmetric key operations
  KEYS: trusted: Add asymmetric key support for TPM2

 .../security/keys/trusted-encrypted.rst       |   25 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |   11 +
 crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
 crypto/asymmetric_keys/asymmetric_type.c      |    4 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm-buf.c                    |   56 +-
 drivers/char/tpm/tpm-sysfs.c                  |    2 +-
 drivers/char/tpm/tpm2-cmd.c                   |    9 +-
 drivers/char/tpm/tpm2-sessions.c              |  524 ++++---
 drivers/firmware/Kconfig                      |    1 -
 drivers/md/dm-crypt.c                         |    2 +-
 drivers/platform/cznic/Kconfig                |    3 +-
 include/keys/trusted-type.h                   |   19 +-
 include/keys/trusted_tpm.h                    |   18 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |   20 +-
 include/linux/tpm_buf.h                       |    6 +-
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   56 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   15 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |  103 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1329 +++++++++++++++--
 30 files changed, 1872 insertions(+), 410 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v5 01/17] KEYS: trusted: Validate public_len before reading key attributes
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 02/17] KEYS: Factor out KEYCTL_PKEY Jarkko Sakkinen
                   ` (17 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg, stable

tpm2_load_cmd() reads the 32-bit objectAttributes field at pub + 4
without checking that public_len is at least 8 bytes. Reject blobs with
public_len < 8 before dereferencing pub + 4.

Cc: stable@vger.kernel.org
Fixes: e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 30188a0ec676..621f8ffba17e 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -425,6 +425,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
 
+	if (public_len < 8)
+		return -EINVAL;
+
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
 	attrs = get_unaligned_be32(pub + 4);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 02/17] KEYS: Factor out KEYCTL_PKEY
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 01/17] KEYS: trusted: Validate public_len before reading key attributes Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 03/17] tpm2-sessions: Drop TPM2_NAME_SIZE Jarkko Sakkinen
                   ` (16 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Factor out CONFIG_KEYCTL_PKEY from CONFIG_ASYMMETRIC_KEY_TYPE as a hidden
helper symbol and make keyctl public-key support a per-subsystem choice:

- Add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY to gate keyctl public-key
  operations on software asymmetric keys so systems that only need
  CONFIG_ASYMMETRIC_KEY_TYPE for in-kernel signature verification do not
  expose KEYCTL_PKEY_* operations.
- Switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and
  TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to
  CONFIG_KEYCTL_PKEY while keeping them enabled by default.

Suggested-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 crypto/asymmetric_keys/Kconfig           | 11 +++++++++++
 crypto/asymmetric_keys/asymmetric_keys.h |  2 ++
 crypto/asymmetric_keys/asymmetric_type.c |  4 ++++
 drivers/firmware/Kconfig                 |  1 -
 drivers/platform/cznic/Kconfig           |  3 +--
 security/keys/Kconfig                    |  3 +++
 security/keys/Makefile                   |  2 +-
 security/keys/internal.h                 |  2 +-
 security/keys/keyctl.c                   |  2 +-
 9 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/crypto/asymmetric_keys/Kconfig b/crypto/asymmetric_keys/Kconfig
index c73979b2e98e..a1fa64a38f24 100644
--- a/crypto/asymmetric_keys/Kconfig
+++ b/crypto/asymmetric_keys/Kconfig
@@ -10,6 +10,17 @@ menuconfig ASYMMETRIC_KEY_TYPE
 
 if ASYMMETRIC_KEY_TYPE
 
+config ASYMMETRIC_KEY_PKEY
+	bool "Asymmetric key keyctl public-key operations"
+	select KEYCTL_PKEY
+	help
+	  This option exposes keyctl public-key operations (query, encrypt,
+	  decrypt, sign, and verify) on asymmetric keys to userspace.
+	  In-kernel signature verification (such as module and kexec
+	  verification) does not require this option.
+
+	  If you are unsure as to whether this is required, answer N.
+
 config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	tristate "Asymmetric public-key crypto algorithm subtype"
 	select MPILIB
diff --git a/crypto/asymmetric_keys/asymmetric_keys.h b/crypto/asymmetric_keys/asymmetric_keys.h
index dc854cb737fb..7b669f41f297 100644
--- a/crypto/asymmetric_keys/asymmetric_keys.h
+++ b/crypto/asymmetric_keys/asymmetric_keys.h
@@ -13,5 +13,7 @@ extern int __asymmetric_key_hex_to_key_id(const char *id,
 					  struct asymmetric_key_id *match_id,
 					  size_t hexlen);
 
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 extern int asymmetric_key_eds_op(struct kernel_pkey_params *params,
 				 const void *in, void *out);
+#endif
diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
index 16a7ae16593c..8b8a75b70527 100644
--- a/crypto/asymmetric_keys/asymmetric_type.c
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -565,6 +565,7 @@ static struct key_restriction *asymmetric_lookup_restriction(
 	return ret;
 }
 
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 int asymmetric_key_eds_op(struct kernel_pkey_params *params,
 			  const void *in, void *out)
 {
@@ -603,6 +604,7 @@ static int asymmetric_key_verify_signature(struct kernel_pkey_params *params,
 
 	return verify_signature(params->key, &sig);
 }
+#endif
 
 struct key_type key_type_asymmetric = {
 	.name			= "asymmetric",
@@ -614,9 +616,11 @@ struct key_type key_type_asymmetric = {
 	.destroy		= asymmetric_key_destroy,
 	.describe		= asymmetric_key_describe,
 	.lookup_restriction	= asymmetric_lookup_restriction,
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 	.asym_query		= query_asymmetric_key,
 	.asym_eds_op		= asymmetric_key_eds_op,
 	.asym_verify_signature	= asymmetric_key_verify_signature,
+#endif
 };
 EXPORT_SYMBOL_GPL(key_type_asymmetric);
 
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index 43e2e2a64594..9f382ba10fc9 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -288,7 +288,6 @@ config TURRIS_MOX_RWTM_KEYCTL
 	bool "Turris Mox rWTM ECDSA message signing"
 	default y
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
 	select CZNIC_PLATFORMS
 	select TURRIS_SIGNING_KEY
 	help
diff --git a/drivers/platform/cznic/Kconfig b/drivers/platform/cznic/Kconfig
index 61cff5f7e02e..850319fb8c67 100644
--- a/drivers/platform/cznic/Kconfig
+++ b/drivers/platform/cznic/Kconfig
@@ -80,7 +80,6 @@ config TURRIS_OMNIA_MCU_KEYCTL
 	bool "Turris Omnia MCU ECDSA message signing"
 	default y
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
 	depends on TURRIS_OMNIA_MCU_GPIO
 	select TURRIS_SIGNING_KEY
 	help
@@ -93,6 +92,6 @@ endif # TURRIS_OMNIA_MCU
 config TURRIS_SIGNING_KEY
 	tristate
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
+	select KEYCTL_PKEY
 
 endif # CZNIC_PLATFORMS
diff --git a/security/keys/Kconfig b/security/keys/Kconfig
index 3f64d60d0f8a..c0cd894d306c 100644
--- a/security/keys/Kconfig
+++ b/security/keys/Kconfig
@@ -22,6 +22,9 @@ menuconfig KEYS
 
 if KEYS
 
+config KEYCTL_PKEY
+	bool
+
 config KEYS_REQUEST_CACHE
 	bool "Enable temporary caching of the last request_key() result"
 	help
diff --git a/security/keys/Makefile b/security/keys/Makefile
index 5f40807f05b3..aafd28f7ee2f 100644
--- a/security/keys/Makefile
+++ b/security/keys/Makefile
@@ -22,7 +22,7 @@ obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_SYSCTL) += sysctl.o
 obj-$(CONFIG_PERSISTENT_KEYRINGS) += persistent.o
 obj-$(CONFIG_KEY_DH_OPERATIONS) += dh.o
-obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += keyctl_pkey.o
+obj-$(CONFIG_KEYCTL_PKEY) += keyctl_pkey.o
 
 #
 # Key types
diff --git a/security/keys/internal.h b/security/keys/internal.h
index 4cf6b8070227..1821ba1165c1 100644
--- a/security/keys/internal.h
+++ b/security/keys/internal.h
@@ -316,7 +316,7 @@ static inline long compat_keyctl_dh_compute(
 #endif
 #endif
 
-#ifdef CONFIG_ASYMMETRIC_KEY_TYPE
+#ifdef CONFIG_KEYCTL_PKEY
 extern long keyctl_pkey_query(key_serial_t,
 			      const char __user *,
 			      struct keyctl_pkey_query __user *);
diff --git a/security/keys/keyctl.c b/security/keys/keyctl.c
index d6b10fd67627..417bba539bb1 100644
--- a/security/keys/keyctl.c
+++ b/security/keys/keyctl.c
@@ -28,7 +28,7 @@ static const unsigned char keyrings_capabilities[2] = {
 	[0] = (KEYCTL_CAPS0_CAPABILITIES |
 	       (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS)	? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
 	       (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS)	? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
-	       (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE)	? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
+	       (IS_ENABLED(CONFIG_KEYCTL_PKEY)		? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
 	       (IS_ENABLED(CONFIG_BIG_KEYS)		? KEYCTL_CAPS0_BIG_KEY : 0) |
 	       KEYCTL_CAPS0_INVALIDATE |
 	       KEYCTL_CAPS0_RESTRICT_KEYRING |
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 03/17] tpm2-sessions: Drop TPM2_NAME_SIZE
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 01/17] KEYS: trusted: Validate public_len before reading key attributes Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 02/17] KEYS: Factor out KEYCTL_PKEY Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 04/17] KEYS: trusted: Open code tpm2_buf_append_auth() Jarkko Sakkinen
                   ` (15 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg, stable

TPM2_NAME_SIZE was left to include/linux/tpm.h when the base definitions
were consolidated. Switch struct tpm2_auth to TPM2_MAX_NAME_SIZE and
convert TPM2_NAME_SIZE users to TPM2_NULL_NAME_SIZE, removing the
redundant TPM2_NAME_SIZE definition.

Cc: stable@vger.kernel.org # v7.3+
Fixes: 88c5a62c3af7 ("tpm: Move TPM common base definitions to the command header")
Reviewed-by: Jonathan McDowell <noodles@meta.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm-sysfs.c     | 2 +-
 drivers/char/tpm/tpm2-sessions.c | 2 +-
 include/linux/tpm.h              | 8 +-------
 3 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index d915307ae833..0f726cf65189 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -318,7 +318,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;
diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index 9608e545602c..94c11135beb3 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -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
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 0f97abc82846..2eb17e1ccd58 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -94,12 +94,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
  */
@@ -166,7 +160,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;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 04/17] KEYS: trusted: Open code tpm2_buf_append_auth()
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (2 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 03/17] tpm2-sessions: Drop TPM2_NAME_SIZE Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 05/17] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd Jarkko Sakkinen
                   ` (14 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg, Jonathan McDowell

tpm2_buf_append_auth() has a single call site and most of its parameters
are redundant. Open code it to the call site so that less cross-referencing
is required while browsing the source code.

Reviewed-by: Jonathan McDowell <noodles@earth.li>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 40 ++++-------------------
 1 file changed, 7 insertions(+), 33 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 621f8ffba17e..2f7c8e571522 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -197,36 +197,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,
 	return 0;
 }
 
-/**
- * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
- *
- * @buf: an allocated tpm_buf instance
- * @session_handle: session handle
- * @nonce: the session nonce, may be NULL if not used
- * @nonce_len: the session nonce length, may be 0 if not used
- * @attributes: the session attributes
- * @hmac: the session HMAC or password, may be NULL if not used
- * @hmac_len: the session HMAC or password length, maybe 0 if not used
- */
-static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
-				 const u8 *nonce, u16 nonce_len,
-				 u8 attributes,
-				 const u8 *hmac, u16 hmac_len)
-{
-	tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
-	tpm_buf_append_u32(buf, session_handle);
-	tpm_buf_append_u16(buf, nonce_len);
-
-	if (nonce && nonce_len)
-		tpm_buf_append(buf, nonce, nonce_len);
-
-	tpm_buf_append_u8(buf, attributes);
-	tpm_buf_append_u16(buf, hmac_len);
-
-	if (hmac && hmac_len)
-		tpm_buf_append(buf, hmac, hmac_len);
-}
-
 /**
  * tpm2_seal_trusted() - seal the payload of a trusted key
  *
@@ -540,9 +510,13 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		 * could repeat our actions with the exfiltrated
 		 * password.
 		 */
-		tpm2_buf_append_auth(buf, private->policyhandle,
-				     NULL /* nonce */, 0, 0,
-				     private->blobauth, private->blobauth_len);
+		tpm_buf_append_u32(buf, 9 + private->blobauth_len);
+		tpm_buf_append_u32(buf, private->policyhandle);
+		tpm_buf_append_u16(buf, 0);
+		tpm_buf_append_u8(buf, 0);
+		tpm_buf_append_u16(buf, private->blobauth_len);
+		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
+
 		if (tpm2_chip_auth(chip)) {
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
 						    NULL, 0);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 05/17] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (3 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 04/17] KEYS: trusted: Open code tpm2_buf_append_auth() Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 06/17] tpm2-sessions: Re-orchestrate tpm2_read_public() use Jarkko Sakkinen
                   ` (13 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

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.

Reviewed-by: Jonathan McDowell <noodles@meta.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 2f7c8e571522..32d90a1f8357 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -470,10 +470,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
-	struct tpm_header *head;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	u16 data_len;
-	int offset;
 	u8 *data;
 	int rc;
 
@@ -517,15 +515,9 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		tpm_buf_append_u16(buf, private->blobauth_len);
 		tpm_buf_append(buf, private->blobauth, private->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);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 06/17] tpm2-sessions: Re-orchestrate tpm2_read_public() use
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (4 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 05/17] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 07/17] tpm-buf: Make tpm_buf_read*() fallible Jarkko Sakkinen
                   ` (12 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Move TPM handle name resolution out of tpm_buf_append_name(), so that
callers resolve object names explicitly before constructing HMAC-protected
commands:

1. Export tpm2_read_public(), so that trusted_tpm2 can resolve handle
   names, and store non-object handles in big-endian byte order matching
   the TPM 2.0 handle Name definition.
2. Call tpm2_read_public() explicitly in tpm2_seal_trusted(),
   tpm2_load_cmd(), and tpm2_unseal_cmd() before starting the auth
   session, and pass the pre-resolved name and size to
   tpm_buf_append_name().
3. Rework tpm_buf_append_name() and tpm_buf_fill_hmac_session() to use
   the caller-supplied name and size directly.

This reduces fallible exits from tpm2_buf_append_name().

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 122 +++++++---------------
 include/linux/tpm.h                       |  10 +-
 security/keys/trusted-keys/trusted_tpm2.c |  33 +++++-
 4 files changed, 84 insertions(+), 90 deletions(-)

diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index d58c306f7d93..c5b66f470287 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -194,11 +194,12 @@ 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);
-		if (rc) {
-			tpm2_end_auth_session(chip);
+		__be32 pcr_idx_be = cpu_to_be32(pcr_idx);
+
+		rc = tpm_buf_append_name(chip, buf, pcr_idx,
+					 (u8 *)&pcr_idx_be, sizeof(pcr_idx_be));
+		if (rc)
 			return rc;
-		}
 		tpm_buf_append_hmac_session(chip, buf, 0, NULL, 0);
 	} else {
 		tpm_buf_append_handle(buf, pcr_idx);
diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index 94c11135beb3..fa107719b4f9 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -136,8 +136,8 @@ struct tpm2_auth {
 	 * handle, but they are part of the session by name, which
 	 * we must compute and remember
 	 */
-	u32 name_h[AUTH_MAX_NAMES];
 	u8 name[AUTH_MAX_NAMES][TPM2_MAX_NAME_SIZE];
+	u16 name_size_tbl[AUTH_MAX_NAMES];
 };
 
 #ifdef CONFIG_TCG_TPM2_HMAC
@@ -163,7 +163,17 @@ static int name_size(const u8 *name)
 	}
 }
 
-static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
+/**
+ * tpm2_read_public() - Resolve TPM name for a handle
+ * @chip:	TPM chip to use.
+ * @handle:	TPM handle.
+ * @name:	Buffer for returning the name blob. Must have a capacity of at
+ *		least %TPM2_MAX_NAME_SIZE bytes.
+ *
+ * Return: Size of the TPM handle name on success, or a negative errno when
+ * TPM2_ReadPublic fails.
+ */
+int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 {
 	u32 mso = tpm2_handle_mso(handle);
 	off_t offset = TPM_HEADER_SIZE;
@@ -172,7 +182,7 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 
 	if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
 	    mso != TPM2_MSO_NVRAM) {
-		memcpy(name, &handle, sizeof(u32));
+		put_unaligned_be32(handle, name);
 		return sizeof(u32);
 	}
 
@@ -189,7 +199,8 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 		return tpm_ret_to_err(rc);
 
 	/* Skip TPMT_PUBLIC: */
-	offset += tpm_buf_read_u16(buf, &offset);
+	rc = tpm_buf_read_u16(buf, &offset);
+	offset += rc;
 
 	/*
 	 * Ensure space for the length field of TPM2B_NAME and hashAlg field of
@@ -212,39 +223,34 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	memcpy(name, &buf->data[offset], rc);
 	return name_size_alg;
 }
+EXPORT_SYMBOL_GPL(tpm2_read_public);
 #endif /* CONFIG_TCG_TPM2_HMAC */
 
 /**
- * tpm_buf_append_name() - add a handle area to the buffer
- * @chip: the TPM chip structure
- * @buf: The buffer to be appended
- * @handle: The handle to be appended
- * @name: The name of the handle (may be NULL)
+ * tpm_buf_append_name() - Append a handle and store TPM name
+ * @chip:	TPM chip to use.
+ * @buf:	TPM buffer containing the TPM command in-transit.
+ * @handle:	TPM handle to be appended.
+ * @name:	TPM name of the handle.
+ * @name_size:	Size of the TPM name.
  *
  * In order to compute session HMACs, we need to know the names of the
- * objects pointed to by the handles.  For most objects, this is simply
- * the actual 4 byte handle or an empty buf (in these cases @name
- * should be NULL) but for volatile objects, permanent objects and NV
- * areas, the name is defined as the hash (according to the name
- * algorithm which should be set to sha256) of the public area to
- * which the two byte algorithm id has been appended.  For these
- * objects, the @name pointer should point to this.  If a name is
- * required but @name is NULL, then TPM2_ReadPublic() will be called
- * on the handle to obtain the name.
+ * objects pointed to by the handles. For permanent and PCR handles this is
+ * the 4-byte big-endian handle; for volatile objects, persistent objects
+ * and NV areas this is the name resolved with tpm2_read_public().
  *
  * As with most tpm_buf operations, success is assumed because failure
  * will be caused by an incorrect programming model and indicated by a
  * kernel message.
  *
- * Ends the authorization session on failure.
+ * Return: 0 on success, or -EIO when the authorization area state is
+ * malformed.
  */
 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
-			u32 handle, u8 *name)
+			u32 handle, u8 *name, u16 name_size)
 {
 #ifdef CONFIG_TCG_TPM2_HMAC
-	enum tpm2_mso_type mso = tpm2_handle_mso(handle);
 	struct tpm2_auth *auth;
-	u16 name_size_alg;
 	int slot;
 	int ret;
 #endif
@@ -263,45 +269,26 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 	auth = chip->auth;
 	if (auth->session != tpm_buf_length(buf)) {
-		dev_err(&chip->dev, "session state malformed");
+		dev_err(&chip->dev, "session state malformed\n");
+		ret = -EIO;
+		goto err;
+	}
+	if (name_size > TPM2_MAX_NAME_SIZE) {
+		dev_err(&chip->dev, "name too long: %u\n", name_size);
 		ret = -EIO;
 		goto err;
 	}
 	tpm_buf_append_u32(buf, handle);
 	auth->session += 4;
-
-	if (mso == TPM2_MSO_PERSISTENT ||
-	    mso == TPM2_MSO_VOLATILE ||
-	    mso == TPM2_MSO_NVRAM) {
-		if (!name) {
-			ret = tpm2_read_public(chip, handle, auth->name[slot]);
-		} else {
-			ret = name_size(name);
-		}
-
-		if (ret < 0)
-			goto err;
-
-		name_size_alg = ret;
-	} else {
-		if (name) {
-			dev_err(&chip->dev, "handle 0x%08x does not use a name\n",
-				handle);
-			ret = -EIO;
-			goto err;
-		}
-	}
-
-	auth->name_h[slot] = handle;
-	if (name)
-		memcpy(auth->name[slot], name, name_size_alg);
+	memcpy(auth->name[slot], name, name_size);
+	auth->name_size_tbl[slot] = name_size;
 #endif
 	return 0;
 
 #ifdef CONFIG_TCG_TPM2_HMAC
 err:
 	tpm2_end_auth_session(chip);
-	return tpm_ret_to_err(ret);
+	return ret;
 #endif
 }
 EXPORT_SYMBOL_GPL(tpm_buf_append_name);
@@ -629,22 +616,8 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 	attrs = chip->cc_attrs_tbl[i];
 
 	handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
+	offset_s += handles * sizeof(u32);
 
-	/*
-	 * just check the names, it's easy to make mistakes.  This
-	 * would happen if someone added a handle via
-	 * tpm_buf_append_u32() instead of tpm_buf_append_name()
-	 */
-	for (i = 0; i < handles; i++) {
-		u32 handle = tpm_buf_read_u32(buf, &offset_s);
-
-		if (auth->name_h[i] != handle) {
-			dev_err(&chip->dev, "invalid handle 0x%08x\n", handle);
-			ret = -EIO;
-			goto err;
-		}
-	}
-	/* point offset_s to the start of the sessions */
 	val = tpm_buf_read_u32(buf, &offset_s);
 	/* point offset_p to the start of the parameters */
 	offset_p = offset_s + val;
@@ -705,23 +678,8 @@ 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++) {
-		enum tpm2_mso_type mso = tpm2_handle_mso(auth->name_h[i]);
-
-		if (mso == TPM2_MSO_PERSISTENT ||
-		    mso == TPM2_MSO_VOLATILE ||
-		    mso == TPM2_MSO_NVRAM) {
-			ret = name_size(auth->name[i]);
-			if (ret < 0)
-				goto err;
-
-			sha256_update(&sctx, auth->name[i], ret);
-		} else {
-			__be32 h = cpu_to_be32(auth->name_h[i]);
-
-			sha256_update(&sctx, (u8 *)&h, 4);
-		}
-	}
+	for (i = 0; i < handles; i++)
+		sha256_update(&sctx, auth->name[i], auth->name_size_tbl[i]);
 	if (offset_s != tpm_buf_length(buf))
 		sha256_update(&sctx, &buf->data[offset_s],
 			      tpm_buf_length(buf) - offset_s);
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 2eb17e1ccd58..042f36600db1 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -301,7 +301,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);
@@ -315,6 +315,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>
 
@@ -338,6 +339,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)
+{
+	put_unaligned_be32(handle, name);
+	return sizeof(u32);
+}
 #endif	/* CONFIG_TCG_TPM2_HMAC */
 
 #endif
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 32d90a1f8357..575b488a4f78 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -211,9 +211,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_options *options)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	struct tpm_buf *sized __free(kfree) = NULL;
+	u16 parent_name_size;
 	int blob_len = 0;
 	int hash;
 	u32 flags;
@@ -230,6 +232,12 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		goto out_put;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		goto out_put;
@@ -253,7 +261,8 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 
 	tpm_buf_init_sized(sized, TPM_BUFSIZE);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		goto out;
 
@@ -358,7 +367,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 {
 	u8 *blob_ref __free(kfree) = NULL;
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
@@ -412,6 +423,12 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		return rc;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -425,7 +442,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		return rc;
 
@@ -470,11 +488,19 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 blob_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 blob_name_size;
 	u16 data_len;
 	u8 *data;
 	int rc;
 
+	rc = tpm2_read_public(chip, blob_handle, blob_name);
+	if (rc < 0)
+		return rc;
+
+	blob_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -488,7 +514,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
 
-	rc = tpm_buf_append_name(chip, buf, blob_handle, NULL);
+	rc = tpm_buf_append_name(chip, buf, blob_handle, blob_name,
+				 blob_name_size);
 	if (rc)
 		return rc;
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 07/17] tpm-buf: Make tpm_buf_read*() fallible
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (5 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 06/17] tpm2-sessions: Re-orchestrate tpm2_read_public() use Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 08/17] KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE Jarkko Sakkinen
                   ` (11 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Make tpm_buf_read_u8(), tpm_buf_read_u16() and tpm_buf_read_u32() flows
unambiguous, and fallible by writing the updated offset to @offset.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm-buf.c                |  56 ++++---
 drivers/char/tpm/tpm2-sessions.c          | 188 ++++++++++++++++------
 include/linux/tpm_buf.h                   |   6 +-
 security/keys/trusted-keys/trusted_tpm2.c |  10 +-
 4 files changed, 178 insertions(+), 82 deletions(-)

diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c
index 233e81d3f149..a2d79b3a9385 100644
--- a/drivers/char/tpm/tpm-buf.c
+++ b/drivers/char/tpm/tpm-buf.c
@@ -204,39 +204,35 @@ void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle)
  * @offset:	offset within the buffer
  * @count:	the number of bytes to read
  * @output:	the output buffer
+ *
+ * Return: true on success, and @offset is advanced by @count bytes, or false
+ * if the read would cross the end of the buffer.
  */
-static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
+static bool tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
 {
 	off_t next_offset;
 
-	if (buf->flags & TPM_BUF_INVALID)
-		return;
-
 	next_offset = *offset + count;
-	if (next_offset > buf->length) {
-		WARN(1, "tpm_buf: read out of boundary\n");
-		buf->flags |= TPM_BUF_INVALID;
-		return;
-	}
+	if (next_offset > buf->length)
+		return false;
 
 	memcpy(output, &buf->data[*offset], count);
 	*offset = next_offset;
+	return true;
 }
 
 /**
  * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 8-bit word
  *
- * Return: next 8-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value)
 {
-	u8 value = 0;
-
-	tpm_buf_read(buf, offset, sizeof(value), &value);
-
-	return value;
+	return tpm_buf_read(buf, offset, sizeof(*value), value);
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
 
@@ -244,16 +240,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
  * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 16-bit word
  *
- * Return: next 16-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value)
 {
-	u16 value = 0;
+	__be16 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be16_to_cpu(value);
+	*value = be16_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
 
@@ -261,16 +261,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
  * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 32-bit word
  *
- * Return: next 32-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value)
 {
-	u32 value = 0;
+	__be32 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be32_to_cpu(value);
+	*value = be32_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);
 
diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index fa107719b4f9..185bae525519 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -179,6 +179,7 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	int rc, name_size_alg;
+	u16 size;
 
 	if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
 	    mso != TPM2_MSO_NVRAM) {
@@ -199,8 +200,10 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 		return tpm_ret_to_err(rc);
 
 	/* Skip TPMT_PUBLIC: */
-	rc = tpm_buf_read_u16(buf, &offset);
-	offset += rc;
+	if (!tpm_buf_read_u16(buf, &offset, &size))
+		return -EIO;
+
+	offset += size;
 
 	/*
 	 * Ensure space for the length field of TPM2B_NAME and hashAlg field of
@@ -209,18 +212,20 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	if (offset + 4 > tpm_buf_length(buf))
 		return -EIO;
 
-	rc = tpm_buf_read_u16(buf, &offset);
+	if (!tpm_buf_read_u16(buf, &offset, &size))
+		return -EIO;
+
 	name_size_alg = name_size(&buf->data[offset]);
 	if (name_size_alg < 0)
 		return name_size_alg;
 
-	if (rc != name_size_alg)
+	if (size != name_size_alg)
 		return -EIO;
 
-	if (offset + rc > tpm_buf_length(buf))
+	if (offset + size > tpm_buf_length(buf))
 		return -EIO;
 
-	memcpy(name, &buf->data[offset], rc);
+	memcpy(name, &buf->data[offset], size);
 	return name_size_alg;
 }
 EXPORT_SYMBOL_GPL(tpm2_read_public);
@@ -618,21 +623,41 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 	handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
 	offset_s += handles * sizeof(u32);
 
-	val = tpm_buf_read_u32(buf, &offset_s);
+	if (!tpm_buf_read_u32(buf, &offset_s, &val)) {
+		ret = -EIO;
+		goto err;
+	}
+
 	/* point offset_p to the start of the parameters */
 	offset_p = offset_s + val;
 	for (i = 1; offset_s < offset_p; i++) {
-		u32 handle = tpm_buf_read_u32(buf, &offset_s);
+		u32 handle;
 		u16 len;
 		u8 a;
 
+		if (!tpm_buf_read_u32(buf, &offset_s, &handle)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		/* nonce (already in auth) */
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		offset_s += len;
 
-		a = tpm_buf_read_u8(buf, &offset_s);
+		if (!tpm_buf_read_u8(buf, &offset_s, &a)) {
+			ret = -EIO;
+			goto err;
+		}
+
+		if (!tpm_buf_read_u16(buf, &offset_s, &len)) {
+			ret = -EIO;
+			goto err;
+		}
 
-		len = tpm_buf_read_u16(buf, &offset_s);
 		if (handle == auth->handle && auth->attrs == a) {
 			hmac = &buf->data[offset_s];
 			/*
@@ -665,7 +690,11 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 			  auth->tpm_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
 			  auth->scratch);
 
-		len = tpm_buf_read_u16(buf, &offset_p);
+		if (!tpm_buf_read_u16(buf, &offset_p, &len)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_encrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
@@ -740,7 +769,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 	struct sha256_ctx sctx;
 	struct hmac_sha256_ctx hctx;
 	u16 tag = be16_to_cpu(head->tag);
-	int parm_len, len, i, handles;
+	u32 parm_len;
+	int i, handles;
+	u8 session_attrs;
+	u16 len;
 
 	if (!auth)
 		return rc;
@@ -770,26 +802,38 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	/* point to area beyond handles */
 	offset_s = TPM_HEADER_SIZE + handles * 4;
-	parm_len = tpm_buf_read_u32(buf, &offset_s);
+	if (!tpm_buf_read_u32(buf, &offset_s, &parm_len))
+		goto out;
+
 	offset_p = offset_s;
 	offset_s += parm_len;
 	/* skip over any sessions before ours */
 	for (i = 0; i < auth->session - 1; i++) {
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len))
+			goto out;
+
 		offset_s += len + 1;
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len))
+			goto out;
+
 		offset_s += len;
 	}
 	/* TPM nonce */
-	len = tpm_buf_read_u16(buf, &offset_s);
+	if (!tpm_buf_read_u16(buf, &offset_s, &len))
+		goto out;
+
 	if (offset_s + len > tpm_buf_length(buf))
 		goto out;
 	if (len != SHA256_DIGEST_SIZE)
 		goto out;
 	memcpy(auth->tpm_nonce, &buf->data[offset_s], len);
 	offset_s += len;
-	attrs = tpm_buf_read_u8(buf, &offset_s);
-	len = tpm_buf_read_u16(buf, &offset_s);
+	if (!tpm_buf_read_u8(buf, &offset_s, &session_attrs))
+		goto out;
+
+	if (!tpm_buf_read_u16(buf, &offset_s, &len))
+		goto out;
+
 	if (offset_s + len != tpm_buf_length(buf))
 		goto out;
 	if (len != SHA256_DIGEST_SIZE)
@@ -831,8 +875,8 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 			  auth->our_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
 			  auth->scratch);
 
-		len = tpm_buf_read_u16(buf, &offset_p);
-		if (offset_p + len > tpm_buf_length(buf)) {
+		if (!tpm_buf_read_u16(buf, &offset_p, &len) ||
+		    offset_p + len > tpm_buf_length(buf)) {
 			rc = -EINVAL;
 			goto out;
 		}
@@ -888,7 +932,7 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
 	struct tpm_header *head = (struct tpm_header *)buf->data;
 	u32 tot_len = be32_to_cpu(head->length);
 	off_t offset = TPM_HEADER_SIZE;
-	u32 val;
+	u16 val;
 
 	/* we're starting after the header so adjust the length */
 	tot_len -= TPM_HEADER_SIZE;
@@ -897,8 +941,12 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
 	if (tot_len != 4 + 2 + sizeof(auth->tpm_nonce))
 		return -EINVAL;
 
-	auth->handle = tpm_buf_read_u32(buf, &offset);
-	val = tpm_buf_read_u16(buf, &offset);
+	if (!tpm_buf_read_u32(buf, &offset, &auth->handle))
+		return -EIO;
+
+	if (!tpm_buf_read_u16(buf, &offset, &val))
+		return -EIO;
+
 	if (val != sizeof(auth->tpm_nonce))
 		return -EINVAL;
 	memcpy(auth->tpm_nonce, &buf->data[offset], sizeof(auth->tpm_nonce));
@@ -1070,15 +1118,20 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	off_t offset_r = TPM_HEADER_SIZE, offset_t;
 	u16 len = TPM_HEADER_SIZE;
 	u32 total_len = be32_to_cpu(head->length);
-	u32 val, param_len, keyhandle;
+	u32 val32, param_len, keyhandle;
+	u16 val;
+
+	if (!tpm_buf_read_u32(buf, &offset_r, &keyhandle))
+		return -EIO;
 
-	keyhandle = tpm_buf_read_u32(buf, &offset_r);
 	if (handle)
 		*handle = keyhandle;
 	else
 		tpm2_flush_context(chip, keyhandle);
 
-	param_len = tpm_buf_read_u32(buf, &offset_r);
+	if (!tpm_buf_read_u32(buf, &offset_r, &param_len))
+		return -EIO;
+
 	/*
 	 * param_len doesn't include the header, but all the other
 	 * lengths and offsets do, so add it to parm len to make
@@ -1088,7 +1141,10 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	if (param_len + 8 > total_len)
 		return -EINVAL;
-	len = tpm_buf_read_u16(buf, &offset_r);
+
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_t = offset_r;
 	if (offset_r + len > tpm_buf_length(buf))
 		return -EINVAL;
@@ -1102,64 +1158,88 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 
 	/* validate the public key */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
 
 	/* key type (must be what we asked for) */
 	if (val != TPM_ALG_ECC)
 		return -EINVAL;
-	val = tpm_buf_read_u16(buf, &offset_t);
+
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
 
 	/* name algorithm */
 	if (val != TPM_ALG_SHA256)
 		return -EINVAL;
-	val = tpm_buf_read_u32(buf, &offset_t);
+
+	if (!tpm_buf_read_u32(buf, &offset_t, &val32))
+		return -EIO;
 
 	/* object properties */
-	if (val != TPM2_OA_NULL_KEY)
+	if (val32 != TPM2_OA_NULL_KEY)
 		return -EINVAL;
 
 	/* auth policy (empty) */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != 0)
 		return -EINVAL;
 
 	/* symmetric key parameters */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_AES)
 		return -EINVAL;
 
 	/* symmetric key length */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != AES_KEY_BITS)
 		return -EINVAL;
 
 	/* symmetric encryption scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_CFB)
 		return -EINVAL;
 
 	/* signing scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_NULL)
 		return -EINVAL;
 
 	/* ECC Curve */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM2_ECC_NIST_P256)
 		return -EINVAL;
 
 	/* KDF Scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_NULL)
 		return -EINVAL;
 
 	/* extract public key (x and y points) */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != EC_PT_SZ)
 		return -EINVAL;
 	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
 	offset_t += val;
-	val = tpm_buf_read_u16(buf, &offset_t);
+
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != EC_PT_SZ)
 		return -EINVAL;
 	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
@@ -1175,30 +1255,40 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 		return -EINVAL;
 
 	/* creation data (skip) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (offset_r > param_len)
 		return -EINVAL;
 
 	/* creation digest (must be sha256) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
 		return -EINVAL;
 
 	/* TPMT_TK_CREATION follows */
 	/* tag, must be TPM_ST_CREATION (0x8021) */
-	val = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &val))
+		return -EIO;
+
 	if (val != TPM2_ST_CREATION || offset_r > param_len)
 		return -EINVAL;
 
 	/* hierarchy */
-	val = tpm_buf_read_u32(buf, &offset_r);
-	if (val != hierarchy || offset_r > param_len)
+	if (!tpm_buf_read_u32(buf, &offset_r, &val32))
+		return -EIO;
+
+	if (val32 != hierarchy || offset_r > param_len)
 		return -EINVAL;
 
 	/* the ticket digest HMAC (might not be sha256) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (offset_r > param_len)
 		return -EINVAL;
@@ -1207,7 +1297,9 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	 * finally we have the name, which is a sha256 digest plus a 2
 	 * byte algorithm type
 	 */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	if (offset_r + len != param_len + 8)
 		return -EINVAL;
 	if (len != SHA256_DIGEST_SIZE + 2)
diff --git a/include/linux/tpm_buf.h b/include/linux/tpm_buf.h
index f8c105d8b8bf..d0f4c1c3ee2d 100644
--- a/include/linux/tpm_buf.h
+++ b/include/linux/tpm_buf.h
@@ -52,9 +52,9 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length);
 void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value);
 void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value);
 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);
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value);
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value);
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value);
 void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle);
 
 #endif
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 575b488a4f78..e20e54203b83 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -217,6 +217,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	struct tpm_buf *sized __free(kfree) = NULL;
 	u16 parent_name_size;
 	int blob_len = 0;
+	u32 size;
 	int hash;
 	u32 flags;
 	int rc;
@@ -323,18 +324,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		goto out;
 
-	blob_len = tpm_buf_read_u32(buf, &offset);
-	if (buf->flags & TPM_BUF_INVALID) {
-		rc = -E2BIG;
+	if (!tpm_buf_read_u32(buf, &offset, &size)) {
+		rc = -EIO;
 		goto out;
 	}
 
-	if (buf->length - offset < blob_len) {
+	if (buf->length - offset < size) {
 		rc = -EFAULT;
 		goto out;
 	}
 
-	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], blob_len);
+	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], size);
 	if (blob_len < 0)
 		rc = blob_len;
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 08/17] KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (6 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 07/17] tpm-buf: Make tpm_buf_read*() fallible Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 09/17] lib/asn1_encoder: Add asn1_encode_integer_bytes() Jarkko Sakkinen
                   ` (10 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

tpm2_unseal_cmd() rejects unsealed payloads shorter than MIN_KEY_SIZE (32
bytes) with -EFAULT even when loading an ASN.1 TPMKey (OID_TPMSealedData,
2.23.133.10.1.5) blob.

While kernel-generated trusted keys ("new <keylen>") and legacy old_format
blobs are at least 32 bytes long, external TPM2 tools allow sealing any
non-empty payload up to 128 bytes.

Keep MIN_KEY_SIZE for legacy old_format blobs and accept 1..MAX_KEY_SIZE
bytes for ASN.1 TPMKey sealed blobs.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index e20e54203b83..6b4cd07176cb 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -490,8 +490,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	struct trusted_key_tpm *private = options->private;
 	u8 blob_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	unsigned int data_len;
 	u16 blob_name_size;
-	u16 data_len;
 	u8 *data;
 	int rc;
 
@@ -555,9 +555,11 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
 
 	if (!rc) {
+		unsigned int min_len = payload->old_format ? MIN_KEY_SIZE : 1;
+
 		data_len = be16_to_cpup(
 			(__be16 *)&buf->data[TPM_HEADER_SIZE + 4]);
-		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE)
+		if (data_len < min_len || data_len > MAX_KEY_SIZE)
 			return -EFAULT;
 
 		if (tpm_buf_length(buf) < TPM_HEADER_SIZE + 6 + data_len)
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 09/17] lib/asn1_encoder: Add asn1_encode_integer_bytes()
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (7 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 08/17] KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 10/17] tpm: Export tpm2_create_primary() Jarkko Sakkinen
                   ` (9 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Add a helper encoding a positive integer from a byte array in big-endian
format.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 include/linux/asn1_encoder.h |  3 ++
 lib/asn1_encoder.c           | 56 ++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/include/linux/asn1_encoder.h b/include/linux/asn1_encoder.h
index d17484dffb74..e206bd425854 100644
--- a/include/linux/asn1_encoder.h
+++ b/include/linux/asn1_encoder.h
@@ -12,6 +12,9 @@ unsigned char *
 asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 		    s64 integer);
 unsigned char *
+asn1_encode_integer_bytes(unsigned char *data, const unsigned char *end_data,
+			  const unsigned char *integer, u32 integer_len);
+unsigned char *
 asn1_encode_oid(unsigned char *data, const unsigned char *end_data,
 		u32 oid[], int oid_len);
 unsigned char *
diff --git a/lib/asn1_encoder.c b/lib/asn1_encoder.c
index 92f35aae13b1..8fea70dc0846 100644
--- a/lib/asn1_encoder.c
+++ b/lib/asn1_encoder.c
@@ -10,6 +10,8 @@
 #include <linux/string.h>
 #include <linux/module.h>
 
+static int asn1_encode_length(unsigned char **data, int *data_len, int len);
+
 /**
  * asn1_encode_integer() - encode positive integer to ASN.1
  * @data:	pointer to the pointer to the data
@@ -85,6 +87,60 @@ asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 }
 EXPORT_SYMBOL_GPL(asn1_encode_integer);
 
+/**
+ * asn1_encode_integer_bytes() - encode positive integer bytes to ASN.1
+ * @data:		pointer to the pointer to the data
+ * @end_data:		end of data pointer, points one beyond last usable byte in @data
+ * @bytes:		integer bytes
+ * @bytes_len:		amount of bytes
+ *
+ * Encode a positive integer from a byte array in big-endian format. Strip
+ * leading zeros.
+ */
+unsigned char *
+asn1_encode_integer_bytes(unsigned char *data, const unsigned char *end_data,
+			  const unsigned char *bytes, u32 bytes_len)
+{
+	int data_len = end_data - data;
+	bool add_pad;
+	int ret;
+
+	if (IS_ERR(data))
+		return data;
+
+	if (!bytes || !bytes_len)
+		return ERR_PTR(-EINVAL);
+
+	/* Strip leading zeros: */
+	while (bytes_len > 1 && bytes[0] == 0) {
+		bytes++;
+		bytes_len--;
+	}
+
+	add_pad = bytes[0] & 0x80;
+
+	if (data_len < 2)
+		return ERR_PTR(-EINVAL);
+
+	*(data++) = _tag(UNIV, PRIM, INT);
+	data_len--;
+
+	ret = asn1_encode_length(&data, &data_len, bytes_len + add_pad);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (data_len < bytes_len + add_pad)
+		return ERR_PTR(-EINVAL);
+
+	if (add_pad)
+		*(data++) = 0;
+
+	memcpy(data, bytes, bytes_len);
+	data += bytes_len;
+	return data;
+}
+EXPORT_SYMBOL_GPL(asn1_encode_integer_bytes);
+
 /* calculate the base 128 digit values setting the top bit of the first octet */
 static int asn1_encode_oid_digit(unsigned char **_data, int *data_len, u32 oid)
 {
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 10/17] tpm: Export tpm2_create_primary()
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (8 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 09/17] lib/asn1_encoder: Add asn1_encode_integer_bytes() Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 11/17] tpm: Add persistent hierarchies to tpm2_create_primary() Jarkko Sakkinen
                   ` (8 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Move tpm2_create_primary() outside CONFIG_TCG_TPM2_HMAC and export it for
callers such as trusted keys.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/Kconfig         |   2 +-
 drivers/char/tpm/tpm2-sessions.c | 103 ++++++++++++++++---------------
 include/linux/tpm.h              |   2 +
 3 files changed, 56 insertions(+), 51 deletions(-)

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index 35aff3003a32..7481f2c8c768 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -9,6 +9,7 @@ menuconfig TCG_TPM
 	imply SECURITYFS
 	select CRYPTO
 	select CRYPTO_HASH_INFO
+	select CRYPTO_LIB_SHA256
 	help
 	  If you have a TPM security chip in your system, which
 	  implements the Trusted Computing Group's specification,
@@ -32,7 +33,6 @@ config TCG_TPM2_HMAC
 	default n
 	select CRYPTO_ECDH
 	select CRYPTO_LIB_AESCFB
-	select CRYPTO_LIB_SHA256
 	select CRYPTO_LIB_UTILS
 	help
 	  Setting this causes us to deploy a scheme which uses request
diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index 185bae525519..4b827112be4d 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -413,9 +413,6 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_hmac_session);
 
 #ifdef CONFIG_TCG_TPM2_HMAC
 
-static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
-			       u32 *handle, u8 *name);
-
 /*
  * assume hash sha256 and nonces u, v of size SHA256_DIGEST_SIZE but
  * otherwise standard tpm2_KDFa.  Note output is in bytes not bits.
@@ -1083,6 +1080,50 @@ int tpm2_start_auth_session(struct tpm_chip *chip)
 }
 EXPORT_SYMBOL(tpm2_start_auth_session);
 
+static int tpm2_create_null_primary(struct tpm_chip *chip)
+{
+	u32 null_key;
+	int rc;
+
+	rc = tpm2_create_primary(chip, TPM2_RH_NULL, &null_key,
+				 chip->null_key_name);
+
+	if (rc == TPM2_RC_SUCCESS) {
+		unsigned int offset = 0; /* dummy offset for null key context */
+
+		rc = tpm2_save_context(chip, null_key, chip->null_key_context,
+				       sizeof(chip->null_key_context), &offset);
+		tpm2_flush_context(chip, null_key);
+	}
+
+	return rc;
+}
+
+/**
+ * tpm2_sessions_init() - start of day initialization for the sessions code
+ * @chip: TPM chip
+ *
+ * Derive and context save the null primary and allocate memory in the
+ * struct tpm_chip for the authorizations.
+ *
+ * Return:
+ * * 0		- OK
+ * * -errno	- A system error
+ * * TPM_RC	- A TPM error
+ */
+int tpm2_sessions_init(struct tpm_chip *chip)
+{
+	int rc;
+
+	rc = tpm2_create_null_primary(chip);
+	if (rc) {
+		dev_err(&chip->dev, "null key creation failed with %d\n", rc);
+		return rc;
+	}
+
+	return rc;
+}
+#endif /* CONFIG_TCG_TPM2_HMAC */
 /*
  * A mask containing the object attributes for the kernel held null primary key
  * used in HMAC encryption. For more information on specific attributes look up
@@ -1234,7 +1275,9 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	if (val != EC_PT_SZ)
 		return -EINVAL;
+#ifdef CONFIG_TCG_TPM2_HMAC
 	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
+#endif
 	offset_t += val;
 
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
@@ -1242,7 +1285,9 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	if (val != EC_PT_SZ)
 		return -EINVAL;
+#ifdef CONFIG_TCG_TPM2_HMAC
 	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
+#endif
 	offset_t += val;
 
 	/* original length of the whole TPM2B */
@@ -1305,11 +1350,13 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	if (len != SHA256_DIGEST_SIZE + 2)
 		return -EINVAL;
 
+#ifdef CONFIG_TCG_TPM2_HMAC
 	if (memcmp(chip->null_key_name, &buf->data[offset_r],
 		   SHA256_DIGEST_SIZE + 2) != 0) {
 		dev_err(&chip->dev, "NULL Seed name comparison failed\n");
 		return -EINVAL;
 	}
+#endif
 
 	return 0;
 }
@@ -1334,8 +1381,8 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
  * * -errno	- A system error
  * * TPM_RC	- A TPM error
  */
-static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
-			       u32 *handle, u8 *name)
+int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
+			u8 *name)
 {
 	struct tpm_buf *template __free(kfree) = NULL;
 	struct tpm_buf *buf __free(kfree) = NULL;
@@ -1431,48 +1478,4 @@ static int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy,
 
 	return rc;
 }
-
-static int tpm2_create_null_primary(struct tpm_chip *chip)
-{
-	u32 null_key;
-	int rc;
-
-	rc = tpm2_create_primary(chip, TPM2_RH_NULL, &null_key,
-				 chip->null_key_name);
-
-	if (rc == TPM2_RC_SUCCESS) {
-		unsigned int offset = 0; /* dummy offset for null key context */
-
-		rc = tpm2_save_context(chip, null_key, chip->null_key_context,
-				       sizeof(chip->null_key_context), &offset);
-		tpm2_flush_context(chip, null_key);
-	}
-
-	return rc;
-}
-
-/**
- * tpm2_sessions_init() - start of day initialization for the sessions code
- * @chip: TPM chip
- *
- * Derive and context save the null primary and allocate memory in the
- * struct tpm_chip for the authorizations.
- *
- * Return:
- * * 0		- OK
- * * -errno	- A system error
- * * TPM_RC	- A TPM error
- */
-int tpm2_sessions_init(struct tpm_chip *chip)
-{
-	int rc;
-
-	rc = tpm2_create_null_primary(chip);
-	if (rc) {
-		dev_err(&chip->dev, "null key creation failed with %d\n", rc);
-		return rc;
-	}
-
-	return rc;
-}
-#endif /* CONFIG_TCG_TPM2_HMAC */
+EXPORT_SYMBOL_GPL(tpm2_create_primary);
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 042f36600db1..9cc401813a19 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -249,6 +249,8 @@ 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_find_hash_alg(unsigned int crypto_id);
+int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
+			u8 *name);
 
 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
 {
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 11/17] tpm: Add persistent hierarchies to tpm2_create_primary()
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (9 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 10/17] tpm: Export tpm2_create_primary() Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 12/17] KEYS: trusted: Extend TPMKey support Jarkko Sakkinen
                   ` (7 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

TPMKey files can specify a 0x40 MSO permanent handle as parent, requiring
TPM2_CreatePrimary with the TCG H-2 NIST P-256 template [1].

Populate chip->null_ec_key_{x,y} in tpm2_parse_create_primary() only for
TPM2_RH_NULL.

Verify the returned key name against the locally computed TPMT_PUBLIC
digest, and flush the transient handle on validation failure.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#section-3.1.8

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm2-sessions.c | 191 ++++++++++++++++---------------
 1 file changed, 97 insertions(+), 94 deletions(-)

diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index 4b827112be4d..40fbe6fde091 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -1124,10 +1124,11 @@ int tpm2_sessions_init(struct tpm_chip *chip)
 	return rc;
 }
 #endif /* CONFIG_TCG_TPM2_HMAC */
+
 /*
- * A mask containing the object attributes for the kernel held null primary key
- * used in HMAC encryption. For more information on specific attributes look up
- * to "8.3 TPMA_OBJECT (Object Attributes)".
+ * A mask containing the object attributes for the TCG mandated H-2 ECC P-256
+ * primary storage key template. For more information on specific attributes
+ * look up "8.3 TPMA_OBJECT (Object Attributes)".
  */
 #define TPM2_OA_NULL_KEY ( \
 	TPM2_OA_NO_DA | \
@@ -1157,21 +1158,23 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 {
 	struct tpm_header *head = (struct tpm_header *)buf->data;
 	off_t offset_r = TPM_HEADER_SIZE, offset_t;
-	u16 len = TPM_HEADER_SIZE;
+	u8 resp_name[TPM2_NULL_NAME_SIZE];
 	u32 total_len = be32_to_cpu(head->length);
 	u32 val32, param_len, keyhandle;
-	u16 val;
+#ifdef CONFIG_TCG_TPM2_HMAC
+	const u8 *ec_x, *ec_y;
+#endif
+	int rc = -EIO;
+	u16 val, len;
 
 	if (!tpm_buf_read_u32(buf, &offset_r, &keyhandle))
 		return -EIO;
 
-	if (handle)
-		*handle = keyhandle;
-	else
-		tpm2_flush_context(chip, keyhandle);
-
 	if (!tpm_buf_read_u32(buf, &offset_r, &param_len))
-		return -EIO;
+		goto err;
+	if (total_len < TPM_HEADER_SIZE + 8 ||
+	    param_len > total_len - TPM_HEADER_SIZE - 8)
+		goto err_inval;
 
 	/*
 	 * param_len doesn't include the header, but all the other
@@ -1180,185 +1183,185 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	 */
 	param_len += TPM_HEADER_SIZE;
 
-	if (param_len + 8 > total_len)
-		return -EINVAL;
-
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_t = offset_r;
-	if (offset_r + len > tpm_buf_length(buf))
-		return -EINVAL;
-	if (name) {
-		/*
-		 * now we have the public area, compute the name of
-		 * the object
-		 */
-		put_unaligned_be16(TPM_ALG_SHA256, name);
-		sha256(&buf->data[offset_r], len, name + 2);
-	}
+	offset_r += len;
+	if (offset_r > param_len || offset_r > tpm_buf_length(buf))
+		goto err_inval;
+
+	/*
+	 * Now we have the public area, compute the name of the object.
+	 */
+	put_unaligned_be16(TPM_ALG_SHA256, resp_name);
+	sha256(&buf->data[offset_t], len, resp_name + 2);
 
 	/* validate the public key */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
+		goto err;
 
 	/* key type (must be what we asked for) */
 	if (val != TPM_ALG_ECC)
-		return -EINVAL;
+		goto err_inval;
 
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
+		goto err;
 
 	/* name algorithm */
 	if (val != TPM_ALG_SHA256)
-		return -EINVAL;
+		goto err_inval;
 
 	if (!tpm_buf_read_u32(buf, &offset_t, &val32))
-		return -EIO;
+		goto err;
 
 	/* object properties */
 	if (val32 != TPM2_OA_NULL_KEY)
-		return -EINVAL;
+		goto err_inval;
 
 	/* auth policy (empty) */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != 0)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric key parameters */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_AES)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric key length */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != AES_KEY_BITS)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric encryption scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_CFB)
-		return -EINVAL;
+		goto err_inval;
 
 	/* signing scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err_inval;
 
 	/* ECC Curve */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM2_ECC_NIST_P256)
-		return -EINVAL;
+		goto err_inval;
 
 	/* KDF Scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err_inval;
 
 	/* extract public key (x and y points) */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
-	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
+	if (val != EC_PT_SZ || offset_t + val > offset_r)
+		goto err_inval;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
+	ec_x = &buf->data[offset_t];
 #endif
 	offset_t += val;
 
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
-	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
+	if (val != EC_PT_SZ || offset_t + val > offset_r)
+		goto err_inval;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
+	ec_y = &buf->data[offset_t];
 #endif
 	offset_t += val;
 
-	/* original length of the whole TPM2B */
-	offset_r += len;
-
 	/* should have exactly consumed the TPM2B public structure */
 	if (offset_t != offset_r)
-		return -EINVAL;
-	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* creation data (skip) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* creation digest (must be sha256) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* TPMT_TK_CREATION follows */
 	/* tag, must be TPM_ST_CREATION (0x8021) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM2_ST_CREATION || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* hierarchy */
 	if (!tpm_buf_read_u32(buf, &offset_r, &val32))
-		return -EIO;
-
+		goto err;
 	if (val32 != hierarchy || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* the ticket digest HMAC (might not be sha256) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/*
 	 * finally we have the name, which is a sha256 digest plus a 2
 	 * byte algorithm type
 	 */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
+		goto err;
+	if (offset_r + len != param_len + 8 || len != sizeof(resp_name))
+		goto err_inval;
+	if (offset_r + len > tpm_buf_length(buf))
+		goto err;
 
-	if (offset_r + len != param_len + 8)
-		return -EINVAL;
-	if (len != SHA256_DIGEST_SIZE + 2)
-		return -EINVAL;
+	if (memcmp(resp_name, &buf->data[offset_r], sizeof(resp_name)) != 0) {
+		dev_err(&chip->dev, "primary key name comparison failed\n");
+		goto err_inval;
+	}
 
 #ifdef CONFIG_TCG_TPM2_HMAC
-	if (memcmp(chip->null_key_name, &buf->data[offset_r],
-		   SHA256_DIGEST_SIZE + 2) != 0) {
-		dev_err(&chip->dev, "NULL Seed name comparison failed\n");
-		return -EINVAL;
+	if (hierarchy == TPM2_RH_NULL) {
+		if (name == chip->null_key_name) {
+			memcpy(chip->null_ec_key_x, ec_x, EC_PT_SZ);
+			memcpy(chip->null_ec_key_y, ec_y, EC_PT_SZ);
+		} else if (memcmp(chip->null_key_name, resp_name,
+				  sizeof(resp_name)) != 0) {
+			dev_err(&chip->dev,
+				"NULL Seed name comparison failed\n");
+			goto err_inval;
+		}
 	}
 #endif
 
+	if (name)
+		memcpy(name, resp_name, sizeof(resp_name));
+
+	if (handle)
+		*handle = keyhandle;
+	else
+		tpm2_flush_context(chip, keyhandle);
+
 	return 0;
+
+err_inval:
+	rc = -EINVAL;
+err:
+	tpm2_flush_context(chip, keyhandle);
+	return rc;
 }
 
 /**
@@ -1470,7 +1473,7 @@ int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
 	tpm_buf_append_u32(buf, 0);
 
 	rc = tpm_transmit_cmd(chip, buf, 0,
-			      "attempting to create NULL primary");
+			      "attempting to create primary key");
 
 	if (rc == TPM2_RC_SUCCESS)
 		rc = tpm2_parse_create_primary(chip, buf, handle, hierarchy,
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 12/17] KEYS: trusted: Extend TPMKey support
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (10 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 11/17] tpm: Add persistent hierarchies to tpm2_create_primary() Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 13/17] KEYS: trusted: Reject zero-length keys in symmetric consumers Jarkko Sakkinen
                   ` (6 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Extend tpm2key.asn1 with the optional [1] policy, [2] secret, [3]
authPolicy, [4] description, and [5] rsaParent fields.

Create a temporary TCG H-2 NIST P-256 primary key in tpm2_seal_trusted()
and tpm2_load_cmd() when the parent handle is TPM2_RH_OWNER, TPM2_RH_NULL,
or TPM2_RH_PLATFORM.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 .../security/keys/trusted-encrypted.rst       |  18 ++-
 include/keys/trusted_tpm.h                    |   1 -
 include/linux/tpm_command.h                   |   2 +
 security/keys/trusted-keys/tpm2key.asn1       |  19 +++
 security/keys/trusted-keys/trusted_tpm2.c     | 150 +++++++++++++++---
 5 files changed, 161 insertions(+), 29 deletions(-)

diff --git a/Documentation/security/keys/trusted-encrypted.rst b/Documentation/security/keys/trusted-encrypted.rst
index ddff7c7c2582..c6a507ef228a 100644
--- a/Documentation/security/keys/trusted-encrypted.rst
+++ b/Documentation/security/keys/trusted-encrypted.rst
@@ -561,6 +561,11 @@ policy::
     TPMKey ::= SEQUENCE {
         type		OBJECT IDENTIFIER
         emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL
+        policy		[1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
+        secret		[2] EXPLICIT OCTET STRING OPTIONAL
+        authPolicy	[3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
+        description	[4] EXPLICIT UTF8String OPTIONAL
+        rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL
         parent		INTEGER
         pubkey		OCTET STRING
         privkey		OCTET STRING
@@ -592,12 +597,13 @@ phrase.  This is used by most user space consumers to decide whether
 to prompt for a password.
 
 parent represents the parent key handle, either in the 0x81 MSO space,
-like 0x81000001 for the RSA primary storage key.  Userspace programmes
-also support specifying the primary handle in the 0x40 MSO space.  If
-this happens the Elliptic Curve variant of the primary key using the
-TCG defined template will be generated on the fly into a volatile
-object and used as the parent.  The current kernel code only supports
-the 0x81 MSO form.
+like 0x81000001 for a persistent primary storage key, or a hierarchy
+handle in the 0x40 MSO permanent handle space (TPM_RH_OWNER 0x40000001,
+TPM_RH_NULL 0x40000007, or TPM_RH_PLATFORM 0x4000000c).  When a 0x40
+MSO hierarchy handle is used without rsaParent, the Elliptic Curve
+variant of the primary key using the TCG defined H-2 NIST P-256 template
+is generated on the fly into a volatile object, used as the parent, and
+flushed afterwards.
 
 pubkey is the binary representation of TPM2B_PRIVATE excluding the
 initial TPM2B header, which can be reconstructed from the ASN.1 octet
diff --git a/include/keys/trusted_tpm.h b/include/keys/trusted_tpm.h
index 9a83269821e5..b8b1f5c8fbc3 100644
--- a/include/keys/trusted_tpm.h
+++ b/include/keys/trusted_tpm.h
@@ -26,5 +26,4 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options);
-
 #endif
diff --git a/include/linux/tpm_command.h b/include/linux/tpm_command.h
index 9ea1ee3cbef1..6576a7df6d8c 100644
--- a/include/linux/tpm_command.h
+++ b/include/linux/tpm_command.h
@@ -259,8 +259,10 @@ enum tpm2_cc_attrs {
 };
 
 enum tpm2_permanent_handles {
+	TPM2_RH_OWNER		= 0x40000001,
 	TPM2_RH_NULL		= 0x40000007,
 	TPM2_RS_PW		= 0x40000009,
+	TPM2_RH_PLATFORM	= 0x4000000C,
 };
 
 /* Most Significant Octet for key types  */
diff --git a/security/keys/trusted-keys/tpm2key.asn1 b/security/keys/trusted-keys/tpm2key.asn1
index f57f869ad600..d15dfcf593a5 100644
--- a/security/keys/trusted-keys/tpm2key.asn1
+++ b/security/keys/trusted-keys/tpm2key.asn1
@@ -5,7 +5,26 @@
 TPMKey ::= SEQUENCE {
 	type		OBJECT IDENTIFIER ({tpm2_key_type}),
 	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence OPTIONAL,
+	secret		[2] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_secret}),
+	authPolicy	[3] EXPLICIT TPMAuthPolicySequence OPTIONAL,
+	description	[4] EXPLICIT UTF8String OPTIONAL,
+	rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL ({tpm2_key_rsa_parent}),
 	parent		INTEGER ({tpm2_key_parent}),
 	pubkey		OCTET STRING ({tpm2_key_pub}),
 	privkey		OCTET STRING ({tpm2_key_priv})
 	}
+
+TPMPolicySequence ::= SEQUENCE OF TPMPolicy ({tpm2_key_policy})
+
+TPMPolicy ::= SEQUENCE {
+	commandCode	[0] EXPLICIT INTEGER,
+	commandPolicy	[1] EXPLICIT OCTET STRING
+	}
+
+TPMAuthPolicySequence ::= SEQUENCE OF TPMAuthPolicy ({tpm2_key_auth_policy})
+
+TPMAuthPolicy ::= SEQUENCE {
+	name		[0] EXPLICIT UTF8String OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence
+	}
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 6b4cd07176cb..a19ab2a62f65 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -101,6 +101,7 @@ struct tpm2_key_context {
 	u32 pub_len;
 	const u8 *priv;
 	u32 priv_len;
+	bool has_policy;
 };
 
 static int tpm2_key_decode(struct trusted_key_payload *payload,
@@ -119,6 +120,9 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 	if (ret < 0)
 		return ret;
 
+	if (ctx.has_policy && !private->policyhandle)
+		return -EOPNOTSUPP;
+
 	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
 		return -EINVAL;
 
@@ -128,7 +132,8 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 
 	*buf = blob;
 	*blob_len = ctx.priv_len + ctx.pub_len;
-	private->keyhandle = ctx.parent;
+	if (!private->keyhandle)
+		private->keyhandle = ctx.parent;
 
 	memcpy(blob, ctx.priv, ctx.priv_len);
 	blob += ctx.priv_len;
@@ -138,13 +143,58 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 	return 0;
 }
 
+int tpm2_key_policy(void *context, size_t hdrlen,
+		    unsigned char tag,
+		    const void *value, size_t vlen)
+{
+	struct tpm2_key_context *ctx = context;
+
+	ctx->has_policy = true;
+	return 0;
+}
+
+int tpm2_key_secret(void *context, size_t hdrlen,
+		    unsigned char tag,
+		    const void *value, size_t vlen)
+{
+	return -EOPNOTSUPP;
+}
+
+int tpm2_key_auth_policy(void *context, size_t hdrlen,
+			 unsigned char tag,
+			 const void *value, size_t vlen)
+{
+	return -EOPNOTSUPP;
+}
+
+int tpm2_key_rsa_parent(void *context, size_t hdrlen,
+			unsigned char tag,
+			const void *value, size_t vlen)
+{
+	const u8 *v = value;
+
+	if (vlen != 1)
+		return -EBADMSG;
+	if (*v)
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
 int tpm2_key_parent(void *context, size_t hdrlen,
 		  unsigned char tag,
 		  const void *value, size_t vlen)
 {
 	struct tpm2_key_context *ctx = context;
 	const u8 *v = value;
-	int i;
+	size_t i;
+
+	if (vlen > 1 && v[0] == 0) {
+		v++;
+		vlen--;
+	}
+	if (!vlen || vlen > sizeof(u32))
+		return -EBADMSG;
 
 	ctx->parent = 0;
 	for (i = 0; i < vlen; i++) {
@@ -172,7 +222,6 @@ int tpm2_key_type(void *context, size_t hdrlen,
 
 	return 0;
 }
-
 int tpm2_key_pub(void *context, size_t hdrlen,
 	       unsigned char tag,
 	       const void *value, size_t vlen)
@@ -197,6 +246,48 @@ int tpm2_key_priv(void *context, size_t hdrlen,
 	return 0;
 }
 
+/**
+ * tpm2_load_parent() - resolve a parent handle for TPM 2.0 child operations
+ *
+ * @chip: TPM chip to use
+ * @parent: persistent/volatile object handle or hierarchy handle
+ * @handle: returned usable parent handle
+ * @name: buffer populated with the parent handle name
+ * @name_size: returned byte length of @name
+ * @created: set to true if a temporary primary key was created and must be flushed
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int tpm2_load_parent(struct tpm_chip *chip, u32 parent, u32 *handle,
+			    u8 name[TPM2_MAX_NAME_SIZE], u16 *name_size,
+			    bool *created)
+{
+	int rc;
+
+	*created = false;
+	if (tpm2_handle_mso(parent) != TPM2_MSO_PERMANENT) {
+		*handle = parent;
+		rc = tpm2_read_public(chip, parent, name);
+		if (rc < 0)
+			return rc;
+		*name_size = rc;
+		return 0;
+	}
+
+	if (parent != TPM2_RH_OWNER &&
+	    parent != TPM2_RH_NULL &&
+	    parent != TPM2_RH_PLATFORM)
+		return -EINVAL;
+
+	rc = tpm2_create_primary(chip, parent, handle, name);
+	if (rc)
+		return tpm_ret_to_err(rc);
+
+	*name_size = TPM2_NULL_NAME_SIZE;
+	*created = true;
+	return 0;
+}
+
 /**
  * tpm2_seal_trusted() - seal the payload of a trusted key
  *
@@ -215,7 +306,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	struct tpm_buf *sized __free(kfree) = NULL;
+	bool created_parent = false;
 	u16 parent_name_size;
+	u32 parent_handle;
 	int blob_len = 0;
 	u32 size;
 	int hash;
@@ -233,21 +326,20 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
-	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
-	if (rc < 0)
+	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
+			      parent_name, &parent_name_size, &created_parent);
+	if (rc)
 		goto out_put;
 
-	parent_name_size = rc;
-
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
-		goto out_put;
+		goto out_parent;
 
 	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
 	if (!buf) {
 		rc = -ENOMEM;
 		tpm2_end_auth_session(chip);
-		goto out_put;
+		goto out_parent;
 	}
 
 	tpm_buf_init(buf, TPM_BUFSIZE);
@@ -257,19 +349,18 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (!sized) {
 		rc = -ENOMEM;
 		tpm2_end_auth_session(chip);
-		goto out_put;
+		goto out_parent;
 	}
 
 	tpm_buf_init_sized(sized, TPM_BUFSIZE);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
 				 parent_name_size);
 	if (rc)
 		goto out;
 
 	tpm_buf_append_hmac_session(chip, buf, TPM2_SA_DECRYPT,
 				    private->keyauth, TPM_DIGEST_SIZE);
-
 	/* sensitive */
 	tpm_buf_append_u16(sized, private->blobauth_len);
 
@@ -342,6 +433,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (!rc)
 		payload->blob_len = blob_len;
 
+out_parent:
+	if (created_parent)
+		tpm2_flush_context(chip, parent_handle);
 out_put:
 	tpm_put_ops(chip);
 	return tpm_ret_to_err(rc);
@@ -369,16 +463,23 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	struct trusted_key_tpm *private = options->private;
 	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	bool created_parent = false;
 	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
+	u32 parent_handle;
 	u8 *blob, *pub;
 	int rc;
 	u32 attrs;
 
 	rc = tpm2_key_decode(payload, options, &blob, &blob_len);
 	if (rc) {
+		if (rc != -EBADMSG ||
+		    (payload->blob_len &&
+		     payload->blob[0] == (ASN1_CONS_BIT | ASN1_SEQ)))
+			return rc;
+
 		/* old form */
 		blob = payload->blob;
 		blob_len = payload->blob_len;
@@ -423,29 +524,29 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
-	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
-	if (rc < 0)
+	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
+			      parent_name, &parent_name_size, &created_parent);
+	if (rc)
 		return rc;
 
-	parent_name_size = rc;
-
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
-		return rc;
+		goto out;
 
 	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
 	if (!buf) {
 		tpm2_end_auth_session(chip);
-		return -ENOMEM;
+		rc = -ENOMEM;
+		goto out;
 	}
 
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
 				 parent_name_size);
 	if (rc)
-		return rc;
+		goto out;
 
 	tpm_buf_append_hmac_session(chip, buf, 0, private->keyauth,
 				    TPM_DIGEST_SIZE);
@@ -454,12 +555,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 
 	if (buf->flags & TPM_BUF_INVALID) {
 		tpm2_end_auth_session(chip);
-		return -E2BIG;
+		rc = -E2BIG;
+		goto out;
 	}
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
 	if (rc)
-		return rc;
+		goto out;
 
 	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
@@ -467,6 +569,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 		*blob_handle = be32_to_cpup(
 			(__be32 *)&buf->data[TPM_HEADER_SIZE]);
 
+out:
+	if (created_parent)
+		tpm2_flush_context(chip, parent_handle);
+
 	return tpm_ret_to_err(rc);
 }
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 13/17] KEYS: trusted: Reject zero-length keys in symmetric consumers
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (11 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 12/17] KEYS: trusted: Extend TPMKey support Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:55 ` [PATCH v5 14/17] KEYS: trusted: Add private backend data to trusted_key_payload Jarkko Sakkinen
                   ` (5 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Asymmetric trusted keys will not carry a raw symmetric key payload
(key_len == 0). Reject zero-length trusted keys in encrypted-keys,
af_alg, dm-crypt, and trusted_update() resealing.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 crypto/af_alg.c                                  | 2 ++
 drivers/md/dm-crypt.c                            | 2 +-
 security/keys/encrypted-keys/masterkey_trusted.c | 5 +++++
 security/keys/trusted-keys/trusted_core.c        | 2 +-
 4 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index ab84c4488a15..133cb88e3c12 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -323,6 +323,8 @@ static const u8 *key_data_ptr_trusted(const struct key *key,
 	if (IS_ERR_OR_NULL(tkp))
 		return ERR_PTR(-EKEYREVOKED);
 
+	if (!tkp->key_len)
+		return ERR_PTR(-EINVAL);
 	*datalen = tkp->key_len;
 
 	return tkp->key;
diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 02038a9cd21d..1e034c747d39 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -2492,7 +2492,7 @@ static int set_key_trusted(struct crypt_config *cc, struct key *key)
 	if (!tkp)
 		return -EKEYREVOKED;
 
-	if (cc->key_size != tkp->key_len)
+	if (!tkp->key_len || cc->key_size != tkp->key_len)
 		return -EINVAL;
 
 	memcpy(cc->key, tkp->key, cc->key_size);
diff --git a/security/keys/encrypted-keys/masterkey_trusted.c b/security/keys/encrypted-keys/masterkey_trusted.c
index e6d22ce77e98..8c6e6bdd8ef7 100644
--- a/security/keys/encrypted-keys/masterkey_trusted.c
+++ b/security/keys/encrypted-keys/masterkey_trusted.c
@@ -36,6 +36,11 @@ struct key *request_trusted_key(const char *trusted_desc,
 
 	down_read(&tkey->sem);
 	tpayload = tkey->payload.data[0];
+	if (!tpayload->key_len) {
+		up_read(&tkey->sem);
+		key_put(tkey);
+		return ERR_PTR(-EINVAL);
+	}
 	*master_key = tpayload->key;
 	*master_keylen = tpayload->key_len;
 error:
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
index 27756fc906b9..665b2c37734b 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -249,7 +249,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	if (key_is_negative(key))
 		return -ENOKEY;
 	p = key->payload.data[0];
-	if (!p->migratable)
+	if (!p->migratable || !p->key_len)
 		return -EPERM;
 	if (datalen == 0 || datalen > 32767 || !prep->data)
 		return -EINVAL;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 14/17] KEYS: trusted: Add private backend data to trusted_key_payload
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (12 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 13/17] KEYS: trusted: Reject zero-length keys in symmetric consumers Jarkko Sakkinen
@ 2026-11-27  5:55 ` Jarkko Sakkinen
  2026-11-27  5:56 ` [PATCH v5 15/17] KEYS: trusted: Factor out TPM2 blob loading Jarkko Sakkinen
                   ` (4 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:55 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Add a backend-private pointer to struct trusted_key_payload and free it
sensitively via trusted_payload_free() on teardown and error paths. This
prepares for asymmetric TPM2 keys, which retain backend key metadata
across runtime pkey operations instead of unsealing a raw key at load
time.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 include/keys/trusted-type.h               |  1 +
 security/keys/trusted-keys/trusted_core.c | 18 +++++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index 3db61b57cf73..790ffabb0d18 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -33,6 +33,7 @@ struct trusted_key_payload {
 	unsigned int blob_len;
 	unsigned char migratable;
 	unsigned char old_format;
+	void *private;
 	unsigned char key[MAX_KEY_SIZE + 1];
 	unsigned char blob[MAX_BLOB_SIZE];
 };
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
index 665b2c37734b..1db6497ba297 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -149,6 +149,14 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
 	return p;
 }
 
+static void trusted_payload_free(struct trusted_key_payload *p)
+{
+	if (!p)
+		return;
+	kfree_sensitive(p->private);
+	kfree_sensitive(p);
+}
+
 /*
  * trusted_instantiate - create a new trusted key
  *
@@ -223,7 +231,7 @@ static int trusted_instantiate(struct key *key,
 	if (!ret)
 		rcu_assign_keypointer(key, payload);
 	else
-		kfree_sensitive(payload);
+		trusted_payload_free(payload);
 	return ret;
 }
 
@@ -232,7 +240,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)
 	struct trusted_key_payload *p;
 
 	p = container_of(rcu, struct trusted_key_payload, rcu);
-	kfree_sensitive(p);
+	trusted_payload_free(p);
 }
 
 /*
@@ -269,7 +277,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = datablob_parse(&datablob, new_p);
 	if (ret != Opt_update) {
 		ret = -EINVAL;
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -283,7 +291,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = static_call(trusted_key_seal)(new_p, datablob);
 	if (ret < 0) {
 		pr_info("key_seal failed (%d)\n", ret);
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -322,7 +330,7 @@ static long trusted_read(const struct key *key, char *buffer,
  */
 static void trusted_destroy(struct key *key)
 {
-	kfree_sensitive(key->payload.data[0]);
+	trusted_payload_free(key->payload.data[0]);
 }
 
 struct key_type key_type_trusted = {
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 15/17] KEYS: trusted: Factor out TPM2 blob loading
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (13 preceding siblings ...)
  2026-11-27  5:55 ` [PATCH v5 14/17] KEYS: trusted: Add private backend data to trusted_key_payload Jarkko Sakkinen
@ 2026-11-27  5:56 ` Jarkko Sakkinen
  2026-11-27  5:56 ` [PATCH v5 16/17] KEYS: trusted: Add asymmetric key operations Jarkko Sakkinen
                   ` (3 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:56 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Prepare for asymmetric TPM2 key operations, which reload the TPM key blob
and append key authorization on each pkey call, by factoring out common
helpers:

1. Decouple tpm2_key_decode() from struct trusted_key_options.
2. Factor out tpm2_load_blob() and tpm2_buf_append_key_auth() from
   tpm2_load_cmd() and tpm2_unseal_cmd().

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 security/keys/trusted-keys/trusted_tpm2.c | 202 ++++++++++++----------
 1 file changed, 112 insertions(+), 90 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index a19ab2a62f65..467e8570b106 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -97,6 +97,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 
 struct tpm2_key_context {
 	u32 parent;
+	enum OID oid;
 	const u8 *pub;
 	u32 pub_len;
 	const u8 *priv;
@@ -104,11 +105,11 @@ struct tpm2_key_context {
 	bool has_policy;
 };
 
-static int tpm2_key_decode(struct trusted_key_payload *payload,
-			   struct trusted_key_options *options,
-			   u8 **buf, unsigned int *blob_len)
+static int tpm2_key_decode(const struct trusted_key_payload *payload,
+			   const struct trusted_key_tpm *private,
+			   u32 *parent, u8 **buf, unsigned int *blob_len,
+			   enum OID *oid)
 {
-	struct trusted_key_tpm *private = options->private;
 	int ret;
 	struct tpm2_key_context ctx;
 	u8 *blob;
@@ -132,8 +133,10 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 
 	*buf = blob;
 	*blob_len = ctx.priv_len + ctx.pub_len;
-	if (!private->keyhandle)
-		private->keyhandle = ctx.parent;
+	if (oid)
+		*oid = ctx.oid;
+	if (parent)
+		*parent = ctx.parent;
 
 	memcpy(blob, ctx.priv, ctx.priv_len);
 	blob += ctx.priv_len;
@@ -209,18 +212,22 @@ int tpm2_key_type(void *context, size_t hdrlen,
 		unsigned char tag,
 		const void *value, size_t vlen)
 {
+	struct tpm2_key_context *ctx = context;
 	enum OID oid = look_up_OID(value, vlen);
 
-	if (oid != OID_TPMSealedData) {
+	if (oid == OID_TPMSealedData) {
+		ctx->oid = oid;
+		return 0;
+	}
+
+	if (IS_ENABLED(CONFIG_DEBUG_KERNEL)) {
 		char buffer[50];
 
 		sprint_oid(value, vlen, buffer, sizeof(buffer));
-		pr_debug("OID is \"%s\" which is not TPMSealedData\n",
-			 buffer);
-		return -EINVAL;
+		pr_debug("OID \"%s\" is not supported\n", buffer);
 	}
 
-	return 0;
+	return -EINVAL;
 }
 int tpm2_key_pub(void *context, size_t hdrlen,
 	       unsigned char tag,
@@ -441,6 +448,69 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	return tpm_ret_to_err(rc);
 }
 
+static int tpm2_load_blob(struct tpm_chip *chip,
+			  struct trusted_key_tpm *private,
+			  const u8 *blob, unsigned int blob_len,
+			  u32 *blob_handle)
+{
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
+	struct tpm_buf *buf __free(kfree) = NULL;
+	bool created_parent = false;
+	u16 parent_name_size;
+	u32 parent_handle;
+	int rc;
+
+	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
+			      parent_name, &parent_name_size, &created_parent);
+	if (rc)
+		return rc;
+
+	rc = tpm2_start_auth_session(chip);
+	if (rc)
+		goto out;
+
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf) {
+		tpm2_end_auth_session(chip);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	tpm_buf_init(buf, TPM_BUFSIZE);
+	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
+
+	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
+				 parent_name_size);
+	if (rc)
+		goto out;
+
+	tpm_buf_append_hmac_session(chip, buf, 0, private->keyauth,
+				    TPM_DIGEST_SIZE);
+
+	tpm_buf_append(buf, blob, blob_len);
+
+	if (buf->flags & TPM_BUF_INVALID) {
+		tpm2_end_auth_session(chip);
+		rc = -E2BIG;
+		goto out;
+	}
+
+	rc = tpm_buf_fill_hmac_session(chip, buf);
+	if (rc)
+		goto out;
+
+	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
+	rc = tpm_buf_check_hmac_response(chip, buf, rc);
+	if (!rc)
+		*blob_handle = get_unaligned_be32(&buf->data[TPM_HEADER_SIZE]);
+
+out:
+	if (created_parent)
+		tpm2_flush_context(chip, parent_handle);
+
+	return tpm_ret_to_err(rc);
+}
+
 /**
  * tpm2_load_cmd() - execute a TPM2_Load command
  *
@@ -461,19 +531,15 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 {
 	u8 *blob_ref __free(kfree) = NULL;
 	struct trusted_key_tpm *private = options->private;
-	u8 parent_name[TPM2_MAX_NAME_SIZE];
-	struct tpm_buf *buf __free(kfree) = NULL;
-	bool created_parent = false;
-	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
-	u32 parent_handle;
+	u32 parent = 0;
 	u8 *blob, *pub;
 	int rc;
 	u32 attrs;
 
-	rc = tpm2_key_decode(payload, options, &blob, &blob_len);
+	rc = tpm2_key_decode(payload, private, &parent, &blob, &blob_len, NULL);
 	if (rc) {
 		if (rc != -EBADMSG ||
 		    (payload->blob_len &&
@@ -487,6 +553,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	} else {
 		/* Bind for cleanup: */
 		blob_ref = blob;
+		if (!private->keyhandle)
+			private->keyhandle = parent;
 	}
 
 	/* new format carries keyhandle but old format doesn't */
@@ -524,56 +592,36 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
-	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
-			      parent_name, &parent_name_size, &created_parent);
-	if (rc)
-		return rc;
-
-	rc = tpm2_start_auth_session(chip);
-	if (rc)
-		goto out;
-
-	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
-	if (!buf) {
-		tpm2_end_auth_session(chip);
-		rc = -ENOMEM;
-		goto out;
-	}
-
-	tpm_buf_init(buf, TPM_BUFSIZE);
-	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
-
-	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
-				 parent_name_size);
-	if (rc)
-		goto out;
-
-	tpm_buf_append_hmac_session(chip, buf, 0, private->keyauth,
-				    TPM_DIGEST_SIZE);
-
-	tpm_buf_append(buf, blob, blob_len);
+	return tpm2_load_blob(chip, private, blob, blob_len, blob_handle);
+}
 
-	if (buf->flags & TPM_BUF_INVALID) {
-		tpm2_end_auth_session(chip);
-		rc = -E2BIG;
-		goto out;
+static void tpm2_buf_append_key_auth(struct tpm_chip *chip,
+				     struct tpm_buf *buf,
+				     struct trusted_key_tpm *private,
+				     u8 hmac_attrs)
+{
+	if (!private->policyhandle) {
+		tpm_buf_append_hmac_session(chip, buf, hmac_attrs,
+					    private->blobauth,
+					    private->blobauth_len);
+		return;
 	}
 
-	rc = tpm_buf_fill_hmac_session(chip, buf);
-	if (rc)
-		goto out;
-
-	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
-	rc = tpm_buf_check_hmac_response(chip, buf, rc);
-	if (!rc)
-		*blob_handle = be32_to_cpup(
-			(__be32 *)&buf->data[TPM_HEADER_SIZE]);
-
-out:
-	if (created_parent)
-		tpm2_flush_context(chip, parent_handle);
+	/*
+	 * FIXME: The policy session was generated outside the kernel so we
+	 * don't know the nonce and thus can't calculate a HMAC on it.
+	 * Therefore, the user can only really use TPM2_PolicyPassword and we
+	 * must send down the plain text password, which could be intercepted.
+	 */
+	tpm_buf_append_u32(buf, 9 + private->blobauth_len);
+	tpm_buf_append_u32(buf, private->policyhandle);
+	tpm_buf_append_u16(buf, 0);
+	tpm_buf_append_u8(buf, 0);
+	tpm_buf_append_u16(buf, private->blobauth_len);
+	tpm_buf_append(buf, private->blobauth, private->blobauth_len);
 
-	return tpm_ret_to_err(rc);
+	if (tpm2_chip_auth(chip))
+		tpm_buf_append_hmac_session(chip, buf, hmac_attrs, NULL, 0);
 }
 
 /**
@@ -625,33 +673,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
-	if (!private->policyhandle) {
-		tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
-					    private->blobauth,
-					    private->blobauth_len);
-	} else {
-		/*
-		 * FIXME: The policy session was generated outside the
-		 * kernel so we don't known the nonce and thus can't
-		 * calculate a HMAC on it.  Therefore, the user can
-		 * only really use TPM2_PolicyPassword and we must
-		 * send down the plain text password, which could be
-		 * intercepted.  We can still encrypt the returned
-		 * key, but that's small comfort since the interposer
-		 * could repeat our actions with the exfiltrated
-		 * password.
-		 */
-		tpm_buf_append_u32(buf, 9 + private->blobauth_len);
-		tpm_buf_append_u32(buf, private->policyhandle);
-		tpm_buf_append_u16(buf, 0);
-		tpm_buf_append_u8(buf, 0);
-		tpm_buf_append_u16(buf, private->blobauth_len);
-		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
-
-		if (tpm2_chip_auth(chip))
-			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
-						    NULL, 0);
-	}
+	tpm2_buf_append_key_auth(chip, buf, private, TPM2_SA_ENCRYPT);
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
 	if (rc)
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 16/17] KEYS: trusted: Add asymmetric key operations
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (14 preceding siblings ...)
  2026-11-27  5:56 ` [PATCH v5 15/17] KEYS: trusted: Factor out TPM2 blob loading Jarkko Sakkinen
@ 2026-11-27  5:56 ` Jarkko Sakkinen
  2026-11-27  5:56 ` [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2 Jarkko Sakkinen
                   ` (2 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:56 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Add optional asymmetric key callbacks (asym_query, asym_eds_op, and
asym_verify_signature) to struct trusted_key_ops and wire them to
key_type_trusted in trusted_core via static calls when a backend
provides them.

Gate the support with CONFIG_TRUSTED_KEYS_ASYMMETRIC, exposed when a
backend selects HAVE_TRUSTED_KEYS_ASYMMETRIC.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 include/keys/trusted-type.h               | 14 ++++
 security/keys/trusted-keys/Kconfig        | 14 ++++
 security/keys/trusted-keys/trusted_core.c | 83 +++++++++++++++++++++++
 3 files changed, 111 insertions(+)

diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index 790ffabb0d18..ced67057c5be 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -27,6 +27,9 @@
 #define MAX_PCRINFO_SIZE		64
 #define MAX_DIGEST_SIZE			64
 
+struct kernel_pkey_params;
+struct kernel_pkey_query;
+
 struct trusted_key_payload {
 	struct rcu_head rcu;
 	unsigned int key_len;
@@ -62,6 +65,17 @@ struct trusted_key_ops {
 	/* Optional: Get a randomized key. */
 	int (*get_random)(unsigned char *key, size_t key_len);
 
+	/* Optional: Asymmetric key operations. */
+	int (*asym_query)(const struct trusted_key_payload *p,
+			  const struct kernel_pkey_params *params,
+			  struct kernel_pkey_query *info);
+	int (*asym_eds_op)(const struct trusted_key_payload *p,
+			   struct kernel_pkey_params *params,
+			   const void *in, void *out);
+	int (*asym_verify_signature)(const struct trusted_key_payload *p,
+				     struct kernel_pkey_params *params,
+				     const void *in, const void *in2);
+
 	/* Exit key interface. */
 	void (*exit)(void);
 };
diff --git a/security/keys/trusted-keys/Kconfig b/security/keys/trusted-keys/Kconfig
index e5a4a53aeab2..30b49460c3d9 100644
--- a/security/keys/trusted-keys/Kconfig
+++ b/security/keys/trusted-keys/Kconfig
@@ -4,6 +4,20 @@ config HAVE_TRUSTED_KEYS
 config HAVE_TRUSTED_KEYS_DEBUG
 	bool
 
+config HAVE_TRUSTED_KEYS_ASYMMETRIC
+	bool
+
+config TRUSTED_KEYS_ASYMMETRIC
+	bool "Asymmetric trusted keys"
+	depends on HAVE_TRUSTED_KEYS_ASYMMETRIC
+	default y
+	select KEYCTL_PKEY
+	help
+	  Allow trusted key backends that support asymmetric keys to load
+	  public/private key blobs and expose them through the keyctl
+	  public-key operations (query, encrypt, decrypt, sign, and verify)
+	  without changing support for sealed symmetric keys.
+
 config TRUSTED_KEYS_DEBUG
 	bool "Debug trusted keys"
 	depends on HAVE_TRUSTED_KEYS_DEBUG
diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
index 1db6497ba297..fb39f29aa7a5 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -18,6 +18,7 @@
 #include <linux/hex.h>
 #include <linux/init.h>
 #include <linux/key-type.h>
+#include <linux/keyctl.h>
 #include <linux/module.h>
 #include <linux/parser.h>
 #include <linux/random.h>
@@ -64,6 +65,14 @@ DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
 			*trusted_key_sources[0].ops->unseal);
 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
 			*trusted_key_sources[0].ops->get_random);
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+DEFINE_STATIC_CALL_NULL(trusted_key_asym_query,
+			*trusted_key_sources[0].ops->asym_query);
+DEFINE_STATIC_CALL_NULL(trusted_key_asym_eds_op,
+			*trusted_key_sources[0].ops->asym_eds_op);
+DEFINE_STATIC_CALL_NULL(trusted_key_asym_verify_signature,
+			*trusted_key_sources[0].ops->asym_verify_signature);
+#endif
 static void (*trusted_key_exit)(void);
 static unsigned char migratable __ro_after_init;
 
@@ -333,6 +342,78 @@ static void trusted_destroy(struct key *key)
 	trusted_payload_free(key->payload.data[0]);
 }
 
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int trusted_asym_query(const struct kernel_pkey_params *params,
+			      struct kernel_pkey_query *info)
+{
+	const struct trusted_key_payload *p;
+
+	p = dereference_key_locked(params->key);
+	if (!p || p->key_len || !p->private)
+		return -EOPNOTSUPP;
+
+	return static_call(trusted_key_asym_query)(p, params, info);
+}
+
+static int trusted_asym_eds_op(struct kernel_pkey_params *params,
+			       const void *in, void *out)
+{
+	const struct trusted_key_payload *p;
+
+	p = dereference_key_locked(params->key);
+	if (!p || p->key_len || !p->private)
+		return -EOPNOTSUPP;
+
+	return static_call(trusted_key_asym_eds_op)(p, params, in, out);
+}
+
+static int trusted_asym_verify_signature(struct kernel_pkey_params *params,
+					 const void *in, const void *in2)
+{
+	const struct trusted_key_payload *p;
+
+	p = dereference_key_locked(params->key);
+	if (!p || p->key_len || !p->private)
+		return -EOPNOTSUPP;
+
+	return static_call(trusted_key_asym_verify_signature)(p, params,
+							      in, in2);
+}
+
+static void trusted_asym_init(const struct trusted_key_ops *ops)
+{
+	if (!ops->asym_query || !ops->asym_eds_op ||
+	    !ops->asym_verify_signature)
+		return;
+
+	static_call_update(trusted_key_asym_query, ops->asym_query);
+	static_call_update(trusted_key_asym_eds_op, ops->asym_eds_op);
+	static_call_update(trusted_key_asym_verify_signature,
+			   ops->asym_verify_signature);
+	key_type_trusted.asym_query = trusted_asym_query;
+	key_type_trusted.asym_eds_op = trusted_asym_eds_op;
+	key_type_trusted.asym_verify_signature = trusted_asym_verify_signature;
+}
+
+static void trusted_asym_reset(void)
+{
+	static_call_update(trusted_key_asym_query, NULL);
+	static_call_update(trusted_key_asym_eds_op, NULL);
+	static_call_update(trusted_key_asym_verify_signature, NULL);
+	key_type_trusted.asym_query = NULL;
+	key_type_trusted.asym_eds_op = NULL;
+	key_type_trusted.asym_verify_signature = NULL;
+}
+#else
+static inline void trusted_asym_init(const struct trusted_key_ops *ops)
+{
+}
+
+static inline void trusted_asym_reset(void)
+{
+}
+#endif
+
 struct key_type key_type_trusted = {
 	.name = "trusted",
 	.instantiate = trusted_instantiate,
@@ -384,6 +465,7 @@ static int __init init_trusted(void)
 		static_call_update(trusted_key_seal, trusted_key_sources[i].ops->seal);
 		static_call_update(trusted_key_unseal, trusted_key_sources[i].ops->unseal);
 		static_call_update(trusted_key_get_random, get_random);
+		trusted_asym_init(trusted_key_sources[i].ops);
 		migratable = trusted_key_sources[i].ops->migratable;
 
 		ret = trusted_key_sources[i].ops->init();
@@ -393,6 +475,7 @@ static int __init init_trusted(void)
 			static_call_update(trusted_key_seal, NULL);
 			static_call_update(trusted_key_unseal, NULL);
 			static_call_update(trusted_key_get_random, NULL);
+			trusted_asym_reset();
 			migratable = 0;
 		}
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (15 preceding siblings ...)
  2026-11-27  5:56 ` [PATCH v5 16/17] KEYS: trusted: Add asymmetric key operations Jarkko Sakkinen
@ 2026-11-27  5:56 ` Jarkko Sakkinen
  2026-11-27  6:48   ` Jarkko Sakkinen
  2026-11-27  6:22 ` [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
  2026-11-27 19:31 ` Eric Biggers
  18 siblings, 1 reply; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  5:56 UTC (permalink / raw)
  To: keyrings
  Cc: Jarkko Sakkinen, dm-devel, linux-crypto, linux-doc,
	linux-integrity, linux-kernel, linux-security-module,
	Ahmad Fatoum, Alasdair Kergon, Andrew Morton, Benjamin Marzinski,
	David Gstir, David Howells, David Laight, David S. Miller,
	David Woodhouse, Eric Biggers, Herbert Xu, Ignat Korchagin,
	James Bottomley, James Morris, James Prestwood, Jason Gunthorpe,
	Jonathan Corbet, Jonathan McDowell, Lukas Wunner,
	Marek Behún, Mike Snitzer, Mikulas Patocka, Mimi Zohar,
	Paul Moore, Peter Huewe, Randy Dunlap, Serge E. Hallyn,
	Shuah Khan, Sumit Garg

Extend the TPM 2.0 trusted key backend to load OID_TPMLoadableKey
(2.23.133.10.1.3) RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521)
keys and wire query, sign, verify, and RSA encrypt/decrypt operations to
trusted_key_tpm_ops.

Select HAVE_TRUSTED_KEYS_ASYMMETRIC when CONFIG_TRUSTED_KEYS_TPM is
enabled.

Co-developed-by: James Prestwood <prestwoj@gmail.com>
Signed-off-by: James Prestwood <prestwoj@gmail.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 .../security/keys/trusted-encrypted.rst       |   7 +-
 include/keys/trusted-type.h                   |   4 +-
 include/keys/trusted_tpm.h                    |  17 +
 include/linux/tpm_command.h                   |  11 +
 security/keys/trusted-keys/Kconfig            |   1 +
 security/keys/trusted-keys/trusted_tpm1.c     |  24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 987 +++++++++++++++++-
 7 files changed, 1046 insertions(+), 5 deletions(-)

diff --git a/Documentation/security/keys/trusted-encrypted.rst b/Documentation/security/keys/trusted-encrypted.rst
index c6a507ef228a..30a8c6d1c0c8 100644
--- a/Documentation/security/keys/trusted-encrypted.rst
+++ b/Documentation/security/keys/trusted-encrypted.rst
@@ -589,7 +589,12 @@ available are::
                     represents a symmetric key and must be unsealed before
                     use.
 
-The trusted key code only uses the TPM Sealed Data OID.
+The trusted key code uses TPM Sealed Data for symmetric keys.
+
+When ``CONFIG_TRUSTED_KEYS_ASYMMETRIC`` is enabled, TPM 2.0 backend accepts TPM
+Loadable keys through the ``trusted`` key type. Use the keyctl pkey-operations
+for these keys. For RSA OAEP encryption and decryption, specify ``enc=oaep`` and
+a ``hash=`` matching the key's scheme, if fixed.
 
 emptyAuth is true if the key has well known authorization "".  If it
 is false or not present, the key requires an explicit authorization
diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h
index ced67057c5be..f34c38af1488 100644
--- a/include/keys/trusted-type.h
+++ b/include/keys/trusted-type.h
@@ -19,7 +19,9 @@
 
 #define MIN_KEY_SIZE			32
 #define MAX_KEY_SIZE			128
-#if IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
+#if IS_ENABLED(CONFIG_TRUSTED_KEYS_TPM)
+#define MAX_BLOB_SIZE			2048
+#elif IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
 #define MAX_BLOB_SIZE			1152
 #else
 #define MAX_BLOB_SIZE                   512
diff --git a/include/keys/trusted_tpm.h b/include/keys/trusted_tpm.h
index b8b1f5c8fbc3..18f3ed6f5ce8 100644
--- a/include/keys/trusted_tpm.h
+++ b/include/keys/trusted_tpm.h
@@ -18,6 +18,12 @@ struct trusted_key_tpm {
 	u32 policydigest_len;
 	unsigned char policydigest[MAX_DIGEST_SIZE];
 	u32 policyhandle;
+	u32 attrs;
+	u16 alg_type;
+	u16 scheme;
+	u16 scheme_hash;
+	u16 key_bits;
+	u16 coord_size;
 };
 
 int tpm2_seal_trusted(struct tpm_chip *chip,
@@ -26,4 +32,15 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options);
+int tpm2_asym_query(const struct trusted_key_payload *payload,
+		    const struct kernel_pkey_params *params,
+		    struct kernel_pkey_query *info);
+int tpm2_asym_eds_op(struct tpm_chip *chip,
+		     const struct trusted_key_payload *payload,
+		     struct kernel_pkey_params *params,
+		     const void *in, void *out);
+int tpm2_asym_verify_signature(struct tpm_chip *chip,
+			       const struct trusted_key_payload *payload,
+			       struct kernel_pkey_params *params,
+			       const void *in, const void *in2);
 #endif
diff --git a/include/linux/tpm_command.h b/include/linux/tpm_command.h
index 6576a7df6d8c..498b82cf3142 100644
--- a/include/linux/tpm_command.h
+++ b/include/linux/tpm_command.h
@@ -184,6 +184,7 @@ enum tpm2_structures {
 	TPM2_ST_NO_SESSIONS	= 0x8001,
 	TPM2_ST_SESSIONS	= 0x8002,
 	TPM2_ST_CREATION	= 0x8021,
+	TPM2_ST_HASHCHECK	= 0x8024,
 };
 
 /* Indicates from what layer of the software stack the error comes from */
@@ -218,12 +219,15 @@ 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_SIGN			= 0x015D,
 	TPM2_CC_UNSEAL			= 0x015E,
 	TPM2_CC_CONTEXT_LOAD		= 0x0161,
 	TPM2_CC_CONTEXT_SAVE		= 0x0162,
 	TPM2_CC_FLUSH_CONTEXT		= 0x0165,
 	TPM2_CC_READ_PUBLIC		= 0x0173,
+	TPM2_CC_RSA_ENCRYPT		= 0x0174,
 	TPM2_CC_START_AUTH_SESS		= 0x0176,
 	TPM2_CC_VERIFY_SIGNATURE	= 0x0177,
 	TPM2_CC_GET_CAPABILITY		= 0x017A,
@@ -278,6 +282,8 @@ enum tpm2_mso_type {
 enum tpm2_curves {
 	TPM2_ECC_NONE		= 0x0000,
 	TPM2_ECC_NIST_P256	= 0x0003,
+	TPM2_ECC_NIST_P384	= 0x0004,
+	TPM2_ECC_NIST_P521	= 0x0005,
 };
 
 enum tpm2_object_attributes {
@@ -457,6 +463,7 @@ struct tpm2_context {
 /* If you add a new hash to this, increment TPM_MAX_HASHES below */
 enum tpm_algorithms {
 	TPM_ALG_ERROR		= 0x0000,
+	TPM_ALG_RSA		= 0x0001,
 	TPM_ALG_SHA1		= 0x0004,
 	TPM_ALG_AES		= 0x0006,
 	TPM_ALG_KEYEDHASH	= 0x0008,
@@ -465,6 +472,10 @@ enum tpm_algorithms {
 	TPM_ALG_SHA512		= 0x000D,
 	TPM_ALG_NULL		= 0x0010,
 	TPM_ALG_SM3_256		= 0x0012,
+	TPM_ALG_RSASSA		= 0x0014,
+	TPM_ALG_RSAES		= 0x0015,
+	TPM_ALG_OAEP		= 0x0017,
+	TPM_ALG_ECDSA		= 0x0018,
 	TPM_ALG_ECC		= 0x0023,
 	TPM_ALG_CFB		= 0x0043,
 };
diff --git a/security/keys/trusted-keys/Kconfig b/security/keys/trusted-keys/Kconfig
index 30b49460c3d9..fce92c8c4d0b 100644
--- a/security/keys/trusted-keys/Kconfig
+++ b/security/keys/trusted-keys/Kconfig
@@ -38,6 +38,7 @@ config TRUSTED_KEYS_TPM
 	depends on TCG_TPM >= TRUSTED_KEYS
 	default y
 	select HAVE_TRUSTED_KEYS_DEBUG
+	select HAVE_TRUSTED_KEYS_ASYMMETRIC
 	select CRYPTO_HASH_INFO
 	select CRYPTO_LIB_SHA1
 	select CRYPTO_LIB_UTILS
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 3b7af11d30c4..7ff5110027d0 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -943,6 +943,9 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 		}
 	}
 
+	if (!p->key_len)
+		p->private = no_free_ptr(private);
+
 	return ret;
 }
 
@@ -951,6 +954,22 @@ static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
 	return tpm_get_random(chip, key, key_len);
 }
 
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int trusted_tpm_asym_eds_op(const struct trusted_key_payload *p,
+				   struct kernel_pkey_params *params,
+				   const void *in, void *out)
+{
+	return tpm2_asym_eds_op(chip, p, params, in, out);
+}
+
+static int trusted_tpm_asym_verify_signature(const struct trusted_key_payload *p,
+					     struct kernel_pkey_params *params,
+					     const void *in, const void *in2)
+{
+	return tpm2_asym_verify_signature(chip, p, params, in, in2);
+}
+#endif
+
 static int __init init_digests(void)
 {
 	int i;
@@ -1002,5 +1021,10 @@ struct trusted_key_ops trusted_key_tpm_ops = {
 	.seal = trusted_tpm_seal,
 	.unseal = trusted_tpm_unseal,
 	.get_random = trusted_tpm_get_random,
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+	.asym_query = tpm2_asym_query,
+	.asym_eds_op = trusted_tpm_asym_eds_op,
+	.asym_verify_signature = trusted_tpm_asym_verify_signature,
+#endif
 	.exit = trusted_tpm_exit,
 };
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 467e8570b106..dee7fb4e99a8 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -4,6 +4,11 @@
  * Copyright (C) 2014 Intel Corporation
  */
 
+#include <crypto/hash_info.h>
+#include <crypto/internal/ecc.h>
+#include <linux/cleanup.h>
+#include <linux/keyctl.h>
+#include <linux/slab.h>
 #include <linux/asn1_encoder.h>
 #include <linux/oid_registry.h>
 #include <linux/string.h>
@@ -124,6 +129,13 @@ static int tpm2_key_decode(const struct trusted_key_payload *payload,
 	if (ctx.has_policy && !private->policyhandle)
 		return -EOPNOTSUPP;
 
+	if (ctx.priv_len < 2 || ctx.pub_len < 2)
+		return -EBADMSG;
+
+	if (get_unaligned_be16(ctx.priv) + 2 != ctx.priv_len ||
+	    get_unaligned_be16(ctx.pub) + 2 != ctx.pub_len)
+		return -EBADMSG;
+
 	if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
 		return -EINVAL;
 
@@ -215,7 +227,7 @@ int tpm2_key_type(void *context, size_t hdrlen,
 	struct tpm2_key_context *ctx = context;
 	enum OID oid = look_up_OID(value, vlen);
 
-	if (oid == OID_TPMSealedData) {
+	if (oid == OID_TPMSealedData || oid == OID_TPMLoadableKey) {
 		ctx->oid = oid;
 		return 0;
 	}
@@ -448,6 +460,177 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	return tpm_ret_to_err(rc);
 }
 
+static int tpm2_asym_parse_rsa_public(struct trusted_key_tpm *private,
+				      struct tpm_buf *buf, off_t *offset)
+{
+	u32 exponent;
+	u16 mod_size;
+
+	switch (private->scheme) {
+	case TPM_ALG_NULL:
+		private->scheme_hash = TPM_ALG_NULL;
+		break;
+	case TPM_ALG_RSASSA:
+		if (!(private->attrs & TPM2_OA_SIGN))
+			return -EBADMSG;
+		if (!tpm_buf_read_u16(buf, offset, &private->scheme_hash))
+			return -EBADMSG;
+		break;
+	case TPM_ALG_RSAES:
+		if (!(private->attrs & TPM2_OA_DECRYPT))
+			return -EBADMSG;
+		private->scheme_hash = TPM_ALG_NULL;
+		break;
+	case TPM_ALG_OAEP:
+		if (!(private->attrs & TPM2_OA_DECRYPT))
+			return -EBADMSG;
+		if (!tpm_buf_read_u16(buf, offset, &private->scheme_hash))
+			return -EBADMSG;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	if (!tpm_buf_read_u16(buf, offset, &private->key_bits))
+		return -EBADMSG;
+	if (private->key_bits != 2048 &&
+	    private->key_bits != 3072 &&
+	    private->key_bits != 4096)
+		return -EOPNOTSUPP;
+
+	if (!tpm_buf_read_u32(buf, offset, &exponent))
+		return -EBADMSG;
+	if (exponent != 0 && exponent != 0x00010001)
+		return -EOPNOTSUPP;
+
+	if (!tpm_buf_read_u16(buf, offset, &mod_size))
+		return -EBADMSG;
+	if (mod_size != private->key_bits / 8)
+		return -EBADMSG;
+	if (mod_size > tpm_buf_length(buf) - *offset)
+		return -EBADMSG;
+	*offset += mod_size;
+
+	return *offset == tpm_buf_length(buf) ? 0 : -EBADMSG;
+}
+
+static int tpm2_asym_parse_ecc_public(struct trusted_key_tpm *private,
+				      struct tpm_buf *buf, off_t *offset)
+{
+	u16 curve_id, kdf, x_size, y_size;
+
+	if (!(private->attrs & TPM2_OA_SIGN))
+		return -EINVAL;
+
+	switch (private->scheme) {
+	case TPM_ALG_NULL:
+		private->scheme_hash = TPM_ALG_NULL;
+		break;
+	case TPM_ALG_ECDSA:
+		if (!tpm_buf_read_u16(buf, offset, &private->scheme_hash))
+			return -EBADMSG;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	if (!tpm_buf_read_u16(buf, offset, &curve_id))
+		return -EBADMSG;
+
+	switch (curve_id) {
+	case TPM2_ECC_NIST_P256:
+		private->key_bits = 256;
+		private->coord_size = 32;
+		break;
+	case TPM2_ECC_NIST_P384:
+		private->key_bits = 384;
+		private->coord_size = 48;
+		break;
+	case TPM2_ECC_NIST_P521:
+		private->key_bits = 521;
+		private->coord_size = 66;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	if (!tpm_buf_read_u16(buf, offset, &kdf))
+		return -EBADMSG;
+	if (kdf != TPM_ALG_NULL)
+		return -EOPNOTSUPP;
+
+	if (!tpm_buf_read_u16(buf, offset, &x_size))
+		return -EBADMSG;
+	if (!x_size || x_size > private->coord_size ||
+	    x_size > tpm_buf_length(buf) - *offset)
+		return -EBADMSG;
+	*offset += x_size;
+
+	if (!tpm_buf_read_u16(buf, offset, &y_size))
+		return -EBADMSG;
+	if (!y_size || y_size > private->coord_size ||
+	    y_size > tpm_buf_length(buf) - *offset)
+		return -EBADMSG;
+	*offset += y_size;
+
+	return *offset == tpm_buf_length(buf) ? 0 : -EBADMSG;
+}
+
+static int tpm2_asym_parse_public(struct trusted_key_tpm *private,
+				  const u8 *pub, unsigned int pub_len)
+{
+	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 name_alg, policy_len, symmetric;
+	off_t offset = 2;
+
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	tpm_buf_init_sized(buf, TPM_BUFSIZE);
+	tpm_buf_append(buf, pub, pub_len);
+	if (buf->flags & TPM_BUF_INVALID)
+		return -EBADMSG;
+
+	if (!tpm_buf_read_u16(buf, &offset, &private->alg_type) ||
+	    !tpm_buf_read_u16(buf, &offset, &name_alg))
+		return -EBADMSG;
+	if (name_alg == TPM_ALG_ERROR || name_alg == TPM_ALG_NULL)
+		return -EBADMSG;
+
+	if (!tpm_buf_read_u32(buf, &offset, &private->attrs))
+		return -EBADMSG;
+	if (private->attrs & TPM2_OA_RESTRICTED)
+		return -EINVAL;
+	if (!(private->attrs & (TPM2_OA_SIGN | TPM2_OA_DECRYPT)))
+		return -EINVAL;
+
+	if (!tpm_buf_read_u16(buf, &offset, &policy_len))
+		return -EBADMSG;
+	if (policy_len && !private->policyhandle)
+		return -EOPNOTSUPP;
+	if (policy_len > tpm_buf_length(buf) - offset)
+		return -EBADMSG;
+	offset += policy_len;
+
+	if (!tpm_buf_read_u16(buf, &offset, &symmetric))
+		return -EBADMSG;
+	if (symmetric != TPM_ALG_NULL)
+		return -EBADMSG;
+
+	if (!tpm_buf_read_u16(buf, &offset, &private->scheme))
+		return -EBADMSG;
+
+	switch (private->alg_type) {
+	case TPM_ALG_RSA:
+		return tpm2_asym_parse_rsa_public(private, buf, &offset);
+	case TPM_ALG_ECC:
+		return tpm2_asym_parse_ecc_public(private, buf, &offset);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 static int tpm2_load_blob(struct tpm_chip *chip,
 			  struct trusted_key_tpm *private,
 			  const u8 *blob, unsigned int blob_len,
@@ -534,12 +717,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
+	enum OID oid = OID_TPMSealedData;
 	u32 parent = 0;
 	u8 *blob, *pub;
 	int rc;
 	u32 attrs;
 
-	rc = tpm2_key_decode(payload, private, &parent, &blob, &blob_len, NULL);
+	rc = tpm2_key_decode(payload, private, &parent, &blob, &blob_len, &oid);
 	if (rc) {
 		if (rc != -EBADMSG ||
 		    (payload->blob_len &&
@@ -592,6 +776,17 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
+	if (oid == OID_TPMLoadableKey) {
+		if (!IS_ENABLED(CONFIG_TRUSTED_KEYS_ASYMMETRIC))
+			return -EOPNOTSUPP;
+
+		rc = tpm2_asym_parse_public(private, pub, public_len);
+		if (rc)
+			return rc;
+		payload->migratable = 0;
+		payload->key_len = 0;
+	}
+
 	return tpm2_load_blob(chip, private, blob, blob_len, blob_handle);
 }
 
@@ -725,6 +920,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options)
 {
+	struct trusted_key_tpm *private = options->private;
 	u32 blob_handle;
 	int rc;
 
@@ -736,10 +932,795 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 	if (rc)
 		goto out;
 
-	rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
+	if (!private->alg_type)
+		rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
 	tpm2_flush_context(chip, blob_handle);
 
 out:
 	tpm_put_ops(chip);
 	return tpm_ret_to_err(rc);
 }
+
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int tpm2_asym_load(struct tpm_chip *chip,
+			  const struct trusted_key_payload *payload,
+			  struct trusted_key_tpm *private,
+			  u32 *key_handle)
+{
+	u8 *blob __free(kfree) = NULL;
+	unsigned int blob_len;
+	int rc;
+
+	rc = tpm2_key_decode(payload, private, NULL, &blob, &blob_len, NULL);
+	if (rc)
+		return rc;
+
+	return tpm2_load_blob(chip, private, blob, blob_len, key_handle);
+}
+
+static int tpm2_asym_hash_lookup(const struct trusted_key_tpm *private,
+				 const char *hash_algo,
+				 int *hash_id, int *tpm_hash)
+{
+	int id, alg;
+
+	if (!hash_algo)
+		return -EINVAL;
+
+	id = match_string(hash_algo_name, HASH_ALGO__LAST, hash_algo);
+	if (id < 0)
+		return -ENOPKG;
+
+	alg = tpm2_find_hash_alg(id);
+	if (alg < 0)
+		return -ENOPKG;
+
+	if (private->scheme_hash != TPM_ALG_NULL && alg != private->scheme_hash)
+		return -EINVAL;
+
+	if (hash_id)
+		*hash_id = id;
+	if (tpm_hash)
+		*tpm_hash = alg;
+	return 0;
+}
+
+static int tpm2_asym_sig_scheme(const struct trusted_key_tpm *private,
+				const char *encoding, const char *hash_algo,
+				u16 *scheme, int *hash_id, int *tpm_hash)
+{
+	if (!(private->attrs & TPM2_OA_SIGN))
+		return -EOPNOTSUPP;
+	if (!encoding)
+		return -EINVAL;
+
+	switch (private->alg_type) {
+	case TPM_ALG_RSA:
+		if (strcmp(encoding, "pkcs1") != 0)
+			return -ENOPKG;
+		if (private->scheme != TPM_ALG_NULL &&
+		    private->scheme != TPM_ALG_RSASSA)
+			return -ENOPKG;
+		*scheme = TPM_ALG_RSASSA;
+		break;
+	case TPM_ALG_ECC:
+		if (strcmp(encoding, "x962") != 0)
+			return -ENOPKG;
+		if (private->scheme != TPM_ALG_NULL &&
+		    private->scheme != TPM_ALG_ECDSA)
+			return -ENOPKG;
+		*scheme = TPM_ALG_ECDSA;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return tpm2_asym_hash_lookup(private, hash_algo, hash_id, tpm_hash);
+}
+
+static int tpm2_asym_enc_scheme(const struct trusted_key_tpm *private,
+				const char *encoding, const char *hash_algo,
+				u16 *scheme, u16 *scheme_hash)
+{
+	int tpm_hash, ret;
+
+	if (private->alg_type != TPM_ALG_RSA ||
+	    !(private->attrs & TPM2_OA_DECRYPT))
+		return -EOPNOTSUPP;
+	if (!encoding)
+		return -EINVAL;
+
+	if (strcmp(encoding, "pkcs1") == 0) {
+		if (private->scheme != TPM_ALG_NULL &&
+		    private->scheme != TPM_ALG_RSAES)
+			return -ENOPKG;
+		*scheme = TPM_ALG_RSAES;
+		*scheme_hash = TPM_ALG_NULL;
+		return 0;
+	}
+
+	if (strcmp(encoding, "oaep") == 0) {
+		if (private->scheme != TPM_ALG_NULL &&
+		    private->scheme != TPM_ALG_OAEP)
+			return -ENOPKG;
+		ret = tpm2_asym_hash_lookup(private, hash_algo, NULL,
+					    &tpm_hash);
+		if (ret)
+			return ret;
+		*scheme = TPM_ALG_OAEP;
+		*scheme_hash = tpm_hash;
+		return 0;
+	}
+
+	if (strcmp(encoding, "raw") == 0) {
+		if (hash_algo)
+			return -EINVAL;
+		if (private->scheme != TPM_ALG_NULL)
+			return -ENOPKG;
+		*scheme = TPM_ALG_NULL;
+		*scheme_hash = TPM_ALG_NULL;
+		return 0;
+	}
+
+	return -ENOPKG;
+}
+
+static int tpm2_asym_rsa_encrypt(struct tpm_chip *chip,
+				 const struct trusted_key_payload *payload,
+				 struct trusted_key_tpm *private,
+				 struct kernel_pkey_params *params,
+				 const void *in, void *out)
+{
+	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 ciphertext_len, scheme, scheme_hash;
+	u32 key_handle;
+	off_t offset;
+	int rc;
+
+	rc = tpm2_asym_enc_scheme(private, params->encoding, params->hash_algo,
+				  &scheme, &scheme_hash);
+	if (rc)
+		return rc;
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	rc = tpm_try_get_ops(chip);
+	if (rc)
+		return rc;
+
+	rc = tpm2_asym_load(chip, payload, private, &key_handle);
+	if (rc)
+		goto out_ops;
+
+	tpm_buf_init(buf, TPM_BUFSIZE);
+	tpm_buf_reset(buf, TPM2_ST_NO_SESSIONS, TPM2_CC_RSA_ENCRYPT);
+	tpm_buf_append_u32(buf, key_handle);
+	tpm_buf_append_u16(buf, params->in_len);
+	tpm_buf_append(buf, in, params->in_len);
+	tpm_buf_append_u16(buf, scheme);
+	if (scheme == TPM_ALG_OAEP)
+		tpm_buf_append_u16(buf, scheme_hash);
+	tpm_buf_append_u16(buf, 0);
+	if (buf->flags & TPM_BUF_INVALID) {
+		rc = -E2BIG;
+		goto out_key;
+	}
+
+	rc = tpm_transmit_cmd(chip, buf, 2, "TPM2_RSA_Encrypt");
+	if (rc) {
+		rc = tpm_ret_to_err(rc);
+		goto out_key;
+	}
+
+	offset = TPM_HEADER_SIZE;
+	if (!tpm_buf_read_u16(buf, &offset, &ciphertext_len) ||
+	    offset + ciphertext_len > buf->length) {
+		rc = -EIO;
+		goto out_key;
+	}
+	if (params->out_len < ciphertext_len) {
+		rc = -EMSGSIZE;
+		goto out_key;
+	}
+
+	memcpy(out, &buf->data[offset], ciphertext_len);
+	rc = ciphertext_len;
+
+out_key:
+	tpm2_flush_context(chip, key_handle);
+out_ops:
+	tpm_put_ops(chip);
+	return rc;
+}
+
+static int tpm2_asym_rsa_decrypt(struct tpm_chip *chip,
+				 const struct trusted_key_payload *payload,
+				 struct trusted_key_tpm *private,
+				 struct kernel_pkey_params *params,
+				 const void *in, void *out)
+{
+	u8 key_name[TPM2_MAX_NAME_SIZE];
+	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 decrypted_len, key_name_size, scheme, scheme_hash;
+	u32 key_handle, param_size;
+	off_t offset;
+	int rc;
+
+	rc = tpm2_asym_enc_scheme(private, params->encoding, params->hash_algo,
+				  &scheme, &scheme_hash);
+	if (rc)
+		return rc;
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	rc = tpm_try_get_ops(chip);
+	if (rc)
+		return rc;
+
+	rc = tpm2_asym_load(chip, payload, private, &key_handle);
+	if (rc)
+		goto out_ops;
+
+	rc = tpm2_read_public(chip, key_handle, key_name);
+	if (rc < 0)
+		goto out_key;
+	key_name_size = rc;
+
+	rc = tpm2_start_auth_session(chip);
+	if (rc)
+		goto out_key;
+
+	tpm_buf_init(buf, TPM_BUFSIZE);
+	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_RSA_DECRYPT);
+
+	rc = tpm_buf_append_name(chip, buf, key_handle, key_name,
+				 key_name_size);
+	if (rc)
+		goto out_key;
+
+	tpm2_buf_append_key_auth(chip, buf, private,
+				 TPM2_SA_DECRYPT | TPM2_SA_ENCRYPT);
+	tpm_buf_append_u16(buf, params->in_len);
+	tpm_buf_append(buf, in, params->in_len);
+	tpm_buf_append_u16(buf, scheme);
+	if (scheme == TPM_ALG_OAEP)
+		tpm_buf_append_u16(buf, scheme_hash);
+	tpm_buf_append_u16(buf, 0);
+	if (buf->flags & TPM_BUF_INVALID) {
+		tpm2_end_auth_session(chip);
+		rc = -E2BIG;
+		goto out_key;
+	}
+
+	rc = tpm_buf_fill_hmac_session(chip, buf);
+	if (rc)
+		goto out_key;
+
+	rc = tpm_transmit_cmd(chip, buf, 6, "TPM2_RSA_Decrypt");
+	rc = tpm_buf_check_hmac_response(chip, buf, rc);
+	if (rc) {
+		rc = tpm_ret_to_err(rc);
+		goto out_key;
+	}
+
+	offset = TPM_HEADER_SIZE;
+	if (!tpm_buf_read_u32(buf, &offset, &param_size) ||
+	    !tpm_buf_read_u16(buf, &offset, &decrypted_len) ||
+	    param_size < 2 + decrypted_len ||
+	    param_size > buf->length - (offset - 2)) {
+		rc = -EIO;
+		goto out_key;
+	}
+	if (params->out_len < decrypted_len) {
+		rc = -EMSGSIZE;
+		goto out_key;
+	}
+
+	memcpy(out, &buf->data[offset], decrypted_len);
+	rc = decrypted_len;
+
+out_key:
+	tpm2_flush_context(chip, key_handle);
+out_ops:
+	tpm_put_ops(chip);
+	return rc;
+}
+
+static int tpm2_asym_parse_ecdsa_sig(struct tpm_buf *buf, off_t *offset,
+				     size_t param_end,
+				     struct kernel_pkey_params *params,
+				     void *out)
+{
+	u8 der[2 * (2 + ECC_MAX_BYTES + 1)];
+	u16 r_size, s_size;
+	u8 *encoded, *ptr;
+	const u8 *r, *s;
+
+	if (!tpm_buf_read_u16(buf, offset, &r_size) ||
+	    !r_size || r_size > ECC_MAX_BYTES ||
+	    *offset + r_size > param_end)
+		return -EIO;
+	r = &buf->data[*offset];
+	*offset += r_size;
+
+	if (!tpm_buf_read_u16(buf, offset, &s_size) ||
+	    !s_size || s_size > ECC_MAX_BYTES ||
+	    *offset + s_size > param_end)
+		return -EIO;
+	s = &buf->data[*offset];
+	*offset += s_size;
+
+	ptr = der;
+	ptr = asn1_encode_integer_bytes(ptr, der + sizeof(der), r, r_size);
+	ptr = asn1_encode_integer_bytes(ptr, der + sizeof(der), s, s_size);
+	if (IS_ERR(ptr))
+		return PTR_ERR(ptr);
+
+	encoded = asn1_encode_sequence(out, (u8 *)out + params->out_len,
+				       der, ptr - der);
+	if (IS_ERR(encoded))
+		return PTR_ERR(encoded) == -EINVAL ? -EMSGSIZE : PTR_ERR(encoded);
+
+	return encoded - (u8 *)out;
+}
+
+static int tpm2_asym_parse_signature(struct tpm_buf *buf, u16 scheme,
+				     int tpm_hash,
+				     struct kernel_pkey_params *params,
+				     void *out)
+{
+	off_t offset = TPM_HEADER_SIZE;
+	u16 sig_alg, hash_alg, sig_len;
+	size_t param_end;
+	u32 param_size;
+
+	if (!tpm_buf_read_u32(buf, &offset, &param_size) ||
+	    param_size < 4 || param_size > buf->length - offset)
+		return -EIO;
+	param_end = offset + param_size;
+
+	if (!tpm_buf_read_u16(buf, &offset, &sig_alg) ||
+	    !tpm_buf_read_u16(buf, &offset, &hash_alg) ||
+	    sig_alg != scheme || hash_alg != tpm_hash)
+		return -EIO;
+
+	switch (scheme) {
+	case TPM_ALG_RSASSA:
+		if (!tpm_buf_read_u16(buf, &offset, &sig_len) ||
+		    offset + sig_len > param_end)
+			return -EIO;
+		if (sig_len > params->out_len)
+			return -EMSGSIZE;
+		memcpy(out, &buf->data[offset], sig_len);
+		return sig_len;
+	case TPM_ALG_ECDSA:
+		return tpm2_asym_parse_ecdsa_sig(buf, &offset, param_end,
+						 params, out);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int tpm2_asym_sign(struct tpm_chip *chip,
+			  const struct trusted_key_payload *payload,
+			  struct trusted_key_tpm *private,
+			  struct kernel_pkey_params *params,
+			  const void *in, void *out)
+{
+	u8 key_name[TPM2_MAX_NAME_SIZE];
+	struct tpm_buf *buf __free(kfree) = NULL;
+	int hash_id, tpm_hash, rc;
+	u16 key_name_size, scheme;
+	u32 key_handle;
+
+	rc = tpm2_asym_sig_scheme(private, params->encoding, params->hash_algo,
+				  &scheme, &hash_id, &tpm_hash);
+	if (rc)
+		return rc;
+	if (params->in_len != hash_digest_size[hash_id])
+		return -EINVAL;
+
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	rc = tpm_try_get_ops(chip);
+	if (rc)
+		return rc;
+
+	rc = tpm2_asym_load(chip, payload, private, &key_handle);
+	if (rc)
+		goto out_ops;
+
+	rc = tpm2_read_public(chip, key_handle, key_name);
+	if (rc < 0)
+		goto out_key;
+	key_name_size = rc;
+
+	rc = tpm2_start_auth_session(chip);
+	if (rc)
+		goto out_key;
+
+	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, key_name,
+				 key_name_size);
+	if (rc)
+		goto out_key;
+
+	tpm2_buf_append_key_auth(chip, buf, private, TPM2_SA_DECRYPT);
+
+	/* digest (TPM2B_DIGEST) */
+	tpm_buf_append_u16(buf, params->in_len);
+	tpm_buf_append(buf, in, params->in_len);
+
+	/* inScheme (TPMT_SIG_SCHEME) */
+	tpm_buf_append_u16(buf, scheme);
+	tpm_buf_append_u16(buf, tpm_hash);
+
+	/* validation (TPMT_TK_HASHCHECK): NULL ticket */
+	tpm_buf_append_u16(buf, TPM2_ST_HASHCHECK);
+	tpm_buf_append_u32(buf, TPM2_RH_NULL);
+	tpm_buf_append_u16(buf, 0);
+
+	if (buf->flags & TPM_BUF_INVALID) {
+		tpm2_end_auth_session(chip);
+		rc = -E2BIG;
+		goto out_key;
+	}
+
+	rc = tpm_buf_fill_hmac_session(chip, buf);
+	if (rc)
+		goto out_key;
+
+	rc = tpm_transmit_cmd(chip, buf, 4, "TPM2_Sign");
+	rc = tpm_buf_check_hmac_response(chip, buf, rc);
+	if (rc) {
+		rc = tpm_ret_to_err(rc);
+		goto out_key;
+	}
+
+	rc = tpm2_asym_parse_signature(buf, scheme, tpm_hash, params, out);
+
+out_key:
+	tpm2_flush_context(chip, key_handle);
+out_ops:
+	tpm_put_ops(chip);
+	return rc;
+}
+
+static int tpm2_asym_decode_der_int(const u8 **p, const u8 *end,
+				    u16 max_len, const u8 **out, u16 *out_len)
+{
+	u32 len;
+
+	if (*p >= end || *(*p)++ != ASN1_INT)
+		return -EBADMSG;
+	if (*p >= end)
+		return -EBADMSG;
+
+	len = *(*p)++;
+	if (!len || len >= 0x80 || *p + len > end)
+		return -EBADMSG;
+
+	/* Negative DER INTEGER or non-minimal zero padding */
+	if (**p & 0x80)
+		return -EBADMSG;
+	if (len > 1 && (*p)[0] == 0x00 && !((*p)[1] & 0x80))
+		return -EBADMSG;
+	if (len > 1 && **p == 0x00) {
+		(*p)++;
+		len--;
+	}
+	if (len > max_len)
+		return -EBADMSG;
+
+	*out = *p;
+	*out_len = len;
+	*p += len;
+	return 0;
+}
+
+static int tpm2_asym_decode_ecdsa_sig(const u8 *der, u32 der_len,
+				      u16 coord_size,
+				      const u8 **r, u16 *r_len,
+				      const u8 **s, u16 *s_len)
+{
+	const u8 *end = der + der_len;
+	const u8 *p = der;
+	u32 seq_len;
+	int ret;
+
+	if (p >= end || *p++ != (ASN1_CONS_BIT | ASN1_SEQ))
+		return -EBADMSG;
+	if (p >= end)
+		return -EBADMSG;
+
+	if (*p < 0x80) {
+		seq_len = *p++;
+	} else if (*p == 0x81) {
+		if (++p >= end)
+			return -EBADMSG;
+		seq_len = *p++;
+		if (seq_len < 0x80)
+			return -EBADMSG;
+	} else {
+		return -EBADMSG;
+	}
+
+	if (p + seq_len != end)
+		return -EBADMSG;
+
+	ret = tpm2_asym_decode_der_int(&p, end, coord_size, r, r_len);
+	if (ret)
+		return ret;
+
+	ret = tpm2_asym_decode_der_int(&p, end, coord_size, s, s_len);
+	if (ret)
+		return ret;
+
+	return p == end ? 0 : -EBADMSG;
+}
+
+static unsigned int tpm2_asym_der_len_size(unsigned int len)
+{
+	if (len < 128)
+		return 1;
+	if (len <= 255)
+		return 2;
+	return 3;
+}
+
+static int tpm2_asym_rsa_query(const struct trusted_key_tpm *private,
+			       const struct kernel_pkey_params *params,
+			       struct kernel_pkey_query *info)
+{
+	u16 max_data_size = TPM2_MAX_DIGEST_SIZE;
+	u16 mod_size = private->key_bits / 8;
+	int hash_id, ret;
+
+	if (!params->encoding)
+		return -EINVAL;
+
+	memset(info, 0, sizeof(*info));
+	info->key_size = private->key_bits;
+
+	if (strcmp(params->encoding, "pkcs1") == 0) {
+		if (params->hash_algo) {
+			ret = tpm2_asym_hash_lookup(private, params->hash_algo,
+						    &hash_id, NULL);
+			if (ret)
+				return ret;
+			max_data_size = hash_digest_size[hash_id];
+		}
+
+		if ((private->attrs & TPM2_OA_SIGN) &&
+		    (private->scheme == TPM_ALG_NULL ||
+		     private->scheme == TPM_ALG_RSASSA)) {
+			info->max_data_size = max_data_size;
+			info->max_sig_size = mod_size;
+			info->supported_ops |= KEYCTL_SUPPORTS_SIGN |
+					       KEYCTL_SUPPORTS_VERIFY;
+		}
+		if ((private->attrs & TPM2_OA_DECRYPT) &&
+		    (private->scheme == TPM_ALG_NULL ||
+		     private->scheme == TPM_ALG_RSAES)) {
+			info->max_enc_size = mod_size;
+			info->max_dec_size = mod_size;
+			info->supported_ops |= KEYCTL_SUPPORTS_ENCRYPT |
+					       KEYCTL_SUPPORTS_DECRYPT;
+		}
+		return info->supported_ops ? 0 : -ENOPKG;
+	}
+
+	if (strcmp(params->encoding, "oaep") == 0) {
+		if (!(private->attrs & TPM2_OA_DECRYPT) ||
+		    (private->scheme != TPM_ALG_NULL &&
+		     private->scheme != TPM_ALG_OAEP))
+			return -ENOPKG;
+
+		ret = tpm2_asym_hash_lookup(private, params->hash_algo, NULL, NULL);
+		if (ret)
+			return ret;
+
+		/* keyutils can size the output buffer from either limit. */
+		info->max_enc_size = mod_size;
+		info->max_dec_size = mod_size;
+		info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT |
+				      KEYCTL_SUPPORTS_DECRYPT;
+		return 0;
+	}
+
+	if (strcmp(params->encoding, "raw") == 0) {
+		if (params->hash_algo)
+			return -EINVAL;
+		if (!(private->attrs & TPM2_OA_DECRYPT) ||
+		    private->scheme != TPM_ALG_NULL)
+			return -ENOPKG;
+
+		info->max_data_size = mod_size;
+		info->max_enc_size = mod_size;
+		info->max_dec_size = mod_size;
+		info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT |
+				      KEYCTL_SUPPORTS_DECRYPT;
+		return 0;
+	}
+
+	return -ENOPKG;
+}
+
+static int tpm2_asym_ecc_query(const struct trusted_key_tpm *private,
+			       const struct kernel_pkey_params *params,
+			       struct kernel_pkey_query *info)
+{
+	u16 max_data_size = TPM2_MAX_DIGEST_SIZE;
+	unsigned int int_len, seq_payload;
+	int hash_id, ret;
+
+	if (!params->encoding || strcmp(params->encoding, "x962") != 0)
+		return -ENOPKG;
+
+	if (params->hash_algo) {
+		ret = tpm2_asym_hash_lookup(private, params->hash_algo,
+					    &hash_id, NULL);
+		if (ret)
+			return ret;
+		max_data_size = hash_digest_size[hash_id];
+	}
+
+	int_len = private->coord_size + 1;
+	seq_payload = 2 * (1 + tpm2_asym_der_len_size(int_len) + int_len);
+
+	memset(info, 0, sizeof(*info));
+	info->key_size = private->key_bits;
+	info->max_sig_size = 1 + tpm2_asym_der_len_size(seq_payload) +
+			     seq_payload;
+	info->max_data_size = max_data_size;
+	info->supported_ops = KEYCTL_SUPPORTS_SIGN | KEYCTL_SUPPORTS_VERIFY;
+	return 0;
+}
+
+/**
+ * tpm2_asym_query() - query capabilities of an asymmetric TPM 2.0 trusted key
+ *
+ * @payload: trusted key payload
+ * @params: public-key operation parameters
+ * @info: returned key capabilities and maximum sizes
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+int tpm2_asym_query(const struct trusted_key_payload *payload,
+		    const struct kernel_pkey_params *params,
+		    struct kernel_pkey_query *info)
+{
+	const struct trusted_key_tpm *private = payload->private;
+
+	switch (private->alg_type) {
+	case TPM_ALG_RSA:
+		return tpm2_asym_rsa_query(private, params, info);
+	case TPM_ALG_ECC:
+		return tpm2_asym_ecc_query(private, params, info);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+/**
+ * tpm2_asym_eds_op() - encrypt, decrypt, or sign with an asymmetric TPM 2.0 key
+ *
+ * @chip: TPM chip to use
+ * @payload: trusted key payload
+ * @params: public-key operation parameters
+ * @in: input plaintext, ciphertext, or digest buffer
+ * @out: output ciphertext, plaintext, or signature buffer
+ *
+ * Return: output byte count on success, negative errno on failure.
+ */
+int tpm2_asym_eds_op(struct tpm_chip *chip,
+		     const struct trusted_key_payload *payload,
+		     struct kernel_pkey_params *params,
+		     const void *in, void *out)
+{
+	struct trusted_key_tpm *private = payload->private;
+
+	switch (params->op) {
+	case kernel_pkey_encrypt:
+		return tpm2_asym_rsa_encrypt(chip, payload, private,
+					     params, in, out);
+	case kernel_pkey_decrypt:
+		return tpm2_asym_rsa_decrypt(chip, payload, private,
+					     params, in, out);
+	case kernel_pkey_sign:
+		return tpm2_asym_sign(chip, payload, private, params, in, out);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+/**
+ * tpm2_asym_verify_signature() - verify a signature with an asymmetric TPM 2.0 key
+ *
+ * @chip: TPM chip to use
+ * @payload: trusted key payload
+ * @params: public-key operation parameters
+ * @in: digest buffer
+ * @in2: signature buffer
+ *
+ * Return: 0 on valid signature, negative errno on failure.
+ */
+int tpm2_asym_verify_signature(struct tpm_chip *chip,
+			       const struct trusted_key_payload *payload,
+			       struct kernel_pkey_params *params,
+			       const void *in, const void *in2)
+{
+	struct trusted_key_tpm *private = payload->private;
+	struct tpm_buf *buf __free(kfree) = NULL;
+	int hash_id, tpm_hash, rc;
+	u16 scheme, r_len, s_len;
+	const u8 *r, *s;
+	u32 key_handle;
+
+	rc = tpm2_asym_sig_scheme(private, params->encoding, params->hash_algo,
+				  &scheme, &hash_id, &tpm_hash);
+	if (rc)
+		return rc;
+	if (params->in_len != hash_digest_size[hash_id])
+		return -EINVAL;
+
+	if (scheme == TPM_ALG_ECDSA) {
+		rc = tpm2_asym_decode_ecdsa_sig(in2, params->in2_len,
+						private->coord_size,
+						&r, &r_len, &s, &s_len);
+		if (rc)
+			return rc;
+	}
+
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	rc = tpm_try_get_ops(chip);
+	if (rc)
+		return rc;
+
+	rc = tpm2_asym_load(chip, payload, private, &key_handle);
+	if (rc)
+		goto out_ops;
+
+	tpm_buf_init(buf, TPM_BUFSIZE);
+	tpm_buf_reset(buf, TPM2_ST_NO_SESSIONS, TPM2_CC_VERIFY_SIGNATURE);
+	tpm_buf_append_u32(buf, key_handle);
+	tpm_buf_append_u16(buf, params->in_len);
+	tpm_buf_append(buf, in, params->in_len);
+	tpm_buf_append_u16(buf, scheme);
+	tpm_buf_append_u16(buf, tpm_hash);
+
+	if (scheme == TPM_ALG_RSASSA) {
+		tpm_buf_append_u16(buf, params->in2_len);
+		tpm_buf_append(buf, in2, params->in2_len);
+	} else {
+		tpm_buf_append_u16(buf, r_len);
+		tpm_buf_append(buf, r, r_len);
+		tpm_buf_append_u16(buf, s_len);
+		tpm_buf_append(buf, s, s_len);
+	}
+
+	if (buf->flags & TPM_BUF_INVALID) {
+		rc = -E2BIG;
+		goto out_key;
+	}
+
+	rc = tpm_transmit_cmd(chip, buf, 0, "TPM2_VerifySignature");
+	if (rc > 0)
+		rc = -EKEYREJECTED;
+
+out_key:
+	tpm2_flush_context(chip, key_handle);
+out_ops:
+	tpm_put_ops(chip);
+	return rc;
+}
+#endif
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH v5 00/17] KEYS: trusted: asymmetric keys
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (16 preceding siblings ...)
  2026-11-27  5:56 ` [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2 Jarkko Sakkinen
@ 2026-11-27  6:22 ` Jarkko Sakkinen
  2026-11-27 19:31 ` Eric Biggers
  18 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  6:22 UTC (permalink / raw)
  To: keyrings
  Cc: dm-devel, linux-crypto, linux-doc, linux-integrity, linux-kernel,
	linux-security-module, Ahmad Fatoum, Alasdair Kergon,
	Andrew Morton, Benjamin Marzinski, David Gstir, David Howells,
	David Laight, David S. Miller, David Woodhouse, Eric Biggers,
	Herbert Xu, Ignat Korchagin, James Bottomley, James Morris,
	James Prestwood, Jason Gunthorpe, Jonathan Corbet,
	Jonathan McDowell, Lukas Wunner, Marek Behún, Mike Snitzer,
	Mikulas Patocka, Mimi Zohar, Paul Moore, Peter Huewe,
	Randy Dunlap, Serge E. Hallyn, Shuah Khan, Sumit Garg

On Fri, Nov 27, 2026 at 07:55:45AM +0200, Jarkko Sakkinen wrote:
> OVERVIEW
> ========
> 
> Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys
> (OID_TPMLoadableKey, 2.23.133.10.1.3) and expose keyctl pkey-operations
> directly on key_type_trusted. In addition there are some TPMKey
> interoperability fixes.
> 
> I.e. this my old series with a twist: instead of introducing a separate
> subtype for asymmetric keys, factor out pkey-operations into a separate
> CONFIG_KEYCTL_PKEY-feature, and wire asymmetric operations to
> key_type_trusted.
> 
> This work was originally derived from [3], is direct offspring of [2],
> and folds in the prerequisite tpm2-sessions and tpm-buf cleanups from
> [4].
> 
> [1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
> [2] https://lore.kernel.org/linux-integrity/20240520184727.22038-1-jarkko@kernel.org/
> [3] https://lore.kernel.org/all/20200518172704.29608-1-prestwoj@gmail.com/
> [4] https://lore.kernel.org/linux-integrity/20261116005105.1573016-1-jarkko@kernel.org/
> 
> CHANGES
> =======
> 
> v5:
> - tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*()
>   patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
>   Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(),
>   and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with
>   tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
> - trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2"
>   into a framework patch ("KEYS: trusted: Add asymmetric key
>   operations", adding optional asym_* callbacks on struct
>   trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend
>   patch.
> - trusted: Move "KEYS: trusted: Validate public_len before reading key
>   attributes" to patch 01/17 and add the Fixes tag for e5fb5d2c5a03.
> 
> v4:
> - KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
>   subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software
>   asymmetric keys, and switch TURRIS_SIGNING_KEY,
>   TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from
>   CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
> - trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select
>   CONFIG_KEYCTL_PKEY.
> - trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into
>   separate patches for public_len bounds checking, the private backend
>   pointer in trusted_key_payload, and TPM2 blob loading.
> 
> v3:
> - KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series
>   and make it an off-by-default prompt instead of selecting it from
>   CONFIG_ASYMMETRIC_KEY_TYPE.
> - trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on
>   CONFIG_KEYCTL_PKEY instead of selecting it.
> - trusted: Use unsigned int instead of u16 for data_len and min_len in
>   tpm2_unseal_cmd().
> - asn1: The reason for not supporting implicit tags was that it follows
>   what other functions do in asn1_encoder.c, and is that way coherent.
> 
> v2:
> - lib/asn1_encoder: Remove unreachable !bytes_len branch in
>   asn1_encode_integer_bytes().
> - tpm: Bounds-check ECC coordinates, defer state updates until after
>   validation, and verify subsequent TPM2_RH_NULL primaries against
>   chip->null_key_name in tpm2_parse_create_primary().
> - trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so
>   tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
> - trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt()
>   and tpm2_asym_parse_signature().
> 
> VERSIONS
> ========
> 
> v4: https://lore.kernel.org/keyrings/20261126205021.2200819-1-jarkko@kernel.org/
> v3: https://lore.kernel.org/keyrings/20261125180616.1854753-1-jarkko@kernel.org/
> v2: https://lore.kernel.org/keyrings/20261124194043.1651679-1-jarkko@kernel.org/
> v1: https://lore.kernel.org/keyrings/20261124120044.1481752-1-jarkko@kernel.org/
> 
> Cc: dm-devel@lists.linux.dev
> Cc: keyrings@vger.kernel.org
> Cc: linux-crypto@vger.kernel.org
> Cc: linux-doc@vger.kernel.org
> Cc: linux-integrity@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Cc: linux-security-module@vger.kernel.org
> Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
> Cc: Alasdair Kergon <agk@redhat.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Benjamin Marzinski <bmarzins@redhat.com>
> Cc: David Gstir <david@sigma-star.at>
> Cc: David Howells <dhowells@redhat.com>
> Cc: David Laight <david.laight.linux@gmail.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: David Woodhouse <dwmw2@infradead.org>
> Cc: Eric Biggers <ebiggers@kernel.org>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Cc: Ignat Korchagin <ignat@linux.win>
> Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
> Cc: James Morris <jmorris@namei.org>
> Cc: James Prestwood <prestwoj@gmail.com>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: Jonathan Corbet <corbet@lwn.net>
> Cc: Jonathan McDowell <noodles@meta.com>
> Cc: Lukas Wunner <lukas@wunner.de>
> Cc: Marek Behún <kabel@kernel.org>
> Cc: Mike Snitzer <snitzer@kernel.org>
> Cc: Mikulas Patocka <mpatocka@redhat.com>
> Cc: Mimi Zohar <zohar@linux.ibm.com>
> Cc: Paul Moore <paul@paul-moore.com>
> Cc: Peter Huewe <peterhuewe@gmx.de>
> Cc: Randy Dunlap <rdunlap@infradead.org>
> Cc: "Serge E. Hallyn" <serge@hallyn.com>
> Cc: Shuah Khan <skhan@linuxfoundation.org>
> Cc: Sumit Garg <sumit.garg@kernel.org>
> 
> Jarkko Sakkinen (17):
>   KEYS: trusted: Validate public_len before reading key attributes
>   KEYS: Factor out KEYCTL_PKEY
>   tpm2-sessions: Drop TPM2_NAME_SIZE
>   KEYS: trusted: Open code tpm2_buf_append_auth()
>   KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
>   tpm2-sessions: Re-orchestrate tpm2_read_public() use
>   tpm-buf: Make tpm_buf_read*() fallible
>   KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE
>   lib/asn1_encoder: Add asn1_encode_integer_bytes()
>   tpm: Export tpm2_create_primary()
>   tpm: Add persistent hierarchies to tpm2_create_primary()
>   KEYS: trusted: Extend TPMKey support
>   KEYS: trusted: Reject zero-length keys in symmetric consumers
>   KEYS: trusted: Add private backend data to trusted_key_payload
>   KEYS: trusted: Factor out TPM2 blob loading
>   KEYS: trusted: Add asymmetric key operations
>   KEYS: trusted: Add asymmetric key support for TPM2
> 
>  .../security/keys/trusted-encrypted.rst       |   25 +-
>  crypto/af_alg.c                               |    2 +
>  crypto/asymmetric_keys/Kconfig                |   11 +
>  crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
>  crypto/asymmetric_keys/asymmetric_type.c      |    4 +
>  drivers/char/tpm/Kconfig                      |    2 +-
>  drivers/char/tpm/tpm-buf.c                    |   56 +-
>  drivers/char/tpm/tpm-sysfs.c                  |    2 +-
>  drivers/char/tpm/tpm2-cmd.c                   |    9 +-
>  drivers/char/tpm/tpm2-sessions.c              |  524 ++++---
>  drivers/firmware/Kconfig                      |    1 -
>  drivers/md/dm-crypt.c                         |    2 +-
>  drivers/platform/cznic/Kconfig                |    3 +-
>  include/keys/trusted-type.h                   |   19 +-
>  include/keys/trusted_tpm.h                    |   18 +-
>  include/linux/asn1_encoder.h                  |    3 +
>  include/linux/tpm.h                           |   20 +-
>  include/linux/tpm_buf.h                       |    6 +-
>  include/linux/tpm_command.h                   |   13 +
>  lib/asn1_encoder.c                            |   56 +
>  security/keys/Kconfig                         |    3 +
>  security/keys/Makefile                        |    2 +-
>  .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
>  security/keys/internal.h                      |    2 +-
>  security/keys/keyctl.c                        |    2 +-
>  security/keys/trusted-keys/Kconfig            |   15 +
>  security/keys/trusted-keys/tpm2key.asn1       |   19 +
>  security/keys/trusted-keys/trusted_core.c     |  103 +-
>  security/keys/trusted-keys/trusted_tpm1.c     |   24 +
>  security/keys/trusted-keys/trusted_tpm2.c     | 1329 +++++++++++++++--
>  30 files changed, 1872 insertions(+), 410 deletions(-)
> 
> -- 
> 2.47.3
> 

Surprsingly after rebase and splitting asymmetric operations to their
own patch Sashiko found nothing to complain about.

Most of the stuff is number of years old just piggybacked into this
huge patch set.

Br, Jarkko

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2
  2026-11-27  5:56 ` [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2 Jarkko Sakkinen
@ 2026-11-27  6:48   ` Jarkko Sakkinen
  0 siblings, 0 replies; 21+ messages in thread
From: Jarkko Sakkinen @ 2026-11-27  6:48 UTC (permalink / raw)
  To: keyrings
  Cc: dm-devel, linux-crypto, linux-doc, linux-integrity, linux-kernel,
	linux-security-module, Ahmad Fatoum, Alasdair Kergon,
	Andrew Morton, Benjamin Marzinski, David Gstir, David Howells,
	David Laight, David S. Miller, David Woodhouse, Eric Biggers,
	Herbert Xu, Ignat Korchagin, James Bottomley, James Morris,
	James Prestwood, Jason Gunthorpe, Jonathan Corbet,
	Jonathan McDowell, Lukas Wunner, Marek Behún, Mike Snitzer,
	Mikulas Patocka, Mimi Zohar, Paul Moore, Peter Huewe,
	Randy Dunlap, Serge E. Hallyn, Shuah Khan, Sumit Garg

On Fri, Nov 27, 2026 at 07:56:02AM +0200, Jarkko Sakkinen wrote:
> Extend the TPM 2.0 trusted key backend to load OID_TPMLoadableKey
> (2.23.133.10.1.3) RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521)
> keys and wire query, sign, verify, and RSA encrypt/decrypt operations to
> trusted_key_tpm_ops.
> 
> Select HAVE_TRUSTED_KEYS_ASYMMETRIC when CONFIG_TRUSTED_KEYS_TPM is
> enabled.
> 
> Co-developed-by: James Prestwood <prestwoj@gmail.com>
> Signed-off-by: James Prestwood <prestwoj@gmail.com>
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

This should be in fairly safe grounds because the patch has been
manually written (with TCG specs), both TPM2 protocol and ASN.1
shenanigans, and reviewed and tested countless of times (over the
years).

Further, it has pretty much passed all Sashiko reviews so far, and
thus this patch likely is not all that wrong.

Br, Jarkko

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v5 00/17] KEYS: trusted: asymmetric keys
  2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
                   ` (17 preceding siblings ...)
  2026-11-27  6:22 ` [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
@ 2026-11-27 19:31 ` Eric Biggers
  18 siblings, 0 replies; 21+ messages in thread
From: Eric Biggers @ 2026-11-27 19:31 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: keyrings, dm-devel, linux-crypto, linux-doc, linux-integrity,
	linux-kernel, linux-security-module, Ahmad Fatoum,
	Alasdair Kergon, Andrew Morton, Benjamin Marzinski, David Gstir,
	David Howells, David Laight, David S. Miller, David Woodhouse,
	Herbert Xu, Ignat Korchagin, James Bottomley, James Morris,
	James Prestwood, Jason Gunthorpe, Jonathan Corbet,
	Jonathan McDowell, Lukas Wunner, Marek Behún, Mike Snitzer,
	Mikulas Patocka, Mimi Zohar, Paul Moore, Peter Huewe,
	Randy Dunlap, Serge E. Hallyn, Shuah Khan, Sumit Garg

On Fri, Nov 27, 2026 at 07:55:45AM +0200, Jarkko Sakkinen wrote:
> OVERVIEW
> ========
> 
> Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys
> (OID_TPMLoadableKey, 2.23.133.10.1.3) and expose keyctl pkey-operations
> directly on key_type_trusted. In addition there are some TPMKey
> interoperability fixes.
> 
> I.e. this my old series with a twist: instead of introducing a separate
> subtype for asymmetric keys, factor out pkey-operations into a separate
> CONFIG_KEYCTL_PKEY-feature, and wire asymmetric operations to
> key_type_trusted.
> 
> This work was originally derived from [3], is direct offspring of [2],
> and folds in the prerequisite tpm2-sessions and tpm-buf cleanups from
> [4].
> 
> [1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
> [2] https://lore.kernel.org/linux-integrity/20240520184727.22038-1-jarkko@kernel.org/
> [3] https://lore.kernel.org/all/20200518172704.29608-1-prestwoj@gmail.com/
> [4] https://lore.kernel.org/linux-integrity/20261116005105.1573016-1-jarkko@kernel.org/

Could you describe the motivation for these changes here?  And also
include more context directly, for people who aren't already familiar
with all the earlier patches linked to above.

Also, this series is missing a base-commit, and I don't know how to
apply it for review.

- Eric

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2026-11-27 19:31 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  5:55 [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 01/17] KEYS: trusted: Validate public_len before reading key attributes Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 02/17] KEYS: Factor out KEYCTL_PKEY Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 03/17] tpm2-sessions: Drop TPM2_NAME_SIZE Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 04/17] KEYS: trusted: Open code tpm2_buf_append_auth() Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 05/17] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 06/17] tpm2-sessions: Re-orchestrate tpm2_read_public() use Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 07/17] tpm-buf: Make tpm_buf_read*() fallible Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 08/17] KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 09/17] lib/asn1_encoder: Add asn1_encode_integer_bytes() Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 10/17] tpm: Export tpm2_create_primary() Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 11/17] tpm: Add persistent hierarchies to tpm2_create_primary() Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 12/17] KEYS: trusted: Extend TPMKey support Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 13/17] KEYS: trusted: Reject zero-length keys in symmetric consumers Jarkko Sakkinen
2026-11-27  5:55 ` [PATCH v5 14/17] KEYS: trusted: Add private backend data to trusted_key_payload Jarkko Sakkinen
2026-11-27  5:56 ` [PATCH v5 15/17] KEYS: trusted: Factor out TPM2 blob loading Jarkko Sakkinen
2026-11-27  5:56 ` [PATCH v5 16/17] KEYS: trusted: Add asymmetric key operations Jarkko Sakkinen
2026-11-27  5:56 ` [PATCH v5 17/17] KEYS: trusted: Add asymmetric key support for TPM2 Jarkko Sakkinen
2026-11-27  6:48   ` Jarkko Sakkinen
2026-11-27  6:22 ` [PATCH v5 00/17] KEYS: trusted: asymmetric keys Jarkko Sakkinen
2026-11-27 19:31 ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox