From 69c6cb410fdb764b03ada71cb0069208629a5f6d Mon Sep 17 00:00:00 2001 From: David Garske Date: Sat, 19 Sep 2026 10:38:23 -0700 Subject: [PATCH 1/2] tpm: pair the PCR bank algorithm with its digest size --- include/tpm.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/include/tpm.h b/include/tpm.h index 7802f734dd..e83abbf90d 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -40,10 +40,31 @@ extern WOLFTPM2_KEY wolftpm_srk; #ifndef WOLFBOOT_TPM_SEAL_NV_BASE #define WOLFBOOT_TPM_SEAL_NV_BASE 0x01400300 #endif +/* The PCR bank algorithm and its digest size must always agree. They used to + * be defined together under a single #ifndef on the algorithm, which meant + * overriding only WOLFBOOT_TPM_PCR_ALG left WOLFBOOT_TPM_PCR_DIG_SZ undefined + * - and an undefined macro is 0 to the preprocessor, so the size checks + * downstream silently changed meaning. Select the pair with one switch, and + * refuse a half-specified override. + * + * Note the algorithm cannot be tested with #if: TPM_ALG_SHA256 and friends are + * enum constants, not macros, so every #if comparison against them evaluates + * 0 == 0 and is always true. */ +#if defined(WOLFBOOT_TPM_PCR_ALG) != defined(WOLFBOOT_TPM_PCR_DIG_SZ) + #error "Define both WOLFBOOT_TPM_PCR_ALG and WOLFBOOT_TPM_PCR_DIG_SZ, or neither" +#endif #ifndef WOLFBOOT_TPM_PCR_ALG - /* Prefer SHA2-256 for PCR's, and all TPM 2.0 devices support it */ - #define WOLFBOOT_TPM_PCR_ALG TPM_ALG_SHA256 - #define WOLFBOOT_TPM_PCR_DIG_SZ 32 + #ifdef WOLFBOOT_TPM_PCR_SHA384 + /* SHA2-384 bank. Present on parts like the Infineon SLB9672, absent on + * the SLB9670 - selecting it on a part with no such bank makes every + * extend fail, which measured boot treats as fatal. */ + #define WOLFBOOT_TPM_PCR_ALG TPM_ALG_SHA384 + #define WOLFBOOT_TPM_PCR_DIG_SZ 48 + #else + /* Prefer SHA2-256 for PCR's, and all TPM 2.0 devices support it */ + #define WOLFBOOT_TPM_PCR_ALG TPM_ALG_SHA256 + #define WOLFBOOT_TPM_PCR_DIG_SZ 32 + #endif #endif #define WOLFBOOT_MAX_SEAL_SZ MAX_SYM_DATA From a19f008e111646f34eab35f3684b9f08e8101c87 Mon Sep 17 00:00:00 2001 From: David Garske Date: Sat, 19 Sep 2026 13:22:46 -0700 Subject: [PATCH 2/2] tpm: reject a measured-boot PCR bank wider than the image hash --- include/tpm.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index e83abbf90d..96847a5013 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -67,6 +67,17 @@ extern WOLFTPM2_KEY wolftpm_srk; #endif #endif +/* The measured-boot extend path hands wolfBoot_tpm2_extend() the image's own + * hash buffer and the TPM reads WOLFBOOT_TPM_PCR_DIG_SZ bytes from it, so an + * image hash narrower than the selected PCR bank would read past the buffer + * and extend the PCR with adjacent memory. Require the image hash to be at + * least as wide as the bank. */ +#if defined(WOLFBOOT_MEASURED_BOOT) && \ + (WOLFBOOT_SHA_DIGEST_SIZE < WOLFBOOT_TPM_PCR_DIG_SZ) + #error "measured boot: image hash is narrower than the TPM PCR bank; " \ + "widen the image HASH or select a smaller WOLFBOOT_TPM_PCR_ALG" +#endif + #define WOLFBOOT_MAX_SEAL_SZ MAX_SYM_DATA /* API's that are callable from non-secure code */