From e47310c3d5ef6fc10e60562d2d88da0927bc1fce Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 18 Dec 2019 23:36:25 +0100 Subject: WIP Revamp ChaCha20 seeding - chacha20_prng_block() uses counter in the state struct - chacha20_setup() replaces chacha20_prng_reseed() and fills the whole state struct, fixing a bug where only half of the key was being set; as a result of 'counter' being set, a state struct filled with entropy from the TRNG makes reseeding occur after a random number of rounds instead of after a fixed 2^32-1 rounds - decrementing of the block counter is done in chacha20_prng_block() - chacha output is copied to buf _after_ the interrupt driven transmission of buf to UART has finished, to stop the race between reading and refilling of buf --- src/cc20rng/cc20_prng.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/cc20rng/cc20_prng.h') diff --git a/src/cc20rng/cc20_prng.h b/src/cc20rng/cc20_prng.h index 7b597d0..08f78d7 100644 --- a/src/cc20rng/cc20_prng.h +++ b/src/cc20rng/cc20_prng.h @@ -3,17 +3,22 @@ #include -#define CHACHA20_MAX_BLOCK_COUNTER 0xffffffff -#define CHACHA20_NUM_WORDS 16 -#define CHACHA20_BLOCK_SIZE (CHACHA20_NUM_WORDS * 4) +#define CHACHA20_BLOCK_SIZE_WORDS 16 +#define CHACHA20_BLOCK_SIZE (CHACHA20_BLOCK_SIZE_WORDS * 4) struct cc20_state { - uint32_t i[CHACHA20_NUM_WORDS]; + union { + struct { + uint32_t constant[4]; + uint32_t key[8]; + uint32_t counter; + uint32_t nonce[3]; + } s; + uint32_t i[CHACHA20_BLOCK_SIZE_WORDS]; + }; }; -extern void chacha20_prng_reseed(struct cc20_state *cc, uint32_t *entropy); -extern void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, - struct cc20_state *out); -extern int chacha20_prng_self_test(); +void chacha20_prng_block(struct cc20_state *cc, uint8_t *out); +int chacha20_prng_self_test(); #endif /* __STM32_CHACHA20_H */ -- cgit v1.2.3