Skip to content

Commit 574f68b

Browse files
authored
Merge pull request #548 from danielinux/encrypt_cache_in_stack
Move encrypt cache to stack
2 parents d13f326 + 076cd1d commit 574f68b

File tree

8 files changed

+52
-10
lines changed

8 files changed

+52
-10
lines changed

docs/encrypted_partitions.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ as template. The file `hal/stm32l0_chacha_ram.ld` contains the changes described
179179
all the needed symbols in RAM.
180180

181181

182+
### Using a custom buffer as encrypt/decrypt cache
183+
184+
By default, encryption support requires a buffer of the same size as the external flash page size to be allocated in RAM.
185+
You can provide a custom pre-allocated buffer by passing its address via the option `ENCRYPT_CACHE`, e.g.:
186+
187+
`ENCRYPT_CACHE=0x20010000`
188+
182189
### API usage in the application
183190

184191
When transferring the image, the application can still use the libwolfboot API functions to store the encrypted firmware. When called from the application,

include/image.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ int wolfBot_get_dts_size(void *dts_addr);
7474
# endif
7575
#endif
7676

77+
7778
/* Helpers for memory alignment */
7879
#ifndef XALIGNED
7980
#if defined(__GNUC__) || defined(__llvm__) || \
@@ -90,6 +91,15 @@ int wolfBot_get_dts_size(void *dts_addr);
9091
#endif
9192
#endif
9293

94+
#ifndef XALIGNED_STACK
95+
/* Don't enforce stack alignment on IAR */
96+
#if defined (__IAR_SYSTEMS_ICC__)
97+
#define XALIGNED_STACK(x)
98+
#else
99+
#define XALIGNED_STACK(x) XALIGNED(x)
100+
#endif
101+
#endif
102+
93103

94104
#ifndef WOLFBOOT_FLAGS_INVERT
95105
#define SECT_FLAG_NEW 0x0F

options.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@ ifeq ($(RAM_CODE),1)
770770
endif
771771
endif
772772

773+
# Support external encryption cache
774+
#
775+
ifeq ($(ENCRYPT),1)
776+
ifeq ($(ENCRYPT_CACHE),1)
777+
CFLAGS+=-D"WOLFBOOT_ENCRYPT_CACHE=$(ENCRYPT_CACHE)"
778+
endif
779+
endif
780+
773781
# support for elf32 or elf64 loader
774782
ifeq ($(ELF),1)
775783
CFLAGS+=-DWOLFBOOT_ELF

src/libwolfboot.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
177177

178178
#include <stddef.h>
179179
#include <string.h>
180-
static uint8_t NVM_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(16)));
180+
static uint8_t NVM_CACHE[NVM_CACHE_SIZE] XALIGNED(16);
181181
static int nvm_cached_sector = 0;
182182
static uint8_t get_base_offset(uint8_t *base, uintptr_t off)
183183
{
@@ -1334,16 +1334,21 @@ int wolfBoot_fallback_is_possible(void)
13341334

13351335
#ifdef EXT_ENCRYPTED
13361336
#include "encrypt.h"
1337+
13371338
#if !defined(EXT_FLASH) && !defined(MMU)
1338-
#error option EXT_ENCRYPTED requires EXT_FLASH or MMU mode
1339+
#error option EXT_ENCRYPTED requires EXT_FLASH or MMU mode
13391340
#endif
13401341

1341-
1342-
1343-
#ifdef NVM_FLASH_WRITEONCE
1344-
#define ENCRYPT_CACHE NVM_CACHE
1342+
#ifndef WOLFBOOT_ENCRYPT_CACHE
1343+
#ifdef NVM_FLASH_WRITEONCE
1344+
#define ENCRYPT_CACHE NVM_CACHE
1345+
#else
1346+
#ifdef WOLFBOOT_SMALL_STACK
1347+
static uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] XALIGNED(32);
1348+
#endif
1349+
#endif
13451350
#else
1346-
static uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(32)));
1351+
#define ENCRYPT_CACHE (WOLFBOOT_ENCRYPT_CACHE)
13471352
#endif
13481353

13491354
#if defined(EXT_ENCRYPTED) && defined(MMU)
@@ -1356,6 +1361,11 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
13561361
int ret = 0;
13571362
int sel_sec = 0;
13581363
uint32_t trailer_relative_off = 4;
1364+
1365+
#if !defined(WOLFBOOT_SMALL_STACK) && !defined(NVM_FLASH_WRITEONCE) && !defined(WOLFBOOT_ENCRYPT_CACHE)
1366+
uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] XALIGNED_STACK(32);
1367+
#endif
1368+
13591369
#ifdef MMU
13601370
XMEMCPY(ENCRYPT_KEY, k, ENCRYPT_KEY_SIZE);
13611371
XMEMCPY(ENCRYPT_KEY + ENCRYPT_KEY_SIZE, nonce, ENCRYPT_NONCE_SIZE);
@@ -1692,6 +1702,9 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16921702
int sz = len, i, step;
16931703
uint8_t part;
16941704
uint32_t iv_counter = 0;
1705+
#if defined(EXT_ENCRYPTED) && !defined(WOLFBOOT_SMALL_STACK) && !defined(NVM_FLASH_WRITEONCE)
1706+
uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] XALIGNED_STACK(32);
1707+
#endif
16951708

16961709
row_offset = address & (ENCRYPT_BLOCK_SIZE - 1);
16971710
if (row_offset != 0) {

src/update_disk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extern uint8_t _end_wb[];
7979
*/
8080
void RAMFUNCTION wolfBoot_start(void)
8181
{
82-
uint8_t p_hdr[IMAGE_HEADER_SIZE] __attribute__((aligned(16)));
82+
uint8_t p_hdr[IMAGE_HEADER_SIZE] XALIGNED_STACK(16);
8383
struct stage2_parameter *stage2_params;
8484
struct wolfBoot_image os_image;
8585
int pA_ver = 0, pB_ver = 0;

tools/config.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,5 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO
111111
NO_ARM_ASM \
112112
SIGN_SECONDARY \
113113
WOLFHSM_CLIENT \
114-
WOLFHSM_CLIENT_LOCAL_KEYS
114+
WOLFHSM_CLIENT_LOCAL_KEYS \
115+
ENCRYPT_CACHE

tools/keytools/user_settings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include <stdint.h>
3030

3131
/* System */
32-
#define WOLFBOOT_KEYTOOLS
32+
#ifndef WOLFBOOT_KEYTOOLS
33+
#define WOLFBOOT_KEYTOOLS
34+
#endif
3335
#define SINGLE_THREADED
3436
#define WOLFCRYPT_ONLY
3537

tools/unit-tests/unit-extflash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <stdlib.h>
4848
#include <string.h>
4949
#include "user_settings.h"
50+
#include "image.h"
5051

5152
#include "libwolfboot.c"
5253

0 commit comments

Comments
 (0)