Skip to content

Commit 24538d1

Browse files
committed
Relocate temporary key slot to the end of the partition
1 parent b97f794 commit 24538d1

3 files changed

Lines changed: 30 additions & 16 deletions

File tree

src/libwolfboot.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
#define XMEMCPY memcpy
3737
#define XMEMCMP memcmp
3838
#endif
39+
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE))
3940
#else
4041
#define XMEMCPY memcpy
42+
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP))
4143
#endif
4244

4345
#ifndef NULL
@@ -56,8 +58,8 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
5658
#ifndef TRAILER_SKIP
5759
# define TRAILER_SKIP 0
5860
#endif
59-
#define PART_BOOT_ENDFLAGS ((WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
60-
#define PART_UPDATE_ENDFLAGS ((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
61+
#define PART_BOOT_ENDFLAGS (WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET)
62+
#define PART_UPDATE_ENDFLAGS (WOLFBOOT_PARTITION_UPDATE_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET)
6163

6264
#ifdef NVM_FLASH_WRITEONCE
6365
#include <stddef.h>
@@ -507,7 +509,6 @@ int wolfBoot_fallback_is_possible(void)
507509
#error option EXT_ENCRYPTED requires EXT_FLASH
508510
#endif
509511

510-
#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 + ENCRYPT_NONCE_SIZE)))
511512

512513

513514
#ifdef NVM_FLASH_WRITEONCE
@@ -618,8 +619,8 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len)
618619
switch(part) {
619620
case PART_UPDATE:
620621
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
621-
/* Do not encrypt last sector */
622-
if (iv_counter == (WOLFBOOT_PARTITION_SIZE - 1) / ENCRYPT_BLOCK_SIZE) {
622+
/* Do not encrypt last sectors */
623+
if (iv_counter >= (ENCRYPT_TMP_SECRET_OFFSET - ENCRYPT_BLOCK_SIZE) / ENCRYPT_BLOCK_SIZE) {
623624
return ext_flash_write(address, data, len);
624625
}
625626
break;
@@ -680,7 +681,7 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
680681
case PART_UPDATE:
681682
iv_counter = (address - WOLFBOOT_PARTITION_UPDATE_ADDRESS) / ENCRYPT_BLOCK_SIZE;
682683
/* Do not decrypt last sector */
683-
if (iv_counter == (WOLFBOOT_PARTITION_SIZE - 1) / ENCRYPT_BLOCK_SIZE) {
684+
if (iv_counter >= (ENCRYPT_TMP_SECRET_OFFSET - ENCRYPT_BLOCK_SIZE) / ENCRYPT_BLOCK_SIZE) {
684685
return ext_flash_read(address, data, len);
685686
}
686687
break;

tools/scripts/prepare_encrypted_update.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
2-
# SIZE is WOLFBOOT_PARTITION_SIZE - 5
3-
SIZE=131067
2+
# SIZE is WOLFBOOT_PARTITION_SIZE - 49 (44B: key + nonce, 5B: "pBOOT")
3+
SIZE=131023
44
VERSION=8
55
APP=test-app/image_v"$VERSION"_signed_and_encrypted.bin
66

tools/uart-flash-server/ufserver.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const char msgEraseSwap[] = "Erase swap blocks ";
6565
extern uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr);
6666

6767
const char blinker[]="-\\|/";
68+
static int valid_update = 1;
6869

6970
void printmsg(const char *msg)
7071
{
@@ -178,6 +179,7 @@ uint8_t *mmap_firmware(const char *fname)
178179
uint8_t *base_fw;
179180
struct stat st;
180181
int fd;
182+
uint32_t signature_word;
181183
if (stat(fname, &st) != 0) {
182184
perror ("stat");
183185
return (void *)-1;
@@ -188,19 +190,32 @@ uint8_t *mmap_firmware(const char *fname)
188190
perror("open");
189191
return (void *)-1;
190192
}
191-
if (st.st_size <= FIRMWARE_PARTITION_SIZE) {
193+
if (read(fd, &signature_word, sizeof(uint32_t)) != (sizeof(uint32_t))) {
194+
perror("read");
195+
return (void *)-1;
196+
}
197+
if ((st.st_size <= FIRMWARE_PARTITION_SIZE)) {
192198
uint8_t pad = 0xFF;
193199
int i;
194-
const char update_flags[] = "pBOOT";
195200
int fsize = st.st_size;
196-
lseek(fd, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, SEEK_SET);
197201
lseek(fd, fsize, SEEK_SET);
198-
for (i = 0; i < (FIRMWARE_PARTITION_SIZE - (fsize + 5)); i++)
202+
for (i = 0; i < (FIRMWARE_PARTITION_SIZE - (fsize)); i++)
199203
write(fd, &pad, 1);
200-
write(fd, update_flags, 5);
204+
lseek(fd, FIRMWARE_PARTITION_SIZE, SEEK_SET);
201205
for (i = 0; i < SWAP_SIZE; i++)
202206
write(fd, &pad, 1);
203207
}
208+
if (strncmp((char *)&signature_word, "WOLF", 4) != 0) {
209+
fprintf(stderr, "Warning: the binary file provided does not appear to contain a valid firmware partition file. (If the update is encrypted, this is OK)\n");
210+
valid_update = 0;
211+
} else {
212+
int i;
213+
const char update_flags[] = "pBOOT";
214+
lseek(fd, FIRMWARE_PARTITION_SIZE - 5, SEEK_SET);
215+
write(fd, update_flags, 5);
216+
for (i = 0; i < SWAP_SIZE; i++)
217+
write(fd, update_flags, 5);
218+
}
204219
base_fw = mmap(NULL, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
205220
if (base_fw == (void *)(-1)) {
206221
perror("mmap");
@@ -415,9 +430,7 @@ int main(int argc, char *argv[])
415430
fprintf(stderr, "Error opening binary file '%s'.\n", argv[1]);
416431
exit(2);
417432
}
418-
if (strncmp((char *)base_fw, "WOLF", 4) != 0) {
419-
fprintf(stderr, "Warning: the binary file provided does not appear to contain a valid firmware partition file.\n");
420-
} else {
433+
if (valid_update) {
421434
printf("%s has a wolfboot manifest header\n", basename(argv[1]));
422435
base_fw_ver = fw_version(base_fw);
423436
printf("%s contains version %u\n", basename(argv[1]), base_fw_ver);

0 commit comments

Comments
 (0)