Skip to contentSkip to conversation

[PATCH v8 0/3]

PATCH In review Jarkko Sakkinen 1 participant 7 messages May 24

Subsystems (7)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

v8 cover [PATCH v8 0/3]

This series introduces key type for operating with asymmetric keys using a TPM2 chip.

Change Log
==========

v8:
- Reset patch change logs given the overhaul of the code and patches.
- Have only single new subkey type.
- Make key type only use TPM operations.
- Use TPM2_Sign for both ECC and RSA keys.
- Align key descriptions with other key types.

Previous versions
=================

* v7: https://lore.kernel.org/linux-integrity/[email protected]/
* v6: https://lore.kernel.org/linux-integrity/[email protected]/
* v5: https://lore.kernel.org/linux-integrity/[email protected]/
* v4: https://lore.kernel.org/linux-integrity/[email protected]/
* v3: https://lore.kernel.org/linux-integrity/[email protected]/
* v2: https://lore.kernel.org/linux-integrity/[email protected]/
* v1: https://lore.kernel.org/linux-integrity/[email protected]/
* Derived from https://lore.kernel.org/all/[email protected]/

Jarkko Sakkinen (3):
lib/asn1_encoder: Add asn1_encode_integer_bytes()
crypto: Migrate TPMKey ASN.1 objects from trusted-keys
keys: asymmetric: tpm2_asymmetric

 crypto/Kconfig                            |    7 +
 crypto/Makefile                           |    6 +
 crypto/asymmetric_keys/Kconfig            |   17 +
 crypto/asymmetric_keys/Makefile           |    1 +
 crypto/asymmetric_keys/tpm2_asymmetric.c  | 1096 +++++++++++++++++++++
 crypto/tpm2_key.asn1                      |   11 +
 crypto/tpm2_key.c                         |  150 +++
 include/crypto/tpm2_key.h                 |   46 +
 include/linux/asn1_encoder.h              |    3 +
 include/linux/tpm.h                       |   10 +
 lib/asn1_encoder.c                        |   62 ++
 security/keys/trusted-keys/Kconfig        |    2 +-
 security/keys/trusted-keys/Makefile       |    2 -
 security/keys/trusted-keys/tpm2key.asn1   |   11 -
 security/keys/trusted-keys/trusted_tpm2.c |  119 +--
 15 files changed, 1421 insertions(+), 122 deletions(-)
 create mode 100644 crypto/asymmetric_keys/tpm2_asymmetric.c
 create mode 100644 crypto/tpm2_key.asn1
 create mode 100644 crypto/tpm2_key.c
 create mode 100644 include/crypto/tpm2_key.h
 delete mode 100644 security/keys/trusted-keys/tpm2key.asn1
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v8 1/3 lib/asn1_encoder: Add asn1_encode_integer_bytes()

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

---
 include/linux/asn1_encoder.h |  3 ++
 lib/asn1_encoder.c           | 62 ++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
include/linux/asn1_encoder.h +3 -0
index d17484dffb74..e206bd425854 100644
@@ -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 *
lib/asn1_encoder.c +62 -0

74 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v8 2/3 crypto: Migrate TPMKey ASN.1 objects from trusted-keys

In reply to Jarkko Sakkinen

Migrate the TPMKey ASN.1 code from trusted-keys to the crypto subsystem, and put the code behind CRYPTO_TPM2_KEY Kconfig flag.

---
 crypto/Kconfig                            |   7 +
 crypto/Makefile                           |   6 +
 crypto/tpm2_key.asn1                      |  11 ++
 crypto/tpm2_key.c                         | 150 ++++++++++++++++++++++
 include/crypto/tpm2_key.h                 |  46 +++++++
 security/keys/trusted-keys/Kconfig        |   2 +-
 security/keys/trusted-keys/Makefile       |   2 -
 security/keys/trusted-keys/tpm2key.asn1   |  11 --
 security/keys/trusted-keys/trusted_tpm2.c | 119 ++---------------
 9 files changed, 232 insertions(+), 122 deletions(-)
 create mode 100644 crypto/tpm2_key.asn1
 create mode 100644 crypto/tpm2_key.c
 create mode 100644 include/crypto/tpm2_key.h
 delete mode 100644 security/keys/trusted-keys/tpm2key.asn1
crypto/Kconfig +7 -0
index 103d1f58cb7c..5476d80372a1 100644
@@ -3,6 +3,13 @@
 # Generic algorithms support
 #
 
+config CRYPTO_TPM2_KEY
+	bool
+	depends on CRYPTO
+	select ASN1
+	select OID_REGISTRY
+	default n
+
 #
 # async_tx api: hardware offloaded memory transfer/transform support
 #
