Skip to content

Commit d21dfc2

Browse files
committed
Merge branch 'master' into psoc6_hwcrypto
2 parents ec3234b + e575224 commit d21dfc2

5 files changed

Lines changed: 99 additions & 54 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ The bootloader consists of the following components:
3535
- The core bootloader
3636
- A small application library used by the application to interact with the bootloader [src/libwolfboot.c](src/libwolfboot.c)
3737

38-
Only ARM Cortex-M boot mechanism is supported at this stage. Support for more architectures and
39-
microcontrollers will be added later. Relocating the interrupt vector can be disabled if needed.
40-
4138
## Integrating wolfBoot in an existing project
4239

4340
### Required steps

hal/stm32wb.c

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ PKA_HandleTypeDef hpka = { };
9797
#define FLASH_ACR_LATENCY_MASK (0x07)
9898

9999
#ifndef WOLFSSL_STM32_PKA
100-
#define FLASH_SR_BSY (1 << 16)
100+
#define FLASH_SR_BSY (1 << 16)
101+
#define FLASH_SR_CFGBSY (1 << 18)
101102
#define FLASH_SR_SIZERR (1 << 6)
102103
#define FLASH_SR_PGAERR (1 << 5)
103104
#define FLASH_SR_WRPERR (1 << 4)
@@ -109,11 +110,12 @@ PKA_HandleTypeDef hpka = { };
109110

110111
#define FLASH_CR_PER (1 << 1)
111112
#define FLASH_CR_PG (1 << 0)
113+
#define FLASH_CR_FSTPG (1 << 18)
112114

113115
#endif /* !WOLFSSL_STM32_PKA */
114116

115117
#define FLASH_CR_PNB_SHIFT 3
116-
#define FLASH_CR_PNB_MASK 0x3f
118+
#define FLASH_CR_PNB_MASK 0xFF
117119

118120
#define FLASH_KEY1 (0x45670123)
119121
#define FLASH_KEY2 (0xCDEF89AB)
@@ -128,7 +130,7 @@ static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
128130

129131
static RAMFUNCTION void flash_wait_complete(void)
130132
{
131-
while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY)
133+
while ((FLASH_SR & (FLASH_SR_BSY | FLASH_SR_CFGBSY)) != 0)
132134
;
133135
}
134136

@@ -137,21 +139,50 @@ static void RAMFUNCTION flash_clear_errors(void)
137139
FLASH_SR |= ( FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_PROGERR);
138140
}
139141

