Skip to content

Commit 53bf4d0

Browse files
committed
Encrypt: API design
1 parent 1d24d32 commit 53bf4d0

9 files changed

Lines changed: 119 additions & 43 deletions

File tree

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ factory.bin: $(BOOT_IMG) wolfboot-align.bin $(PRIVATE_KEY)
258258
@echo "\t[MERGE] $@"
259259
@cat wolfboot-align.bin test-app/image_v1_signed.bin > $@
260260

261+
update.bin: $(BOOT_IMG)
262+
@echo "\t[SIGN] $(BOOT_IMG)"
263+
$(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 2
264+
265+
update_enc.bin: $(BOOT_IMG)
266+
@echo "\t[SIGN+ENC] $(BOOT_IMG)"
267+
@printf "0123456789abcdef0123456789abcdef" | dd of=test_enc_key.bin
268+
$(Q)$(SIGN_TOOL) $(SIGN_OPTIONS) --encrypt test_enc_key.bin $(BOOT_IMG) $(PRIVATE_KEY) 2
269+
261270
wolfboot.elf: include/target.h $(OBJS) $(LSCRIPT) FORCE
262271
@echo "\t[LD] $@"
263272
$(Q)$(LD) $(LDFLAGS) -Wl,--start-group $(OBJS) -Wl,--end-group -o $@

include/encrypt.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#ifndef ENCRYPT_H_INCLUDED
2525
#define ENCRYPT_H_INCLUDED
26+
#ifdef __WOLFBOOT
2627
#include <stdint.h>
2728
#include <wolfssl/wolfcrypt/settings.h>
2829
#include <wolfssl/wolfcrypt/sha256.h>
@@ -33,12 +34,10 @@
3334
#include <wolfssl/wolfcrypt/chacha.h>
3435
#include <wolfssl/wolfcrypt/pwdbased.h>
3536

36-
#define ENCRYPT_BLOCK_SIZE 16
37-
#define ENCRYPT_KEY_SIZE 32 /* Chacha20-256 */
3837

39-
int ext_flash_set_encrypt_key(const uint8_t *key, int len);
40-
int ext_flash_set_encrypt_password(const uint8_t *pwd, int len);
38+
/* Internal read/write functions (not exported in the libwolfboot API) */
4139
int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len);
4240
int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len);
4341

42+
#endif /* __WOLFBOOT */
4443
#endif /* ENCRYPT_H_INCLUDED */

include/image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr);
9999
#include "hal.h"
100100

101101

102-
#ifdef EXT_ENCRYPTED
102+
#if defined(EXT_ENCRYPTED) && defined(__WOLFBOOT)
103103
#include "encrypt.h"
104104
#define ext_flash_check_write ext_flash_encrypt_write
105105
#define ext_flash_check_read ext_flash_decrypt_read

include/wolfboot/wolfboot.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,10 @@ int wolfBoot_dualboot_candidate(void);
118118
# error "No valid hash algorithm defined!"
119119
#endif
120120

121-
121+
/* Encryption support */
122+
#define ENCRYPT_BLOCK_SIZE 16
123+
#define ENCRYPT_KEY_SIZE 32 /* Chacha20-256 */
124+
int wolfBoot_set_encrypt_key(const uint8_t *key, int len);
125+
int wolfBoot_erase_encrypt_key(void);
126+
int wolfBoot_set_encrypt_password(const uint8_t *pwd, int len);
122127
#endif /* !WOLFBOOT_H */

src/libwolfboot.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@
2626
#include "wolfboot/wolfboot.h"
2727
#include "image.h"
2828

29-
#ifdef EXT_ENCRYPTED
30-
#include "encrypt.h"
29+
#if defined(EXT_ENCRYPTED)
30+
#if defined(__WOLFBOOT)
31+
#include "encrypt.h"
32+
#else
33+
#include <stddef.h>
34+
#include <string.h>
35+
#define XMEMSET memset
36+
#define XMEMCPY memcpy
37+
#define XMEMCMP memcmp
38+
#endif
3139
#endif
3240

3341
#ifndef NULL
@@ -46,15 +54,18 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
4654
#define PART_UPDATE_ENDFLAGS ((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
4755

4856
#ifdef NVM_FLASH_WRITEONCE
49-
5057
#include <stddef.h>
51-
extern void *memcpy(void *dst, const void *src, size_t n);
58+
#include <string.h>
59+
#define XMEMSET memset
60+
#define XMEMCPY memcpy
61+
#define XMEMCMP memcmp
62+
5263
static uint8_t NVM_CACHE[NVM_CACHE_SIZE];
5364
int RAMFUNCTION hal_trailer_write(uint32_t addr, uint8_t val) {
5465
uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1));
5566
uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1);
5667
int ret = 0;
57-
memcpy(NVM_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
68+
XMEMCPY(NVM_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
5869
ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE);
5970
if (ret != 0)
6071
return ret;
@@ -68,11 +79,11 @@ int RAMFUNCTION hal_set_partition_magic(uint32_t addr)
6879
uint32_t off = addr % NVM_CACHE_SIZE;
6980
uint32_t base = addr - off;
7081
int ret;
71-
memcpy(NVM_CACHE, (void *)base, NVM_CACHE_SIZE);
82+
XMEMCPY(NVM_CACHE, (void *)base, NVM_CACHE_SIZE);
7283
ret = hal_flash_erase(base, WOLFBOOT_SECTOR_SIZE);
7384
if (ret != 0)
7485
return ret;
75-
memcpy(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t));
86+
XMEMCPY(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t));
7687
ret = hal_flash_write(base, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
7788
return ret;
7889
}
@@ -488,9 +499,6 @@ int wolfBoot_fallback_is_possible(void)
488499

