]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
tpm2: add sm3 256 hash support
authorHeiko Schocher <hs@nabladev.com>
Tue, 18 Nov 2025 04:30:41 +0000 (05:30 +0100)
committerTom Rini <trini@konsulko.com>
Thu, 4 Dec 2025 15:38:58 +0000 (09:38 -0600)
add sm3 256 hash support, so TPM2 chips which report
5 pcrs with sm3 hash do not fail with:

  u-boot=> tpm2 autostart
  tpm2_get_pcr_info: too many pcrs: 5
  Error: -90

Signed-off-by: Heiko Schocher <hs@nabladev.com>
cmd/tpm-v2.c
include/tpm-v2.h
lib/efi_loader/efi_tcg2.c
lib/tpm-v2.c
lib/tpm_tcg2.c

index 346e21d27bb41bd20286831d4b5519e5a9f66a28..847b2691581b128fc01251547c64f5355cdd7efd 100644 (file)
@@ -589,6 +589,7 @@ U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
 "        * sha256\n"
 "        * sha384\n"
 "        * sha512\n"
+"        * sm3_256\n"
 "    <on|off> is one of:\n"
 "        * on  - Select all available PCRs associated with the specified\n"
 "                algorithm (bank)\n"
index f3eb2ef5643a7b662215b6db5e9891d06522f998..a776d24d71f14cd2e19656a98e530087627c56d6 100644 (file)
@@ -345,6 +345,18 @@ static const struct digest_info hash_algo_list[] = {
                false,
 #endif
        },
+       {
+               "sm3_256",
+               TPM2_ALG_SM3_256,
+               TCG2_BOOT_HASH_ALG_SM3_256,
+               TPM2_SM3_256_DIGEST_SIZE,
+#if IS_ENABLED(CONFIG_SM3)
+               true,
+#else
+               false,
+#endif
+       },
+
 };
 
 /* NV index attributes */
index 1832eeb5dce70285816f5d36215a0a69bf2bc37f..bdf78897d470a72dd5f40c92f3cec124d2d252af 100644 (file)
@@ -430,6 +430,9 @@ static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
                case TPM2_ALG_SHA512:
                        hash_calculate("sha512", regs->reg, regs->num, hash);
                        break;
+               case TPM2_ALG_SM3_256:
+                       hash_calculate("sm3_256", regs->reg, regs->num, hash);
+                       break;
                default:
                        continue;
                }
index 5b21c57ae421eb393a3190f18812b7d7d098f57d..f443b738f821185ef3891423ef9e739d39ca5b52 100644 (file)
@@ -686,10 +686,10 @@ int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs)
 
        pcrs->count = get_unaligned_be32(response);
        /*
-        * We only support 4 algorithms for now so check against that
+        * check against the supported algorithms in hash_algo_list,
         * instead of TPM2_NUM_PCR_BANKS
         */
-       if (pcrs->count > 4 || pcrs->count < 1) {
+       if (pcrs->count > ARRAY_SIZE(hash_algo_list) || pcrs->count < 1) {
                printf("%s: too many pcrs: %u\n", __func__, pcrs->count);
                return -EMSGSIZE;
        }
index c314b401d0b33158251d8995376776de71bbb4c7..d41228f75a9afd01f1bd310c99c8f3bd6238c351 100644 (file)
@@ -12,6 +12,7 @@
 #include <u-boot/sha1.h>
 #include <u-boot/sha256.h>
 #include <u-boot/sha512.h>
+#include <u-boot/sm3.h>
 #include <version_string.h>
 #include <asm/io.h>
 #include <linux/bitops.h>
@@ -143,6 +144,12 @@ int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,
                        sha512_finish(&ctx_512, final);
                        len = TPM2_SHA512_DIGEST_SIZE;
                        break;
+#endif
+#if IS_ENABLED(CONFIG_SM3)
+               case TPM2_ALG_SM3_256:
+                       sm3_hash(input, length, final);
+                       len = TPM2_SM3_256_DIGEST_SIZE;
+                       break;
 #endif
                default:
                        printf("%s: unsupported algorithm %x\n", __func__,
@@ -319,6 +326,7 @@ static int tcg2_replay_eventlog(struct tcg2_event_log *elog,
                        case TPM2_ALG_SHA256:
                        case TPM2_ALG_SHA384:
                        case TPM2_ALG_SHA512:
+                       case TPM2_ALG_SM3_256:
                                len = tpm2_algorithm_to_len(algo);
                                break;
                        default:
@@ -431,6 +439,7 @@ static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog,
                case TPM2_ALG_SHA256:
                case TPM2_ALG_SHA384:
                case TPM2_ALG_SHA512:
+               case TPM2_ALG_SM3_256:
                        len = get_unaligned_le16(&event->digest_sizes[i].digest_size);
                        if (tpm2_algorithm_to_len(algo) != len) {
                                log_err("EventLog invalid algorithm length\n");