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.
This commit is contained in:
Frank Denis 2015-01-18 10:08:36 +01:00
parent add0fcede4
commit 0b4fb379d4
3 changed files with 16 additions and 62 deletions

View File

@ -79,17 +79,25 @@ randombytes_stir(void)
} }
}); });
#else #else
if (implementation != NULL && implementation->stir != NULL) {
implementation->stir(); implementation->stir();
}
#endif #endif
} }
/*
* randombytes_uniform() derives from OpenBSD's arc4random_uniform()
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
*/
uint32_t uint32_t
randombytes_uniform(const uint32_t upper_bound) randombytes_uniform(const uint32_t upper_bound)
{ {
#ifdef __EMSCRIPTEN__
uint32_t min; uint32_t min;
uint32_t r; uint32_t r;
if (implementation != NULL && implementation->uniform != NULL) {
return implementation->uniform(upper_bound);
}
if (upper_bound < 2) { if (upper_bound < 2) {
return 0; return 0;
} }
@ -99,9 +107,6 @@ randombytes_uniform(const uint32_t upper_bound)
} while (r < min); } while (r < min);
return r % upper_bound; return r % upper_bound;
#else
return implementation->uniform(upper_bound);
#endif
} }
void void
@ -124,11 +129,10 @@ randombytes_buf(void * const buf, const size_t size)
int int
randombytes_close(void) randombytes_close(void)
{ {
#ifdef __EMSCRIPTEN__ if (implementation != NULL && implementation->close != NULL) {
return 0;
#else
return implementation->close(); return implementation->close();
#endif }
return 0;
} }
void void

View File

@ -311,28 +311,6 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
(unsigned char *) &stream.nonce, stream.key); (unsigned char *) &stream.nonce, stream.key);
} }
/*
* randombytes_salsa20_random_uniform() derives from OpenBSD's arc4random_uniform()
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
*/
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 * const char *
randombytes_salsa20_implementation_name(void) 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(.implementation_name =) randombytes_salsa20_implementation_name,
SODIUM_C99(.random =) randombytes_salsa20_random, SODIUM_C99(.random =) randombytes_salsa20_random,
SODIUM_C99(.stir =) randombytes_salsa20_random_stir, 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(.buf =) randombytes_salsa20_random_buf,
SODIUM_C99(.close =) randombytes_salsa20_random_close SODIUM_C99(.close =) randombytes_salsa20_random_close
}; };

View File

@ -43,12 +43,6 @@ randombytes_sysrandom_stir(void)
{ {
} }
uint32_t
randombytes_sysrandom_uniform(const uint32_t upper_bound)
{
return arc4random_uniform(upper_bound);
}
void void
randombytes_sysrandom_buf(void * const buf, const size_t size) 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 #endif
} }
/* #endif /* __OpenBSD__ */
* randombytes_sysrandom_uniform() derives from OpenBSD's arc4random_uniform()
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
*/
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
const char * const char *
randombytes_sysrandom_implementation_name(void) 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(.implementation_name =) randombytes_sysrandom_implementation_name,
SODIUM_C99(.random =) randombytes_sysrandom, SODIUM_C99(.random =) randombytes_sysrandom,
SODIUM_C99(.stir =) randombytes_sysrandom_stir, SODIUM_C99(.stir =) randombytes_sysrandom_stir,
SODIUM_C99(.uniform =) randombytes_sysrandom_uniform, SODIUM_C99(.uniform =) NULL,
SODIUM_C99(.buf =) randombytes_sysrandom_buf, SODIUM_C99(.buf =) randombytes_sysrandom_buf,
SODIUM_C99(.close =) randombytes_sysrandom_close SODIUM_C99(.close =) randombytes_sysrandom_close
}; };