489500
#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)
490501

491-
/* Buffer used for encryption/decryption */
492-
static ChaCha chacha;
493-
static int chacha_initialized = 0;
494502

495503
#ifdef NVM_FLASH_WRITEONCE
496504
#define KEY_CACHE NVM_CACHE
@@ -505,39 +513,24 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k)
505513
uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1));
506514
uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1);
507515
int ret = 0;
508-
memcpy(KEY_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
516+
XMEMCPY(KEY_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
509517
ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE);
510518
if (ret != 0)
511519
return ret;
512-
memcpy(KEY_CACHE + addr_off, k, ENCRYPT_KEY_SIZE);
520+
XMEMCPY(KEY_CACHE + addr_off, k, ENCRYPT_KEY_SIZE);
513521
ret = hal_flash_write(addr_align, KEY_CACHE, WOLFBOOT_SECTOR_SIZE);
514522
return ret;
515523
}
516524

517-
static int chacha_init(void)
518-
{
519-
uint8_t *key = (uint8_t *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET);
520-
uint8_t ff[ENCRYPT_KEY_SIZE];
521-
XMEMSET(ff, 0xFF, ENCRYPT_KEY_SIZE);
522-
if (XMEMCMP(key, ff, ENCRYPT_KEY_SIZE) == 0)
523-
return -1;
524-
XMEMSET(ff, 0xFF, ENCRYPT_KEY_SIZE);
525-
if (XMEMCMP(key, ff, ENCRYPT_KEY_SIZE) == 0)
526-
return -1;
527-
wc_Chacha_SetKey(&chacha, key, ENCRYPT_KEY_SIZE);
528-
chacha_initialized = 1;
529-
return 0;
530-
}
531-
532-
int wolfBoot_set_encrypt_key(const uint8_t *key, int len)
525+
int RAMFUNCTION wolfBoot_set_encrypt_key(const uint8_t *key, int len)
533526
{
534527
if (len != ENCRYPT_KEY_SIZE)
535528
return -1;
536529
hal_set_key(key);
537530
return 0;
538531
}
539532

540-
int wolfBoot_erase_encrypt_key(void)
533+
int RAMFUNCTION wolfBoot_erase_encrypt_key(void)
541534
{
542535
uint8_t ff[ENCRYPT_KEY_SIZE];
543536
int i;
@@ -546,12 +539,33 @@ int wolfBoot_erase_encrypt_key(void)
546539
return 0;
547540
}
548541

549-
int wolfBoot_set_encrypt_password(const uint8_t *pwd, int len)
542+
int RAMFUNCTION wolfBoot_set_encrypt_password(const uint8_t *pwd, int len)
550543
{
551544
/* TODO */
552545
return -1;
553546
}
554547

548+
#ifdef __WOLFBOOT
549+
550+
static ChaCha chacha;
551+
static int chacha_initialized = 0;
552+
553+
static int chacha_init(void)
554+
{
555+
uint8_t *key = (uint8_t *)(WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET);
556+
uint8_t ff[ENCRYPT_KEY_SIZE];
557+
XMEMSET(ff, 0xFF, ENCRYPT_KEY_SIZE);
558+
if (XMEMCMP(key, ff, ENCRYPT_KEY_SIZE) == 0)
559+
return -1;
560+
XMEMSET(ff, 0xFF, ENCRYPT_KEY_SIZE);
561+
if (XMEMCMP(key, ff, ENCRYPT_KEY_SIZE) == 0)
562+
return -1;
563+
wc_Chacha_SetKey(&chacha, key, ENCRYPT_KEY_SIZE);
564+
chacha_initialized = 1;
565+
return 0;
566+
}
567+
568+
555569
#define PART_ADDRESS(a) ((a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) && \
556570
(a <= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))?\
557571
(PART_UPDATE):\
@@ -628,6 +642,7 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
628642
}
629643
return len;
630644
}
645+
#endif
631646

632647
#endif /* EXT_ENCRYPTED */
633648

test-app/ARM.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MEMORY
22
{
33
FLASH (rx) : ORIGIN = ##WOLFBOOT_TEST_APP_ADDRESS##, LENGTH = ##WOLFBOOT_TEST_APP_SIZE##
4-
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 16K /* Run in lowmem */
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K /* Run in lowmem */
55
}
66

77
SECTIONS

