Skip to content

Commit 2552a28

Browse files
committed
Use constant-time compare for key lookup
F/231
1 parent 73f03f2 commit 2552a28

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/image.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,21 @@ uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset,
23722372

23732373
#if !defined(WOLFBOOT_NO_SIGN) && !defined(WOLFBOOT_RENESAS_SCEPROTECT)
23742374

2375+
/* Compare fixed-size key hints without early exit to avoid leaking hash prefix
2376+
* matches through lookup timing. */
2377+
static int keyslot_CT_hint_matches(const uint8_t *expected,
2378+
const uint8_t *actual)
2379+
{
2380+
uint8_t diff = 0;
2381+
uint32_t i;
2382+
2383+
for (i = 0; i < WOLFBOOT_SHA_DIGEST_SIZE; i++) {
2384+
diff |= expected[i] ^ actual[i];
2385+
}
2386+
2387+
return diff == 0;
2388+
}
2389+
23752390
/**
23762391
* @brief Get the key slot ID by SHA hash.
23772392
*
@@ -2387,7 +2402,7 @@ int keyslot_id_by_sha(const uint8_t *hint)
23872402

23882403
for (id = 0; id < keystore_num_pubkeys(); id++) {
23892404
key_hash(id, digest);
2390-
if (memcmp(digest, hint, WOLFBOOT_SHA_DIGEST_SIZE) == 0) {
2405+
if (keyslot_CT_hint_matches(digest, hint)) {
23912406
return id;
23922407
}
23932408
}

0 commit comments

Comments
 (0)