From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: linux-integrity@vger.kernel.org
Cc: Jarkko Sakkinen <jarkko@kernel.org>, keyrings@vger.kernel.org
Subject: [PATCH 1/6] tpm: consolidate TPM to crypto hash algorithm conversion
Date: Fri, 24 May 2024 09:04:54 -0400 [thread overview]
Message-ID: <20240524130459.21510-2-James.Bottomley@HansenPartnership.com> (raw)
In-Reply-To: <20240524130459.21510-1-James.Bottomley@HansenPartnership.com>
linux crypto and the TPM use different numeric algorithm identifiers
for hash (and other) functions. The conversion array for this already
exists in two separate places. The new policy sessions code would
have to add a third copy, so instead of increasing the duplication,
move the definition to a single consolidated place in tpm.h so the
policy code can use it as is.
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
---
drivers/char/tpm/tpm2-cmd.c | 8 ----
include/linux/tpm.h | 52 ++++++++++++++++++++++-
security/keys/trusted-keys/trusted_tpm2.c | 20 +--------
3 files changed, 53 insertions(+), 27 deletions(-)
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 0cdf892ec2a7..f4428e715dd8 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -14,14 +14,6 @@
#include "tpm.h"
#include <crypto/hash_info.h>
-static struct tpm2_hash tpm2_hash_map[] = {
- {HASH_ALGO_SHA1, TPM_ALG_SHA1},
- {HASH_ALGO_SHA256, TPM_ALG_SHA256},
- {HASH_ALGO_SHA384, TPM_ALG_SHA384},
- {HASH_ALGO_SHA512, TPM_ALG_SHA512},
- {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
-};
-
int tpm2_get_timeouts(struct tpm_chip *chip)
{
/* Fixed timeouts for TPM2 */
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index c17e4efbb2e5..07f532456a0c 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -418,11 +418,61 @@ enum tpm2_session_attributes {
TPM2_SA_AUDIT = BIT(7),
};
-struct tpm2_hash {
+static const struct {
unsigned int crypto_id;
unsigned int tpm_id;
+} tpm2_hash_map[] = {
+ {HASH_ALGO_SHA1, TPM_ALG_SHA1},
+ {HASH_ALGO_SHA256, TPM_ALG_SHA256},
+ {HASH_ALGO_SHA384, TPM_ALG_SHA384},
+ {HASH_ALGO_SHA512, TPM_ALG_SHA512},
+ {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
};
+/**
+ * tpm2_crypto_to_alg() - convert a crypto hash to a TPM alg id
+ *
+ * @hash: the crypto subsystem view of the hash
+ *
+ * Return: TPM algorithm id or -1 if no mapping was found.
+ */
+static inline int tpm2_crypto_to_alg(int hash)
+{
+ int i;
+ int tpm_alg = -1;
+
+ for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
+ if (hash == tpm2_hash_map[i].crypto_id) {
+ tpm_alg = tpm2_hash_map[i].tpm_id;
+ break;
+ }
+ }
+
+ return tpm_alg;
+}
+
+/**
+ * tpm2_alg_to_crypto() - convert a TPM alg id to a crypto hash
+ *
+ * @hash: the TPM alg id view of the hash
+ *
+ * Return: TPM algorithm id or -1 if no mapping was found.
+ */
+static inline int tpm2_alg_to_crypto(int hash)
+{
+ int i;
+ int crypto_hash = -1;
+
+ for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
+ if (hash == tpm2_hash_map[i].tpm_id) {
+ crypto_hash = tpm2_hash_map[i].crypto_id;
+ break;
+ }
+ }
+
+ return crypto_hash;
+}
+
int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal);
void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal);
int tpm_buf_init_sized(struct tpm_buf *buf);
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index dfeec06301ce..94ff9ccae66e 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -18,14 +18,6 @@
#include "tpm2key.asn1.h"
-static struct tpm2_hash tpm2_hash_map[] = {
- {HASH_ALGO_SHA1, TPM_ALG_SHA1},
- {HASH_ALGO_SHA256, TPM_ALG_SHA256},
- {HASH_ALGO_SHA384, TPM_ALG_SHA384},
- {HASH_ALGO_SHA512, TPM_ALG_SHA512},
- {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
-};
-
static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
static int tpm2_key_encode(struct trusted_key_payload *payload,
@@ -231,19 +223,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
off_t offset = TPM_HEADER_SIZE;
struct tpm_buf buf, sized;
int blob_len = 0;
- u32 hash;
+ int hash = tpm2_crypto_to_alg(options->hash);
u32 flags;
- int i;
int rc;
- for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
- if (options->hash == tpm2_hash_map[i].crypto_id) {
- hash = tpm2_hash_map[i].tpm_id;
- break;
- }
- }
-
- if (i == ARRAY_SIZE(tpm2_hash_map))
+ if (hash < 0)
return -EINVAL;
if (!options->keyhandle)
--
2.35.3
next prev parent reply other threads:[~2024-05-24 13:05 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-24 13:04 [PATCH 0/6] Add policy to sealed keys James Bottomley
2024-05-24 13:04 ` James Bottomley [this message]
2024-05-24 13:40 ` [PATCH 1/6] tpm: consolidate TPM to crypto hash algorithm conversion Jarkko Sakkinen
2024-05-24 13:52 ` Jarkko Sakkinen
2024-05-27 3:45 ` Ben Boeckel
2024-05-27 11:18 ` Jarkko Sakkinen
2024-07-16 11:13 ` Jarkko Sakkinen
2024-05-24 13:04 ` [PATCH 2/6] tpm: add policy sessions James Bottomley
2024-07-16 11:53 ` Jarkko Sakkinen
2024-07-16 14:07 ` Jarkko Sakkinen
2024-07-16 14:08 ` Jarkko Sakkinen
2024-07-16 14:12 ` Jarkko Sakkinen
2024-07-18 2:30 ` James Bottomley
2024-07-19 13:21 ` Jarkko Sakkinen
2024-07-19 13:26 ` Jarkko Sakkinen
2024-05-24 13:04 ` [PATCH 3/6] KEYS: trusted: add PCR policy to TPM2 keys James Bottomley
2024-07-16 12:01 ` Jarkko Sakkinen
2024-05-24 13:04 ` [PATCH 4/6] KEYS: trusted: add ability to specify arbitrary policy James Bottomley
2024-07-16 12:01 ` Jarkko Sakkinen
2024-05-24 13:04 ` [PATCH 5/6] KEYS: trusted: implement counter/timer policy James Bottomley
2024-07-16 12:03 ` Jarkko Sakkinen
2024-05-24 13:04 ` [PATCH 6/6] KEYS: trusted: add support for TPM keys with signed policy James Bottomley
2024-07-16 12:03 ` Jarkko Sakkinen
2024-05-24 13:24 ` [PATCH 0/6] Add policy to sealed keys Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240524130459.21510-2-James.Bottomley@HansenPartnership.com \
--to=james.bottomley@hansenpartnership.com \
--cc=jarkko@kernel.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-integrity@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).