Fill RAM with random data using xorwow.

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 <joachim@assured.se>
This commit is contained in:
Joachim Strömbergson 2024-04-04 15:27:31 +02:00 committed by dehanj
parent 09df7ae97f
commit eade3e11c5
No known key found for this signature in database
GPG Key ID: 3707A9DBF4BB8F1A
3 changed files with 21 additions and 9 deletions

View File

@ -1 +1 @@
321924aa3b26507f2a02325750e63c83b306c7831f8e2c87e8d198cecf8cc1c1 application_fpga.bin 621e2b838775564ce625bf6ce9def8b85a56f31768742ac05b6fb3a0b056ccc6 application_fpga.bin

View File

@ -1 +1 @@
06d0aafcc763307420380a8c5a324f3fccfbba6af7ff6fe0facad684ebd69dd43234c8531a096c77c2dc3543f8b8b629c94136ca7e257ca560da882e4dbbb025 firmware.bin fdeda3316d3b27b21dfd7f626a87155c28fff6470173cc25e560d5e87f5ce6468146922a60e8ef31015769d67ccae519437975231d284a9a602c128d014f239c firmware.bin

View File

@ -54,6 +54,7 @@ static enum state loading_commands(const struct frame_header *hdr,
const uint8_t *cmd, enum state state, const uint8_t *cmd, enum state state,
struct context *ctx); struct context *ctx);
static void run(const 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 scramble_ram(void);
static void print_hw_version(void) static void print_hw_version(void)
@ -363,24 +364,35 @@ static void run(const struct context *ctx)
__builtin_unreachable(); __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) static void scramble_ram(void)
{ {
uint32_t *ram = (uint32_t *)(TK1_RAM_BASE); 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 // Set RAM address and data scrambling values
*ram_rand = rnd_word(); *ram_rand = rnd_word();
*ram_scramble = rnd_word(); *ram_scramble = rnd_word();
// Fill RAM with random data (FW does not use RAM, has its stack in // Fill RAM with random data
// FW_RAM) // 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++) { for (uint32_t w = 0; w < TK1_RAM_SIZE / 4; w++) {
ram[w] = rnd; data_state = xorwow(data_state, data_acc);
rnd += rnd_incr; 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_rand = rnd_word();
*ram_scramble = rnd_word(); *ram_scramble = rnd_word();
} }