From da5bd87c135749b11c866aaf341c6c2c7c2ab9b2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 4 Nov 2024 13:06:11 -0500 Subject: Minor code cleanup --- stdlib/rng.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'stdlib') diff --git a/stdlib/rng.c b/stdlib/rng.c index c69a2771..357537b6 100644 --- a/stdlib/rng.c +++ b/stdlib/rng.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "arrays.h" @@ -22,7 +23,7 @@ public _Thread_local RNG_t default_rng; struct RNGState_t { chacha_ctx chacha; size_t unused_bytes; - uint8_t buf[16*64]; + uint8_t random_bytes[1024]; }; PUREFUNC static Text_t RNG$as_text(const RNG_t *rng, bool colorize, const TypeInfo_t *type) @@ -63,12 +64,12 @@ public RNG_t RNG$new(Array_t seed) static void rekey(RNG_t rng) { // Fill the buffer with the keystream - chacha_encrypt_bytes(&rng->chacha, rng->buf, rng->buf, sizeof(rng->buf)); + chacha_encrypt_bytes(&rng->chacha, rng->random_bytes, rng->random_bytes, sizeof(rng->random_bytes)); // Immediately reinitialize for backtracking resistance - chacha_keysetup(&rng->chacha, rng->buf, KEYSZ/8); - chacha_ivsetup(&rng->chacha, rng->buf + KEYSZ); - memset(rng->buf, 0, KEYSZ + IVSZ); - rng->unused_bytes = sizeof(rng->buf) - KEYSZ - IVSZ; + chacha_keysetup(&rng->chacha, rng->random_bytes, KEYSZ/8); + chacha_ivsetup(&rng->chacha, rng->random_bytes + KEYSZ); + explicit_bzero(rng->random_bytes, KEYSZ + IVSZ); + rng->unused_bytes = sizeof(rng->random_bytes) - KEYSZ - IVSZ; } static void random_bytes(RNG_t rng, uint8_t *dest, size_t needed) @@ -76,7 +77,7 @@ static void random_bytes(RNG_t rng, uint8_t *dest, size_t needed) while (needed > 0) { if (rng->unused_bytes > 0) { size_t to_get = MIN(needed, rng->unused_bytes); - uint8_t *keystream = rng->buf + sizeof(rng->buf) - rng->unused_bytes; + uint8_t *keystream = rng->random_bytes + sizeof(rng->random_bytes) - rng->unused_bytes; memcpy(dest, keystream, to_get); memset(keystream, 0, to_get); dest += to_get; -- cgit v1.2.3