Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/libwolfboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -3336,8 +3336,11 @@ int aes_init(void)

XMEMSET(&aes_enc, 0, sizeof(aes_enc));
XMEMSET(&aes_dec, 0, sizeof(aes_dec));
wc_AesInit(&aes_enc, NULL, WOLFBOOT_DEVID_CRYPT);
wc_AesInit(&aes_dec, NULL, WOLFBOOT_DEVID_CRYPT);
if ((wc_AesInit(&aes_enc, NULL, WOLFBOOT_DEVID_CRYPT) != 0) ||
(wc_AesInit(&aes_dec, NULL, WOLFBOOT_DEVID_CRYPT) != 0)) {
ret = -1;
goto exit;
}

if (!encrypt_key_is_valid(key, ENCRYPT_KEY_SIZE)) {
ret = -1;
Expand Down Expand Up @@ -3369,14 +3372,26 @@ int aes_init(void)

/* AES_ENCRYPTION is used for both directions in CTR
* IV is set later with "wc_AesSetIV" */
wc_AesSetKeyDirect(&aes_enc, key, ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
wc_AesSetKeyDirect(&aes_dec, key, ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
if ((wc_AesSetKeyDirect(&aes_enc, key, ENCRYPT_KEY_SIZE, NULL,
AES_ENCRYPTION) != 0) ||
(wc_AesSetKeyDirect(&aes_dec, key, ENCRYPT_KEY_SIZE, NULL,
AES_ENCRYPTION) != 0)) {
ret = -1;
goto exit;
}

/* Set global IV nonce used in aes_set_iv */
XMEMCPY(encrypt_iv_nonce, stored_nonce, ENCRYPT_NONCE_SIZE);
encrypt_initialized = 1;

exit:
if (ret != 0) {
wc_AesFree(&aes_enc);
wc_AesFree(&aes_dec);
ForceZero(&aes_enc, sizeof(aes_enc));
ForceZero(&aes_dec, sizeof(aes_dec));
encrypt_initialized = 0;
}
#if defined(CUSTOM_ENCRYPT_KEY) && !defined(WOLFBOOT_RENESAS_TSIP)
ForceZero(key, sizeof(key));
ForceZero(stored_nonce, sizeof(stored_nonce));
Expand Down
43 changes: 35 additions & 8 deletions src/update_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ static uint32_t get_decrypted_blob_version(uint8_t *hdr)
*
* @param block_offset Block offset for IV counter (0 = start of image).
*/
static void disk_crypto_set_iv(uint32_t block_offset)
static int disk_crypto_set_iv(uint32_t block_offset)
{
int ret = 0;
#if defined(ENCRYPT_WITH_CHACHA)
wc_Chacha_SetIV(&chacha, disk_encrypt_nonce, block_offset);
ret = wc_Chacha_SetIV(&chacha, disk_encrypt_nonce, block_offset);
#elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256)
/* For AES CTR, we need to construct the IV with the counter.
* The sign tool uses the IV directly without byte-reversal,
Expand All @@ -307,11 +308,12 @@ static void disk_crypto_set_iv(uint32_t block_offset)
iv[14] = (uint8_t)(ctr >> 8);
iv[15] = (uint8_t)(ctr);

wc_AesSetIV(&aes_dec, iv);
ret = wc_AesSetIV(&aes_dec, iv);
/* Scrub the stack copy: the counter bytes are derived from the
* secret disk-encryption nonce (matches aes_set_iv in libwolfboot.c). */
wc_ForceZero(iv, sizeof(iv));
#endif
return ret;
}

/**
Expand All @@ -330,10 +332,12 @@ static int decrypt_header(const uint8_t *src, uint8_t *dst)
uint32_t magic;

/* Reset IV to start of image (block 0) */
disk_crypto_set_iv(0);
if (disk_crypto_set_iv(0) != 0)
return -1;

/* Decrypt header - CTR mode handles counter increment internally */
crypto_decrypt(dst, src, IMAGE_HEADER_SIZE);
if (crypto_decrypt(dst, src, IMAGE_HEADER_SIZE) != 0)
return -1;
Comment thread
dgarske marked this conversation as resolved.
Resolved

magic = *((uint32_t*)dst);
if (magic != WOLFBOOT_MAGIC)
Expand All @@ -347,6 +351,19 @@ static void disk_crypto_clear(void)
wc_ForceZero(disk_encrypt_nonce, sizeof(disk_encrypt_nonce));
}

/* The cipher context holds the expanded key; wipe it before the handoff. */
static void disk_crypto_clear_ctx(void)
{
#if defined(ENCRYPT_WITH_CHACHA)
wc_ForceZero(&chacha, sizeof(chacha));
#elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256)
wc_AesFree(&aes_dec);
wc_AesFree(&aes_enc);
wc_ForceZero(&aes_dec, sizeof(aes_dec));
wc_ForceZero(&aes_enc, sizeof(aes_enc));
#endif
}

#ifdef DISK_GOLDEN_ENCRYPT_KEY
/* Re-key the cipher and the IV nonce for the slot about to be read. Both are
* keyed once up front, which is enough only while every slot shares a key. */
Expand Down Expand Up @@ -1339,9 +1356,15 @@ void RAMFUNCTION wolfBoot_start(void)
wolfBoot_printf("Encrypted disk images require aligned header size\r\n");
wolfBoot_panic();
}
disk_crypto_set_iv(IMAGE_HEADER_SIZE / ENCRYPT_BLOCK_SIZE);
crypto_decrypt((uint8_t*)load_address, (uint8_t*)load_address,
os_image.fw_size);
if ((disk_crypto_set_iv(IMAGE_HEADER_SIZE / ENCRYPT_BLOCK_SIZE)
!= 0) ||
(crypto_decrypt((uint8_t*)load_address,
(uint8_t*)load_address, os_image.fw_size) != 0)) {
wolfBoot_printf("Error decrypting %s\r\n", part_name);
SET_FAILOVER(DISK_FAILOVER_VERIFY);
selected ^= 1;
continue;
}
BENCHMARK_END("done");
}
#endif
Expand Down Expand Up @@ -1681,6 +1704,10 @@ void RAMFUNCTION wolfBoot_start(void)
/* Before hal_prepare_boot(), so a hook still has the MMU and caches as
* wolfBoot set them up. */
wolfBoot_hook_preboot(&os_image);
#endif
#ifdef DISK_ENCRYPT
disk_crypto_clear();
disk_crypto_clear_ctx();
#endif
hal_prepare_boot();

