Skip to content

Commit 98e617d

Browse files
jhoenickeprusnak
authored andcommitted
startup: use custom reset_handler
+ group confidential data in one place + zero all SRAM where needed
1 parent a01ba51 commit 98e617d

21 files changed

+145
-63
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
OBJS += startup.o
12
OBJS += buttons.o
23
OBJS += layout.o
34
OBJS += oled.o

Makefile.include

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ LD = $(PREFIX)gcc
77
OBJCOPY = $(PREFIX)objcopy
88
OBJDUMP = $(PREFIX)objdump
99
AR = $(PREFIX)ar
10+
AS = $(PREFIX)as
1011
FLASH = st-flash
1112
OPENOCD = openocd
1213

1314
OPTFLAGS ?= -O3
1415
DBGFLAGS ?= -g -DNDEBUG
16+
CPUFLAGS ?= -mcpu=cortex-m3 -mthumb
17+
FPUFLAGS ?= -msoft-float
1518

1619
CFLAGS += $(OPTFLAGS) \
1720
$(DBGFLAGS) \
@@ -40,10 +43,10 @@ CFLAGS += $(OPTFLAGS) \
4043
-ffunction-sections \
4144
-fdata-sections \
4245
-fstack-protector-all \
43-
-mcpu=cortex-m3 \
44-
-mthumb \
45-
-msoft-float \
46+
$(CPUFLAGS) \
47+
$(FPUFLAGS) \
4648
-DSTM32F2 \
49+
-DCONFIDENTIAL='__attribute__((section("confidential")))' \
4750
-I$(TOOLCHAIN_DIR)/include \
4851
-I$(TOP_DIR) \
4952
-I$(TOP_DIR)gen \
@@ -83,9 +86,8 @@ LDFLAGS += --static \
8386
-T$(LDSCRIPT) \
8487
-nostartfiles \
8588
-Wl,--gc-sections \
86-
-mcpu=cortex-m3 \
87-
-mthumb \
88-
-msoft-float
89+
$(CPUFLAGS) \
90+
$(FPUFLAGS)
8991

9092
all: $(NAME).bin
9193

@@ -128,6 +130,9 @@ $(NAME).list: $(NAME).elf
128130
$(NAME).elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f2.a $(TOP_DIR)/libtrezor.a
129131
$(LD) -o $(NAME).elf $(OBJS) -ltrezor -lopencm3_stm32f2 $(LDFLAGS)
130132

133+
%.o: %.s Makefile
134+
$(AS) $(CPUFLAGS) -o $@ $<
135+
131136
%.o: %.c Makefile
132137
$(CC) $(CFLAGS) -MMD -o $@ -c $<
133138

bootloader/bootloader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void show_unofficial_warning(const uint8_t *hash)
7878

7979
void __attribute__((noreturn)) load_app(void)
8080
{
81+
// zero out SRAM
82+
memset_reg(_ram_start, _ram_end, 0);
83+
8184
load_vector_table((const vector_table_t *) FLASH_APP_START);
8285
}
8386

bootloader/bootloader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
#define VERSION_MAJOR 1
2424
#define VERSION_MINOR 3
25-
#define VERSION_PATCH 2
25+
#define VERSION_PATCH 3
2626

2727
#define STR(X) #X
2828
#define VERSTR(X) STR(X)
2929

3030
#define VERSION_MAJOR_CHAR "\x01"
3131
#define VERSION_MINOR_CHAR "\x03"
32-
#define VERSION_PATCH_CHAR "\x02"
32+
#define VERSION_PATCH_CHAR "\x03"
3333

3434
#include <stdbool.h>
3535
#include "memory.h"

firmware/Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
ifeq ($(FASTFLASH),1)
2-
APPVER = 2.0.0
1+
APPVER = 1.0.0
32

3+
ifeq ($(FASTFLASH),1)
44
OBJS += fastflash.o
55
OBJS += bootloader.o
6-
else
7-
APPVER = 1.0.0
86
endif
97

