Skip to content

Commit 845eef7

Browse files
committed
Encryption: address alignment fixes, stm32wb fixes
1 parent 53bf4d0 commit 845eef7

6 files changed

Lines changed: 122 additions & 44 deletions

File tree

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ ifeq ($(WOLFTPM),1)
184184
OBJS += lib/wolfTPM/src/tpm2.o \
185185
lib/wolfTPM/src/tpm2_packet.o \
186186
lib/wolfTPM/src/tpm2_tis.o \
187-
lib/wolfTPM/src/tpm2_wrap.o \
188-
hal/spi/spi_drv_$(SPI_TARGET).o
187+
lib/wolfTPM/src/tpm2_wrap.o
189188
CFLAGS+=-DWOLFBOOT_TPM -DSIZEOF_LONG=4 -Ilib/wolfTPM \
190189
-DMAX_COMMAND_SIZE=1024 -DMAX_RESPONSE_SIZE=1024 -DWOLFTPM2_MAX_BUFFER=1500 \
191190
-DMAX_SESSION_NUM=1 -DMAX_DIGEST_BUFFER=973 \
@@ -194,6 +193,9 @@ ifeq ($(WOLFTPM),1)
194193
CFLAGS+=-DWOLFTPM_SLB9670
195194
# Use TPM for hashing (slow)
196195
#CFLAGS+=-DWOLFBOOT_HASH_TPM
196+
ifneq ($(SPI_FLASH),1)
197+
WOLFCRYPT_OBJS+=hal/spi/spi_drv_$(SPI_TARGET).o
198+
endif
197199
endif
198200
OBJS+=$(WOLFCRYPT_OBJS)
199201

src/libwolfboot.c

Lines changed: 94 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444

4545
#define NVM_CACHE_SIZE WOLFBOOT_SECTOR_SIZE
4646

47-
uint32_t ext_cache;
47+
static uint32_t ext_cache;
4848
static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
4949

50+
5051
#ifndef TRAILER_SKIP
5152
# define TRAILER_SKIP 0
5253
#endif
@@ -56,11 +57,8 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
5657
#ifdef NVM_FLASH_WRITEONCE
5758
#include <stddef.h>
5859
#include <string.h>
59-
#define XMEMSET memset
60-
#define XMEMCPY memcpy
61-
#define XMEMCMP memcmp
60+
static uint8_t NVM_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(16)));
6261

63-
static uint8_t NVM_CACHE[NVM_CACHE_SIZE];
6462
int RAMFUNCTION hal_trailer_write(uint32_t addr, uint8_t val) {
6563
uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1));
6664
uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1);
@@ -497,28 +495,30 @@ int wolfBoot_fallback_is_possible(void)
497495
#error option EXT_ENCRYPTED requires EXT_FLASH
498496
#endif
499497

500-
#define ENCRYPT_TMP_SECRET_OFFSET (((WOLFBOOT_SECTOR_SIZE - (sizeof(uint32_t) + (2 + WOLFBOOT_SECTOR_SIZE) / (WOLFBOOT_PARTITION_SIZE * 8)) + ENCRYPT_KEY_SIZE)) / ENCRYPT_KEY_SIZE * ENCRYPT_KEY_SIZE)
498+
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP + (sizeof(uint32_t) + 1 + ((1 + WOLFBOOT_PARTITION_SIZE) / (WOLFBOOT_SECTOR_SIZE * 8)) + ENCRYPT_KEY_SIZE)))
501499

502500

503501
#ifdef NVM_FLASH_WRITEONCE
504-
#define KEY_CACHE NVM_CACHE
502+
#define ENCRYPT_CACHE NVM_CACHE
505503
#else
506-
static uint8_t KEY_CACHE[NVM_CACHE_SIZE];
504+
static uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(32)));
507505
#endif
508506

509507