Expand Down
111 changes: 109 additions & 2 deletions tools/unit-tests/unit-update-disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ static int mock_authenticity_saw_not_ext;
static int mock_verify_authenticity_ret;
static int mock_part_mismatch_part;
static int mock_last_read_part;
/* Cipher failures to inject: the header IV (counter 0), the payload IV and
* the payload decryption, each for this many calls */
static int mock_fail_header_iv;
static int mock_fail_payload_iv;
static int mock_fail_payload_decrypt;
static int mock_fail_header_decrypt;
static int mock_ctx_wiped_at_boot;

ChaCha chacha;

Expand Down Expand Up @@ -95,26 +102,49 @@ static void reset_mocks(void)
mock_flash_protect_called = 0;
mock_flash_protect_addr = 0;
mock_flash_protect_len = 0;
mock_fail_header_iv = 0;
mock_fail_payload_iv = 0;
mock_fail_payload_decrypt = 0;
mock_fail_header_decrypt = 0;
mock_ctx_wiped_at_boot = 0;
wolfBoot_panicked = 0;
}

int chacha_init(void)
{
/* Stands in for the expanded key held in the context */
memset(&chacha, 0x5A, sizeof(chacha));
return 0;
}

int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
{
(void)ctx;
(void)inIv;
(void)counter;
if ((counter == 0) && (mock_fail_header_iv > 0)) {
mock_fail_header_iv--;
return -1;
}
if ((counter != 0) && (mock_fail_payload_iv > 0)) {
mock_fail_payload_iv--;
return -1;
}
return 0;
}

int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input, word32 msglen)
{
(void)ctx;
if ((msglen == TEST_PAYLOAD_SIZE) && (mock_fail_payload_decrypt > 0)) {
mock_fail_payload_decrypt--;
return -1;
}
memmove(output, input, msglen);
/* The header still decrypts to a valid one: only the return code fails */
if ((msglen == IMAGE_HEADER_SIZE) && (mock_fail_header_decrypt > 0)) {
mock_fail_header_decrypt--;
return -1;
}
return 0;
}

