KEYS: asymmetric: tpm2_key_rsa
Subsystems (8)
Jarkko Sakkinen
maintainercover KEYS: asymmetric: tpm2_key_rsa
## Overview
Introduce tpm2_key_rsa implementing asymmetric TPM RSA key. This key type can be enabled with CONFIG_ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE config option. Carves groundwork for similar modules in future, such as tpm2_key_ecdsa.
## Testing
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 serial=cat key.priv.der | keyctl padd asymmetric tpm @u
echo "abcdefg" > plaintext.txt
keyctl pkey_encrypt $serial 0 plaintext.txt enc=pkcs1 > encrypted.dat keyctl pkey_decrypt $serial 0 encrypted.dat enc=pkcs1 > decrypted.dat keyctl pkey_sign $serial 0 plaintext.txt enc=pkcs1 hash=sha256 > signed.dat keyctl pkey_verify $serial 0 plaintext.txt signed.dat enc=pkcs1 hash=sha256
## References
- Derived from https://lore.kernel.org/all/[email protected]/
- Last RFC: https://lore.kernel.org/linux-integrity/[email protected]/T/#t
James Prestwood (1):
keys: asymmetric: ASYMMETRIC_TPM2_KEY_RSA_SUBTYPE
Jarkko Sakkinen (5):
crypto: rsa-pkcs1pad: export rsa1_asn_lookup()
lib: Expand asn1_encode_integer() to variable size integers
tpm: Export tpm2_load_context()
KEYS: trusted: Move tpm2_key_decode() to the TPM driver
tpm: tpm2_key: Extend parser to TPM_LoadableKey
crypto/asymmetric_keys/Kconfig | 16 +
crypto/asymmetric_keys/Makefile | 1 +
crypto/asymmetric_keys/tpm2_key_rsa.c | 726 ++++++++++++++++++
crypto/rsa-pkcs1pad.c | 16 +-
drivers/char/tpm/Kconfig | 1 +
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 | 119 +++
.../char/tpm}/tpm2key.asn1 | 0
include/crypto/rsa-pkcs1pad.h | 20 +
include/crypto/tpm2_key.h | 35 +
include/linux/asn1_encoder.h | 3 +-
include/linux/tpm.h | 4 +
lib/asn1_encoder.c | 185 ++---
security/keys/trusted-keys/Makefile | 2 -
security/keys/trusted-keys/trusted_tpm2.c | 130 +---
18 files changed, 1138 insertions(+), 265 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
maintainerPATCH 1/6 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
maintainerPATCH 2/6 lib: Expand asn1_encode_integer() to variable size integers
In reply to Jarkko SakkinenExpand asn1_encode_integer() to variable size integers, meaning that it will get a blob in big-endian format as integer and length of the blob as parameters. This is required in order to encode RSA public key modulus.
--- include/linux/asn1_encoder.h | 3 +- lib/asn1_encoder.c | 185 ++++++++++++---------- security/keys/trusted-keys/trusted_tpm2.c | 4 +- 3 files changed, 103 insertions(+), 89 deletions(-)
include/linux/asn1_encoder.h +2 -1
@@ -9,9 +9,10 @@#include <linux/bug.h> #define asn1_oid_len (oid) (sizeof(oid)/sizeof(u32)) +unsigned char *asn1_encode_integer (unsigned char *data,const unsigned char *end_data, -s64 integer); +const u8 *integer,int integer_len);unsigned char *asn1_encode_oid (unsigned char *data,const unsigned char *end_data,u32 oid[],int oid_len);
lib/asn1_encoder.c +98 -87
233 lines not on this page · raw message
security/keys/trusted-keys/trusted_tpm2.c +3 -1
@@ -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; +u32 key_handle;int ret; priv_len =get_unaligned_be16 (src) +2 ;
@@ -77,7 +78,8 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, goto err; } - work =asn1_encode_integer (work, end_work, options->keyhandle); + key_handle =cpu_to_be32 (options->keyhandle); + work =asn1_encode_integer (work, end_work, (u8 *)&key_handle,4 ); work =asn1_encode_octet_string (work, end_work, pub, pub_len); work =asn1_encode_octet_string (work, end_work, priv, priv_len);
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerPATCH 3/6 tpm: Export tpm2_load_context()
In reply to Jarkko SakkinenExport tpm2_load_context() so that the null key can be loaded as the parent of a asymmetric TPM2 key.
--- 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
maintainerPATCH 4/6 KEYS: trusted: Move tpm2_key_decode() to the TPM driver
In reply to Jarkko SakkinenMove tpm2_key_decode() to the TPM driver and export the symbols to make them callable from trusted keys. It can re-used for asymmetric keys.
---
drivers/char/tpm/Kconfig | 1 +
drivers/char/tpm/Makefile | 5 +
drivers/char/tpm/tpm2_key.c | 111 ++++++++++++++++
.../char/tpm}/tpm2key.asn1 | 0
include/crypto/tpm2_key.h | 33 +++++
security/keys/trusted-keys/Makefile | 2 -
security/keys/trusted-keys/trusted_tpm2.c | 121 +++---------------
7 files changed, 166 insertions(+), 107 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 +1 -0
@@ -7,6 +7,7 @@ menuconfig TCG_TPM tristate "TPM Hardware Support" depends on HAS_IOMEM imply SECURITYFS + select ASN1 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 +111 -0
111 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 *blob; +u32 blob_len; +const u8 *pub; +u32 pub_len; +const u8 *priv; +u32 priv_len; +}; + +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->blob); +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 +16 -105
153 lines not on this page · raw message
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerPATCH 5/6 tpm: tpm2_key: Extend parser to TPM_LoadableKey
In reply to Jarkko SakkinenExtend parser to TPM_LoadableKey. Add field for oid to struct tpm2_key so that callers can differentiate different key types.
--- drivers/char/tpm/tpm2_key.c | 14 +++++++++++--- include/crypto/tpm2_key.h | 2 ++ security/keys/trusted-keys/trusted_tpm2.c | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm2_key.c +11 -3
@@ -32,16 +32,24 @@ int tpm2_key_type(void *context, size_t hdrlen, const void *value, size_t vlen) { enum OID oid = look_up_OID(value, vlen); - -if (oid != OID_TPMSealedData) { +struct tpm2_key *key = context; + +switch (oid) { +case OID_TPMSealedData: +pr_info ("TPMSealedData\n" ); +break ; +case OID_TPMLoadableKey: +pr_info ("TPMLodableKey\n" ); +break ; +default :char buffer[50 ]; -sprint_oid (value, vlen, buffer,sizeof (buffer));pr_debug ("OID is \"%s\" which is not TPMSealedData\n" , buffer);return -EINVAL ; } + key->oid = oid;return 0 ; }
include/crypto/tpm2_key.h +2 -0
@@ -2,12 +2,14 @@ #ifndef __LINUX_TPM2_KEY_H__ #define __LINUX_TPM2_KEY_H__ +#include <linux/oid_registry.h> #include <linux/slab.h> /* * TPM2 ASN.1 key */ struct tpm2_key { + enum OID oid; u32 parent; const u8 *blob; u32 blob_len;
security/keys/trusted-keys/trusted_tpm2.c +5 -0
@@ -304,6 +304,11 @@ static int tpm2_load_cmd(struct tpm_chip *chip, blob = payload->blob; payload->old_format =1 ; }else { +if (key.oid != OID_TPMSealedData) { +tpm2_key_destroy (&key); +return -EINVAL ; + } + blob =kmalloc (key.priv_len + key.pub_len +4 ,GFP_KERNEL );if (blob) { options->keyhandle = key.parent;
Signature · 1 line
2.45.1
Jarkko Sakkinen
maintainerPATCH 6/6 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 | 16 + crypto/asymmetric_keys/Makefile | 1 + crypto/asymmetric_keys/tpm2_key_rsa.c | 726 ++++++++++++++++++++++++++ include/linux/tpm.h | 2 + 4 files changed, 745 insertions(+) create mode 100644 crypto/asymmetric_keys/tpm2_key_rsa.c
crypto/asymmetric_keys/Kconfig +16 -0
@@ -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,21 @@ 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 + select ASN1 + select ASN1_ENCODER + 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 +726 -0
726 lines not on this page · raw message
include/linux/tpm.h +2 -0
@@ -43,6 +43,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 ,
@@ -271,6 +272,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 5 lines · Jarkko Sakkinen
## Overview
Introduce
tpm2_key_rsaimplementing asymmetric TPM RSA key. This key type can be enabled withCONFIG_ASYMMETRIC_TPM2_KEY_RSA_SUBTYPEconfig option. Carves groundwork for similar modules in future, such astpm2_key_ecdsa.
Cc to Eric (forgot).
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 8 lines · Jarkko Sakkinen
Quoted 5 lines · Jarkko Sakkinen
## Overview
Introduce
tpm2_key_rsaimplementing asymmetric TPM RSA key. This key type can be enabled withCONFIG_ASYMMETRIC_TPM2_KEY_RSA_SUBTYPEconfig option. Carves groundwork for similar modules in future, such astpm2_key_ecdsa.Cc to Eric (forgot).
Also my idea is to take the (working) model from tpm_tis. At this point it is just tpm2_key_rsa but it would be easy to lay out tpm2_key later on and make the different key types as submodules. That would be a kind of model, which would support algorithmic agility side of TPM2.
So there might tpm2_key at some point with tpm2_key_rsa and tpm2_key_ecdsa as its submodules.
BR, Jarkko