108
NAME = trezor
@@ -93,6 +91,7 @@ CFLAGS += -DUSE_ETHEREUM=1
9391

9492
bootloader.o: ../fastflash/bootloader.bin
9593
$(OBJCOPY) -I binary -O elf32-littlearm -B arm \
94+
--redefine-sym _binary_$(shell echo -n "$<" | tr -c "[:alnum:]" "_")_start=__bootloader_start__ \
9695
--redefine-sym _binary_$(shell echo -n "$<" | tr -c "[:alnum:]" "_")_size=__bootloader_size__ \
97-
--rename-section .data=.bootloader \
96+
--rename-section .data=.rodata \
9897
$< $@

firmware/ethereum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
static bool ethereum_signing = false;
3838
static uint32_t data_total, data_left;
3939
static EthereumTxRequest msg_tx_request;
40-
static uint8_t privkey[32];
40+
static CONFIDENTIAL uint8_t privkey[32];
4141
static uint8_t chain_id;
4242
struct SHA3_CTX keccak_ctx;
4343

firmware/fastflash.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
#include <stdint.h>
2525
#include <string.h>
2626

27-
extern uint8_t __bootloader_loadaddr__[];
28-
extern uint8_t __bootloader_runaddr__[];
29-
extern uint8_t __bootloader_size__[];
30-
31-
void load_bootloader(void)
32-
{
33-
memcpy(__bootloader_runaddr__, __bootloader_loadaddr__, (size_t) __bootloader_size__);
34-
}
27+
#define bootloader_vec ((vector_table_t *) 0x20000000)
3528

3629
void __attribute__((noreturn)) run_bootloader(void)
3730
{
38-
load_vector_table((const vector_table_t *) __bootloader_runaddr__);
31+
extern uint8_t __bootloader_start__[];
32+
extern uint8_t __bootloader_size__[];
33+
34+
// zero out SRAM
35+
memset_reg(_ram_start, _ram_end, 0);
36+
37+
// copy bootloader
38+
memcpy(bootloader_vec, __bootloader_start__, (size_t) __bootloader_size__);
39+
40+
load_vector_table(bootloader_vec);
3941
}

firmware/fastflash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#ifndef __FASTFLASH_H__
2121
#define __FASTFLASH_H__
2222

23-
void load_bootloader(void);
2423
void __attribute__((noreturn)) run_bootloader(void);
2524

2625
#endif

firmware/fsm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const CoinType *fsm_getCoin(bool has_name, const char *name)
177177

178178
HDNode *fsm_getDerivedNode(const char *curve, uint32_t *address_n, size_t address_n_count)
179179
{
180-
static HDNode node;
180+
static CONFIDENTIAL HDNode node;
181181
if (!storage_getRootNode(&node, curve, true)) {
182182
fsm_sendFailure(FailureType_Failure_NotInitialized, _("Device not initialized or passphrase request cancelled or unsupported curve"));
183183
layoutHome();

firmware/signing.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static uint32_t inputs_count;
3232
static uint32_t outputs_count;
3333
static const CoinType *coin;
3434
static const HDNode *root;
35-
static HDNode node;
35+
static CONFIDENTIAL HDNode node;
3636
static bool signing = false;
3737
enum {
3838
STAGE_REQUEST_1_INPUT,
@@ -54,7 +54,8 @@ static TxInputType input;
5454
static TxOutputBinType bin_output;
5555
static TxStruct to, tp, ti;
5656
static SHA256_CTX hashers[3];
57-
static uint8_t privkey[32], pubkey[33], sig[64];
57+
static uint8_t CONFIDENTIAL privkey[32];
58+
static uint8_t pubkey[33], sig[64];
5859
static uint8_t hash_prevouts[32], hash_sequence[32],hash_outputs[32];
5960
static uint8_t hash_check[32];
6061
static uint64_t to_spend, authorized_amount, spending, change_spend;

0 commit comments

Comments
 (0)