Skip to content

Commit 5fc7321

Browse files
authored
Merge pull request #72 from wolfSSL/encrypted-update-fix
Fixed bug in encrypted update, removed swap_counter.
2 parents d31bfc2 + b2beb49 commit 5fc7321

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/libwolfboot.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "wolfboot/wolfboot.h"
2727
#include "image.h"
2828

29-
#if defined(EXT_ENCRYPTED)
29+
#if defined(EXT_ENCRYPTED)
3030
#if defined(__WOLFBOOT)
3131
#include "encrypt.h"
3232
#else
@@ -572,23 +572,21 @@ static int chacha_init(void)
572572

573573
static inline uint8_t part_address(uintptr_t a)
574574
{
575-
if ( 1 &&
575+
if ( 1 &&
576576
#if WOLFBOOT_PARTITION_UPDATE_ADDRESS != 0
577-
(a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) &&
577+
(a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) &&
578578
#endif
579579
(a <= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))
580580
return PART_UPDATE;
581-
if ( 1 &&
581+
if ( 1 &&
582582
#if WOLFBOOT_PARTITION_SWAP_ADDRESS != 0
583-
(a >= WOLFBOOT_PARTITION_SWAP_ADDRESS) &&
583+
(a >= WOLFBOOT_PARTITION_SWAP_ADDRESS) &&
584584
#endif
585585
(a <= WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE))
586586
return PART_SWAP;
587587
return PART_NONE;
588588
}
589589

590-
static uint32_t swap_counter = 0;
591-
592590
int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
593591
{
594592
uint32_t iv_counter;
@@ -612,7 +610,7 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
612610
part = part_address(address);
613611
switch(part) {
614612
case PART_UPDATE:
615-
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
613+
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
616614
/* Do not encrypt last sector */
617615
if (iv_counter == (WOLFBOOT_PARTITION_SIZE - 1) / ENCRYPT_BLOCK_SIZE) {
618616
return ext_flash_write(address, data, len);
@@ -621,8 +619,8 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
621619
case PART_SWAP:
622620
{
623621
uint32_t row_number;
624-
row_number = (address - WOLFBOOT_PARTITION_SWAP_ADDRESS) / ENCRYPT_BLOCK_SIZE;
625-
iv_counter = ((swap_counter++) << 8) + row_number;
622+
row_number = (address - WOLFBOOT_PARTITION_SWAP_ADDRESS) / ENCRYPT_BLOCK_SIZE;
623+
iv_counter = row_number;
626624
break;
627625
}
628626
default:
@@ -658,7 +656,7 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
658656
int sz = len;
659657
uint32_t row_address = address, row_offset;
660658
int i;
661-
659+
662660
row_offset = address & (ENCRYPT_BLOCK_SIZE - 1);
663661
if (row_offset != 0) {
664662
row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1);
@@ -673,7 +671,7 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
673671
part = part_address(row_address);
674672
switch(part) {
675673
case PART_UPDATE:
676-
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
674+
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
677675
/* Do not decrypt last sector */
678676
if (iv_counter == (WOLFBOOT_PARTITION_SIZE - 1) / ENCRYPT_BLOCK_SIZE) {
679677
return ext_flash_read(address, data, len);
@@ -682,8 +680,8 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
682680
case PART_SWAP:
683681
{
684682
uint32_t row_number;
685-
row_number = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
686-
iv_counter = (swap_counter << 8) + row_number;
683+
row_number = (address - WOLFBOOT_PARTITION_SWAP_ADDRESS) / ENCRYPT_BLOCK_SIZE;
684+
iv_counter = row_number;
687685
break;
688686
}
689687
default:

tools/uart-flash-server/ufserver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ uint8_t *mmap_firmware(const char *fname)
188188
perror("open");
189189
return (void *)-1;
190190
}
191-
if (st.st_size < FIRMWARE_PARTITION_SIZE) {
191+
if (st.st_size <= FIRMWARE_PARTITION_SIZE) {
192192
uint8_t pad = 0xFF;
193193
int i;
194194
const char update_flags[] = "pBOOT";

0 commit comments

Comments
 (0)