crypto/Makefile +6 -0
index 162242593c7c..e232f9b9bee6 100644
@@ -206,3 +206,9 @@ obj-$(CONFIG_CRYPTO_KDF800108_CTR) += kdf_sp800108.o
 obj-$(CONFIG_CRYPTO_DF80090A) += df_sp80090a.o
 
 obj-$(CONFIG_CRYPTO_KRB5) += krb5/
+
+ifdef CONFIG_CRYPTO_TPM2_KEY
+$(obj)/tpm2_key.asn1.o: $(obj)/tpm2_key.asn1.h $(obj)/tpm2_key.asn1.c
+$(obj)/tpm2_key.o: $(obj)/tpm2_key.asn1.h
+obj-y += tpm2_key.o tpm2_key.asn1.o
+endif
crypto/tpm2_key.asn1 new file +11 -0
new file mode 100644
index 000000000000..553bf996af59
@@ -0,0 +1,11 @@
+---
+--- ASN.1 for TPM 2.0 keys
+---
+
+TPMKey ::= SEQUENCE {
+	type		OBJECT IDENTIFIER ({tpm2_key_get_type}),
+	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL ({tpm2_key_get_empty_auth}),
+	parent		INTEGER ({tpm2_key_get_parent}),
+	pubkey		OCTET STRING ({tpm2_get_public}),
+	privkey		OCTET STRING ({tpm2_get_private})
+	}
crypto/tpm2_key.c new file +150 -0

150 lines not on this page · raw message

include/crypto/tpm2_key.h new file +46 -0

46 lines not on this page · raw message

security/keys/trusted-keys/Kconfig +1 -1
index e5a4a53aeab2..09b1ec1d5bc2 100644
@@ -27,9 +27,9 @@ config TRUSTED_KEYS_TPM
 	select CRYPTO_HASH_INFO
 	select CRYPTO_LIB_SHA1
 	select CRYPTO_LIB_UTILS
+	select CRYPTO_TPM2_KEY
 	select ASN1_ENCODER
 	select OID_REGISTRY
-	select ASN1
 	select HAVE_TRUSTED_KEYS
 	help
 	  Enable use of the Trusted Platform Module (TPM) as trusted key
security/keys/trusted-keys/Makefile +0 -2
index 5fc053a21dad..ac09d2d90051 100644
@@ -7,9 +7,7 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o
 trusted-y += trusted_core.o
 trusted-$(CONFIG_TRUSTED_KEYS_TPM) += trusted_tpm1.o
 
-$(obj)/trusted_tpm2.o: $(obj)/tpm2key.asn1.h
 trusted-$(CONFIG_TRUSTED_KEYS_TPM) += trusted_tpm2.o
-trusted-$(CONFIG_TRUSTED_KEYS_TPM) += tpm2key.asn1.o
 
 trusted-$(CONFIG_TRUSTED_KEYS_TEE) += trusted_tee.o
 
security/keys/trusted-keys/tpm2key.asn1 deleted +0 -11
deleted file mode 100644
index f57f869ad600..000000000000
@@ -1,11 +0,0 @@
----
---- ASN.1 for TPM 2.0 keys
----
-
-TPMKey ::= SEQUENCE {
-	type		OBJECT IDENTIFIER ({tpm2_key_type}),
-	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
-	parent		INTEGER ({tpm2_key_parent}),
-	pubkey		OCTET STRING ({tpm2_key_pub}),
-	privkey		OCTET STRING ({tpm2_key_priv})
-	}
security/keys/trusted-keys/trusted_tpm2.c +11 -108

150 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v8 3/3 keys: asymmetric: tpm2_asymmetric

In reply to Jarkko Sakkinen

tpm2_asymmetric is a key type for external keys generated outside the TPM chip but later imported to the chip's key hierarchy as leaf keys.

The key type supports ECC-NIST-P256/384/521 and RSA keys and provides signing and verification operations for each. In addition, for RSA encryption and decryption operations are supported.

---
 crypto/asymmetric_keys/Kconfig           |   17 +
 crypto/asymmetric_keys/Makefile          |    1 +
 crypto/asymmetric_keys/tpm2_asymmetric.c | 1096 ++++++++++++++++++++++
 include/linux/tpm.h                      |   10 +
 4 files changed, 1124 insertions(+)
 create mode 100644 crypto/asymmetric_keys/tpm2_asymmetric.c
crypto/asymmetric_keys/Kconfig +17 -0
index e50bd9b3e27b..a93e13d5768f 100644
@@ -15,6 +15,7 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	select MPILIB
 	select CRYPTO_HASH_INFO
 	select CRYPTO_AKCIPHER
