KEYS: trusted_tpm1: HMAC fix and cleanup
Subsystems (5)
cover KEYS: trusted_tpm1: HMAC fix and cleanup
Patch 1 fixes the HMAC-SHA1 comparison in trusted_tpm1 to be constant-time.
Patch 2 simplifies the SHA-1 and HMAC-SHA1 computation in trusted_tpm1 by using library APIs instead of crypto_shash. Note that this depends on the SHA-1 and HMAC-SHA1 library APIs that were merged for v6.17-rc1.
Patch 3 is a trusted_tpm1 cleanup that moves private functionality out of a public header.
Eric Biggers (3):
KEYS: trusted_tpm1: Compare HMAC values in constant time
KEYS: trusted_tpm1: Use SHA-1 library instead of crypto_shash
KEYS: trusted_tpm1: Move private functionality out of public header
include/keys/trusted_tpm.h | 79 ------ security/keys/trusted-keys/Kconfig | 5 +- security/keys/trusted-keys/trusted_tpm1.c | 284 ++++++++-------------- 3 files changed, 100 insertions(+), 268 deletions(-)
base-commit: d6084bb815c453de27af8071a23163a711586a6c
Signature · 1 line
2.50.1
PATCH 1/3 KEYS: trusted_tpm1: Compare HMAC values in constant time
To prevent timing attacks, HMAC value comparison needs to be constant time. Replace the memcmp() with the correct function, crypto_memneq().
--- security/keys/trusted-keys/trusted_tpm1.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +4 -3
@@ -5,10 +5,11 @@* * See Documentation/security/keys/trusted-encrypted.rst */ #include <crypto/hash_info.h> +#include <crypto/utils.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/parser.h> #include <linux/string.h> #include <linux/err.h>
@@ -239,11 +240,11 @@ int TSS_checkhmac1(unsigned char *buffer,TPM_NONCE_SIZE , enonce,TPM_NONCE_SIZE , ononce, 1, continueflag, 0, 0);if (ret <0 ) goto out; -if (memcmp (testhmac, authdata,SHA1_DIGEST_SIZE )) +if (crypto_memneq (testhmac, authdata,SHA1_DIGEST_SIZE )) ret = -EINVAL ; out:kfree_sensitive (sdesc);return ret; }
@@ -332,20 +333,20 @@ static int TSS_checkhmac2(unsigned char *buffer, ret =TSS_rawhmac (testhmac1, key1, keylen1,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , enonce1,TPM_NONCE_SIZE , ononce,1 , continueflag1,0 ,0 );if (ret <0 ) goto out; -if (memcmp (testhmac1, authdata1,SHA1_DIGEST_SIZE )) { +if (crypto_memneq (testhmac1, authdata1,SHA1_DIGEST_SIZE )) { ret = -EINVAL ; goto out; } ret =TSS_rawhmac (testhmac2, key2, keylen2,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , enonce2,TPM_NONCE_SIZE , ononce,1 , continueflag2,0 ,0 );if (ret <0 ) goto out; -if (memcmp (testhmac2, authdata2,SHA1_DIGEST_SIZE )) +if (crypto_memneq (testhmac2, authdata2,SHA1_DIGEST_SIZE )) ret = -EINVAL ; out:kfree_sensitive (sdesc);return ret; }
base-commit: d6084bb815c453de27af8071a23163a711586a6c
Signature · 1 line
2.50.1
Jarkko Sakkinen
maintainerRe: [PATCH 1/3] KEYS: trusted_tpm1: Compare HMAC values in constant time
Quoted 6 lines · Eric Biggers
To prevent timing attacks, HMAC value comparison needs to be constant time. Replace the
memcmp()with the correct function,crypto_memneq().Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Eric Biggers <[email protected]>
Was crypto_memneq() available at the time?
Quoted 57 lines
--- security/keys/trusted-keys/trusted_tpm1.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)security/keys/trusted-keys/trusted_tpm1.c +4
-3@@ -5,10 +5,11 @@* * See Documentation/security/keys/trusted-encrypted.rst */ #include <crypto/hash_info.h> +#include <crypto/utils.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/parser.h> #include <linux/string.h> #include <linux/err.h> @@ -239,11 +240,11 @@ int TSS_checkhmac1(unsigned char *buffer,TPM_NONCE_SIZE , enonce,TPM_NONCE_SIZE , ononce, 1, continueflag, 0, 0);if (ret <0 ) goto out; -if (memcmp (testhmac, authdata,SHA1_DIGEST_SIZE )) +if (crypto_memneq (testhmac, authdata,SHA1_DIGEST_SIZE )) ret = -EINVAL ; out:kfree_sensitive (sdesc);return ret; }@@ -332,20 +333,20 @@ static int TSS_checkhmac2(unsigned char *buffer, ret =TSS_rawhmac (testhmac1, key1, keylen1,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , enonce1,TPM_NONCE_SIZE , ononce,1 , continueflag1,0 ,0 );if (ret <0 ) goto out; -if (memcmp (testhmac1, authdata1,SHA1_DIGEST_SIZE )) { +if (crypto_memneq (testhmac1, authdata1,SHA1_DIGEST_SIZE )) { ret = -EINVAL ; goto out; } ret =TSS_rawhmac (testhmac2, key2, keylen2,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , enonce2,TPM_NONCE_SIZE , ononce,1 , continueflag2,0 ,0 );if (ret <0 ) goto out; -if (memcmp (testhmac2, authdata2,SHA1_DIGEST_SIZE )) +if (crypto_memneq (testhmac2, authdata2,SHA1_DIGEST_SIZE )) ret = -EINVAL ; out:kfree_sensitive (sdesc);return ret; }
Quoted 4 lines
base-commit:
d6084bb815c453de27af8071a23163a711586a6cSignature · 1 line
2.50.1
BR, Jarkko
Re: [PATCH 1/3] KEYS: trusted_tpm1: Compare HMAC values in constant time
Quoted 9 lines · Jarkko Sakkinen
Quoted 6 lines · Eric Biggers
To prevent timing attacks, HMAC value comparison needs to be constant time. Replace the
memcmp()with the correct function,crypto_memneq().Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Eric Biggers <[email protected]>Was
crypto_memneq()available at the time?
No. The Fixes commit is still correct, though, as it's the commit that introduced the memcmp(). Technically it was still a bug at that time, even if there wasn't a helper function available yet.
- Eric
Jarkko Sakkinen
maintainerRe: [PATCH 1/3] KEYS: trusted_tpm1: Compare HMAC values in constant time
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Jarkko Sakkinen
Quoted 6 lines · Eric Biggers
To prevent timing attacks, HMAC value comparison needs to be constant time. Replace the
memcmp()with the correct function,crypto_memneq().Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Eric Biggers <[email protected]>
Quoted 5 lines
Quoted 1 line
Was
crypto_memneq()available at the time?No. The Fixes commit is still correct, though, as it's the commit that introduced the
memcmp(). Technically it was still a bug at that time, even if there wasn't a helper function available yet.
Add a remark to the commit message.
Quoted 2 lines
- Eric
BR, Jarkko
Re: [PATCH 1/3] KEYS: trusted_tpm1: Compare HMAC values in constant time
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Eric Biggers
Quoted 8 lines · Jarkko Sakkinen
Quoted 6 lines · Eric Biggers
To prevent timing attacks, HMAC value comparison needs to be constant time. Replace the
memcmp()with the correct function,crypto_memneq().Fixes d00a1c72f7f4 ("keys: add new trusted key-type")Signed-off-by Eric Biggers <[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Was
crypto_memneq()available at the time?No. The Fixes commit is still correct, though, as it's the commit that introduced the
memcmp(). Technically it was still a bug at that time, even if there wasn't a helper function available yet.Add a remark to the commit message.
I don't know what the point is (both commits are over a decade old), but sure I did that in v2.
- Eric
PATCH 2/3 KEYS: trusted_tpm1: Use SHA-1 library instead of crypto_shash
In reply to Eric BiggersUse the SHA-1 and HMAC-SHA1 library functions instead of crypto_shash. This is simpler and faster.
Replace the selection of CRYPTO, CRYPTO_HMAC, and CRYPTO_SHA1 with CRYPTO_LIB_SHA1 and CRYPTO_LIB_UTILS. The latter is needed for crypto_memneq() which was previously being pulled in via CRYPTO.
--- security/keys/trusted-keys/Kconfig | 5 +- security/keys/trusted-keys/trusted_tpm1.c | 221 ++++------------------ 2 files changed, 36 insertions(+), 190 deletions(-)
security/keys/trusted-keys/Kconfig +2 -3
@@ -3,14 +3,13 @@ config HAVE_TRUSTED_KEYS config TRUSTED_KEYS_TPM bool "TPM-based trusted keys" depends on TCG_TPM >= TRUSTED_KEYS default y - select CRYPTO - select CRYPTO_HMAC - select CRYPTO_SHA1 select CRYPTO_HASH_INFO + select CRYPTO_LIB_SHA1 + select CRYPTO_LIB_UTILS select ASN1_ENCODER select OID_REGISTRY select ASN1 select HAVE_TRUSTED_KEYS help
security/keys/trusted-keys/trusted_tpm1.c +34 -187
420 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [PATCH 2/3] KEYS: trusted_tpm1: Use SHA-1 library instead of crypto_shash
Quoted 8 lines · Eric Biggers
Use the SHA-1 and HMAC-SHA1 library functions instead of
crypto_shash. This is simpler and faster.Replace the selection of CRYPTO,
CRYPTO_HMAC, andCRYPTO_SHA1withCRYPTO_LIB_SHA1andCRYPTO_LIB_UTILS. The latter is needed forcrypto_memneq()which was previously being pulled in via CRYPTO.Signed-off-by Eric Biggers <[email protected]>
BR, Jarkko
PATCH 3/3 KEYS: trusted_tpm1: Move private functionality out of public header
In reply to Eric BiggersMove functionality used only by trusted_tpm1.c out of the public header <keys/trusted_tpm.h>. Specifically, change the exported functions into static functions, since they are not used outside trusted_tpm1.c, and move various other definitions and inline functions to trusted_tpm1.c.
--- include/keys/trusted_tpm.h | 79 ---------------------- security/keys/trusted-keys/trusted_tpm1.c | 80 ++++++++++++++++++++--- 2 files changed, 72 insertions(+), 87 deletions(-)
include/keys/trusted_tpm.h +0 -79
@@ -3,94 +3,15 @@#define __TRUSTED_TPM_H#include <keys/trusted-type.h> #include <linux/tpm_command.h> -/* implementation specific TPM constants */ -#define TPM_SIZE_OFFSET 2 -#define TPM_RETURN_OFFSET 6 -#define TPM_DATA_OFFSET 10 - -#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) -#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) -#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) -extern struct trusted_key_ops trusted_key_tpm_ops; -struct osapsess { -uint32_t handle; -unsigned char secret[SHA1_DIGEST_SIZE ]; -unsigned char enonce[TPM_NONCE_SIZE ]; -}; - -/* discrete values, but have to store in uint16_t for TPM use */ -enum { - SEAL_keytype =1 , - SRK_keytype =4 -}; - -int TSS_authhmac (unsigned char *digest,const unsigned char *key, -unsigned int keylen,unsigned char *h1, -unsigned char *h2,unsigned int h3, ...); -int TSS_checkhmac1 (unsigned char *buffer, -const uint32_t command, -const unsigned char *ononce, -const unsigned char *key, -unsigned int keylen, ...); - -int trusted_tpm_send (unsigned char *cmd,size_t buflen); -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce); -int tpm2_seal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options);int tpm2_unseal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options); -#define TPM_DEBUG 0 - -#if TPM_DEBUG -static inline void dump_options (struct trusted_key_options *o) -{ -pr_info ("sealing key type %d\n" , o->keytype); -pr_info ("sealing key handle %0X\n" , o->keyhandle); -pr_info ("pcrlock %d\n" , o->pcrlock); -pr_info ("pcrinfo %d\n" , o->pcrinfo_len); -print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , -16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); -} - -static inline void dump_sess (struct osapsess *s) -{ -print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , -16 ,1 , &s->handle,4 ,0 ); -pr_info ("secret:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); -pr_info ("trusted-key: enonce:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -int len; - -pr_info ("\ntpm buffer\n" ); - len =LOAD32 (buf,TPM_SIZE_OFFSET ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); -} -#else -static inline void dump_options (struct trusted_key_options *o) -{ -} - -static inline void dump_sess (struct osapsess *s) -{ -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -} -#endif #endif
security/keys/trusted-keys/trusted_tpm1.c +72 -8
164 lines not on this page · raw message
Signature · 1 line
2.50.1
Jarkko Sakkinen
maintainerRe: [PATCH 3/3] KEYS: trusted_tpm1: Move private functionality out of public header
Quoted 289 lines · Eric Biggers
Move functionality used only by
trusted_tpm1.cout of the public header <keys/trusted_tpm.h>. Specifically, change the exported functions into static functions, since they are not used outsidetrusted_tpm1.c, and move various other definitions and inline functions totrusted_tpm1.c.Signed-off-by Eric Biggers <[email protected]>--- include/keys/trusted_tpm.h | 79 ---------------------- security/keys/trusted-keys/trusted_tpm1.c | 80 ++++++++++++++++++++--- 2 files changed, 72 insertions(+), 87 deletions(-)include/keys/trusted_tpm.h +0
-79@@ -3,94 +3,15 @@#define __TRUSTED_TPM_H#include <keys/trusted-type.h> #include <linux/tpm_command.h> -/* implementation specific TPM constants */ -#define TPM_SIZE_OFFSET 2 -#define TPM_RETURN_OFFSET 6 -#define TPM_DATA_OFFSET 10 - -#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) -#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) -#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) -extern struct trusted_key_ops trusted_key_tpm_ops; -struct osapsess { -uint32_t handle; -unsigned char secret[SHA1_DIGEST_SIZE ]; -unsigned char enonce[TPM_NONCE_SIZE ]; -}; - -/* discrete values, but have to store in uint16_t for TPM use */ -enum { - SEAL_keytype =1 , - SRK_keytype =4 -}; - -int TSS_authhmac (unsigned char *digest,const unsigned char *key, -unsigned int keylen,unsigned char *h1, -unsigned char *h2,unsigned int h3, ...); -int TSS_checkhmac1 (unsigned char *buffer, -const uint32_t command, -const unsigned char *ononce, -const unsigned char *key, -unsigned int keylen, ...); - -int trusted_tpm_send (unsigned char *cmd,size_t buflen); -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce); -int tpm2_seal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options);int tpm2_unseal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options); -#define TPM_DEBUG 0 - -#if TPM_DEBUG -static inline void dump_options (struct trusted_key_options *o) -{ -pr_info ("sealing key type %d\n" , o->keytype); -pr_info ("sealing key handle %0X\n" , o->keyhandle); -pr_info ("pcrlock %d\n" , o->pcrlock); -pr_info ("pcrinfo %d\n" , o->pcrinfo_len); -print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , -16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); -} - -static inline void dump_sess (struct osapsess *s) -{ -print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , -16 ,1 , &s->handle,4 ,0 ); -pr_info ("secret:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); -pr_info ("trusted-key: enonce:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -int len; - -pr_info ("\ntpm buffer\n" ); - len =LOAD32 (buf,TPM_SIZE_OFFSET ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); -} -#else -static inline void dump_options (struct trusted_key_options *o) -{ -} - -static inline void dump_sess (struct osapsess *s) -{ -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -} -#endif #endif security/keys/trusted-keys/trusted_tpm1.c +72
-8@@ -22,10 +22,78 @@#include <keys/trusted_tpm.h> static struct tpm_chip *chip;static struct tpm_digest *digests; +/* implementation specific TPM constants */ +#define TPM_SIZE_OFFSET 2 +#define TPM_RETURN_OFFSET 6 +#define TPM_DATA_OFFSET 10 + +#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) +#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) +#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) + +struct osapsess { +uint32_t handle; +unsigned char secret[SHA1_DIGEST_SIZE ]; +unsigned char enonce[TPM_NONCE_SIZE ]; +}; + +/* discrete values, but have to store in uint16_t for TPM use */ +enum { + SEAL_keytype =1 , + SRK_keytype =4 +}; + +#define TPM_DEBUG 0 + +#if TPM_DEBUG +static inline void dump_options (struct trusted_key_options *o) +{ +pr_info ("sealing key type %d\n" , o->keytype); +pr_info ("sealing key handle %0X\n" , o->keyhandle); +pr_info ("pcrlock %d\n" , o->pcrlock); +pr_info ("pcrinfo %d\n" , o->pcrinfo_len); +print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , +16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); +} + +static inline void dump_sess (struct osapsess *s) +{ +print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , +16 ,1 , &s->handle,4 ,0 ); +pr_info ("secret:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); +pr_info ("trusted-key: enonce:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +int len; + +pr_info ("\ntpm buffer\n" ); + len =LOAD32 (buf,TPM_SIZE_OFFSET ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); +} +#else +static inline void dump_options (struct trusted_key_options *o) +{ +} + +static inline void dump_sess (struct osapsess *s) +{ +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +} +#endif +static int TSS_rawhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen, ...) {struct hmac_sha1_ctx hmac_ctx;va_list argp;@@ -54,11 +122,11 @@ static int TSS_rawhmac(unsigned char *digest, const unsigned char *key, }/* * calculate authorization info fields to send to TPM */ -int TSS_authhmac (unsigned char *digest,const unsigned char *key, +static int TSS_authhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen,unsigned char *h1,unsigned char *h2,unsigned int h3, ...) {unsigned char paramdigest[SHA1_DIGEST_SIZE ];struct sha1_ctx sha_ctx;@@ -92,16 +160,15 @@ int TSS_authhmac(unsigned char *digest, const unsigned char *key, ret =TSS_rawhmac (digest, key, keylen,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , h1,TPM_NONCE_SIZE , h2,1 , &c,0 ,0 );return ret; } -EXPORT_SYMBOL_GPL (TSS_authhmac);/* * verify the AUTH1_COMMAND (Seal) result from TPM */ -int TSS_checkhmac1 (unsigned char *buffer, +static int TSS_checkhmac1 (unsigned char *buffer,const uint32_t command,const unsigned char *ononce,const unsigned char *key,unsigned int keylen, ...) {@@ -157,11 +224,10 @@ int TSS_checkhmac1(unsigned char *buffer,if (crypto_memneq (testhmac, authdata,SHA1_DIGEST_SIZE ))return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (TSS_checkhmac1);/* * verify the AUTH2_COMMAND (unseal) result from TPM */ static int TSS_checkhmac2(unsigned char *buffer,@@ -242,11 +308,11 @@ static int TSS_checkhmac2(unsigned char *buffer,/* * For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (unsigned char *cmd,size_t buflen) {struct tpm_buf buf;int rc;if (!chip)@@ -268,11 +334,10 @@ int trusted_tpm_send(unsigned char *cmd, size_t buflen) rc = -EPERM ;tpm_put_ops (chip);return rc; } -EXPORT_SYMBOL_GPL (trusted_tpm_send);/* * Lock a trusted key, by extending a selected PCR. * * Prevents a trusted key that is sealed to PCRs from being accessed. @@ -322,11 +387,11 @@ static int osap(struct tpm_buf *tb, struct osapsess *s, }/* * Create an object independent authorisation protocol (oiap) session */ -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) +static int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) {int ret;if (!chip)return -ENODEV ;@@ -339,11 +404,10 @@ int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce) *handle =LOAD32 (tb->data,TPM_DATA_OFFSET );memcpy (nonce, &tb->data[TPM_DATA_OFFSET +sizeof (uint32_t )],TPM_NONCE_SIZE );return 0 ; } -EXPORT_SYMBOL_GPL (oiap);struct tpm_digests {unsigned char encauth[SHA1_DIGEST_SIZE ];unsigned char pubauth[SHA1_DIGEST_SIZE ];unsigned char xorwork[SHA1_DIGEST_SIZE *2 ];Signature · 1 line
2.50.1
IMHO, this could followed (as next logical steps):
1. Get rid of LOAD*() (tpm_buf_read*)
2. I think we should delete dump_* given modern times and countless ways to acquire that data (e.g., with eBPF).
BR, Jarkko
Re: [PATCH 3/3] KEYS: trusted_tpm1: Move private functionality out of public header
Quoted 293 lines · Jarkko Sakkinen
Quoted 289 lines · Eric Biggers
Move functionality used only by
trusted_tpm1.cout of the public header <keys/trusted_tpm.h>. Specifically, change the exported functions into static functions, since they are not used outsidetrusted_tpm1.c, and move various other definitions and inline functions totrusted_tpm1.c.Signed-off-by Eric Biggers <[email protected]>--- include/keys/trusted_tpm.h | 79 ---------------------- security/keys/trusted-keys/trusted_tpm1.c | 80 ++++++++++++++++++++--- 2 files changed, 72 insertions(+), 87 deletions(-)include/keys/trusted_tpm.h +0
-79@@ -3,94 +3,15 @@#define __TRUSTED_TPM_H#include <keys/trusted-type.h> #include <linux/tpm_command.h> -/* implementation specific TPM constants */ -#define TPM_SIZE_OFFSET 2 -#define TPM_RETURN_OFFSET 6 -#define TPM_DATA_OFFSET 10 - -#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) -#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) -#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) -extern struct trusted_key_ops trusted_key_tpm_ops; -struct osapsess { -uint32_t handle; -unsigned char secret[SHA1_DIGEST_SIZE ]; -unsigned char enonce[TPM_NONCE_SIZE ]; -}; - -/* discrete values, but have to store in uint16_t for TPM use */ -enum { - SEAL_keytype =1 , - SRK_keytype =4 -}; - -int TSS_authhmac (unsigned char *digest,const unsigned char *key, -unsigned int keylen,unsigned char *h1, -unsigned char *h2,unsigned int h3, ...); -int TSS_checkhmac1 (unsigned char *buffer, -const uint32_t command, -const unsigned char *ononce, -const unsigned char *key, -unsigned int keylen, ...); - -int trusted_tpm_send (unsigned char *cmd,size_t buflen); -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce); -int tpm2_seal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options);int tpm2_unseal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options); -#define TPM_DEBUG 0 - -#if TPM_DEBUG -static inline void dump_options (struct trusted_key_options *o) -{ -pr_info ("sealing key type %d\n" , o->keytype); -pr_info ("sealing key handle %0X\n" , o->keyhandle); -pr_info ("pcrlock %d\n" , o->pcrlock); -pr_info ("pcrinfo %d\n" , o->pcrinfo_len); -print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , -16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); -} - -static inline void dump_sess (struct osapsess *s) -{ -print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , -16 ,1 , &s->handle,4 ,0 ); -pr_info ("secret:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); -pr_info ("trusted-key: enonce:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -int len; - -pr_info ("\ntpm buffer\n" ); - len =LOAD32 (buf,TPM_SIZE_OFFSET ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); -} -#else -static inline void dump_options (struct trusted_key_options *o) -{ -} - -static inline void dump_sess (struct osapsess *s) -{ -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -} -#endif #endif security/keys/trusted-keys/trusted_tpm1.c +72
-8@@ -22,10 +22,78 @@#include <keys/trusted_tpm.h> static struct tpm_chip *chip;static struct tpm_digest *digests; +/* implementation specific TPM constants */ +#define TPM_SIZE_OFFSET 2 +#define TPM_RETURN_OFFSET 6 +#define TPM_DATA_OFFSET 10 + +#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) +#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) +#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) + +struct osapsess { +uint32_t handle; +unsigned char secret[SHA1_DIGEST_SIZE ]; +unsigned char enonce[TPM_NONCE_SIZE ]; +}; + +/* discrete values, but have to store in uint16_t for TPM use */ +enum { + SEAL_keytype =1 , + SRK_keytype =4 +}; + +#define TPM_DEBUG 0 + +#if TPM_DEBUG +static inline void dump_options (struct trusted_key_options *o) +{ +pr_info ("sealing key type %d\n" , o->keytype); +pr_info ("sealing key handle %0X\n" , o->keyhandle); +pr_info ("pcrlock %d\n" , o->pcrlock); +pr_info ("pcrinfo %d\n" , o->pcrinfo_len); +print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , +16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); +} + +static inline void dump_sess (struct osapsess *s) +{ +print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , +16 ,1 , &s->handle,4 ,0 ); +pr_info ("secret:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); +pr_info ("trusted-key: enonce:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +int len; + +pr_info ("\ntpm buffer\n" ); + len =LOAD32 (buf,TPM_SIZE_OFFSET ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); +} +#else +static inline void dump_options (struct trusted_key_options *o) +{ +} + +static inline void dump_sess (struct osapsess *s) +{ +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +} +#endif +static int TSS_rawhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen, ...) {struct hmac_sha1_ctx hmac_ctx;va_list argp;@@ -54,11 +122,11 @@ static int TSS_rawhmac(unsigned char *digest, const unsigned char *key, }/* * calculate authorization info fields to send to TPM */ -int TSS_authhmac (unsigned char *digest,const unsigned char *key, +static int TSS_authhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen,unsigned char *h1,unsigned char *h2,unsigned int h3, ...) {unsigned char paramdigest[SHA1_DIGEST_SIZE ];struct sha1_ctx sha_ctx;@@ -92,16 +160,15 @@ int TSS_authhmac(unsigned char *digest, const unsigned char *key, ret =TSS_rawhmac (digest, key, keylen,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , h1,TPM_NONCE_SIZE , h2,1 , &c,0 ,0 );return ret; } -EXPORT_SYMBOL_GPL (TSS_authhmac);/* * verify the AUTH1_COMMAND (Seal) result from TPM */ -int TSS_checkhmac1 (unsigned char *buffer, +static int TSS_checkhmac1 (unsigned char *buffer,const uint32_t command,const unsigned char *ononce,const unsigned char *key,unsigned int keylen, ...) {@@ -157,11 +224,10 @@ int TSS_checkhmac1(unsigned char *buffer,if (crypto_memneq (testhmac, authdata,SHA1_DIGEST_SIZE ))return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (TSS_checkhmac1);/* * verify the AUTH2_COMMAND (unseal) result from TPM */ static int TSS_checkhmac2(unsigned char *buffer,@@ -242,11 +308,11 @@ static int TSS_checkhmac2(unsigned char *buffer,/* * For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (unsigned char *cmd,size_t buflen) {struct tpm_buf buf;int rc;if (!chip)@@ -268,11 +334,10 @@ int trusted_tpm_send(unsigned char *cmd, size_t buflen) rc = -EPERM ;tpm_put_ops (chip);return rc; } -EXPORT_SYMBOL_GPL (trusted_tpm_send);/* * Lock a trusted key, by extending a selected PCR. * * Prevents a trusted key that is sealed to PCRs from being accessed. @@ -322,11 +387,11 @@ static int osap(struct tpm_buf *tb, struct osapsess *s, }/* * Create an object independent authorisation protocol (oiap) session */ -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) +static int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) {int ret;if (!chip)return -ENODEV ;@@ -339,11 +404,10 @@ int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce) *handle =LOAD32 (tb->data,TPM_DATA_OFFSET );memcpy (nonce, &tb->data[TPM_DATA_OFFSET +sizeof (uint32_t )],TPM_NONCE_SIZE );return 0 ; } -EXPORT_SYMBOL_GPL (oiap);struct tpm_digests {unsigned char encauth[SHA1_DIGEST_SIZE ];unsigned char pubauth[SHA1_DIGEST_SIZE ];unsigned char xorwork[SHA1_DIGEST_SIZE *2 ];Signature · 1 line
2.50.1IMHO, this could followed (as next logical steps):
Quoted 4 lines
1. Get rid of LOAD*() (tpm_buf_read*)
2. I think we should delete dump_* given modern times and countless ways to acquire that data (e.g., with eBPF).
Sure, would you be interested in sending a follow-up patch? I don't have much interest in this driver myself.
- Eric
Jarkko Sakkinen
maintainerRe: [PATCH 3/3] KEYS: trusted_tpm1: Move private functionality out of public header
Quoted 294 lines · Eric Biggers
Quoted 293 lines · Jarkko Sakkinen
Quoted 289 lines · Eric Biggers
Move functionality used only by
trusted_tpm1.cout of the public header <keys/trusted_tpm.h>. Specifically, change the exported functions into static functions, since they are not used outsidetrusted_tpm1.c, and move various other definitions and inline functions totrusted_tpm1.c.Signed-off-by Eric Biggers <[email protected]>--- include/keys/trusted_tpm.h | 79 ---------------------- security/keys/trusted-keys/trusted_tpm1.c | 80 ++++++++++++++++++++--- 2 files changed, 72 insertions(+), 87 deletions(-)include/keys/trusted_tpm.h +0
-79@@ -3,94 +3,15 @@#define __TRUSTED_TPM_H#include <keys/trusted-type.h> #include <linux/tpm_command.h> -/* implementation specific TPM constants */ -#define TPM_SIZE_OFFSET 2 -#define TPM_RETURN_OFFSET 6 -#define TPM_DATA_OFFSET 10 - -#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) -#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) -#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) -extern struct trusted_key_ops trusted_key_tpm_ops; -struct osapsess { -uint32_t handle; -unsigned char secret[SHA1_DIGEST_SIZE ]; -unsigned char enonce[TPM_NONCE_SIZE ]; -}; - -/* discrete values, but have to store in uint16_t for TPM use */ -enum { - SEAL_keytype =1 , - SRK_keytype =4 -}; - -int TSS_authhmac (unsigned char *digest,const unsigned char *key, -unsigned int keylen,unsigned char *h1, -unsigned char *h2,unsigned int h3, ...); -int TSS_checkhmac1 (unsigned char *buffer, -const uint32_t command, -const unsigned char *ononce, -const unsigned char *key, -unsigned int keylen, ...); - -int trusted_tpm_send (unsigned char *cmd,size_t buflen); -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce); -int tpm2_seal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options);int tpm2_unseal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options); -#define TPM_DEBUG 0 - -#if TPM_DEBUG -static inline void dump_options (struct trusted_key_options *o) -{ -pr_info ("sealing key type %d\n" , o->keytype); -pr_info ("sealing key handle %0X\n" , o->keyhandle); -pr_info ("pcrlock %d\n" , o->pcrlock); -pr_info ("pcrinfo %d\n" , o->pcrinfo_len); -print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , -16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); -} - -static inline void dump_sess (struct osapsess *s) -{ -print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , -16 ,1 , &s->handle,4 ,0 ); -pr_info ("secret:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); -pr_info ("trusted-key: enonce:\n" ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , -16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -int len; - -pr_info ("\ntpm buffer\n" ); - len =LOAD32 (buf,TPM_SIZE_OFFSET ); -print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); -} -#else -static inline void dump_options (struct trusted_key_options *o) -{ -} - -static inline void dump_sess (struct osapsess *s) -{ -} - -static inline void dump_tpm_buf (unsigned char *buf) -{ -} -#endif #endif security/keys/trusted-keys/trusted_tpm1.c +72
-8@@ -22,10 +22,78 @@#include <keys/trusted_tpm.h> static struct tpm_chip *chip;static struct tpm_digest *digests; +/* implementation specific TPM constants */ +#define TPM_SIZE_OFFSET 2 +#define TPM_RETURN_OFFSET 6 +#define TPM_DATA_OFFSET 10 + +#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) +#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) +#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) + +struct osapsess { +uint32_t handle; +unsigned char secret[SHA1_DIGEST_SIZE ]; +unsigned char enonce[TPM_NONCE_SIZE ]; +}; + +/* discrete values, but have to store in uint16_t for TPM use */ +enum { + SEAL_keytype =1 , + SRK_keytype =4 +}; + +#define TPM_DEBUG 0 + +#if TPM_DEBUG +static inline void dump_options (struct trusted_key_options *o) +{ +pr_info ("sealing key type %d\n" , o->keytype); +pr_info ("sealing key handle %0X\n" , o->keyhandle); +pr_info ("pcrlock %d\n" , o->pcrlock); +pr_info ("pcrinfo %d\n" , o->pcrinfo_len); +print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , +16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); +} + +static inline void dump_sess (struct osapsess *s) +{ +print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , +16 ,1 , &s->handle,4 ,0 ); +pr_info ("secret:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->secret,SHA1_DIGEST_SIZE ,0 ); +pr_info ("trusted-key: enonce:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->enonce,SHA1_DIGEST_SIZE ,0 ); +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +int len; + +pr_info ("\ntpm buffer\n" ); + len =LOAD32 (buf,TPM_SIZE_OFFSET ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); +} +#else +static inline void dump_options (struct trusted_key_options *o) +{ +} + +static inline void dump_sess (struct osapsess *s) +{ +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +} +#endif +static int TSS_rawhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen, ...) {struct hmac_sha1_ctx hmac_ctx;va_list argp;@@ -54,11 +122,11 @@ static int TSS_rawhmac(unsigned char *digest, const unsigned char *key, }/* * calculate authorization info fields to send to TPM */ -int TSS_authhmac (unsigned char *digest,const unsigned char *key, +static int TSS_authhmac (unsigned char *digest,const unsigned char *key,unsigned int keylen,unsigned char *h1,unsigned char *h2,unsigned int h3, ...) {unsigned char paramdigest[SHA1_DIGEST_SIZE ];struct sha1_ctx sha_ctx;@@ -92,16 +160,15 @@ int TSS_authhmac(unsigned char *digest, const unsigned char *key, ret =TSS_rawhmac (digest, key, keylen,SHA1_DIGEST_SIZE , paramdigest,TPM_NONCE_SIZE , h1,TPM_NONCE_SIZE , h2,1 , &c,0 ,0 );return ret; } -EXPORT_SYMBOL_GPL (TSS_authhmac);/* * verify the AUTH1_COMMAND (Seal) result from TPM */ -int TSS_checkhmac1 (unsigned char *buffer, +static int TSS_checkhmac1 (unsigned char *buffer,const uint32_t command,const unsigned char *ononce,const unsigned char *key,unsigned int keylen, ...) {@@ -157,11 +224,10 @@ int TSS_checkhmac1(unsigned char *buffer,if (crypto_memneq (testhmac, authdata,SHA1_DIGEST_SIZE ))return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (TSS_checkhmac1);/* * verify the AUTH2_COMMAND (unseal) result from TPM */ static int TSS_checkhmac2(unsigned char *buffer,@@ -242,11 +308,11 @@ static int TSS_checkhmac2(unsigned char *buffer,/* * For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (unsigned char *cmd,size_t buflen) {struct tpm_buf buf;int rc;if (!chip)@@ -268,11 +334,10 @@ int trusted_tpm_send(unsigned char *cmd, size_t buflen) rc = -EPERM ;tpm_put_ops (chip);return rc; } -EXPORT_SYMBOL_GPL (trusted_tpm_send);/* * Lock a trusted key, by extending a selected PCR. * * Prevents a trusted key that is sealed to PCRs from being accessed. @@ -322,11 +387,11 @@ static int osap(struct tpm_buf *tb, struct osapsess *s, }/* * Create an object independent authorisation protocol (oiap) session */ -int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) +static int oiap (struct tpm_buf *tb,uint32_t *handle,unsigned char *nonce) {int ret;if (!chip)return -ENODEV ;@@ -339,11 +404,10 @@ int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce) *handle =LOAD32 (tb->data,TPM_DATA_OFFSET );memcpy (nonce, &tb->data[TPM_DATA_OFFSET +sizeof (uint32_t )],TPM_NONCE_SIZE );return 0 ; } -EXPORT_SYMBOL_GPL (oiap);struct tpm_digests {unsigned char encauth[SHA1_DIGEST_SIZE ];unsigned char pubauth[SHA1_DIGEST_SIZE ];unsigned char xorwork[SHA1_DIGEST_SIZE *2 ];Signature · 1 line
2.50.1IMHO, this could followed (as next logical steps):
Quoted 7 lines
Quoted 4 lines
1. Get rid of LOAD*() (tpm_buf_read*)
2. I think we should delete dump_* given modern times and countless ways to acquire that data (e.g., with eBPF).Sure, would you be interested in sending a follow-up patch? I don't have much interest in this driver myself.
OK fine I'll work on that later!
Quoted 2 lines
- Eric
BR, Jarkko