Expand All @@ -128,7 +158,7 @@ void wc_ForceZero(void* mem, size_t len)

int wolfBoot_initialize_encryption(void)
{
return 0;
return chacha_init();
}

int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce)
Expand Down Expand Up @@ -229,8 +259,15 @@ void hal_prepare_boot(void)

void do_boot(const uint32_t *address)
{
size_t i;

mock_do_boot_called++;
mock_boot_address = address;
mock_ctx_wiped_at_boot = 1;
for (i = 0; i < sizeof(chacha); i++) {
if (((const uint8_t *)&chacha)[i] != 0)
mock_ctx_wiped_at_boot = 0;
}
}

int hal_flash_protect(haladdr_t address, int len)
Expand Down Expand Up @@ -286,6 +323,72 @@ START_TEST(test_update_disk_zeroizes_key_material_before_boot)
for (i = 0; i < ENCRYPT_NONCE_SIZE; i++) {
ck_assert_uint_eq(disk_encrypt_nonce[i], 0);
}
ck_assert_int_eq(mock_ctx_wiped_at_boot, 1);
Comment thread
Copilot marked this conversation as resolved.
Resolved
}
END_TEST

/* Both slots at the same version, so the failover is not a downgrade */
START_TEST(test_update_disk_payload_iv_failure_skips_the_slot)
{
reset_mocks();
build_image(part_b_image, 1, 0xB2);
mock_fail_payload_iv = 1;

wolfBoot_start();

ck_assert_int_eq(wolfBoot_panicked, 0);
ck_assert_int_eq(mock_do_boot_called, 1);
ck_assert_uint_eq(load_buffer[0], 0xB2);
}
END_TEST

START_TEST(test_update_disk_payload_decrypt_failure_skips_the_slot)
{
reset_mocks();
build_image(part_b_image, 1, 0xB2);
mock_fail_payload_decrypt = 1;

wolfBoot_start();

ck_assert_int_eq(wolfBoot_panicked, 0);
ck_assert_int_eq(mock_do_boot_called, 1);
ck_assert_uint_eq(load_buffer[0], 0xB2);
}
END_TEST

START_TEST(test_update_disk_header_decrypt_failure_skips_the_slot)
{
reset_mocks();
build_image(part_b_image, 1, 0xB2);
mock_fail_header_decrypt = 1;

wolfBoot_start();

ck_assert_int_eq(wolfBoot_panicked, 0);
ck_assert_int_eq(mock_do_boot_called, 1);
ck_assert_uint_eq(load_buffer[0], 0xB2);
}
END_TEST

START_TEST(test_update_disk_decrypt_failures_fail_closed)
{
reset_mocks();
mock_fail_payload_decrypt = 1000;
wolfBoot_start();
ck_assert_int_eq(mock_do_boot_called, 0);
ck_assert_int_ne(wolfBoot_panicked, 0);

reset_mocks();
mock_fail_header_iv = 1000;
wolfBoot_start();
ck_assert_int_eq(mock_do_boot_called, 0);
ck_assert_int_ne(wolfBoot_panicked, 0);

reset_mocks();
mock_fail_header_decrypt = 1000;
wolfBoot_start();
ck_assert_int_eq(mock_do_boot_called, 0);
ck_assert_int_ne(wolfBoot_panicked, 0);
}
END_TEST

Expand Down Expand Up @@ -488,6 +591,10 @@ Suite *wolfboot_suite(void)

tcase_add_test(tc, test_update_disk_zeroizes_key_material_on_panic);
tcase_add_test(tc, test_update_disk_zeroizes_key_material_before_boot);
tcase_add_test(tc, test_update_disk_payload_iv_failure_skips_the_slot);
tcase_add_test(tc, test_update_disk_payload_decrypt_failure_skips_the_slot);
tcase_add_test(tc, test_update_disk_header_decrypt_failure_skips_the_slot);
tcase_add_test(tc, test_update_disk_decrypt_failures_fail_closed);
tcase_add_test(tc, test_update_disk_prefers_primary_partition_when_versions_equal);
tcase_add_test(tc, test_update_disk_boots_from_A_when_B_is_blank);
tcase_add_test(tc, test_update_disk_boots_from_B_when_A_is_blank);
Expand Down
Loading