510508
static int RAMFUNCTION hal_set_key(const uint8_t *k)
511509
{
512-
uint32_t addr = ENCRYPT_TMP_SECRET_OFFSET;
510+
uint32_t addr = ENCRYPT_TMP_SECRET_OFFSET + WOLFBOOT_PARTITION_BOOT_ADDRESS;
513511
uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1));
514512
uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1);
515513
int ret = 0;
516-
XMEMCPY(KEY_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
514+
hal_flash_unlock();
515+
XMEMCPY(ENCRYPT_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
517516
ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE);
518517
if (ret != 0)
519518
return ret;
520-
XMEMCPY(KEY_CACHE + addr_off, k, ENCRYPT_KEY_SIZE);
521-
ret = hal_flash_write(addr_align, KEY_CACHE, WOLFBOOT_SECTOR_SIZE);
519+
XMEMCPY(ENCRYPT_CACHE + addr_off, k, ENCRYPT_KEY_SIZE);
520+
ret = hal_flash_write(addr_align, ENCRYPT_CACHE, WOLFBOOT_SECTOR_SIZE);
521+
hal_flash_lock();
522522
return ret;
523523
}
524524

@@ -566,12 +566,23 @@ static int chacha_init(void)
566566
}
567567

568568

569-
#define PART_ADDRESS(a) ((a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) && \
570-
(a <= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))?\
571-
(PART_UPDATE):\
572-
((a >= WOLFBOOT_PARTITION_SWAP_ADDRESS && \
573-
(a <= WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE))?(PART_SWAP):\
574-
PART_NONE)
569+
static inline uint8_t part_address(uintptr_t a)
570+
{
571+
572+
if ( 1 &&
573+
#if WOLFBOOT_PARTITION_UPDATE_ADDRESS != 0
574+
(a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) &&
575+
#endif
576+
(a <= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))
577+
return PART_UPDATE;
578+
if ( 1 &&
579+
#if WOLFBOOT_PARTITION_SWAP_ADDRESS != 0
580+
(a >= WOLFBOOT_PARTITION_SWAP_ADDRESS) &&
581+
#endif
582+
(a <= WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE))
583+
return PART_SWAP;
584+
return PART_NONE;
585+
}
575586

576587
static uint32_t swap_counter = 0;
577588

@@ -580,65 +591,109 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
580591
uint32_t iv[ENCRYPT_BLOCK_SIZE / sizeof(uint32_t)];
581592
uint8_t block[ENCRYPT_BLOCK_SIZE];
582593
uint8_t part;
583-
uint32_t offset;
594+
uint32_t row_number;
595+
int sz = len;
596+
uint32_t row_address = address, row_offset;
584597
int i;
598+
uint8_t enc_block[ENCRYPT_BLOCK_SIZE];
599+
row_offset = address & (ENCRYPT_BLOCK_SIZE - 1);
600+
if (row_offset != 0) {
601+
row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1);
602+
sz += ENCRYPT_BLOCK_SIZE - row_offset;
603+
}
604+
if (sz < ENCRYPT_BLOCK_SIZE) {
605+
sz = ENCRYPT_BLOCK_SIZE;
606+
}
585607
if (!chacha_initialized)
586608
if (chacha_init() < 0)
587609
return -1;
588-
part = PART_ADDRESS(address);
610+
XMEMSET(iv, 0, ENCRYPT_BLOCK_SIZE);
611+
part = part_address(address);
589612
switch(part) {
590613
case PART_UPDATE:
591-
offset = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
614+
row_number = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
592615
break;
593616
case PART_SWAP:
594-
offset = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
617+
row_number = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
618+
iv[1] = swap_counter++;
595619
break;
596620
default:
597621
return -1;
598622
}
599-
XMEMSET(iv, 0, ENCRYPT_BLOCK_SIZE);
600-
if (part == PART_SWAP)
601-
iv[1] = swap_counter++;
602-
for (i = 0; i < len / ENCRYPT_BLOCK_SIZE; i++) {
603-
iv[0] = offset++;
623+
if (sz > len) {
624+
int step = ENCRYPT_BLOCK_SIZE - row_offset;
625+
if (ext_flash_read(row_address, block, ENCRYPT_BLOCK_SIZE) != ENCRYPT_BLOCK_SIZE)
626+
return -1;
627+
XMEMCPY(block + row_offset, data, step);
628+
wc_Chacha_Process(&chacha, enc_block, block, ENCRYPT_BLOCK_SIZE);
629+
ext_flash_write(row_address, enc_block, ENCRYPT_BLOCK_SIZE);
630+
address += step;
631+
data += step;
632+
sz -= step;
633+
}
634+
for (i = 0; i < sz / ENCRYPT_BLOCK_SIZE; i++) {
635+
iv[0] = row_number;
604636
wc_Chacha_SetIV(&chacha, (byte *)iv, ENCRYPT_BLOCK_SIZE);
605637
XMEMCPY(block, data + (ENCRYPT_BLOCK_SIZE * i), ENCRYPT_BLOCK_SIZE);
606-
wc_Chacha_Process(&chacha, block, data + (ENCRYPT_BLOCK_SIZE * i), ENCRYPT_BLOCK_SIZE);
638+
wc_Chacha_Process(&chacha, ENCRYPT_CACHE + (ENCRYPT_BLOCK_SIZE * i), block, ENCRYPT_BLOCK_SIZE);
639+
row_number++;
607640
}
608-
return ext_flash_write(address, data, len);
641+
return ext_flash_write(address, ENCRYPT_CACHE, len);
609642
}
610643

