Skip to content

Commit 4c2d2b7

Browse files
dgarskedanielinux
authored andcommitted
Add support for Armored mode with IAR. Currently only supports ECDSA and Cortex-M. ZD19190
1 parent 519e3b7 commit 4c2d2b7

File tree

5 files changed

+179
-17
lines changed

5 files changed

+179
-17
lines changed

IDE/IAR/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ Using the ST-LINK Utility, perform the following steps:
9090

9191
If you are using a STM32F407-discovery board, a red LED will turn on upon application boot.
9292

93+
## Armored Mode (Glitch Resistance)
9394

94-
95+
If you would like to enable the "Armored" mode (glitch resistance) in IAR you can set the compiler pre-processor macro `WOLFBOOT_ARMORED`. Note: This has only been tested with ECDSA on Cortex-M.

include/image.h

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,18 @@ int wolfBot_get_dts_size(void *dts_addr);
168168

169169

170170
#if (defined(WOLFBOOT_ARMORED) && defined(__WOLFBOOT))
171-
172-
#if !defined(ARCH_ARM) || !defined(__GNUC__)
173-
# error WOLFBOOT_ARMORED only available with arm-gcc compiler
171+
#if !defined(ARCH_ARM) || (!defined(__GNUC__) && \
172+
!(defined(__ICCARM__) && defined(__IAR_SYSTEMS_ICC__)))
173+
# error WOLFBOOT_ARMORED only available for ARM with IAR or gcc compilers
174174
#endif
175175

176+
#if defined(__GNUC__)
176177
#define likely(x) __builtin_expect((x),1)
177178
#define unlikely(x) __builtin_expect((x),0)
179+
#else
180+
#define likely(x) (x)
181+
#define unlikely(x) (x)
182+
#endif
178183

