From eade3e11c5d7bd93902ac415e88a627019cb559e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Str=C3=B6mbergson?= Date: Thu, 4 Apr 2024 15:27:31 +0200 Subject: [PATCH] Fill RAM with random data using xorwow. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xorwow provides significantly better random data, compared to previously used function. Making it harder to predict what data RAM is filled with. It adds a startup time of approx 80 ms, but can be compensated with optimising other parts of the startup routine. This changes both firmware and fpga hashes. Signed-off-by: Joachim Strömbergson --- .../application_fpga.bin.sha256 | 2 +- hw/application_fpga/firmware.bin.sha512 | 2 +- hw/application_fpga/fw/tk1/main.c | 26 ++++++++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/hw/application_fpga/application_fpga.bin.sha256 b/hw/application_fpga/application_fpga.bin.sha256 index b9bbb0a..6e97f7d 100644 --- a/hw/application_fpga/application_fpga.bin.sha256 +++ b/hw/application_fpga/application_fpga.bin.sha256 @@ -1 +1 @@ -321924aa3b26507f2a02325750e63c83b306c7831f8e2c87e8d198cecf8cc1c1 application_fpga.bin +621e2b838775564ce625bf6ce9def8b85a56f31768742ac05b6fb3a0b056ccc6 application_fpga.bin diff --git a/hw/application_fpga/firmware.bin.sha512 b/hw/application_fpga/firmware.bin.sha512 index 676918e..69c9e8c 100644 --- a/hw/application_fpga/firmware.bin.sha512 +++ b/hw/application_fpga/firmware.bin.sha512 @@ -1 +1 @@ -06d0aafcc763307420380a8c5a324f3fccfbba6af7ff6fe0facad684ebd69dd43234c8531a096c77c2dc3543f8b8b629c94136ca7e257ca560da882e4dbbb025 firmware.bin +fdeda3316d3b27b21dfd7f626a87155c28fff6470173cc25e560d5e87f5ce6468146922a60e8ef31015769d67ccae519437975231d284a9a602c128d014f239c firmware.bin diff --git a/hw/application_fpga/fw/tk1/main.c b/hw/application_fpga/fw/tk1/main.c index 6cab4d1..b696c9d 100644 --- a/hw/application_fpga/fw/tk1/main.c +++ b/hw/application_fpga/fw/tk1/main.c @@ -54,6 +54,7 @@ static enum state loading_commands(const struct frame_header *hdr, const uint8_t *cmd, enum state state, struct context *ctx); static void run(const struct context *ctx); +static uint32_t xorwow(uint32_t state, uint32_t acc); static void scramble_ram(void); static void print_hw_version(void) @@ -363,24 +364,35 @@ static void run(const struct context *ctx) __builtin_unreachable(); } +static uint32_t xorwow(uint32_t state, uint32_t acc) +{ + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + state += acc; + return state; +} + static void scramble_ram(void) { uint32_t *ram = (uint32_t *)(TK1_RAM_BASE); - uint32_t rnd = rnd_word(); - uint32_t rnd_incr = rnd_word(); // Set RAM address and data scrambling values *ram_rand = rnd_word(); *ram_scramble = rnd_word(); - // Fill RAM with random data (FW does not use RAM, has its stack in - // FW_RAM) + // Fill RAM with random data + // Get random state and accumulator seeds. + uint32_t data_state = rnd_word(); + uint32_t data_acc = rnd_word(); + for (uint32_t w = 0; w < TK1_RAM_SIZE / 4; w++) { - ram[w] = rnd; - rnd += rnd_incr; + data_state = xorwow(data_state, data_acc); + ram[w] = data_state; } - // Set new scrambling values, for all use of RAM by app + // Set new address and RAM scrambling values, + // for all use of RAM by app. *ram_rand = rnd_word(); *ram_scramble = rnd_word(); }