]> Git Repo - J-u-boot.git/commitdiff
tpm: add flag in hash_algo_list and API to check if algorithm is supported
authorRaymond Mao <[email protected]>
Tue, 24 Dec 2024 16:01:07 +0000 (08:01 -0800)
committerIlias Apalodimas <[email protected]>
Tue, 7 Jan 2025 13:45:51 +0000 (15:45 +0200)
Add a bool var into hash_algo_list to indicate whether the algorithm
is supported or not and move the IS_ENABLED to only cover this var.
So that we can have the name, hash, mask and size no matter the
digest kconfigs are enabled or not.

In before, tpm2_algorithm_to_len() and tcg2_algorithm_to_mask() are used to
identify an unsupported algorithm when they return 0.
It is not the case now when hash_algo_list always provides algorithm size
and mask, thus a new API is introduced to check if an algorithm is
supported by U-Boot.

Suggested-by: Ilias Apalodimas <[email protected]>
Signed-off-by: Raymond Mao <[email protected]>
Reviewed-by: Ilias Apalodimas <[email protected]>
Signed-off-by: Ilias Apalodimas <[email protected]>
include/tpm-v2.h
lib/tpm-v2.c
lib/tpm_tcg2.c

index 87b2c614ad277284aa88c106add8598cc83a59d4..c49eadda26c70c1fb4279c36093c08b41133f984 100644 (file)
@@ -268,6 +268,7 @@ struct digest_info {
        u16 hash_alg;
        u32 hash_mask;
        u16 hash_len;
+       bool supported;
 };
 
 /* Algorithm Registry */
@@ -278,38 +279,50 @@ struct digest_info {
 #define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010
 
 static const struct digest_info hash_algo_list[] = {
-#if IS_ENABLED(CONFIG_SHA1)
        {
                "sha1",
                TPM2_ALG_SHA1,
                TCG2_BOOT_HASH_ALG_SHA1,
                TPM2_SHA1_DIGEST_SIZE,
-       },
+#if IS_ENABLED(CONFIG_SHA1)
+               true,
+#else
+               false,
 #endif
-#if IS_ENABLED(CONFIG_SHA256)
+       },
        {
                "sha256",
                TPM2_ALG_SHA256,
                TCG2_BOOT_HASH_ALG_SHA256,
                TPM2_SHA256_DIGEST_SIZE,
-       },
+#if IS_ENABLED(CONFIG_SHA256)
+               true,
+#else
+               false,
 #endif
-#if IS_ENABLED(CONFIG_SHA384)
+       },
        {
                "sha384",
                TPM2_ALG_SHA384,
                TCG2_BOOT_HASH_ALG_SHA384,
                TPM2_SHA384_DIGEST_SIZE,
-       },
+#if IS_ENABLED(CONFIG_SHA384)
+               true,
+#else
+               false,
 #endif
-#if IS_ENABLED(CONFIG_SHA512)
+       },
        {
                "sha512",
                TPM2_ALG_SHA512,
                TCG2_BOOT_HASH_ALG_SHA512,
                TPM2_SHA512_DIGEST_SIZE,
-       },
+#if IS_ENABLED(CONFIG_SHA512)
+               true,
+#else
+               false,
 #endif
+       },
 };
 
 /* NV index attributes */
@@ -704,6 +717,14 @@ enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);
  */
 const char *tpm2_algorithm_name(enum tpm2_algorithms);
 
+/**
+ * tpm2_algorithm_supported() -  Check if the algorithm supported by U-Boot
+ *
+ * @algorithm_id: algorithm defined in enum tpm2_algorithms
+ * Return: true if supported, otherwise false
+ */
+bool tpm2_algorithm_supported(enum tpm2_algorithms algo);
+
 /**
  * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id
  *
index 0edb0aa90c9f0b6d6b825695fd17c354976a688d..96c164f2a542a98b92499a8a006d7c1a54c525a5 100644 (file)
@@ -884,6 +884,18 @@ const char *tpm2_algorithm_name(enum tpm2_algorithms algo)
        return "";
 }
 
+bool tpm2_algorithm_supported(enum tpm2_algorithms algo)
+{
+       size_t i;
+
+       for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
+               if (hash_algo_list[i].hash_alg == algo)
+                       return hash_algo_list[i].supported;
+       }
+
+       return false;
+}
+
 u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo)
 {
        size_t i;
@@ -908,7 +920,7 @@ bool tpm2_check_active_banks(struct udevice *dev)
 
        for (i = 0; i < pcrs.count; i++) {
                if (tpm2_is_active_bank(&pcrs.selection[i]) &&
-                   !tpm2_algorithm_to_len(pcrs.selection[i].hash))
+                   !tpm2_algorithm_supported(pcrs.selection[i].hash))
                        return false;
        }
 
index 0e267ff0a7df5434d1e7d57dd00e7122a799a239..99671804e3b076a30b32b11453f7c7aaca1ed1b9 100644 (file)
@@ -36,16 +36,17 @@ int tcg2_get_pcr_info(struct udevice *dev, u32 *supported_bank, u32 *active_bank
                return ret;
 
        for (i = 0; i < pcrs.count; i++) {
-               u32 hash_mask = tcg2_algorithm_to_mask(pcrs.selection[i].hash);
+               struct tpms_pcr_selection *sel = &pcrs.selection[i];
+               u32 hash_mask = tcg2_algorithm_to_mask(sel->hash);
 
-               if (hash_mask) {
+               if (tpm2_algorithm_supported(sel->hash))
                        *supported_bank |= hash_mask;
-                       if (tpm2_is_active_bank(&pcrs.selection[i]))
-                               *active_bank |= hash_mask;
-               } else {
-                       printf("%s: unknown algorithm %x\n", __func__,
-                              pcrs.selection[i].hash);
-               }
+               else
+                       log_warning("%s: unknown algorithm %x\n", __func__,
+                                   sel->hash);
+
+               if (tpm2_is_active_bank(sel))
+                       *active_bank |= hash_mask;
        }
 
        *bank_num = pcrs.count;
This page took 0.034496 seconds and 4 git commands to generate.