Skip to content

Commit 644e70e

Browse files
committed
Add auth type coverage for unit-image
F/2259
1 parent f32c275 commit 644e70e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tools/unit-tests/unit-image.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,23 @@ static void patch_image_type_auth(uint8_t *img, uint32_t img_len)
231231
ptr[1] = (uint8_t)(type >> 8);
232232
}
233233

234+
static void patch_image_type_auth_value(uint8_t *img, uint32_t img_len,
235+
uint16_t auth)
236+
{
237+
uint8_t *ptr = NULL;
238+
uint16_t len;
239+
uint16_t type;
240+
241+
(void)img_len;
242+
len = _find_header(img + IMAGE_HEADER_OFFSET, HDR_IMG_TYPE, &ptr);
243+
ck_assert_int_eq(len, sizeof(uint16_t));
244+
type = (uint16_t)(ptr[0] | (ptr[1] << 8));
245+
type = (uint16_t)((type & ~HDR_IMG_TYPE_AUTH_MASK) |
246+
(auth & HDR_IMG_TYPE_AUTH_MASK));
247+
ptr[0] = (uint8_t)(type & 0xFF);
248+
ptr[1] = (uint8_t)(type >> 8);
249+
}
250+
234251
static void patch_image_type_part(uint8_t *img, uint32_t img_len, uint16_t part)
235252
{
236253
uint8_t *ptr = NULL;
@@ -694,6 +711,29 @@ START_TEST(test_verify_authenticity_bad_siglen)
694711
}
695712
END_TEST
696713

714+
START_TEST(test_verify_authenticity_rejects_mismatched_auth_type)
715+
{
716+
struct wolfBoot_image test_img;
717+
uint8_t buf[sizeof(test_img_v200000000_signed_bin)];
718+
int ret;
719+
720+
memcpy(buf, test_img_v200000000_signed_bin, sizeof(buf));
721+
patch_image_type_auth_value(buf, sizeof(buf), HDR_IMG_TYPE_AUTH_RSA2048);
722+
patch_pubkey_hint(buf, sizeof(buf));
723+
724+
find_header_mocked = 0;
725+
find_header_fail = 0;
726+
hdr_cpy_done = 0;
727+
ext_flash_write(0, buf, sizeof(buf));
728+
729+
memset(&test_img, 0, sizeof(struct wolfBoot_image));
730+
test_img.part = PART_UPDATE;
731+
test_img.signature_ok = 1;
732+
ret = wolfBoot_verify_authenticity(&test_img);
733+
ck_assert_int_eq(ret, -1);
734+
}
735+
END_TEST
736+
697737
START_TEST(test_verify_authenticity_rejects_disallowed_key_mask)
698738
{
699739
struct wolfBoot_image test_img;
@@ -898,6 +938,8 @@ Suite *wolfboot_suite(void)
898938
tcase_set_timeout(tcase_verify_authenticity, 20);
899939
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity);
900940
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity_bad_siglen);
941+
tcase_add_test(tcase_verify_authenticity,
942+
test_verify_authenticity_rejects_mismatched_auth_type);
901943
tcase_add_test(tcase_verify_authenticity,
902944
test_verify_authenticity_rejects_disallowed_key_mask);
903945
tcase_add_test(tcase_verify_authenticity,

0 commit comments

Comments
 (0)