1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-25 13:05:08 -07:00

Make the randombytes interface consistent with crypto_onetimeauth

This commit is contained in:
Frank Denis 2013-04-18 13:31:13 +02:00
parent 5ba6aef1da
commit 7aa057dcd8
6 changed files with 61 additions and 61 deletions

View File

@ -128,7 +128,7 @@ libsodium_la_SOURCES = \
crypto_sign/ed25519/ref10/sqrtm1.h \ crypto_sign/ed25519/ref10/sqrtm1.h \
randombytes/randombytes.c \ randombytes/randombytes.c \
randombytes/salsa20/randombytes_salsa20_random.c \ randombytes/salsa20/randombytes_salsa20_random.c \
randombytes/sys/randombytes_sysrandom.c \ randombytes/sysrandom/randombytes_sysrandom.c \
utils.c \ utils.c \
version.c version.c

View File

@ -14,11 +14,11 @@ struct randombytes_implementation
const char *randombytes_salsa20_implementation_name(void); const char *randombytes_salsa20_implementation_name(void);
uint32_t salsa20_random(void); uint32_t randombytes_salsa20_random(void);
void salsa20_random_stir(void); void randombytes_salsa20_random_stir(void);
uint32_t salsa20_random_uniform(const uint32_t upper_bound); uint32_t randombytes_salsa20_random_uniform(const uint32_t upper_bound);
void salsa20_random_buf(void * const buf, const size_t size); void randombytes_salsa20_random_buf(void * const buf, const size_t size);
int salsa20_random_close(void); int randombytes_salsa20_random_close(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -14,11 +14,11 @@ struct randombytes_implementation
const char *randombytes_sysrandom_implementation_name(void); const char *randombytes_sysrandom_implementation_name(void);
uint32_t sysrandom(void); uint32_t randombytes_sysrandom(void);
void sysrandom_stir(void); void randombytes_sysrandom_stir(void);
uint32_t sysrandom_uniform(const uint32_t upper_bound); uint32_t randombytes_sysrandom_uniform(const uint32_t upper_bound);
void sysrandom_buf(void * const buf, const size_t size); void randombytes_sysrandom_buf(void * const buf, const size_t size);
int sysrandom_close(void); int randombytes_sysrandom_close(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -10,11 +10,11 @@
static randombytes_implementation implementation = { static randombytes_implementation implementation = {
.implementation_name = randombytes_sysrandom_implementation_name, .implementation_name = randombytes_sysrandom_implementation_name,
.random = sysrandom, .random = randombytes_sysrandom,
.stir = sysrandom_stir, .stir = randombytes_sysrandom_stir,
.uniform = sysrandom_uniform, .uniform = randombytes_sysrandom_uniform,
.buf = sysrandom_buf, .buf = randombytes_sysrandom_buf,
.close = sysrandom_close .close = randombytes_sysrandom_close
}; };
int int

View File

@ -99,7 +99,7 @@ safe_read(const int fd, void * const buf_, size_t count)
#ifndef _WIN32 #ifndef _WIN32
static int static int
salsa20_random_random_dev_open(void) randombytes_salsa20_random_random_dev_open(void)
{ {
static const char * const devices[] = { static const char * const devices[] = {
# ifndef USE_BLOCKING_RANDOM # ifndef USE_BLOCKING_RANDOM
@ -120,13 +120,13 @@ salsa20_random_random_dev_open(void)
} }
static void static void
salsa20_random_init(void) randombytes_salsa20_random_init(void)
{ {
stream.nonce = sodium_hrtime(); stream.nonce = sodium_hrtime();
assert(stream.nonce != (uint64_t) 0U); assert(stream.nonce != (uint64_t) 0U);
if ((stream.random_data_source_fd = if ((stream.random_data_source_fd =
salsa20_random_random_dev_open()) == -1) { randombytes_salsa20_random_random_dev_open()) == -1) {
abort(); abort();
} }
} }
@ -134,7 +134,7 @@ salsa20_random_init(void)
#else /* _WIN32 */ #else /* _WIN32 */
static void static void
salsa20_random_init(void) randombytes_salsa20_random_init(void)
{ {
stream.nonce = sodium_hrtime(); stream.nonce = sodium_hrtime();
assert(stream.nonce != (uint64_t) 0U); assert(stream.nonce != (uint64_t) 0U);
@ -147,7 +147,7 @@ salsa20_random_init(void)
#endif #endif
void void
salsa20_random_stir(void) randombytes_salsa20_random_stir(void)
{ {
unsigned char m0[3U * SHA256_BLOCK_SIZE - SHA256_MIN_PAD_SIZE]; unsigned char m0[3U * SHA256_BLOCK_SIZE - SHA256_MIN_PAD_SIZE];
unsigned char m1[SHA256_BLOCK_SIZE + crypto_hash_sha256_BYTES]; unsigned char m1[SHA256_BLOCK_SIZE + crypto_hash_sha256_BYTES];
@ -159,7 +159,7 @@ salsa20_random_stir(void)
memset(stream.rnd32, 0, sizeof stream.rnd32); memset(stream.rnd32, 0, sizeof stream.rnd32);
stream.rnd32_outleft = (size_t) 0U; stream.rnd32_outleft = (size_t) 0U;
if (stream.initialized == 0) { if (stream.initialized == 0) {
salsa20_random_init(); randombytes_salsa20_random_init();
stream.initialized = 1; stream.initialized = 1;
} }
memset(m0, 0x69, SHA256_BLOCK_SIZE); memset(m0, 0x69, SHA256_BLOCK_SIZE);
@ -188,18 +188,18 @@ salsa20_random_stir(void)
} }
static void static void
salsa20_random_stir_if_needed(void) randombytes_salsa20_random_stir_if_needed(void)
{ {
const pid_t pid = getpid(); const pid_t pid = getpid();
if (stream.initialized == 0 || stream.pid != pid) { if (stream.initialized == 0 || stream.pid != pid) {
stream.pid = pid; stream.pid = pid;
salsa20_random_stir(); randombytes_salsa20_random_stir();
} }
} }
static uint32_t static uint32_t
salsa20_random_getword(void) randombytes_salsa20_random_getword(void)
{ {
uint32_t val; uint32_t val;
int ret; int ret;
@ -223,7 +223,7 @@ salsa20_random_getword(void)
} }
int int
salsa20_random_close(void) randombytes_salsa20_random_close(void)
{ {
int ret = -1; int ret = -1;
@ -245,19 +245,19 @@ salsa20_random_close(void)
} }
uint32_t uint32_t
salsa20_random(void) randombytes_salsa20_random(void)
{ {
salsa20_random_stir_if_needed(); randombytes_salsa20_random_stir_if_needed();
return salsa20_random_getword(); return randombytes_salsa20_random_getword();
} }
void void
salsa20_random_buf(void * const buf, const size_t size) randombytes_salsa20_random_buf(void * const buf, const size_t size)
{ {
int ret; int ret;
salsa20_random_stir_if_needed(); randombytes_salsa20_random_stir_if_needed();
COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES); COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES);
#ifdef ULONG_LONG_MAX #ifdef ULONG_LONG_MAX
assert(size <= ULONG_LONG_MAX); assert(size <= ULONG_LONG_MAX);
@ -270,12 +270,12 @@ salsa20_random_buf(void * const buf, const size_t size)
} }
/* /*
* salsa20_random_uniform() derives from OpenBSD's arc4random_uniform() * randombytes_salsa20_random_uniform() derives from OpenBSD's arc4random_uniform()
* Copyright (c) 2008, Damien Miller <djm@openbsd.org> * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
*/ */
uint32_t uint32_t
salsa20_random_uniform(const uint32_t upper_bound) randombytes_salsa20_random_uniform(const uint32_t upper_bound)
{ {
uint32_t min; uint32_t min;
uint32_t r; uint32_t r;
@ -285,7 +285,7 @@ salsa20_random_uniform(const uint32_t upper_bound)
} }
min = (uint32_t) (-upper_bound % upper_bound); min = (uint32_t) (-upper_bound % upper_bound);
for (;;) { for (;;) {
r = salsa20_random(); r = randombytes_salsa20_random();
if (r >= min) { if (r >= min) {
break; break;
} }
@ -304,10 +304,10 @@ randombytes_salsa20_implementation(void)
{ {
return (randombytes_implementation) { return (randombytes_implementation) {
.implementation_name = randombytes_salsa20_implementation_name, .implementation_name = randombytes_salsa20_implementation_name,
.random = salsa20_random, .random = randombytes_salsa20_random,
.stir = salsa20_random_stir, .stir = randombytes_salsa20_random_stir,
.uniform = salsa20_random_uniform, .uniform = randombytes_salsa20_random_uniform,
.buf = salsa20_random_buf, .buf = randombytes_salsa20_random_buf,
.close = salsa20_random_close .close = randombytes_salsa20_random_close
}; };
} }

View File

@ -62,7 +62,7 @@ safe_read(const int fd, void * const buf_, size_t count)
#ifndef _WIN32 #ifndef _WIN32
static int static int
sysrandom_random_dev_open(void) randombytes_sysrandom_random_dev_open(void)
{ {
static const char * const devices[] = { static const char * const devices[] = {
# ifndef USE_BLOCKING_RANDOM # ifndef USE_BLOCKING_RANDOM
@ -83,10 +83,10 @@ sysrandom_random_dev_open(void)
} }
static void static void
sysrandom_init(void) randombytes_sysrandom_init(void)
{ {
if ((stream.random_data_source_fd = if ((stream.random_data_source_fd =
sysrandom_random_dev_open()) == -1) { randombytes_sysrandom_random_dev_open()) == -1) {
abort(); abort();
} }
} }
@ -94,7 +94,7 @@ sysrandom_init(void)
#else /* _WIN32 */ #else /* _WIN32 */
static void static void
sysrandom_init(void) randombytes_sysrandom_init(void)
{ {
if (! CryptAcquireContext(&stream.hcrypt_prov, NULL, NULL, if (! CryptAcquireContext(&stream.hcrypt_prov, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
@ -104,24 +104,24 @@ sysrandom_init(void)
#endif #endif
void void
sysrandom_stir(void) randombytes_sysrandom_stir(void)
{ {
if (stream.initialized == 0) { if (stream.initialized == 0) {
sysrandom_init(); randombytes_sysrandom_init();
stream.initialized = 1; stream.initialized = 1;
} }
} }
static void static void
sysrandom_stir_if_needed(void) randombytes_sysrandom_stir_if_needed(void)
{ {
if (stream.initialized == 0) { if (stream.initialized == 0) {
sysrandom_stir(); randombytes_sysrandom_stir();
} }
} }
int int
sysrandom_close(void) randombytes_sysrandom_close(void)
{ {
int ret = -1; int ret = -1;
@ -143,20 +143,20 @@ sysrandom_close(void)
} }
uint32_t uint32_t
sysrandom(void) randombytes_sysrandom(void)
{ {
uint32_t r; uint32_t r;
sysrandom_stir_if_needed(); randombytes_sysrandom_stir_if_needed();
sysrandom_buf(&r, sizeof r); randombytes_sysrandom_buf(&r, sizeof r);
return r; return r;
} }
void void
sysrandom_buf(void * const buf, const size_t size) randombytes_sysrandom_buf(void * const buf, const size_t size)
{ {
sysrandom_stir_if_needed(); randombytes_sysrandom_stir_if_needed();
#ifdef ULONG_LONG_MAX #ifdef ULONG_LONG_MAX
assert(size <= ULONG_LONG_MAX); assert(size <= ULONG_LONG_MAX);
#endif #endif
@ -172,12 +172,12 @@ sysrandom_buf(void * const buf, const size_t size)
} }
/* /*
* sysrandom_uniform() derives from OpenBSD's arc4random_uniform() * randombytes_sysrandom_uniform() derives from OpenBSD's arc4random_uniform()
* Copyright (c) 2008, Damien Miller <djm@openbsd.org> * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
*/ */
uint32_t uint32_t
sysrandom_uniform(const uint32_t upper_bound) randombytes_sysrandom_uniform(const uint32_t upper_bound)
{ {
uint32_t min; uint32_t min;
uint32_t r; uint32_t r;
@ -187,7 +187,7 @@ sysrandom_uniform(const uint32_t upper_bound)
} }
min = (uint32_t) (-upper_bound % upper_bound); min = (uint32_t) (-upper_bound % upper_bound);
for (;;) { for (;;) {
r = sysrandom(); r = randombytes_sysrandom();
if (r >= min) { if (r >= min) {
break; break;
} }
@ -206,10 +206,10 @@ randombytes_sysrandom_implementation(void)
{ {
return (randombytes_implementation) { return (randombytes_implementation) {
.implementation_name = randombytes_sysrandom_implementation_name, .implementation_name = randombytes_sysrandom_implementation_name,
.random = sysrandom, .random = randombytes_sysrandom,
.stir = sysrandom_stir, .stir = randombytes_sysrandom_stir,
.uniform = sysrandom_uniform, .uniform = randombytes_sysrandom_uniform,
.buf = sysrandom_buf, .buf = randombytes_sysrandom_buf,
.close = sysrandom_close .close = randombytes_sysrandom_close
}; };
} }