Asymmetric TPM2 key type
Subsystems (8)
Jarkko Sakkinen
maintainerv2 cover Asymmetric TPM2 key type
## Overview
Introduce tpm2_key_rsa implementing asymmetric TPM RSA key.
I submit this first as RFC as I could not execute the keyctl padd in the following sequence (returns EBADF):
tpm2_createprimary --hierarchy o -G rsa2048 -c owner.txt tpm2_evictcontrol -c owner.txt 0x81000001tpm2_getcap handles-persistent
openssl genrsa -out private.pem 2048tpm2_import -C 0x81000001 -G rsa -i private.pem -u key.pub -r key.priv tpm2_encodeobject -C 0x81000001 -u key.pub -r key.priv -o key.priv.pem openssl asn1parse -inform pem -in key.priv.pem -noout -out key.priv.der key_serial=cat key.priv.der | keyctl padd asymmetric tpm @u
This is derived work from James Prestwood’s earlier work from 2020:
https://lore.kernel.org/all/[email protected]/
## Change Log
v2:
Sorry for rush update. I noticed that not all changes were in the commits. This version just fixes those compilation errors.
James Prestwood (1):
keys: asymmetric: ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE
Jarkko Sakkinen (4):
crypto: rsa-pkcs1pad: export rsa1_asn_lookup()
tpm: export tpm2_load_context()
KEYS: trusted: Do not use WARN when encode fails
KEYS: trusted: Migrate tpm2_key_{encode,decode}() to TPM driver
crypto/asymmetric_keys/Kconfig | 13 +
crypto/asymmetric_keys/Makefile | 1 +
crypto/asymmetric_keys/tpm2_key_rsa.c | 655 ++++++++++++++++++
crypto/rsa-pkcs1pad.c | 16 +-
drivers/char/tpm/Kconfig | 2 +
drivers/char/tpm/Makefile | 5 +
drivers/char/tpm/tpm.h | 2 -
drivers/char/tpm/tpm2-cmd.c | 77 ++
drivers/char/tpm/tpm2-space.c | 61 --
drivers/char/tpm/tpm2_key.c | 192 +++++
.../char/tpm}/tpm2key.asn1 | 0
include/crypto/rsa-pkcs1pad.h | 20 +
include/crypto/tpm2_key.h | 36 +
include/linux/tpm.h | 3 +
security/keys/trusted-keys/Makefile | 2 -
security/keys/trusted-keys/trusted_tpm2.c | 183 +----
16 files changed, 1032 insertions(+), 236 deletions(-)
create mode 100644 crypto/asymmetric_keys/tpm2_key_rsa.c
create mode 100644 drivers/char/tpm/tpm2_key.c
rename {security/keys/trusted-keys => drivers/char/tpm}/tpm2key.asn1 (100%)
create mode 100644 include/crypto/rsa-pkcs1pad.h
create mode 100644 include/crypto/tpm2_key.hSignature · 1 line
2.45.1
Jarkko Sakkinen
maintainerRFC PATCH v2 1/5 crypto: rsa-pkcs1pad: export rsa1_asn_lookup()
ASN.1 template is required for TPM2 asymmetric keys, as it needs to be piggy-packed with the input data before applying TPM2_RSA_Decrypt. This patch prepares crypto subsystem for the addition of those keys.
--- crypto/rsa-pkcs1pad.c | 16 ++++++++++------ include/crypto/rsa-pkcs1pad.h | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 include/crypto/rsa-pkcs1pad.h
crypto/rsa-pkcs1pad.c +10 -6
@@ -7,6 +7,7 @@#include <crypto/algapi.h> #include <crypto/akcipher.h> +#include <crypto/rsa-pkcs1pad.h> #include <crypto/internal/akcipher.h> #include <crypto/internal/rsa.h> #include <linux/err.h>
@@ -79,11 +80,7 @@ static const u8 rsa_digest_info_sha3_512[] = {0x05 ,0x00 ,0x04 ,0x40 }; -static const struct rsa_asn1_template { - const char *name; - const u8 *data; - size_t size; -} rsa_asn1_templates[] = { +static const struct rsa_asn1_template rsa_asn1_templates[] = { #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }_ (md5), _(sha1),
@@ -101,7 +98,13 @@ static const struct rsa_asn1_template { { NULL } }; -static const struct rsa_asn1_template *rsa_lookup_asn1 (const char *name) +/** +* rsa_lookup_asn1() - Lookup the ASN.1 digest info given the hash +* name: hash algorithm name +* +* Returns the ASN.1 digest info on success, and NULL on failure. +*/ +const struct rsa_asn1_template *rsa_lookup_asn1 (const char *name) {const struct rsa_asn1_template *p;
@@ -110,6 +113,7 @@ static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)return p;return NULL ; } +EXPORT_SYMBOL_GPL (rsa_lookup_asn1);struct pkcs1pad_ctx {struct crypto_akcipher *child;
include/crypto/rsa-pkcs1pad.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* RSA padding templates. +*/ + +#ifndef _CRYPTO_RSA_PKCS1PAD_H +#define _CRYPTO_RSA_PKCS1PAD_H + +/* +* Hash algorithm name to ASN.1 template mapping. +*/ +struct rsa_asn1_template { +const char *name; +const u8 *data; +size_t size; +}; + +const struct rsa_asn1_template *rsa_lookup_asn1 (const char *name); + +#endif /* _CRYPTO_RSA_PKCS1PAD_H */
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerRFC PATCH v2 2/5 tpm: export tpm2_load_context()
In reply to Jarkko Sakkinen--- drivers/char/tpm/tpm.h | 2 - drivers/char/tpm/tpm2-cmd.c | 77 +++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-space.c | 61 --------------------------- include/linux/tpm.h | 2 + 4 files changed, 79 insertions(+), 63 deletions(-)
drivers/char/tpm/tpm.h +0 -2
@@ -314,8 +314,6 @@ int tpm_devs_add(struct tpm_chip *chip);void tpm_devs_remove (struct tpm_chip *chip);int tpm2_save_context (struct tpm_chip *chip,u32 handle,u8 *buf,unsigned int buf_size,unsigned int *offset); -int tpm2_load_context (struct tpm_chip *chip,u8 *buf, -unsigned int *offset,u32 *handle);void tpm_bios_log_setup (struct tpm_chip *chip);void tpm_bios_log_teardown (struct tpm_chip *chip);
drivers/char/tpm/tpm2-cmd.c +77 -0
83 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +0 -61
73 lines not on this page · raw message
include/linux/tpm.h +2 -0
@@ -466,6 +466,8 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,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_load_context (struct tpm_chip *chip,const u8 *buf, +unsigned int *offset,u32 *handle);static inline void tpm_buf_append_empty_auth (struct tpm_buf *buf,u32 handle) {
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerRFC PATCH v2 3/5 KEYS: trusted: Do not use WARN when encode fails
In reply to Jarkko SakkinenWhen asn1_encode_sequence() fails, WARN is not the correct solution.
1. asn1_encode_sequence() is not an internal function (located in lib/asn1_encode.c).
2. Location is known, which makes the stack trace useless.
3. Results a crash if panic_on_warn is set.
It is also noteworthy that the use of WARN is undocumented, and it should be avoided unless there is a carefully considered rationale to use it.
Replace WARN with pr_err, and print the return value instead, which is only useful piece of information.
--- security/keys/trusted-keys/trusted_tpm2.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +6 -2
@@ -38,6 +38,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,u8 *end_work = scratch +SCRATCH_SIZE ;u8 *priv, *pub;u16 priv_len, pub_len; +int ret; priv_len =get_unaligned_be16 (src) +2 ; priv = src;
@@ -79,8 +80,11 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, work1 = payload->blob; work1 =asn1_encode_sequence (work1, work1 +sizeof (payload->blob), scratch, work - scratch); -if (WARN (IS_ERR (work1),"BUG: ASN.1 encoder failed" )) -return PTR_ERR (work1); +if (IS_ERR (work1)) { + ret =PTR_ERR (work1); +pr_err ("ASN.1 encode error %d\n" , ret); +return ret; + }return work1 - payload->blob; }
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerRFC PATCH v2 4/5 KEYS: trusted: Migrate tpm2_key_{encode,decode}() to TPM driver
In reply to Jarkko SakkinenMigrate tpm2_key_{encode,decode}() to TPM driver and export the symbols to make them callable from trusted keys.
---
drivers/char/tpm/Kconfig | 5 +
drivers/char/tpm/Makefile | 5 +
drivers/char/tpm/tpm2_key.c | 181 +++++++++++++++++
.../char/tpm}/tpm2key.asn1 | 0
include/crypto/tpm2_key.h | 33 ++++
security/keys/trusted-keys/Makefile | 2 -
security/keys/trusted-keys/trusted_tpm2.c | 187 ++----------------
7 files changed, 242 insertions(+), 171 deletions(-)
create mode 100644 drivers/char/tpm/tpm2_key.c
rename {security/keys/trusted-keys => drivers/char/tpm}/tpm2key.asn1 (100%)
create mode 100644 include/crypto/tpm2_key.hdrivers/char/tpm/Kconfig +5 -0
@@ -3,10 +3,15 @@ # TPM device configuration # +config TCG_TPM2_KEY + bool + menuconfig TCG_TPM tristate "TPM Hardware Support" depends on HAS_IOMEM imply SECURITYFS + select ASN1 + select ASN1_ENCODER select CRYPTO select CRYPTO_HASH_INFO help
drivers/char/tpm/Makefile +5 -0
@@ -17,6 +17,11 @@ tpm-y += eventlog/tpm1.o tpm-y += eventlog/tpm2.o tpm-y += tpm-buf.o +# TPM2 Asymmetric Key +$(obj)/trusted_tpm2.o: $(obj)/tpm2key.asn1.h +tpm-y += tpm2key.asn1.o +tpm-y += tpm2_key.o + tpm-$(CONFIG_TCG_TPM2_HMAC) += tpm2-sessions.o tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o tpm-$(CONFIG_EFI) += eventlog/efi.o
drivers/char/tpm/tpm2_key.c new file +181 -0
181 lines not on this page · raw message
drivers/char/tpm/tpm2key.asn1 renamed from security/keys/trusted-keys/tpm2key.asn1 +0 -0
include/crypto/tpm2_key.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_TPM2_KEY_H__ +#define __LINUX_TPM2_KEY_H__ + +#include <linux/slab.h> + +/* +* TPM2 ASN.1 key +*/ +struct tpm2_key { +u32 parent; +const u8 *pub; +u32 pub_len; +const u8 *priv; +u32 priv_len; +}; + +int tpm2_key_encode (u8 *blob,u32 blob_auth_len,u32 key_handle,u8 *src); +int tpm2_key_decode (const u8 *src,u32 src_len,struct tpm2_key *key, +u32 max_key_len); + +/** +* tpm2_key_free() - Release TPM2 asymmetric key resources and reset values +* @key: TPM2 asymmetric key. +*/ +static inline void tpm2_key_destroy (struct tpm2_key *key) +{ +kfree (key->priv); +kfree (key->pub); +memset (key,0 ,sizeof (*key)); +} + +#endif /* __LINUX_TPM2_KEY_H__ */
security/keys/trusted-keys/Makefile +0 -2
@@ -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/trusted_tpm2.c +18 -169
225 lines not on this page · raw message
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerRFC PATCH v2 5/5 keys: asymmetric: ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE
In reply to Jarkko SakkinenFrom: James Prestwood <[email protected]>
Based on earlier work by James Prestwood.
Add ASN.1 compatible asymmetric TPM2 RSA key subtype:
1. Signing and decryption (with the private key) is handled by TPM2_RSA_Decrypt.
2. Encryption (with the public key) is handled by the kernel RSA implementation.
--- crypto/asymmetric_keys/Kconfig | 13 + crypto/asymmetric_keys/Makefile | 1 + crypto/asymmetric_keys/tpm2_key_rsa.c | 655 ++++++++++++++++++++++++++ drivers/char/tpm/Kconfig | 3 - drivers/char/tpm/tpm2_key.c | 11 + include/crypto/tpm2_key.h | 3 + include/linux/tpm.h | 1 + 7 files changed, 684 insertions(+), 3 deletions(-) create mode 100644 crypto/asymmetric_keys/tpm2_key_rsa.c
crypto/asymmetric_keys/Kconfig +13 -0
@@ -23,6 +23,19 @@ 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_RSA_SUBTYPE + tristate "Asymmetric TPM2 RSA crypto algorithm subtype" + depends on TCG_TPM + select TCG_TPM2_HMAC + select CRYPTO_RSA + select CRYPTO_SHA256 + select CRYPTO_HASH_INFO + help + This option provides support for asymmetric TPM2 key type handling. + If signature generation and/or verification are to be used, + appropriate hash algorithms (such as SHA-256) must be available. + 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
@@ -11,6 +11,7 @@ asymmetric_keys-y := \ signature.o obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o +obj-$(CONFIG_ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE) += tpm2_key_rsa.o # # X.509 Certificate handling
crypto/asymmetric_keys/tpm2_key_rsa.c new file +655 -0
655 lines not on this page · raw message
drivers/char/tpm/Kconfig +0 -3
@@ -3,9 +3,6 @@ # TPM device configuration # -config TCG_TPM2_KEY - bool - menuconfig TCG_TPM tristate "TPM Hardware Support" depends on HAS_IOMEM
drivers/char/tpm/tpm2_key.c +11 -0
@@ -175,6 +175,17 @@ int tpm2_key_decode(const u8 *src, u32 src_len, struct tpm2_key *key,return -ENOMEM ; } + ctx.blob_len = ctx.priv_len + ctx.pub_len; + ctx.blob =kmalloc (ctx.priv_len + ctx.pub_len,GFP_KERNEL ); +if (!ctx.blob) { +kfree (ctx.pub); +kfree (ctx.priv); +return -ENOMEM ; + } + +memcpy ((void *)ctx.blob, ctx.priv, ctx.priv_len); +memcpy ((void *)ctx.blob + ctx.priv_len, ctx.pub, ctx.pub_len); +memcpy (key, &ctx,sizeof (ctx));return 0 ; }
include/crypto/tpm2_key.h +3 -0
@@ -13,6 +13,8 @@ struct tpm2_key {u32 pub_len;const u8 *priv;u32 priv_len; +const u8 *blob; +u32 blob_len; };int tpm2_key_encode (u8 *blob,u32 blob_auth_len,u32 key_handle,u8 *src);
@@ -27,6 +29,7 @@ static inline void tpm2_key_destroy(struct tpm2_key *key) {kfree (key->priv);kfree (key->pub); +kfree (key->blob);memset (key,0 ,sizeof (*key)); }
include/linux/tpm.h +1 -0
@@ -271,6 +271,7 @@ 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_UNSEAL =0x015E , TPM2_CC_CONTEXT_LOAD = 0x0161,
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerQuoted 7 lines · Jarkko Sakkinen
## Overview
Introduce
tpm2_key_rsaimplementing asymmetric TPM RSA key.I submit this first as RFC as I could not execute the keyctl padd in the following sequence (returns EBADF):
Quoted 8 lines
tpm2_createprimary--hierarchy o -G rsa2048 -c owner.txttpm2_evictcontrol-c owner.txt0x81000001tpm2_getcaphandles-persistent
openssl genrsa -out private.pem 2048tpm2_import-C0x81000001-G rsa -i private.pem -u key.pub -r key.privtpm2_encodeobject-C0x81000001-u key.pub -r key.priv -o key.priv.pem openssl asn1parse -inform pem -in key.priv.pem -noout -out key.priv.der key_serial=cat key.priv.der | keyctl padd asymmetric tpm @u
After v2 changes it ends up to -EINVAL and:
OID is "2.23.133.10.1.3" which is not TPMSealedData
which makes total sense. James' old patch set has already TPMLoadableKey parsing PoC'd so I use that as the reference.
After the sequence above successfully completes keyctl public key ops are accesible by using $key_serial as the serial.
BR, Jarkko