]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KEYS: trusted: Replace a redundant instance of tpm2_hash_map
authorJarkko Sakkinen <jarkko@kernel.org>
Sat, 18 Oct 2025 17:41:36 +0000 (20:41 +0300)
committerJarkko Sakkinen <jarkko@kernel.org>
Sat, 29 Nov 2025 20:57:30 +0000 (22:57 +0200)
'trusted_tpm2' duplicates 'tpm2_hash_map' originally part of the TPN
driver, which is suboptimal.

Implement and export `tpm2_find_hash_alg()` in the driver, and substitute
the redundant code in 'trusted_tpm2' with a call to the new function.

Reviewed-by: Jonathan McDowell <noodles@meta.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
drivers/char/tpm/tpm2-cmd.c
include/linux/tpm.h
security/keys/trusted-keys/trusted_tpm2.c

index 7d77f6fbc1520cf4d8ab7a90cc4e8d00f01445ea..5532e53a2dd3da6b39f81ac3a63dc782aad55929 100644 (file)
@@ -18,7 +18,7 @@ static bool disable_pcr_integrity;
 module_param(disable_pcr_integrity, bool, 0444);
 MODULE_PARM_DESC(disable_pcr_integrity, "Disable integrity protection of TPM2_PCR_Extend");
 
-static struct tpm2_hash tpm2_hash_map[] = {
+struct tpm2_hash tpm2_hash_map[] = {
        {HASH_ALGO_SHA1, TPM_ALG_SHA1},
        {HASH_ALGO_SHA256, TPM_ALG_SHA256},
        {HASH_ALGO_SHA384, TPM_ALG_SHA384},
@@ -26,6 +26,18 @@ static struct tpm2_hash tpm2_hash_map[] = {
        {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
 };
 
+int tpm2_find_hash_alg(unsigned int crypto_id)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++)
+               if (crypto_id == tpm2_hash_map[i].crypto_id)
+                       return tpm2_hash_map[i].tpm_id;
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(tpm2_find_hash_alg);
+
 int tpm2_get_timeouts(struct tpm_chip *chip)
 {
        chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
index dc0338a783f3703cac0d3b58784d24b18a29e398..b15360ff78d7640198c5248cd7ba8b2c537a6f0f 100644 (file)
@@ -473,6 +473,7 @@ 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_find_hash_alg(unsigned int crypto_id);
 
 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
 {
index 024be262702fed97514b543ea8fb20706ca4506d..edd7b9d7e4dce059c71cbf6433720218bc5c6dc8 100644 (file)
 
 #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,
@@ -244,20 +236,13 @@ 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;
        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))
-               return -EINVAL;
+       hash = tpm2_find_hash_alg(options->hash);
+       if (hash < 0)
+               return hash;
 
        if (!options->keyhandle)
                return -EINVAL;