Skip to content

Commit 8b73250

Browse files
committed
Set trailer on BOOT partition when storing the enc key
1 parent e586087 commit 8b73250

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

src/libwolfboot.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
196196
uint32_t word_0;
197197
uint32_t word_1;
198198

199+
#ifdef EXT_FLASH
200+
if ((part == PART_UPDATE) && FLAGS_UPDATE_EXT()) {
201+
return 0;
202+
}
203+
#endif
204+
199205
hal_cache_invalidate();
200206

201207
if (part == PART_BOOT) {
@@ -219,6 +225,7 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
219225
word_1 = *((uint32_t*)((uintptr_t)base - (WOLFBOOT_SECTOR_SIZE + magic_off +
220226
sizeof(uint32_t))));
221227

228+
222229
if (word_0 == WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL) {
223230
sel = 0;
224231
goto finish;
@@ -287,6 +294,7 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val)
287294
uintptr_t addr_off = addr & (NVM_CACHE_SIZE - 1);
288295
int ret = 0;
289296

297+
290298
nvm_cached_sector = nvm_select_fresh_sector(part);
291299
addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE);
292300
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
@@ -329,6 +337,7 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
329337
uintptr_t base = (uintptr_t)addr - off;
330338
uintptr_t addr_read, addr_write;
331339
int ret;
340+
332341
nvm_cached_sector = nvm_select_fresh_sector(part);
333342
addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE);
334343
addr_write = base - (!nvm_cached_sector * NVM_CACHE_SIZE);
@@ -1403,6 +1412,7 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
14031412
int RAMFUNCTION wolfBoot_set_encrypt_key(const uint8_t *key,
14041413
const uint8_t *nonce)
14051414
{
1415+
set_partition_magic(PART_BOOT);
14061416
hal_set_key(key, nonce);
14071417
return 0;
14081418
}
@@ -1670,10 +1680,6 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16701680
if (sz < ENCRYPT_BLOCK_SIZE) {
16711681
sz = ENCRYPT_BLOCK_SIZE;
16721682
}
1673-
if (!encrypt_initialized) {
1674-
if (crypto_init() < 0)
1675-
return -1;
1676-
}
16771683
part = part_address(address);
16781684
switch (part) {
16791685
case PART_UPDATE:
@@ -1684,6 +1690,10 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16841690
ENCRYPT_BLOCK_SIZE) {
16851691
return ext_flash_write(address, data, len);
16861692
}
1693+
if (!encrypt_initialized) {
1694+
if (crypto_init() < 0)
1695+
return -1;
1696+
}
16871697
crypto_set_iv(encrypt_iv_nonce, iv_counter);
16881698
break;
16891699
case PART_SWAP:
@@ -1752,10 +1762,6 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17521762
if (row_offset != 0) {
17531763
row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1);
17541764
}
1755-
if (!encrypt_initialized) {
1756-
if (crypto_init() < 0)
1757-
return -1;
1758-
}
17591765
part = part_address(row_address);
17601766
switch (part) {
17611767
case PART_UPDATE:
@@ -1766,6 +1772,11 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17661772
ENCRYPT_BLOCK_SIZE) {
17671773
return ext_flash_read(address, data, len);
17681774
}
1775+
if (!encrypt_initialized) {
1776+
if (crypto_init() < 0) {
1777+
return -1;
1778+
}
1779+
}
17691780
crypto_set_iv(encrypt_iv_nonce, iv_counter);
17701781
break;
17711782
case PART_SWAP:
@@ -1847,18 +1858,15 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst)
18471858
uint32_t dst_offset = 0, iv_counter = 0;
18481859
uint32_t magic, len;
18491860

1850-
wolfBoot_printf("Decrypting %p to %p\n", src, dst);
18511861

18521862
if (!encrypt_initialized) {
18531863
if (crypto_init() < 0) {
1854-
wolfBoot_printf("Error initializing crypto!\n");
18551864
return -1;
18561865
}
18571866
}
18581867

18591868
/* Attempt to decrypt firmware header */
18601869
if (decrypt_header(src) != 0) {
1861-
wolfBoot_printf("Error decrypting header at %p!\n", src);
18621870
return -1;
18631871
}
18641872
len = *((uint32_t*)(dec_hdr + sizeof(uint32_t)));

src/update_flash.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
501501
uint32_t up_v;
502502
#endif
503503

504+
504505
/* No Safety check on open: we might be in the middle of a broken update */
505506
wolfBoot_open_image(&update, PART_UPDATE);
506507
wolfBoot_open_image(&boot, PART_BOOT);
@@ -819,7 +820,8 @@ void RAMFUNCTION wolfBoot_start(void)
819820
bootRet = wolfBoot_get_partition_state(PART_BOOT, &bootState);
820821
updateRet = wolfBoot_get_partition_state(PART_UPDATE, &updateState);
821822

822-
#ifndef DISABLE_BACKUP
823+
824+
#if !defined(DISABLE_BACKUP) && !defined(EXT_ENCRYPTED)
823825
/* resume the final erase in case the power failed before it finished */
824826
resumedFinalErase = wolfBoot_swap_and_final_erase(1);
825827
if (resumedFinalErase != 0)

test-app/app_sim.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ int do_cmd(const char *cmd)
6161
printf("%d\n", wolfBoot_current_firmware_version());
6262
return 0;
6363
}
64+
if (strcmp(cmd, "get_state") == 0) {
65+
uint8_t st = 0;
66+
wolfBoot_get_partition_state(PART_UPDATE, &st);
67+
printf("%02x\n", st);
68+
return 0;
69+
}
6470
if (strcmp(cmd, "success") == 0) {
6571
wolfBoot_success();
6672
return 0;

tools/unit-tests/unit-enc-nvm.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,46 @@ void hal_prepare_boot(void)
9999

100100
int ext_flash_erase(uintptr_t address, int len)
101101
{
102+
printf("%s", __FUNCTION__);
103+
if ((address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) &&
104+
(address < WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE)) {
105+
erased_update++;
106+
memset(address, 0xFF, len);
107+
if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE) {
108+
erased_nvm_bank0++;
109+
} else if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 2 * WOLFBOOT_SECTOR_SIZE) {
110+
erased_nvm_bank1++;
111+
}
112+
} else if ((address >= WOLFBOOT_PARTITION_SWAP_ADDRESS) &&
113+
(address < WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE)) {
114+
erased_swap++;
115+
memset(address, 0xFF, len);
116+
} else {
117+
fail("Invalid address\n");
118+
return -1;
119+
}
102120
return 0;
103121
}
104122

105123
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
106124
{
125+
int i;
126+
uint8_t *a = (uint8_t *)address;
127+
fail_if(locked, "Attempting to write to a locked FLASH");
128+
printf("%s", __FUNCTION__);
129+
for (i = 0; i < len; i++) {
130+
a[i] = data[i];
131+
}
107132
return 0;
108133
}
109134

110135
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
111136
{
137+
int i;
138+
uint8_t *a = (uint8_t *)address;
139+
for (i = 0; i < len; i++) {
140+
data[i] = a[i];
141+
}
112142
return 0;
113143
}
114144

0 commit comments

Comments
 (0)