179184
struct wolfBoot_image {
180185
uint8_t *hdr;
@@ -434,6 +439,8 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
434439
*
435440
* Double check by reading the value in p_res from memory a few times.
436441
*/
442+
#if defined(__GNUC__)
443+
437444
#define VERIFY_FN(img,p_res,fn,...) \
438445
/* Redundant set of r0=50*/ \
439446
asm volatile("mov r0, #50":::"r0"); \
@@ -468,6 +475,59 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
468475
asm volatile("nope:"); \
469476
asm volatile("nop")
470477

478+
#elif defined(__ICCARM__) && defined(__IAR_SYSTEMS_ICC__)
479+
480+
#define VERIFY_FN(img, p_res, fn, ...) \
481+
do { \
482+
__asm volatile( \
483+
"mov r0, #50\n" \
484+
"mov r0, #50\n" \
485+
"mov r0, #50\n" \
486+
: /* No output operands */ \
487+
: /* No input operands */ \
488+
: "r0" /* Clobbered registers */ \
489+
); \
490+
void (*confirm_func)(struct wolfBoot_image *) = \
491+
wolfBoot_image_confirm_signature_ok; \
492+
fn(__VA_ARGS__); \
493+
__asm volatile( \
494+
"cmp r0, #0\n" \
495+
"bne 1f\n" \
496+
"cmp r0, #0\n" \
497+
"bne 1f\n" \
498+
"cmp r0, #0\n" \
499+
"bne 1f\n" \
500+
"cmp r0, #0\n" \
501+
"bne 1f\n" \
502+
"ldr r2, [%0]\n" \
503+
"cmp r2, #1\n" \
504+
"bne 1f\n" \
505+
"ldr r2, [%0]\n" \
506+
"cmp r2, #1\n" \
507+
"bne 1f\n" \
508+
"ldr r2, [%0]\n" \
509+
"cmp r2, #1\n" \
510+
"bne 1f\n" \
511+
"ldr r2, [%0]\n" \
512+
"cmp r2, #1\n" \
513+
"bne 1f\n" \
514+
/* Load 'img' into r0 (first argument to the function) */ \
515+
"mov r0, %1\n" \
516+
/* Load the function pointer into r3 */ \
517+
"mov r3, %2\n" \
518+
"blx r3\n"\
519+
"b 2f\n" \
520+
"1:\n" \
521+
"nop\n" \
522+
"2:\n" \
523+
: /* No output operands */ \
524+
: "r"(p_res), "r"(img), "r"(confirm_func) /* Input operands */ \
525+
: "r0", "r2", "lr" /* Clobbered registers */ \
526+
); \
527+
} while (0)
528+
#endif
529+
530+
471531
/**
472532
* This macro is only invoked after a successful update version check, prior to
473533
* initiating the update installation.
@@ -486,6 +546,8 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
486546
* version is not strictly greater than the current one.
487547
*
488548
*/
549+
#if defined(__GNUC__)
550+
489551
#define VERIFY_VERSION_ALLOWED(fb_ok) \
490552
/* Stash the registry values */ \
491553
asm volatile("push {r4, r5, r6, r7}"); \
@@ -575,6 +637,96 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
575637
/* Restore previously saved registry values */ \
576638
asm volatile("pop {r4, r5, r6, r7}":::"r4", "r5", "r6", "r7")
577639

640+
#elif defined(__ICCARM__) && defined(__IAR_SYSTEMS_ICC__)
641+
642+
#define VERIFY_VERSION_ALLOWED(fb_ok) \
643+
do { \
644+
__asm volatile( \
645+
"push {r4, r5, r6, r7}\n" \
646+
"mov r0, #0\n" \
647+
"mov r4, #1\n" \
648+
"mov r5, #0\n" \
649+
"mov r6, #2\n" \
650+
"mov r7, #0\n" \
651+
"mov r0, #0\n" \
652+
"mov r4, #1\n" \
653+
"mov r5, #0\n" \
654+
"mov r6, #2\n" \
655+
"mov r7, #0\n" \
656+
"mov r0, %0\n" \
657+
"cmp r0, #1\n" \
658+
"bne 1f\n" \
659+
"cmp r0, #1\n" \
660+
"bne 1f\n" \
661+
"cmp r0, #1\n" \
662+
"bne 1f\n" \
663+
"b 2f\n" \
664+
"1:\n" \
665+
"mov r0, #1\n" \
666+
"mov r0, #1\n" \
667+
"mov r0, #1\n" \
668+
"bl wolfBoot_get_image_version\n" \
669+
"mov r5, r0\n" \
670+
"mov r5, r0\n" \
671+
"mov r5, r0\n" \
672+
"mov r0, #1\n" \
673+
"mov r0, #1\n" \
674+
"mov r0, #1\n" \
675+
"bl wolfBoot_get_image_version\n" \
676+
"mov r7, r0\n" \
677+
"mov r7, r0\n" \
678+
"mov r7, r0\n" \
679+
"cmp r5, r7\n" \
680+
"bne .\n" \
681+
"cmp r5, r7\n" \
682+
"bne .-4\n" \
683+
"cmp r5, r7\n" \
684+
"bne .-8\n" \
685+
"cmp r5, r7\n" \
686+
"bne .-12\n" \
687+
"mov r0, #0\n" \
688+
"mov r0, #0\n" \
689+
"mov r0, #0\n" \
690+
"bl wolfBoot_get_image_version\n" \
691+
"mov r4, r0\n" \
692+
"mov r4, r0\n" \
693+
"mov r4, r0\n" \
694+
"mov r0, #0\n" \
695+
"mov r0, #0\n" \
696+
"mov r0, #0\n" \
697+
"bl wolfBoot_get_image_version\n" \
698+
"mov r6, r0\n" \
699+
"mov r6, r0\n" \
700+
"mov r6, r0\n" \
701+
"cmp r4, r6\n" \
702+
"bne .\n" \
703+
"cmp r4, r6\n" \
704+
"bne .-4\n" \
705+
"cmp r4, r6\n" \
706+
"bne .-8\n" \
707+
"cmp r4, r6\n" \
708+
"bne .-12\n" \
709+
"mov r0, #0\n" \
710+
"mov r0, #0\n" \
711+
"mov r0, #0\n" \
712+
"cmp r4, r5\n" \
713+
"bge .\n" \
714+
"cmp r6, r7\n" \
715+
"bge .-4\n" \
716+
"cmp r4, r5\n" \
717+
"bge .-8\n" \
718+
"cmp r6, r7\n" \
719+
"bge .-12\n" \
720+
"2:\n" \
721+
"pop {r4, r5, r6, r7}\n" \
722+
: /* No output operands */ \
723+
: "r"(fb_ok) /* Input operands */ \
724+
: "r0", "r4", "r5", "r6", "r7" /* Clobbered registers */ \
725+
); \
726+
} while (0)
727+
#endif
728+
729+
578730
#define CONFIRM_MASK_VALID(id, mask) \
579731
asm volatile("mov r1, %0" :: "r"(id):"r1"); \
580732
/* id &= 0x0F */ \

src/update_flash.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,12 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
518518

519519

520520
#ifdef WOLFBOOT_ARMORED
521-
# pragma GCC push_options
522-
# pragma GCC optimize("O0")
521+
# ifdef __GNUC__
522+
# pragma GCC push_options
523+
# pragma GCC optimize("O0")
524+
# elif defined(__IAR_SYSTEMS_ICC__)
525+
# pragma optimize=none
526+
# endif
523527
#endif
524528

525529
/* Reserve space for two sectors in case of NVM_FLASH_WRITEONCE, for redundancy */
@@ -951,7 +955,7 @@ void RAMFUNCTION wolfBoot_start(void)
951955
wolfBoot_check_self_update();
952956
#endif
953957

954-
#ifdef NVM_FLASH_WRITEONCE
958+
#ifdef NVM_FLASH_WRITEONCE
955959
/* nvm_select_fresh_sector needs unlocked flash in cases where the unused
956960
* sector needs to be erased */
957961
hal_flash_unlock();
@@ -963,7 +967,7 @@ void RAMFUNCTION wolfBoot_start(void)
963967
bootRet = wolfBoot_get_partition_state(PART_BOOT, &bootState);
964968
updateRet = wolfBoot_get_partition_state(PART_UPDATE, &updateState);
965969

966-
#ifdef NVM_FLASH_WRITEONCE
970+
#ifdef NVM_FLASH_WRITEONCE
967971
hal_flash_lock();
968972
#ifdef EXT_FLASH
969973
ext_flash_lock();
@@ -1039,6 +1043,11 @@ void RAMFUNCTION wolfBoot_start(void)
10391043
hal_prepare_boot();
10401044
do_boot((void *)boot.fw_base);
10411045
}
1046+
10421047
#ifdef WOLFBOOT_ARMORED
1043-
# pragma GCC pop_options
1048+
# ifdef __GNUC__
1049+
# pragma GCC pop_options
1050+
# elif defined(__IAR_SYSTEMS_ICC__)
1051+
# pragma optimize=default
1052+
# endif
10441053
#endif