611644
int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
612645
{
613646
uint32_t iv[ENCRYPT_BLOCK_SIZE / sizeof(uint32_t)];
614647
uint8_t block[ENCRYPT_BLOCK_SIZE];
615648
uint8_t part;
616-
uint32_t offset;
649+
uint32_t row_number;
650+
int sz = len;
651+
uint32_t row_address = address, row_offset;
617652
int i;
653+
654+
row_offset = address & (ENCRYPT_BLOCK_SIZE - 1);
655+
if (row_offset != 0) {
656+
row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1);
657+
sz += ENCRYPT_BLOCK_SIZE - row_offset;
658+
}
659+
if (sz < ENCRYPT_BLOCK_SIZE) {
660+
sz = ENCRYPT_BLOCK_SIZE;
661+
}
618662
if (!chacha_initialized)
619663
if (chacha_init() < 0)
620664
return -1;
621-
part = PART_ADDRESS(address);
665+
XMEMSET(iv, 0, ENCRYPT_BLOCK_SIZE);
666+
part = part_address(row_address);
622667
switch(part) {
623668
case PART_UPDATE:
624-
offset = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
669+
row_number = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
625670
break;
626671
case PART_SWAP:
627-
offset = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
672+
row_number = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
628673
iv[1] = swap_counter;
629674
break;
630675
default:
631676
return -1;
632677
}
633-
if (ext_flash_read(address, data, len) != len)
678+
if (sz > len) {
679+
uint8_t dec_block[ENCRYPT_BLOCK_SIZE];
680+
int step = ENCRYPT_BLOCK_SIZE - row_offset;
681+
if (ext_flash_read(row_address, block, ENCRYPT_BLOCK_SIZE) != ENCRYPT_BLOCK_SIZE)
682+
return -1;
683+
wc_Chacha_Process(&chacha, dec_block, block, ENCRYPT_BLOCK_SIZE);
684+
XMEMCPY(data, dec_block + row_offset, step);
685+
address += step;
686+
data += step;
687+
sz -= step;
688+
}
689+
if (ext_flash_read(address, data, sz) != sz)
634690
return -1;
635-
XMEMSET(iv, 0, ENCRYPT_BLOCK_SIZE);
636-
for (i = 0; i < len / ENCRYPT_BLOCK_SIZE; i++) {
637-
iv[0] = offset++;
691+
for (i = 0; i < sz / ENCRYPT_BLOCK_SIZE; i++) {
692+
iv[0] = row_number;
638693
wc_Chacha_SetIV(&chacha, (byte *)iv, ENCRYPT_BLOCK_SIZE);
639694
XMEMCPY(block, data + (ENCRYPT_BLOCK_SIZE * i), ENCRYPT_BLOCK_SIZE);
640-
wc_Chacha_Process(&chacha, block, data + (ENCRYPT_BLOCK_SIZE * i), ENCRYPT_BLOCK_SIZE);
641-
offset++;
695+
wc_Chacha_Process(&chacha, data + (ENCRYPT_BLOCK_SIZE * i), block, ENCRYPT_BLOCK_SIZE);
696+
row_number++;
642697
}
643698
return len;
644699
}

