From caabc153954ae8230f40f7bd09577b258619ef31 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 14 Mar 2018 17:24:48 +0100 Subject: [PATCH 1/2] sys/random: provide generic random_bytes() --- sys/random/Makefile | 2 +- sys/random/{seed.c => random.c} | 15 +++++++++++++++ sys/random/tinymt32.c | 30 ------------------------------ 3 files changed, 16 insertions(+), 31 deletions(-) rename sys/random/{seed.c => random.c} (71%) diff --git a/sys/random/Makefile b/sys/random/Makefile index 1068de18a3..4e66ead042 100644 --- a/sys/random/Makefile +++ b/sys/random/Makefile @@ -1,4 +1,4 @@ -SRC := seed.c +SRC := random.c BASE_MODULE := prng SUBMODULES := 1 diff --git a/sys/random/seed.c b/sys/random/random.c similarity index 71% rename from sys/random/seed.c rename to sys/random/random.c index b53d4ff814..dc5e6cf38d 100644 --- a/sys/random/seed.c +++ b/sys/random/random.c @@ -39,3 +39,18 @@ void auto_init_random(void) DEBUG("random: using seed value %u\n", (unsigned)seed); random_init(seed); } + +void random_bytes(uint8_t *target, size_t n) +{ + uint32_t random; + uint8_t *random_pos = (uint8_t*)&random; + unsigned _n = 0; + + while (n--) { + if (! (_n++ & 0x3)) { + random = random_uint32(); + random_pos = (uint8_t *) &random; + } + *target++ = *random_pos++; + } +} diff --git a/sys/random/tinymt32.c b/sys/random/tinymt32.c index 8442457e96..9537d7cd7d 100644 --- a/sys/random/tinymt32.c +++ b/sys/random/tinymt32.c @@ -44,36 +44,6 @@ uint32_t random_uint32(void) return tinymt32_generate_uint32(&_random); } -void random_bytes(uint8_t *buf, size_t size) -{ - size_t iter = size; - size_t diff = _align(buf) - buf; - uint32_t tmp; - - /* Fill first <4 unaligned bytes */ - if (diff) { - tmp = tinymt32_generate_uint32(&_random); - if (diff > size) { - diff = size; - } - memcpy(buf, &tmp, diff); - iter -= diff; - } - - /* Fill aligned bytes */ - while (iter >= sizeof(uint32_t)) { - *((uint32_t *) buf) = tinymt32_generate_uint32(&_random); - buf += sizeof(uint32_t); - iter -= sizeof(uint32_t); - } - - /* Fill last bytes */ - if (iter) { - tmp = tinymt32_generate_uint32(&_random); - memcpy(buf, &tmp, iter); - } -} - void random_init_by_array(uint32_t init_key[], int key_length) { tinymt32_init_by_array(&_random, init_key, key_length); From 9f3aa366a3fdeb5e021caf7de979e24e125d013d Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 14 Mar 2018 18:01:12 +0100 Subject: [PATCH 2/2] pkg: tweetnacl: use random_bytes() --- pkg/tweetnacl/Makefile.dep | 1 + pkg/tweetnacl/src/randombytes.c | 13 ++----------- 2 files changed, 3 insertions(+), 11 deletions(-) create mode 100644 pkg/tweetnacl/Makefile.dep diff --git a/pkg/tweetnacl/Makefile.dep b/pkg/tweetnacl/Makefile.dep new file mode 100644 index 0000000000..8030144a9a --- /dev/null +++ b/pkg/tweetnacl/Makefile.dep @@ -0,0 +1 @@ +USEMODULE+=random diff --git a/pkg/tweetnacl/src/randombytes.c b/pkg/tweetnacl/src/randombytes.c index 54a5e2b315..853a26511a 100644 --- a/pkg/tweetnacl/src/randombytes.c +++ b/pkg/tweetnacl/src/randombytes.c @@ -15,15 +15,6 @@ void randombytes(uint8_t *target, uint64_t n) { - uint32_t random; - uint8_t *random_pos = (uint8_t*)&random; - unsigned _n = 0; - - while (n--) { - if (! (_n++ & 0x3)) { - random = random_uint32(); - random_pos = (uint8_t *) &random; - } - *target++ = *random_pos++; - } + /* tweetnacl needs uint64_t as "n" parameter, random provides uint32 */ + random_bytes(target, n); }