From 0b4fb379d408bdb0abd8f14ba3f112c820d2327b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 18 Jan 2015 10:08:36 +0100 Subject: [PATCH] Factorize randombytes_uniform() Don't require randombytes implementations to reimplement this. NULL can be passed instead of a function pointer to use the default implementation. Allow NULL for randombytes_stir() and randombytes_close() as well. --- src/libsodium/randombytes/randombytes.c | 22 +++++++------ .../salsa20/randombytes_salsa20_random.c | 24 +------------- .../sysrandom/randombytes_sysrandom.c | 32 ++----------------- 3 files changed, 16 insertions(+), 62 deletions(-) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index baee86ff..314e9d02 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -79,17 +79,25 @@ randombytes_stir(void) } }); #else - implementation->stir(); + if (implementation != NULL && implementation->stir != NULL) { + implementation->stir(); + } #endif } +/* + * randombytes_uniform() derives from OpenBSD's arc4random_uniform() + * Copyright (c) 2008, Damien Miller + */ uint32_t randombytes_uniform(const uint32_t upper_bound) { -#ifdef __EMSCRIPTEN__ uint32_t min; uint32_t r; + if (implementation != NULL && implementation->uniform != NULL) { + return implementation->uniform(upper_bound); + } if (upper_bound < 2) { return 0; } @@ -99,9 +107,6 @@ randombytes_uniform(const uint32_t upper_bound) } while (r < min); return r % upper_bound; -#else - return implementation->uniform(upper_bound); -#endif } void @@ -124,11 +129,10 @@ randombytes_buf(void * const buf, const size_t size) int randombytes_close(void) { -#ifdef __EMSCRIPTEN__ + if (implementation != NULL && implementation->close != NULL) { + return implementation->close(); + } return 0; -#else - return implementation->close(); -#endif } void diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index e81fa92d..4e51381f 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -311,28 +311,6 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size) (unsigned char *) &stream.nonce, stream.key); } -/* - * randombytes_salsa20_random_uniform() derives from OpenBSD's arc4random_uniform() - * Copyright (c) 2008, Damien Miller - */ - -uint32_t -randombytes_salsa20_random_uniform(const uint32_t upper_bound) -{ - uint32_t min; - uint32_t r; - - if (upper_bound < 2) { - return 0; - } - min = (uint32_t) (-upper_bound % upper_bound); - do { - r = randombytes_salsa20_random(); - } while (r < min); /* LCOV_EXCL_LINE */ - - return r % upper_bound; -} - const char * randombytes_salsa20_implementation_name(void) { @@ -343,7 +321,7 @@ struct randombytes_implementation randombytes_salsa20_implementation = { SODIUM_C99(.implementation_name =) randombytes_salsa20_implementation_name, SODIUM_C99(.random =) randombytes_salsa20_random, SODIUM_C99(.stir =) randombytes_salsa20_random_stir, - SODIUM_C99(.uniform =) randombytes_salsa20_random_uniform, + SODIUM_C99(.uniform =) NULL, SODIUM_C99(.buf =) randombytes_salsa20_random_buf, SODIUM_C99(.close =) randombytes_salsa20_random_close }; diff --git a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c index 539ebca6..6d2ea718 100644 --- a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +++ b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c @@ -43,12 +43,6 @@ randombytes_sysrandom_stir(void) { } -uint32_t -randombytes_sysrandom_uniform(const uint32_t upper_bound) -{ - return arc4random_uniform(upper_bound); -} - void randombytes_sysrandom_buf(void * const buf, const size_t size) { @@ -224,29 +218,7 @@ randombytes_sysrandom_buf(void * const buf, const size_t size) #endif } -/* - * randombytes_sysrandom_uniform() derives from OpenBSD's arc4random_uniform() - * Copyright (c) 2008, Damien Miller - */ - -uint32_t -randombytes_sysrandom_uniform(const uint32_t upper_bound) -{ - uint32_t min; - uint32_t r; - - if (upper_bound < 2) { - return 0; - } - min = (uint32_t) (-upper_bound % upper_bound); - do { - r = randombytes_sysrandom(); - } while (r < min); /* LCOV_EXCL_LINE */ - - return r % upper_bound; -} - -#endif +#endif /* __OpenBSD__ */ const char * randombytes_sysrandom_implementation_name(void) @@ -258,7 +230,7 @@ struct randombytes_implementation randombytes_sysrandom_implementation = { SODIUM_C99(.implementation_name =) randombytes_sysrandom_implementation_name, SODIUM_C99(.random =) randombytes_sysrandom, SODIUM_C99(.stir =) randombytes_sysrandom_stir, - SODIUM_C99(.uniform =) randombytes_sysrandom_uniform, + SODIUM_C99(.uniform =) NULL, SODIUM_C99(.buf =) randombytes_sysrandom_buf, SODIUM_C99(.close =) randombytes_sysrandom_close };