+	select CRYPTO_RSA
 	select CRYPTO_SIG
 	select CRYPTO_HASH
 	help
@@ -23,6 +24,22 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	  appropriate hash algorithms (such as SHA-1) must be available.
 	  ENOPKG will be reported if the requisite algorithm is unavailable.
 
+config ASYMMETRIC_TPM2_KEY_SUBTYPE
+	tristate "Asymmetric TPM2 crypto algorithm subtype"
+	depends on TCG_TPM
+	select CRYPTO_SHA256
+	select CRYPTO_HASH_INFO
+	select CRYPTO_TPM2_KEY
+	select ASN1
+	select ASN1_ENCODER
+	help
+	  This option provides support for asymmetric TPM2 key type handling.
+	  Asymmetric operations such as sign and verify are delegated to the
+	  TPM, and bound to the kernel crypto subsystem. Both RSA and ECDSA
+	  keys are supported.
+
+	  ENOPKG will be reported if the requisite algorithm is unavailable.
+
 config X509_CERTIFICATE_PARSER
 	tristate "X.509 certificate parser"
 	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
crypto/asymmetric_keys/Makefile +1 -0
index bc65d3b98dcb..c83b40d021ac 100644
@@ -11,6 +11,7 @@ asymmetric_keys-y := \
 	signature.o
 
 obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+obj-$(CONFIG_ASYMMETRIC_TPM2_KEY_SUBTYPE) += tpm2_asymmetric.o
 
 #
 # X.509 Certificate handling
crypto/asymmetric_keys/tpm2_asymmetric.c new file +1096 -0

1,096 lines not on this page · raw message

