Skip to content

Commit fb4dd67

Browse files
committed
Make pubkey lookup actually CT by not returning at the match
1 parent bad3f6e commit fb4dd67

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/image.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,13 +2415,14 @@ static int keyslot_CT_hint_matches(const uint8_t *expected,
24152415
int keyslot_id_by_sha(const uint8_t *hint)
24162416
{
24172417
int id;
2418+
int match_id = -1;
24182419

24192420
for (id = 0; id < keystore_num_pubkeys(); id++) {
24202421
key_hash(id, digest);
2421-
if (keyslot_CT_hint_matches(digest, hint)) {
2422-
return id;
2422+
if ((match_id < 0) && keyslot_CT_hint_matches(digest, hint)) {
2423+
match_id = id;
24232424
}
24242425
}
2425-
return -1;
2426+
return match_id;
24262427
}
24272428
#endif /* !WOLFBOOT_NO_SIGN && !WOLFBOOT_RENESAS_SCEPROTECT */

tools/unit-tests/unit-image.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,19 @@ START_TEST(test_verify_signature)
384384
ck_assert_int_eq(verify_called, 1);
385385
}
386386
END_TEST
387+
388+
START_TEST(test_keyslot_id_by_sha_scans_all_slots)
389+
{
390+
int id;
391+
392+
unit_keystore_reset_counters();
393+
id = keyslot_id_by_sha(pubkey_digest);
394+
395+
ck_assert_int_eq(id, 0);
396+
ck_assert_int_eq(unit_keystore_get_buffer_calls(), keystore_num_pubkeys());
397+
ck_assert_int_eq(unit_keystore_get_size_calls(), keystore_num_pubkeys());
398+
}
399+
END_TEST
387400
#endif
388401

389402
#if defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_RSA3072) || \
@@ -745,6 +758,7 @@ Suite *wolfboot_suite(void)
745758
TCase* tcase_verify_signature = tcase_create("verify_signature");
746759
tcase_set_timeout(tcase_verify_signature, 20);
747760
tcase_add_test(tcase_verify_signature, test_verify_signature);
761+
tcase_add_test(tcase_verify_signature, test_keyslot_id_by_sha_scans_all_slots);
748762
suite_add_tcase(s, tcase_verify_signature);
749763
#endif
750764

tools/unit-tests/unit-keystore.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@
9999
#endif
100100
#endif
101101

102-
#define NUM_PUBKEYS 1
102+
#define NUM_PUBKEYS 3
103+
104+
static int keystore_get_buffer_calls;
105+
static int keystore_get_size_calls;
106+
103107
const KEYSTORE_SECTION struct keystore_slot PubKeys[NUM_PUBKEYS] = {
104108

105109
/* Key associated to file 'wolfboot_signing_private_key.der' */
@@ -110,6 +114,20 @@ const KEYSTORE_SECTION struct keystore_slot PubKeys[NUM_PUBKEYS] = {
110114
.pubkey_size = UNIT_PUBKEY_SIZE,
111115
.pubkey = UNIT_PUBKEY_INIT,
112116
},
117+
{
118+
.slot_id = 1,
119+
.key_type = UNIT_KEY_TYPE,
120+
.part_id_mask = 0xFFFFFFFF,
121+
.pubkey_size = UNIT_PUBKEY_SIZE,
122+
.pubkey = { 0x00 },
123+
},
124+
{
125+
.slot_id = 2,
126+
.key_type = UNIT_KEY_TYPE,
127+
.part_id_mask = 0xFFFFFFFF,
128+
.pubkey_size = UNIT_PUBKEY_SIZE,
129+
.pubkey = { 0x01 },
130+
},
113131

114132

115133
};
@@ -123,13 +141,15 @@ uint8_t *keystore_get_buffer(int id)
123141
{
124142
if (id >= keystore_num_pubkeys())
125143
return (uint8_t *)0;
144+
keystore_get_buffer_calls++;
126145
return (uint8_t *)PubKeys[id].pubkey;
127146
}
128147

129148
int keystore_get_size(int id)
130149
{
131150
if (id >= keystore_num_pubkeys())
132151
return -1;
152+
keystore_get_size_calls++;
133153
return (int)PubKeys[id].pubkey_size;
134154
}
135155

@@ -145,4 +165,20 @@ uint32_t keystore_get_key_type(int id)
145165
return PubKeys[id].key_type;
146166
}
147167

168+
void unit_keystore_reset_counters(void)
169+
{
170+
keystore_get_buffer_calls = 0;
171+
keystore_get_size_calls = 0;
172+
}
173+
174+
int unit_keystore_get_buffer_calls(void)
175+
{
176+
return keystore_get_buffer_calls;
177+
}
178+
179+
int unit_keystore_get_size_calls(void)
180+
{
181+
return keystore_get_size_calls;
182+
}
183+
148184
#endif /* WOLFBOOT_NO_SIGN */

0 commit comments

Comments
 (0)