From 5a13d00b6579326b12793a4b957c069358ddc590 Mon Sep 17 00:00:00 2001 From: PeterKietzmann Date: Thu, 9 Jul 2020 11:46:12 +0200 Subject: [PATCH] sys/random: make sha1prng sha256 compatible --- Makefile.dep | 3 +- sys/random/{sha1prng.c => shaxprng.c} | 80 +++++++++++++++++++++------ tests/rng/test.c | 2 + 3 files changed, 66 insertions(+), 19 deletions(-) rename sys/random/{sha1prng.c => shaxprng.c} (54%) diff --git a/Makefile.dep b/Makefile.dep index 783119f4d3..46bf040078 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -782,7 +782,8 @@ ifneq (,$(filter random,$(USEMODULE))) USEMODULE += tinymt32 endif - ifneq (,$(filter prng_sha1prng,$(USEMODULE))) + ifneq (,$(filter prng_sha%prng,$(USEMODULE))) + USEMODULE += prng_shaxprng USEMODULE += hashes endif diff --git a/sys/random/sha1prng.c b/sys/random/shaxprng.c similarity index 54% rename from sys/random/sha1prng.c rename to sys/random/shaxprng.c index 6e7156715f..cdfbd9652c 100644 --- a/sys/random/sha1prng.c +++ b/sys/random/shaxprng.c @@ -14,7 +14,9 @@ * @{ * @file * - * @brief SHA1PRNG random number generator implementation + * @brief SHA based PRNG random number generator implementation + * + * BLABLA * * @author Peter Kietzmann * @} @@ -24,10 +26,52 @@ #include #include "hashes/sha1.h" +#include "hashes/sha256.h" +#include "kernel_defines.h" -#define STATE_SIZE (SHA1_DIGEST_LENGTH) +#if IS_USED(MODULE_PRNG_SHA1PRNG) +/* state size is digset length of SHA-1 */ +#define STATE_SIZE (SHA1_DIGEST_LENGTH) +typedef sha1_context shax_context_t; +#elif IS_USED(MODULE_PRNG_SHA256PRNG) +/* state size is digest length of SHA-256 */ +#define STATE_SIZE (SHA256_DIGEST_LENGTH) +typedef sha256_context_t shax_context_t; +#endif + +static inline void _shax_init(shax_context_t *ctx) +{ + if (IS_USED(MODULE_PRNG_SHA1PRNG)) { + sha1_init((sha1_context *)ctx); + } + else if (IS_USED(MODULE_PRNG_SHA256PRNG)) { + sha256_init((sha256_context_t *)ctx); + } +} + +static inline void _shax_update(shax_context_t *ctx, const void *data, size_t len) +{ + if (IS_USED(MODULE_PRNG_SHA1PRNG)) { + sha1_update((sha1_context *)ctx, data, len); + } + else if (IS_USED(MODULE_PRNG_SHA256PRNG)) { + sha256_update((sha256_context_t *)ctx, data, len); + } +} + +static inline void _shax_final(shax_context_t *ctx, void *digest) +{ + if (IS_USED(MODULE_PRNG_SHA1PRNG)) { + sha1_final((sha1_context *)ctx, digest); + } + else if (IS_USED(MODULE_PRNG_SHA256PRNG)) { + sha256_final((sha256_context_t *)ctx, digest); + } +} + +/* allocate SHA context */ +static shax_context_t ctx; -static sha1_context ctx; static uint32_t datapos = STATE_SIZE; static int8_t digestdata[STATE_SIZE]; static int8_t prng_state[STATE_SIZE]; @@ -62,7 +106,7 @@ void _updatestate(int8_t *state) } } -void _random_bytes(uint8_t *bytes, size_t size) /* TODO: use with global API */ +void _random_bytes(uint8_t *bytes, size_t size) { uint32_t loc = 0; while (loc < size) @@ -88,14 +132,14 @@ void _random_bytes(uint8_t *bytes, size_t size) /* TODO: use with global API */ /* no out data ready, (re)fill internal buffer */ else { - /* reset SHA1 internal state */ - sha1_init(&ctx); + /* reset SHA internal state */ + _shax_init(&ctx); - /* update SHA1 internal state with PRNG state */ - sha1_update(&ctx, (void *)prng_state, sizeof(prng_state)); + /* update SHA internal state with PRNG state */ + _shax_update(&ctx, prng_state, sizeof(prng_state)); /* get the digest */ - sha1_final(&ctx, digestdata); + _shax_final(&ctx, digestdata); /* update PRNG state for next round */ _updatestate(prng_state); @@ -108,9 +152,9 @@ void _random_bytes(uint8_t *bytes, size_t size) /* TODO: use with global API */ void random_init_by_array(uint32_t init_key[], int key_length) { - sha1_init(&ctx); - sha1_update(&ctx, (void *)init_key, key_length); - sha1_final(&ctx, digestdata); + _shax_init(&ctx); + _shax_update(&ctx, init_key, key_length); + _shax_final(&ctx, digestdata); /* copy seeded SHA1 state to PRNG state */ memcpy(prng_state, &ctx.state, STATE_SIZE); @@ -124,13 +168,13 @@ void random_init(uint32_t seed) uint32_t random_uint32(void) { uint32_t ret; - int8_t bytes[sizeof(uint32_t)]; - _random_bytes((uint8_t *)bytes, sizeof(uint32_t)); + uint8_t bytes[sizeof(uint32_t)]; + _random_bytes(bytes, sizeof(bytes)); - ret = ((bytes[0] & 0xff) << 24) - | ((bytes[1] & 0xff) << 16) - | ((bytes[2] & 0xff) << 8) - | (bytes[3] & 0xff); + ret = ((uint32_t)(bytes[0] & 0xff) << 24) + | ((uint32_t)(bytes[1] & 0xff) << 16) + | ((uint32_t)(bytes[2] & 0xff) << 8) + | ((uint32_t)(bytes[3] & 0xff)); return ret; } diff --git a/tests/rng/test.c b/tests/rng/test.c index 41bf6f229e..d796bfcec7 100644 --- a/tests/rng/test.c +++ b/tests/rng/test.c @@ -61,6 +61,8 @@ static void test_init(char *name) puts("Musl C PRNG.\n"); #elif MODULE_PRNG_SHA1PRNG puts("SHA1 PRNG.\n"); +#elif MODULE_PRNG_SHA256PRNG + puts("SHA256 PRNG.\n"); #elif MODULE_PRNG_TINYMT32 puts("Tiny Mersenne Twister PRNG.\n"); #elif MODULE_PRNG_XORSHIFT