include/linux/tpm.h +10 -0
index 202da079d500..d4d5ddc0173a 100644
@@ -45,6 +45,7 @@ enum tpm2_session_types {
 /* 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,
@@ -53,6 +54,9 @@ 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_ECDSA		= 0x0018,
 	TPM_ALG_ECC		= 0x0023,
 	TPM_ALG_CFB		= 0x0043,
 };
@@ -66,6 +70,8 @@ enum tpm_algorithms {
 enum tpm2_curves {
 	TPM2_ECC_NONE		= 0x0000,
 	TPM2_ECC_NIST_P256	= 0x0003,
+	TPM2_ECC_NIST_P384	= 0x0004,
+	TPM2_ECC_NIST_P521	= 0x0005,
 };
 
 struct tpm_digest {
@@ -242,6 +248,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 */
@@ -276,12 +283,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,
Signature · 1 line
2.47.3
author maintainer
#4 lore raw
In reply to Jarkko Sakkinen
Quoted 44 lines · Jarkko Sakkinen

This series introduces key type for operating with asymmetric keys using a TPM2 chip.

Change Log
==========

v8:
- Reset patch change logs given the overhaul of the code and patches.
- Have only single new subkey type.
- Make key type only use TPM operations.
- Use TPM2_Sign for both ECC and RSA keys.
- Align key descriptions with other key types.

Previous versions
=================

* v7: https://lore.kernel.org/linux-integrity/[email protected]/
* v6: https://lore.kernel.org/linux-integrity/[email protected]/
* v5: https://lore.kernel.org/linux-integrity/[email protected]/
* v4: https://lore.kernel.org/linux-integrity/[email protected]/
* v3: https://lore.kernel.org/linux-integrity/[email protected]/
* v2: https://lore.kernel.org/linux-integrity/[email protected]/
* v1: https://lore.kernel.org/linux-integrity/[email protected]/
* Derived from https://lore.kernel.org/all/[email protected]/

Jarkko Sakkinen (3):
lib/asn1_encoder: Add asn1_encode_integer_bytes()
crypto: Migrate TPMKey ASN.1 objects from trusted-keys
keys: asymmetric: tpm2_asymmetric

 crypto/Kconfig                            |    7 +
 crypto/Makefile                           |    6 +
 crypto/asymmetric_keys/Kconfig            |   17 +
 crypto/asymmetric_keys/Makefile           |    1 +
 crypto/asymmetric_keys/tpm2_asymmetric.c  | 1096 +++++++++++++++++++++
 crypto/tpm2_key.asn1                      |   11 +
 crypto/tpm2_key.c                         |  150 +++
 include/crypto/tpm2_key.h                 |   46 +
 include/linux/asn1_encoder.h              |    3 +
 include/linux/tpm.h                       |   10 +
 lib/asn1_encoder.c                        |   62 ++
 security/keys/trusted-keys/Kconfig        |    2 +-
 security/keys/trusted-keys/Makefile       |    2 -
Quoted 12 lines
 security/keys/trusted-keys/tpm2key.asn1   |   11 -
 security/keys/trusted-keys/trusted_tpm2.c |  119 +--
 15 files changed, 1421 insertions(+), 122 deletions(-)
 create mode 100644 crypto/asymmetric_keys/tpm2_asymmetric.c
 create mode 100644 crypto/tpm2_key.asn1
 create mode 100644 crypto/tpm2_key.c
 create mode 100644 include/crypto/tpm2_key.h
 delete mode 100644 security/keys/trusted-keys/tpm2key.asn1
Signature · 1 line
2.47.3

There's some initial test code for this too:

https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd-test.git/tree/overlay/usr/local/bin/tpmdd_tpm2_asymmetric.sh?h=main

Ugh, that's one hell of an url...

BR, Jarkko

author maintainer
#5 lore raw
In reply to Jarkko Sakkinen
Quoted 44 lines · Jarkko Sakkinen

This series introduces key type for operating with asymmetric keys using a TPM2 chip.

Change Log
==========

v8:
- Reset patch change logs given the overhaul of the code and patches.
- Have only single new subkey type.
- Make key type only use TPM operations.
- Use TPM2_Sign for both ECC and RSA keys.
- Align key descriptions with other key types.

Previous versions
=================

* v7: https://lore.kernel.org/linux-integrity/[email protected]/
* v6: https://lore.kernel.org/linux-integrity/[email protected]/
* v5: https://lore.kernel.org/linux-integrity/[email protected]/
* v4: https://lore.kernel.org/linux-integrity/[email protected]/
* v3: https://lore.kernel.org/linux-integrity/[email protected]/
* v2: https://lore.kernel.org/linux-integrity/[email protected]/
* v1: https://lore.kernel.org/linux-integrity/[email protected]/
* Derived from https://lore.kernel.org/all/[email protected]/

Jarkko Sakkinen (3):
lib/asn1_encoder: Add asn1_encode_integer_bytes()
crypto: Migrate TPMKey ASN.1 objects from trusted-keys
keys: asymmetric: tpm2_asymmetric

 crypto/Kconfig                            |    7 +
 crypto/Makefile                           |    6 +
 crypto/asymmetric_keys/Kconfig            |   17 +
 crypto/asymmetric_keys/Makefile           |    1 +
 crypto/asymmetric_keys/tpm2_asymmetric.c  | 1096 +++++++++++++++++++++
 crypto/tpm2_key.asn1                      |   11 +
 crypto/tpm2_key.c                         |  150 +++
 include/crypto/tpm2_key.h                 |   46 +
 include/linux/asn1_encoder.h              |    3 +
 include/linux/tpm.h                       |   10 +
 lib/asn1_encoder.c                        |   62 ++
 security/keys/trusted-keys/Kconfig        |    2 +-
 security/keys/trusted-keys/Makefile       |    2 -
Quoted 12 lines
 security/keys/trusted-keys/tpm2key.asn1   |   11 -
 security/keys/trusted-keys/trusted_tpm2.c |  119 +--
 15 files changed, 1421 insertions(+), 122 deletions(-)
 create mode 100644 crypto/asymmetric_keys/tpm2_asymmetric.c
 create mode 100644 crypto/tpm2_key.asn1
 create mode 100644 crypto/tpm2_key.c
 create mode 100644 include/crypto/tpm2_key.h
 delete mode 100644 security/keys/trusted-keys/tpm2key.asn1
Signature · 1 line
2.47.3

Oops, I deleted the subject line, it was unintentional :-)

BR, Jarkko

author maintainer
#6 lore raw
In reply to Jarkko Sakkinen
Quoted 2 lines · Jarkko Sakkinen

This series introduces key type for operating with asymmetric keys using a TPM2 chip.

This would deserve more explanation but the original trait was to implement TPM2 parts of:

https://datatracker.ietf.org/doc/draft-woodhouse-cert-best-practice/00/

What motivated me to reiterate are actually these coding agents and how all secrets are sprayed across the home directory. So, besides iwd one could use this feature to provide per-session cryptography for coding agents.

There's a lot to do with security and coding agents as we have literally moved to an era where we host indeterministically rogues software in our development workstations.

There's other questions too that we need to eventually answer like for instace, how to deal with persistent agent memory stored at the computer's hard drive?

The irony here is that LLM is really neither rogue nor a lier. It is just a text predictor optimizing for maximum reward and those descriptions are just human interpretations of the output text. It understand neither evil, lying nor quality for that matter ;-)

BR, Jarkko