From 1528bc8d992aba3d856223eca412c19edd3ef064 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 8 Apr 2026 16:46:26 +0200 Subject: [PATCH] Use constant-time TPM secret checks F/2248 --- include/tpm.h | 4 ++++ src/tpm.c | 2 +- src/update_flash.c | 3 ++- src/x86/ahci.c | 3 ++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/tpm.h b/include/tpm.h index fb788914..50b68adf 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -79,6 +79,10 @@ int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey, TPM_ALG_ID* pAlg); #endif +#if defined(WOLFBOOT_TPM_KEYSTORE) || defined(WOLFBOOT_TPM_SEAL) +int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b, uint32_t len); +#endif + #ifdef WOLFBOOT_TPM_KEYSTORE int wolfBoot_check_rot(int key_slot, uint8_t* pubkey_hint); #endif diff --git a/src/tpm.c b/src/tpm.c index 682ab51c..23b3e05d 100644 --- a/src/tpm.c +++ b/src/tpm.c @@ -44,7 +44,7 @@ WOLFTPM2_KEY wolftpm_srk; #endif #if defined(WOLFBOOT_TPM_SEAL) || defined(WOLFBOOT_TPM_KEYSTORE) -static int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b, +int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b, uint32_t len) { uint32_t i; diff --git a/src/update_flash.c b/src/update_flash.c index 5a6920cd..4b2c383c 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -1265,7 +1265,8 @@ int wolfBoot_unlock_disk(void) secretCheck, &secretCheckSz); if (ret == 0) { if (secretSz != secretCheckSz || - memcmp(secret, secretCheck, secretSz) != 0) + wolfBoot_constant_compare(secret, secretCheck, + (uint32_t)secretSz) != 0) { wolfBoot_printf("secret check mismatch!\n"); ret = -1; diff --git a/src/x86/ahci.c b/src/x86/ahci.c index ff5a7a42..0c691797 100644 --- a/src/x86/ahci.c +++ b/src/x86/ahci.c @@ -296,7 +296,8 @@ static int sata_create_and_seal_unlock_secret(const uint8_t *pubkey_hint, secret_check, &secret_check_sz); if (ret == 0) { if (*secret_size != secret_check_sz || - memcmp(secret, secret_check, secret_check_sz) != 0) + wolfBoot_constant_compare(secret, secret_check, + (uint32_t)secret_check_sz) != 0) { wolfBoot_printf("secret check mismatch!\n"); ret = -1;