Skip to content

Commit ceb07ec

Browse files
committed
Fixed encryption alignment and signing tool IV
1 parent c9f8f6a commit ceb07ec

4 files changed

Lines changed: 8 additions & 11 deletions

File tree

src/libwolfboot.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
631631
if (ext_flash_read(row_address, block, ENCRYPT_BLOCK_SIZE) != ENCRYPT_BLOCK_SIZE)
632632
return -1;
633633
XMEMCPY(block + row_offset, data, step);
634+
iv[0] = row_number;
635+
wc_Chacha_SetIV(&chacha, (byte *)iv, ENCRYPT_BLOCK_SIZE);
634636
wc_Chacha_Process(&chacha, enc_block, block, ENCRYPT_BLOCK_SIZE);
635637
ext_flash_write(row_address, enc_block, ENCRYPT_BLOCK_SIZE);
636638
address += step;
@@ -690,6 +692,8 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
690692
int step = ENCRYPT_BLOCK_SIZE - row_offset;
691693
if (ext_flash_read(row_address, block, ENCRYPT_BLOCK_SIZE) != ENCRYPT_BLOCK_SIZE)
692694
return -1;
695+
iv[0] = row_number;
696+
wc_Chacha_SetIV(&chacha, (byte *)iv, ENCRYPT_BLOCK_SIZE);
693697
wc_Chacha_Process(&chacha, dec_block, block, ENCRYPT_BLOCK_SIZE);
694698
XMEMCPY(data, dec_block + row_offset, step);
695699
address += step;

src/update_flash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void RAMFUNCTION wolfBoot_self_update(struct wolfBoot_image *src)
6060
while (pos < src->fw_size) {
6161
uint8_t buffer[FLASHBUFFER_SIZE];
6262
if (src_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
63-
ext_flash_read((uintptr_t)(src->hdr) + src_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
63+
ext_flash_check_read((uintptr_t)(src->hdr) + src_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
6464
hal_flash_write(pos + (uint32_t)&_start_text, buffer, FLASHBUFFER_SIZE);
6565
}
6666
pos += FLASHBUFFER_SIZE;
@@ -129,7 +129,7 @@ static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_imag
129129
wb_flash_erase(dst, dst_sector_offset, WOLFBOOT_SECTOR_SIZE);
130130
while (pos < WOLFBOOT_SECTOR_SIZE) {
131131
if (src_sector_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {
132-
ext_flash_read((uintptr_t)(src->hdr) + src_sector_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
132+
ext_flash_check_read((uintptr_t)(src->hdr) + src_sector_offset + pos, (void *)buffer, FLASHBUFFER_SIZE);
133133
wb_flash_write(dst, dst_sector_offset + pos, buffer, FLASHBUFFER_SIZE);
134134
}
135135
pos += FLASHBUFFER_SIZE;

test-app/app_stm32wb.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ void main(void) {
4747
if ((version == 1) && (updv != 8)) {
4848
uint32_t sz;
4949
boot_led_off();
50-
ext_flash_erase(WOLFBOOT_PARTITION_UPDATE_ADDRESS, WOLFBOOT_PARTITION_SIZE);
51-
while (l < firmware_update_len) {
52-
sz = firmware_update_len - l;
53-
if (sz > WOLFBOOT_SECTOR_SIZE)
54-
sz = WOLFBOOT_SECTOR_SIZE;
55-
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + l, firmware_update + l, sz);
56-
l += sz;
57-
}
5850
#if EXT_ENCRYPTED
5951
wolfBoot_set_encrypt_key((uint8_t *)enc_key, 32);
6052
#endif

tools/keytools/sign.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@
393393
enc_outfile = open(encrypted_output_image_file, 'wb')
394394
cha = ciphers.ChaCha(key, 32)
395395
while(True):
396-
cha.set_iv(off)
396+
iv = struct.pack('<LLLL', off, 0, 0, 0)
397+
cha.set_iv(iv)
397398
buf = outfile.read(16)
398399
if len(buf) == 0:
399400
break

0 commit comments

Comments
 (0)