src/loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_imag
4747
dst_sector_offset = 0;
4848
#ifdef EXT_FLASH
4949
if (PART_IS_EXT(src)) {
50-
uint8_t buffer[FLASHBUFFER_SIZE];
50+
static uint8_t buffer[FLASHBUFFER_SIZE];
5151
wb_flash_erase(dst, dst_sector_offset, WOLFBOOT_SECTOR_SIZE);
5252
while (pos < WOLFBOOT_SECTOR_SIZE) {
5353
if (src_sector_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {

src/update_flash.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
extern unsigned int _start_text;
3434
static volatile const uint32_t __attribute__((used)) wolfboot_version = WOLFBOOT_VERSION;
3535

36+
#ifndef BUFFER_DECLARED
37+
#define BUFFER_DECLARED
38+
static uint8_t buffer[FLASHBUFFER_SIZE];
39+
#endif
40+
3641
static void RAMFUNCTION wolfBoot_erase_bootloader(void)
3742
{
3843
uint32_t *start = (uint32_t *)&_start_text;
@@ -117,7 +122,10 @@ static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_imag
117122
dst_sector_offset = 0;
118123
#ifdef EXT_FLASH
119124
if (PART_IS_EXT(src)) {
120-
uint8_t buffer[FLASHBUFFER_SIZE];
125+
#ifndef BUFFER_DECLARED
126+
#define BUFFER_DECLARED
127+
static uint8_t buffer[FLASHBUFFER_SIZE];
128+
#endif
121129
wb_flash_erase(dst, dst_sector_offset, WOLFBOOT_SECTOR_SIZE);
122130
while (pos < WOLFBOOT_SECTOR_SIZE) {
123131
if (src_sector_offset + pos < (src->fw_size + IMAGE_HEADER_SIZE + FLASHBUFFER_SIZE)) {

test-app/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ ifeq ($(HASH),SHA256)
1616
CFLAGS+=-DWOLFBOOT_HASH_SHA256
1717
endif
1818

19+
ifeq ($(NVM_FLASH_WRITEONCE),1)
20+
CFLAGS+=-DNVM_FLASH_WRITEONCE
21+
endif
22+
1923

2024
ifeq ($(HASH),SHA3_384)
2125
WOLFCRYPT_OBJS+=./lib/wolfssl/wolfcrypt/src/sha3.o
2226
CFLAGS+=-DWOLFBOOT_HASH_SHA3_384
2327
endif
2428

25-
CFLAGS:=-g -ggdb -Wall -Wstack-usage=1024 -ffreestanding -Wno-unused -DPLATFORM_$(TARGET) -I../include -nostartfiles
29+
CFLAGS+=-g -ggdb -Wall -Wstack-usage=1024 -ffreestanding -Wno-unused -DPLATFORM_$(TARGET) -I../include -nostartfiles
2630

2731
APP_OBJS:=app_$(TARGET).o led.o system.o timer.o ../hal/$(TARGET).o ../src/libwolfboot.o
2832
include ../arch.mk

test-app/app_stm32wb.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@
2525
#include <stdint.h>
2626
#include <string.h>
2727
#include "led.h"
28+
#include "hal.h"
2829
#include "wolfboot/wolfboot.h"
2930

3031
#ifdef PLATFORM_stm32wb
32+
char enc_key[33] = "01234566789abcdef0123456789abcdef";
3133

3234
volatile uint32_t time_elapsed = 0;
3335
void main(void) {
36+
hal_init();
3437
boot_led_on();
38+
wolfBoot_success();
39+
#if EXT_ENCRYPTED
40+
wolfBoot_set_encrypt_key((uint8_t *)enc_key, 32);
41+
#endif
42+
43+
3544
/* Wait for reboot */
3645
while(1)
3746
;

0 commit comments

Comments
 (0)