test-app/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ ifeq ($(V),0)
3939
Q=@
4040
endif
4141

42+
ifeq ($(ENCRYPT),1)
43+
CFLAGS+=-DEXT_ENCRYPTED=1
44+
endif
45+
4246
ENTRY_POINT=`cat .entry-point-address`
4347
LSCRIPT:=../config/target-app.ld
4448
LSCRIPT_TEMPLATE:=$(ARCH).ld

test-app/app_stm32f4.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static const char ACK='#';
7878
static uint8_t msg[MSGSIZE];
7979

8080

81+
8182
void uart_write(const char c)
8283
{
8384
uint32_t reg;
@@ -219,6 +220,9 @@ void main(void) {
219220
version = wolfBoot_current_firmware_version();
220221
if ((version & 0x01) == 0)
221222
wolfBoot_success();
223+
#ifdef EXT_ENCRYPTED
224+
wolfBoot_set_encrypt_key("0123456789abcdef0123456789abcdef", 32);
225+
#endif
222226
uart_write(START);
223227
for (i = 3; i >= 0; i--) {
224228
uart_write(v_array[i]);

tools/keytools/sign.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,23 @@
5858
self_update=False
5959
sha_only=False
6060
manual_sign=False
61+
encrypt=False
6162

6263

6364
argc = len(sys.argv)
6465
argv = sys.argv
6566
hash_algo='sha256'
6667

67-
if (argc < 4) or (argc > 8):
68-
print("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--wolfboot-update] image key.der fw_version\n" % sys.argv[0])
68+
if (argc < 4) or (argc > 10):
69+
print("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--wolfboot-update] [--encrypt key.bin] image key.der fw_version\n" % sys.argv[0])
6970
print(" - or - ")
70-
print(" %s [--sha256 | --sha3] [--sha-only] [--wolfboot-update] image pub_key.der fw_version\n" % sys.argv[0])
71+
print(" %s [--sha256 | --sha3] [--sha-only] [--wolfboot-update] [--encrypt key.bin] image pub_key.der fw_version\n" % sys.argv[0])
7172
print(" - or - ")
72-
print(" %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--manual-sign] image pub_key.der fw_version signature.sig\n" % sys.argv[0])
73+
print(" %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--manual-sign] [--encrypt key.bin] image pub_key.der fw_version signature.sig\n" % sys.argv[0])
7374
sys.exit(1)
74-
for i in range(1, len(argv)):
75+
76+
i = 1
77+
while (i < len(argv)):
7578
if (argv[i] == '--ed25519'):
7679
sign='ed25519'
7780
elif (argv[i] == '--ecc256'):
@@ -90,10 +93,14 @@
9093
sha_only = True
9194
elif (argv[i] == '--manual-sign'):
9295
manual_sign = True
93-
96+
elif (argv[i] == '--encrypt'):
97+
encrypt = True
98+
i += 1
99+
encrypt_key_file = argv[i]
94100
else:
95101
i-=1
96102
break
103+
i += 1
97104

98105
image_file = argv[i+1]
99106
key_file = argv[i+2]
@@ -117,6 +124,14 @@
117124
else:
118125
output_image_file = image_file + "_v" + str(fw_version) + "_digest.bin"
119126

127+
if encrypt:
128+
if '.' in image_file:
129+
tokens = image_file.split('.')
130+
encrypted_output_image_file = image_file.rstrip('.' + tokens[-1])
131+
encrypted_output_image_file += "_v" + str(fw_version) + "_signed_and_encrypted.bin"
132+
else:
133+
encrypted_output_image_file = image_file + "_v" + str(fw_version) + "_signed_and_encrypted.bin"
134+
120135
if (self_update):
121136
print("Update type: wolfBoot")
122137
else:
@@ -132,6 +147,11 @@
132147
else:
133148
print ("Output digest: " + output_image_file)
134149

150+
if not encrypt:
151+
print ("Not Encrypted")
152+
else:
153+
print ("Encrypted using: " + encrypt_key_file)
154+
135155
kf = open(key_file, "rb")
136156
wolfboot_key_buffer = kf.read(4096)
137157
wolfboot_key_buffer_len = len(wolfboot_key_buffer)
@@ -364,6 +384,26 @@
364384

365385
infile.close()
366386
outfile.close()
387+
if (encrypt):
388+
sz = 0
389+
off = 0
390+
outfile = open(output_image_file, 'rb')
391+
ekeyfile = open(encrypt_key_file, 'rb')
392+
key = ekeyfile.read(32)
393+
enc_outfile = open(encrypted_output_image_file, 'wb')
394+
cha = ciphers.ChaCha(key, 32)
395+
while(True):
396+
cha.set_iv(off)
397+
buf = outfile.read(16)
398+
if len(buf) == 0:
399+
break
400+
enc_outfile.write(cha.encrypt(buf))
401+
off += 1
402+
outfile.close()
403+
ekeyfile.close()
404+
enc_outfile.close()
405+
406+
367407
print ("Output image successfully created.")
368408
sys.exit(0)
369409

0 commit comments

Comments
 (0)