Skip to content

Commit ee9f21c

Browse files
committed
update: propagate encrypt key read errors
1 parent 491595b commit ee9f21c

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/update_flash.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,16 @@ static int RAMFUNCTION wolfBoot_swap_and_final_erase(int resume)
486486
wolfBoot_printf("In function wolfBoot_final_swap: swapDone = %d\n", swapDone);
487487
if (swapDone == 0) {
488488
/* For encrypted images: Get the encryption key and IV */
489-
wolfBoot_get_encrypt_key((uint8_t*)tmpBuffer,
490-
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE/sizeof(uint32_t)]);
489+
ret = wolfBoot_get_encrypt_key((uint8_t*)tmpBuffer,
490+
(uint8_t*)&tmpBuffer[ENCRYPT_KEY_SIZE / sizeof(uint32_t)]);
491+
if (ret != 0) {
492+
#ifdef EXT_FLASH
493+
ext_flash_lock();
494+
#endif
495+
hal_flash_lock();
496+
wolfBoot_zeroize(tmpBuffer, sizeof(tmpBuffer));
497+
return ret;
498+
}
491499
/* Set the magic trailer in the buffer and write it to the staging sector */
492500
tmpBuffer[TRAILER_OFFSET_WORDS] = WOLFBOOT_MAGIC_TRAIL;
493501

tools/unit-tests/unit-update-flash.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ static int add_payload_type(uint8_t part, uint32_t version, uint32_t size,
5656
uint16_t img_type);
5757

5858
#ifdef CUSTOM_ENCRYPT_KEY
59+
static int mock_get_encrypt_key_ret = 0;
5960
static int mock_set_encrypt_key_ret = 0;
6061
static int mock_set_encrypt_key_calls = 0;
6162

6263
int wolfBoot_get_encrypt_key(uint8_t *k, uint8_t *nonce)
6364
{
6465
int i;
66+
if (mock_get_encrypt_key_ret != 0)
67+
return mock_get_encrypt_key_ret;
6568
for (i = 0; i < ENCRYPT_KEY_SIZE; i++) {
6669
k[i] = (uint8_t)(i + 1);
6770
}
@@ -138,6 +141,7 @@ static void reset_mock_stats(void)
138141
{
139142
wolfBoot_staged_ok = 0;
140143
#ifdef CUSTOM_ENCRYPT_KEY
144+
mock_get_encrypt_key_ret = 0;
141145
mock_set_encrypt_key_ret = 0;
142146
mock_set_encrypt_key_calls = 0;
143147
#endif
@@ -560,6 +564,26 @@ START_TEST (test_final_swap_propagates_encrypt_key_persist_failure)
560564
cleanup_flash();
561565
}
562566
END_TEST
567+
568+
START_TEST (test_final_swap_propagates_encrypt_key_read_failure)
569+
{
570+
int ret;
571+
572+
reset_mock_stats();
573+
prepare_flash();
574+
575+
add_payload(PART_BOOT, 1, TEST_SIZE_SMALL);
576+
add_payload(PART_UPDATE, 2, TEST_SIZE_SMALL);
577+
578+
mock_get_encrypt_key_ret = -7;
579+
ret = wolfBoot_swap_and_final_erase(0);
580+
581+
ck_assert_int_eq(ret, -7);
582+
ck_assert_int_eq(mock_set_encrypt_key_calls, 0);
583+
584+
cleanup_flash();
585+
}
586+
END_TEST
563587
#endif
564588

565589
START_TEST (test_sunnyday_noupdate)
@@ -1013,6 +1037,7 @@ Suite *wolfboot_suite(void)
10131037
#ifdef UNIT_TEST_FALLBACK_ONLY
10141038
#ifdef EXT_ENCRYPTED
10151039
tcase_add_test(fallback_verify, test_fallback_image_verification_rejects_corruption);
1040+
tcase_add_test(fallback_verify, test_final_swap_propagates_encrypt_key_read_failure);
10161041
tcase_add_test(fallback_verify, test_final_swap_propagates_encrypt_key_persist_failure);
10171042
suite_add_tcase(s, fallback_verify);
10181043
#endif
@@ -1050,6 +1075,7 @@ Suite *wolfboot_suite(void)
10501075
#endif
10511076
#ifdef EXT_ENCRYPTED
10521077
tcase_add_test(fallback_verify, test_fallback_image_verification_rejects_corruption);
1078+
tcase_add_test(fallback_verify, test_final_swap_propagates_encrypt_key_read_failure);
10531079
tcase_add_test(fallback_verify, test_final_swap_propagates_encrypt_key_persist_failure);
10541080
#endif
10551081

0 commit comments

Comments
 (0)