142+
143+
144+
void RAMFUNCTION hal_flash_unlock(void)
145+
{
146+
flash_wait_complete();
147+
if ((FLASH_CR & FLASH_CR_LOCK) != 0) {
148+
FLASH_KEY = FLASH_KEY1;
149+
DMB();
150+
FLASH_KEY = FLASH_KEY2;
151+
DMB();
152+
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
153+
;
154+
}
155+
}
156+
157+
void RAMFUNCTION hal_flash_lock(void)
158+
{
159+
flash_wait_complete();
160+
if ((FLASH_CR & FLASH_CR_LOCK) == 0)
161+
FLASH_CR |= FLASH_CR_LOCK;
162+
}
163+
140164
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
141165
{
142166
int i = 0;
143167
uint32_t *src, *dst;
168+
uint32_t pdword[2] __attribute__((aligned(16)));
169+
uint32_t reg;
170+
144171
flash_clear_errors();
145-
FLASH_CR |= FLASH_CR_PG;
172+
reg = FLASH_CR & (~FLASH_CR_FSTPG);
173+
FLASH_CR = reg | FLASH_CR_PG;
146174

147175
while (i < len) {
148176
flash_clear_errors();
149177
if ((len - i > 3) && ((((address + i) & 0x07) == 0) && ((((uint32_t)data) + i) & 0x07) == 0)) {
178+
uint32_t idx = i >> 2;
150179
src = (uint32_t *)data;
151-
dst = (uint32_t *)(address + FLASHMEM_ADDRESS_SPACE);
180+
dst = (uint32_t *)(address);
181+
pdword[0] = src[idx];
182+
pdword[1] = src[idx + 1];
152183
flash_wait_complete();
153-
dst[i >> 2] = src[i >> 2];
154-
dst[(i >> 2) + 1] = src[(i >> 2) + 1];
184+
dst[idx] = pdword[0];
185+
dst[idx + 1] = pdword[1];
155186
flash_wait_complete();
156187
i+=8;
157188
} else {
@@ -176,42 +207,26 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
176207
return 0;
177208
}
178209

179-
void RAMFUNCTION hal_flash_unlock(void)
180-
{
181-
flash_wait_complete();
182-
if ((FLASH_CR & FLASH_CR_LOCK) != 0) {
183-
FLASH_KEY = FLASH_KEY1;
184-
DMB();
185-
FLASH_KEY = FLASH_KEY2;
186-
DMB();
187-
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
188-
;
189-
}
190-
}
191-
192-
void RAMFUNCTION hal_flash_lock(void)
193-
{
194-
flash_wait_complete();
195-
if ((FLASH_CR & FLASH_CR_LOCK) == 0)
196-
FLASH_CR |= FLASH_CR_LOCK;
197-
}
198-
199210

200211
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
201212
{
202-
int start = -1, end = -1;
203213
uint32_t end_address;
204214
uint32_t p;
205215
if (len == 0)
206216
return -1;
217+
address -= FLASHMEM_ADDRESS_SPACE;
207218
end_address = address + len - 1;
219+
flash_wait_complete();
208220
for (p = address; p < end_address; p += FLASH_PAGE_SIZE) {
209-
uint32_t reg = FLASH_CR & (~(FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT));
210-
FLASH_CR = reg | ((p >> 12) << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER | FLASH_CR_PG;
221+
uint32_t reg;
222+
flash_clear_errors();
223+
reg = FLASH_CR & ~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_FSTPG | FLASH_CR_PG);
224+
FLASH_CR = reg | ((p >> 12) << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER;
211225
DMB();
212226
FLASH_CR |= FLASH_CR_STRT;
227+
DMB();
213228
flash_wait_complete();
214-
FLASH_CR &= ~(FLASH_CR_PER | FLASH_CR_PG);
229+
FLASH_CR &= ~(FLASH_CR_PER);
215230
}
216231
return 0;
217232
}
@@ -310,7 +325,6 @@ void hal_prepare_boot(void)
310325
#ifdef SPI_FLASH
311326
spi_release();
312327
#endif
313-
hal_flash_lock();
314328
clock_pll_off();
315329
}
316330

tools/keytools/keygen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ int main(int argc, char** argv)
301301
fclose(f);
302302
printf("** Warning: key file already exist! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ");
303303
fflush(stdout);
304-
gets(reply);
304+
scanf("%s", reply);
305305
printf("Reply is [%s]\n", reply);
306306
if (strcmp(reply, "Yes, I am sure!") != 0) {
307307
printf("Operation aborted by user.");

tools/keytools/sign.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void header_append_u16(uint8_t* header, uint32_t* idx, uint16_t tmp16)
103103
memcpy(&header[*idx], &tmp16, sizeof(tmp16));
104104
*idx += sizeof(tmp16);
105105
}
106-
static void header_append_tag(uint8_t* header, uint32_t* idx, uint16_t tag,
106+
static void header_append_tag(uint8_t* header, uint32_t* idx, uint16_t tag,
107107
uint16_t len, void* data)
108108
{
109109
header_append_u16(header, idx, tag);
@@ -227,7 +227,7 @@ int main(int argc, char** argv)
227227
if (tmpstr) {
228228
*tmpstr = '\0'; /* null terminate at last "." */
229229
}
230-
snprintf(output_image_file, sizeof(output_image_file), "%s_v%s_%s.bin",
230+
snprintf(output_image_file, sizeof(output_image_file), "%s_v%s_%s.bin",
231231
(char*)buf, fw_version, sha_only ? "digest" : "signed");
232232

233233
printf("Update type: %s\n", self_update ? "wolfBoot" : "Firmware");
@@ -256,14 +256,34 @@ int main(int argc, char** argv)
256256
}
257257

258258
/* key type "auto" selection */
259-
if (key_buffer_sz == 64) {
260-
if (sign == SIGN_ECC256) {
261-
printf("Error: key size does not match the cipher selected\n");
259+
if (key_buffer_sz == 32) {
260+
if ((sign != SIGN_ED25519) && !manual_sign && !sha_only ) {
261+
printf("Error: key too short for cipher\n");
262262
goto exit;
263263
}
264-
if (sign == SIGN_AUTO) {
264+
if (sign == SIGN_AUTO && (manual_sign || sha_only)) {
265+
printf("ed25519 public key autodetected\n");
265266
sign = SIGN_ED25519;
266-
printf("ed25519 key autodetected\n");
267+
}
268+
269+
}
270+
else if (key_buffer_sz == 64) {
271+
if (sign == SIGN_ECC256) {
272+
if (!manual_sign && !sha_only) {
273+
printf("Error: key size does not match the cipher selected\n");
274+
goto exit;
275+
} else {
276+
printf("ECC256 public key detected\n");
277+
}
278+
}
279+
if (sign == SIGN_AUTO) {
280+
if (!manual_sign && !sha_only) {
281+
sign = SIGN_ED25519;
282+
printf("ed25519 key autodetected\n");
283+
} else {
284+
sign = SIGN_ECC256;
285+
printf("ecc256 public key autodetected\n");
286+
}
267287
}
268288
}
269289
else if (key_buffer_sz == 96) {
@@ -298,7 +318,7 @@ int main(int argc, char** argv)
298318
}
299319

300320
/* get header and signature sizes */
301-
if (sign == SIGN_ED25519) {
321+
if (sign == SIGN_ED25519) {
302322
header_sz = 256;
303323
signature_sz = 64;
304324
}
@@ -323,7 +343,7 @@ int main(int argc, char** argv)
323343
if (!sha_only && !manual_sign) {
324344
/* import (decode) private key for signing */
325345
if (sign == SIGN_ED25519) {
326-
#ifdef HAVE_ED25519
346+
#ifdef HAVE_ED25519
327347
ret = wc_ed25519_init(&key.ed);
328348
if (ret == 0) {
329349
pubkey = key_buffer + ED25519_KEY_SIZE;
@@ -336,7 +356,7 @@ int main(int argc, char** argv)
336356
#ifdef HAVE_ECC
337357
ret = wc_ecc_init(&key.ecc);
338358
if (ret == 0) {
339-
ret = wc_ecc_import_unsigned(&key.ecc, &key_buffer[0], &key_buffer[32],
359+
ret = wc_ecc_import_unsigned(&key.ecc, &key_buffer[0], &key_buffer[32],
340360
&key_buffer[64], ECC_SECP256R1);
341361
if (ret == 0) {
342362
pubkey = key_buffer; /* first 64 bytes is public porition */
@@ -405,22 +425,22 @@ int main(int argc, char** argv)
405425

406426
/* Append Version field */
407427
fw_version32 = strtol(fw_version, NULL, 10);
408-
header_append_tag(header, &header_idx, HDR_VERSION, HDR_VERSION_LEN,
428+
header_append_tag(header, &header_idx, HDR_VERSION, HDR_VERSION_LEN,
409429
&fw_version32);
410430

411431
/* Append Four pad bytes, so timestamp is aligned */
412432
header_idx += 4; /* memset 0xFF above handles value */
413433

414434
/* Append Timestamp field */
415435
stat(image_file, &attrib);
416-
header_append_tag(header, &header_idx, HDR_TIMESTAMP, HDR_TIMESTAMP_LEN,
436+
header_append_tag(header, &header_idx, HDR_TIMESTAMP, HDR_TIMESTAMP_LEN,
417437
&attrib.st_ctime);
418438

419439
/* Append Image type field */
420440
image_type = (uint16_t)sign;
421441
if (!self_update)
422442
image_type |= HDR_IMG_TYPE_APP;
423-
header_append_tag(header, &header_idx, HDR_IMG_TYPE, HDR_IMG_TYPE_LEN,
443+
header_append_tag(header, &header_idx, HDR_IMG_TYPE, HDR_IMG_TYPE_LEN,
424444
&image_type);
425445

426446
/* Six pad bytes, Sha-3 requires 8-byte alignment. */

tools/keytools/sign.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,27 @@
135135
kf = open(key_file, "rb")
136136
wolfboot_key_buffer = kf.read(4096)
137137
wolfboot_key_buffer_len = len(wolfboot_key_buffer)
138-
if wolfboot_key_buffer_len == 64:
139-
if (sign == 'ecc256'):
140-
print("Error: key size does not match the cipher selected")
138+
if wolfboot_key_buffer_len == 32:
139+
if (sign != 'ed25519' and not manual_sign and not sha_only):
140+
print("Error: key too short for cipher")
141141
sys.exit(1)
142-
if sign == 'auto':
142+
elif sign == 'auto' and (manual_sign or sha_only):
143143
sign = 'ed25519'
144-
print("'ed25519' key autodetected.")
144+
print("'ed25519' public key autodetected.")
145+
elif wolfboot_key_buffer_len == 64:
146+
if (sign == 'ecc256'):
147+
if not manual_sign and not sha_only:
148+
print("Error: key size does not match the cipher selected")
149+
sys.exit(1)
150+
else:
151+
print("Ecc256 public key detected")
152+
if sign == 'auto':
153+
if (manual_sign or sha_only):
154+
sign = 'ecc256'
155+
print("'ecc256' public key autodetected.")
156+
else:
157+
sign = 'ed25519'
158+
print("'ed25519' key autodetected.")
145159
elif wolfboot_key_buffer_len == 96:
146160
if (sign == 'ed25519'):
147161
print("Error: key size does not match the cipher selected")

0 commit comments

Comments
 (0)