From 2f915846ff41191c1a17357f0efaae9d500e9858 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 24 Sep 2019 16:56:49 +0200 Subject: [PATCH] randombytes: make the emscripten version consistent with others --- src/libsodium/randombytes/randombytes.c | 118 ++++++++++++++---------- 1 file changed, 67 insertions(+), 51 deletions(-) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index 8ac085fc..23dd6dd0 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -30,60 +30,28 @@ static const randombytes_implementation *implementation; # ifdef __EMSCRIPTEN__ # define RANDOMBYTES_DEFAULT_IMPLEMENTATION NULL # else -# define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation; +# define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation # endif #endif -static void -randombytes_init_if_needed(void) +#ifdef __EMSCRIPTEN__ +static const char * +javascript_implementation_name(void) { - if (implementation == NULL) { - implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION; - randombytes_stir(); - } -} - -int -randombytes_set_implementation(randombytes_implementation *impl) -{ - implementation = impl; - - return 0; -} - -const char * -randombytes_implementation_name(void) -{ -#ifndef __EMSCRIPTEN__ - randombytes_init_if_needed(); - return implementation->implementation_name(); -#else return "js"; -#endif } -uint32_t -randombytes_random(void) +static uint32_t +javascript_random(void) { -#ifndef __EMSCRIPTEN__ - randombytes_init_if_needed(); - return implementation->random(); -#else return EM_ASM_INT_V({ return Module.getRandomValue(); }); -#endif } -void -randombytes_stir(void) +static void +javascript_stir(void) { -#ifndef __EMSCRIPTEN__ - randombytes_init_if_needed(); - if (implementation->stir != NULL) { - implementation->stir(); - } -#else EM_ASM({ if (Module.getRandomValue === undefined) { try { @@ -111,7 +79,66 @@ randombytes_stir(void) } } }); +} + +static void +javascript_buf(void * const buf, const size_t size) +{ + unsigned char *p = (unsigned char *) buf; + size_t i; + + for (i = (size_t) 0U; i < size; i++) { + p[i] = (unsigned char) randombytes_random(); + } +} #endif + +static void +randombytes_init_if_needed(void) +{ + if (implementation == NULL) { +#ifdef __EMSCRIPTEN__ + static randombytes_implementation javascript_implementation; + javascript_implementation.implementation_name = javascript_implementation_name; + javascript_implementation.random = javascript_random; + javascript_implementation.stir = javascript_stir; + javascript_implementation.buf = javascript_buf; + implementation = &javascript_implementation; +#else + implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION; +#endif + randombytes_stir(); + } +} + +int +randombytes_set_implementation(randombytes_implementation *impl) +{ + implementation = impl; + return 0; +} + +const char * +randombytes_implementation_name(void) +{ + randombytes_init_if_needed(); + return implementation->implementation_name(); +} + +uint32_t +randombytes_random(void) +{ + randombytes_init_if_needed(); + return implementation->random(); +} + +void +randombytes_stir(void) +{ + randombytes_init_if_needed(); + if (implementation->stir != NULL) { + implementation->stir(); + } } uint32_t @@ -120,12 +147,10 @@ randombytes_uniform(const uint32_t upper_bound) uint32_t min; uint32_t r; -#ifndef __EMSCRIPTEN__ randombytes_init_if_needed(); if (implementation->uniform != NULL) { return implementation->uniform(upper_bound); } -#endif if (upper_bound < 2) { return 0; } @@ -142,19 +167,10 @@ randombytes_uniform(const uint32_t upper_bound) void randombytes_buf(void * const buf, const size_t size) { -#ifndef __EMSCRIPTEN__ randombytes_init_if_needed(); if (size > (size_t) 0U) { implementation->buf(buf, size); } -#else - unsigned char *p = (unsigned char *) buf; - size_t i; - - for (i = (size_t) 0U; i < size; i++) { - p[i] = (unsigned char) randombytes_random(); - } -#endif } void