tools/test-renode.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ XMSS_OPTS=WOLFBOOT_XMSS_PARAMS='XMSS-SHA2_10_256' WOLFBOOT_SMALL_STACK=0 \
2525
IMAGE_SIGNATURE_SIZE=2500 IMAGE_HEADER_SIZE=5000
2626

2727
ifneq ("$(wildcard $(WOLFBOOT_ROOT)/tools/keytools/keygen.exe)","")
28-
KEYGEN_TOOL?=$(WOLFBOOT_ROOT)/tools/keytools/keygen.exe
28+
KEYGEN_TOOL?="$(WOLFBOOT_ROOT)/tools/keytools/keygen.exe"
2929
else
30-
KEYGEN_TOOL?=$(WOLFBOOT_ROOT)/tools/keytools/keygen
30+
KEYGEN_TOOL?="$(WOLFBOOT_ROOT)/tools/keytools/keygen"
3131
endif
3232

3333
ifneq ("$(wildcard $(WOLFBOOT_ROOT)/tools/keytools/sign.exe)","")
34-
SIGN_TOOL?=$(WOLFBOOT_ROOT)/tools/keytools/sign.exe
34+
SIGN_TOOL?="$(WOLFBOOT_ROOT)/tools/keytools/sign.exe"
3535
else
36-
SIGN_TOOL?=$(WOLFBOOT_ROOT)/tools/keytools/sign
36+
SIGN_TOOL?="$(WOLFBOOT_ROOT)/tools/keytools/sign"
3737
endif
3838

3939
ifeq ($(TARGET),stm32f7)

tools/test.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ SIGN_ENC_ARGS=
1010
DELTA_DATA_SIZE?=2000
1111

1212
ifneq ("$(wildcard $(WOLFBOOT_ROOT)/tools/keytools/keygen.exe)","")
13-
KEYGEN_TOOL=$(WOLFBOOT_ROOT)/tools/keytools/keygen.exe
13+
KEYGEN_TOOL="$(WOLFBOOT_ROOT)/tools/keytools/keygen.exe"
1414
else
15-
KEYGEN_TOOL=$(WOLFBOOT_ROOT)/tools/keytools/keygen
15+
KEYGEN_TOOL="$(WOLFBOOT_ROOT)/tools/keytools/keygen"
1616
endif
1717

1818
ifneq ("$(wildcard $(WOLFBOOT_ROOT)/tools/keytools/sign.exe)","")
19-
SIGN_TOOL=$(WOLFBOOT_ROOT)/tools/keytools/sign.exe
19+
SIGN_TOOL="$(WOLFBOOT_ROOT)/tools/keytools/sign.exe"
2020
else
21-
SIGN_TOOL=$(WOLFBOOT_ROOT)/tools/keytools/sign
21+
SIGN_TOOL="$(WOLFBOOT_ROOT)/tools/keytools/sign"
2222
endif
2323

2424
# Make sign algorithm argument

0 commit comments

Comments
 (0)