mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/random: make sha1prng sha256 compatible
This commit is contained in:
parent
ad28752e4e
commit
5a13d00b65
@ -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
|
||||
|
||||
|
@ -14,7 +14,9 @@
|
||||
* @{
|
||||
* @file
|
||||
*
|
||||
* @brief SHA1PRNG random number generator implementation
|
||||
* @brief SHA based PRNG random number generator implementation
|
||||
*
|
||||
* BLABLA
|
||||
*
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
* @}
|
||||
@ -24,10 +26,52 @@
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user