From fb3b6f31c1b0c32f27145cbab944266591fc5df8 Mon Sep 17 00:00:00 2001 From: PeterKietzmann Date: Tue, 3 Jul 2018 14:32:24 +0200 Subject: [PATCH] address @gebart's comments --- cpu/cc2538/vectors.c | 4 +++- cpu/samd21/vectors.c | 5 +---- cpu/stm32f4/vectors.c | 4 +++- sys/Makefile.include | 1 + sys/include/puf_sram.h | 13 +++++++++---- sys/puf_sram/puf_sram.c | 11 +++++------ 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/cpu/cc2538/vectors.c b/cpu/cc2538/vectors.c index 2999661ab018..bda410fa72cf 100644 --- a/cpu/cc2538/vectors.c +++ b/cpu/cc2538/vectors.c @@ -29,7 +29,9 @@ extern uint32_t _srelocate; void pre_startup(void) { - puf_sram_uint32((uint8_t *)&_srelocate); + if (!puf_sram_softreset()) { + puf_sram_generate((uint8_t *)&_srelocate, SEED_RAM_LEN); + } } #endif diff --git a/cpu/samd21/vectors.c b/cpu/samd21/vectors.c index 177b07c86c3e..520aa3082fdb 100644 --- a/cpu/samd21/vectors.c +++ b/cpu/samd21/vectors.c @@ -28,13 +28,10 @@ /* SRAM memory marker defined in the linker script */ extern uint32_t _srelocate; -/* stack of the idle thread defined in kernel initialization */ -extern char idle_stack[]; - void pre_startup(void) { /* only generate a new seed when no software (or button) reset was detected */ if (!puf_sram_softreset()) { - puf_sram_uint32((uint8_t *)&_srelocate); + puf_sram_generate((uint8_t *)&_srelocate, SEED_RAM_LEN); } } #endif diff --git a/cpu/stm32f4/vectors.c b/cpu/stm32f4/vectors.c index d929c0f896a2..93ad7065b97b 100644 --- a/cpu/stm32f4/vectors.c +++ b/cpu/stm32f4/vectors.c @@ -29,7 +29,9 @@ extern uint32_t _srelocate; void pre_startup(void) { - puf_sram_uint32((uint8_t *)&_srelocate); + if (!puf_sram_softreset()) { + puf_sram_generate((uint8_t *)&_srelocate, SEED_RAM_LEN); + } } #endif diff --git a/sys/Makefile.include b/sys/Makefile.include index c283bb1392b3..6e7583d6b57a 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -91,6 +91,7 @@ ifneq (native,$(BOARD)) endif ifneq (,$(filter puf_sram,$(USEMODULE))) + # allocate space for 3 x 32 Bit integers: seed, state, marker export PUF_SRAM_LEN ?= 12 export LINKFLAGS += $(LINKFLAGPREFIX)--defsym=_puf_sram_len=$(PUF_SRAM_LEN) endif diff --git a/sys/include/puf_sram.h b/sys/include/puf_sram.h index 1d47eb035302..d693470fc8b9 100644 --- a/sys/include/puf_sram.h +++ b/sys/include/puf_sram.h @@ -48,25 +48,30 @@ extern uint32_t puf_sram_seed; /** * @brief Global seed state, allocated on the stack in puf_sram.c + * 0 means seed was generated from SRAM pattern, + 1 means missing power cycle detected, + 2 means power cycle detected. The state will most likely + be overwritten with 0 in the next steps */ extern uint32_t puf_sram_state; /** - * @brief builds hash from @p SEED_RAM_LEN bytes uninitialized SRAM, writs it + * @brief builds hash from @p SEED_RAM_LEN bytes uninitialized SRAM, writes it * to the global variable @p puf_sram_seed and returns the value * * @param[in] ram pointer to SRAM memory + * @param[in] len length of the memroy to consider * * @return a random number */ -uint32_t puf_sram_uint32(const uint8_t *ram); +uint32_t puf_sram_generate(const uint8_t *ram, size_t len); /** * @brief checks for a memory marker determine whether memory contains old data. Otherwise it assumes a reboot from power down mode * - * @return 0 when preceding power down phase is expected - * @return 1 when reset without power down was detected + * @return 0 when reset with power cycle was detected + * @return 1 when reset without power cycle was detected */ bool puf_sram_softreset(void); diff --git a/sys/puf_sram/puf_sram.c b/sys/puf_sram/puf_sram.c index b90f738b70d2..693aadb449d6 100644 --- a/sys/puf_sram/puf_sram.c +++ b/sys/puf_sram/puf_sram.c @@ -27,17 +27,17 @@ __attribute__((used,section(".puf_stack"))) uint32_t puf_sram_state; /* Allocation of the memory marker */ __attribute__((used,section(".puf_stack"))) uint32_t puf_sram_marker; -uint32_t puf_sram_uint32(const uint8_t *ram) +uint32_t puf_sram_generate(const uint8_t *ram, size_t len) { - /* seting state to 0 means seed was generated from - * SRAM pattern*/ - puf_sram_seed = dek_hash(ram, SEED_RAM_LEN); + /* build hash from start-up pattern */ + puf_sram_seed = dek_hash(ram, len); + /* write marker to a defined section for subsequent reset detection */ puf_sram_marker = PUF_SRAM_MARKER; + /* seting state to 0 means seed was generated from SRAM pattern */ puf_sram_state = 0; return puf_sram_seed; } -#ifndef HAVE_REBOOT_DETECTION bool puf_sram_softreset(void) { if(puf_sram_marker != PUF_SRAM_MARKER){ @@ -47,4 +47,3 @@ bool puf_sram_softreset(void) puf_sram_state = 1; return 1; } -#endif \ No newline at end of file