mirror of
https://github.com/jedisct1/libsodium.git
synced 2024-12-23 20:15:19 -07:00
Make the randombytes interface consistent with crypto_onetimeauth
This commit is contained in:
parent
5ba6aef1da
commit
7aa057dcd8
@ -128,7 +128,7 @@ libsodium_la_SOURCES = \
|
||||
crypto_sign/ed25519/ref10/sqrtm1.h \
|
||||
randombytes/randombytes.c \
|
||||
randombytes/salsa20/randombytes_salsa20_random.c \
|
||||
randombytes/sys/randombytes_sysrandom.c \
|
||||
randombytes/sysrandom/randombytes_sysrandom.c \
|
||||
utils.c \
|
||||
version.c
|
||||
|
||||
|
@ -14,11 +14,11 @@ struct randombytes_implementation
|
||||
|
||||
const char *randombytes_salsa20_implementation_name(void);
|
||||
|
||||
uint32_t salsa20_random(void);
|
||||
void salsa20_random_stir(void);
|
||||
uint32_t salsa20_random_uniform(const uint32_t upper_bound);
|
||||
void salsa20_random_buf(void * const buf, const size_t size);
|
||||
int salsa20_random_close(void);
|
||||
uint32_t randombytes_salsa20_random(void);
|
||||
void randombytes_salsa20_random_stir(void);
|
||||
uint32_t randombytes_salsa20_random_uniform(const uint32_t upper_bound);
|
||||
void randombytes_salsa20_random_buf(void * const buf, const size_t size);
|
||||
int randombytes_salsa20_random_close(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -14,11 +14,11 @@ struct randombytes_implementation
|
||||
|
||||
const char *randombytes_sysrandom_implementation_name(void);
|
||||
|
||||
uint32_t sysrandom(void);
|
||||
void sysrandom_stir(void);
|
||||
uint32_t sysrandom_uniform(const uint32_t upper_bound);
|
||||
void sysrandom_buf(void * const buf, const size_t size);
|
||||
int sysrandom_close(void);
|
||||
uint32_t randombytes_sysrandom(void);
|
||||
void randombytes_sysrandom_stir(void);
|
||||
uint32_t randombytes_sysrandom_uniform(const uint32_t upper_bound);
|
||||
void randombytes_sysrandom_buf(void * const buf, const size_t size);
|
||||
int randombytes_sysrandom_close(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -10,11 +10,11 @@
|
||||
|
||||
static randombytes_implementation implementation = {
|
||||
.implementation_name = randombytes_sysrandom_implementation_name,
|
||||
.random = sysrandom,
|
||||
.stir = sysrandom_stir,
|
||||
.uniform = sysrandom_uniform,
|
||||
.buf = sysrandom_buf,
|
||||
.close = sysrandom_close
|
||||
.random = randombytes_sysrandom,
|
||||
.stir = randombytes_sysrandom_stir,
|
||||
.uniform = randombytes_sysrandom_uniform,
|
||||
.buf = randombytes_sysrandom_buf,
|
||||
.close = randombytes_sysrandom_close
|
||||
};
|
||||
|
||||
int
|
||||
|
@ -99,7 +99,7 @@ safe_read(const int fd, void * const buf_, size_t count)
|
||||
|
||||
#ifndef _WIN32
|
||||
static int
|
||||
salsa20_random_random_dev_open(void)
|
||||
randombytes_salsa20_random_random_dev_open(void)
|
||||
{
|
||||
static const char * const devices[] = {
|
||||
# ifndef USE_BLOCKING_RANDOM
|
||||
@ -120,13 +120,13 @@ salsa20_random_random_dev_open(void)
|
||||
}
|
||||
|
||||
static void
|
||||
salsa20_random_init(void)
|
||||
randombytes_salsa20_random_init(void)
|
||||
{
|
||||
stream.nonce = sodium_hrtime();
|
||||
assert(stream.nonce != (uint64_t) 0U);
|
||||
|
||||
if ((stream.random_data_source_fd =
|
||||
salsa20_random_random_dev_open()) == -1) {
|
||||
randombytes_salsa20_random_random_dev_open()) == -1) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@ -134,7 +134,7 @@ salsa20_random_init(void)
|
||||
#else /* _WIN32 */
|
||||
|
||||
static void
|
||||
salsa20_random_init(void)
|
||||
randombytes_salsa20_random_init(void)
|
||||
{
|
||||
stream.nonce = sodium_hrtime();
|
||||
assert(stream.nonce != (uint64_t) 0U);
|
||||
@ -147,7 +147,7 @@ salsa20_random_init(void)
|
||||
#endif
|
||||
|
||||
void
|
||||
salsa20_random_stir(void)
|
||||
randombytes_salsa20_random_stir(void)
|
||||
{
|
||||
unsigned char m0[3U * SHA256_BLOCK_SIZE - SHA256_MIN_PAD_SIZE];
|
||||
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);
|
||||
stream.rnd32_outleft = (size_t) 0U;
|
||||
if (stream.initialized == 0) {
|
||||
salsa20_random_init();
|
||||
randombytes_salsa20_random_init();
|
||||
stream.initialized = 1;
|
||||
}
|
||||
memset(m0, 0x69, SHA256_BLOCK_SIZE);
|
||||
@ -188,18 +188,18 @@ salsa20_random_stir(void)
|
||||
}
|
||||
|
||||
static void
|
||||
salsa20_random_stir_if_needed(void)
|
||||
randombytes_salsa20_random_stir_if_needed(void)
|
||||
{
|
||||
const pid_t pid = getpid();
|
||||
|
||||
if (stream.initialized == 0 || stream.pid != pid) {
|
||||
stream.pid = pid;
|
||||
salsa20_random_stir();
|
||||
randombytes_salsa20_random_stir();
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
salsa20_random_getword(void)
|
||||
randombytes_salsa20_random_getword(void)
|
||||
{
|
||||
uint32_t val;
|
||||
int ret;
|
||||
@ -223,7 +223,7 @@ salsa20_random_getword(void)
|
||||
}
|
||||
|
||||
int
|
||||
salsa20_random_close(void)
|
||||
randombytes_salsa20_random_close(void)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
@ -245,19 +245,19 @@ salsa20_random_close(void)
|
||||
}
|
||||
|
||||
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
|
||||
salsa20_random_buf(void * const buf, const size_t size)
|
||||
randombytes_salsa20_random_buf(void * const buf, const size_t size)
|
||||
{
|
||||
int ret;
|
||||
|
||||
salsa20_random_stir_if_needed();
|
||||
randombytes_salsa20_random_stir_if_needed();
|
||||
COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES);
|
||||
#ifdef 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>
|
||||
*/
|
||||
|
||||
uint32_t
|
||||
salsa20_random_uniform(const uint32_t upper_bound)
|
||||
randombytes_salsa20_random_uniform(const uint32_t upper_bound)
|
||||
{
|
||||
uint32_t min;
|
||||
uint32_t r;
|
||||
@ -285,7 +285,7 @@ salsa20_random_uniform(const uint32_t upper_bound)
|
||||
}
|
||||
min = (uint32_t) (-upper_bound % upper_bound);
|
||||
for (;;) {
|
||||
r = salsa20_random();
|
||||
r = randombytes_salsa20_random();
|
||||
if (r >= min) {
|
||||
break;
|
||||
}
|
||||
@ -304,10 +304,10 @@ randombytes_salsa20_implementation(void)
|
||||
{
|
||||
return (randombytes_implementation) {
|
||||
.implementation_name = randombytes_salsa20_implementation_name,
|
||||
.random = salsa20_random,
|
||||
.stir = salsa20_random_stir,
|
||||
.uniform = salsa20_random_uniform,
|
||||
.buf = salsa20_random_buf,
|
||||
.close = salsa20_random_close
|
||||
.random = randombytes_salsa20_random,
|
||||
.stir = randombytes_salsa20_random_stir,
|
||||
.uniform = randombytes_salsa20_random_uniform,
|
||||
.buf = randombytes_salsa20_random_buf,
|
||||
.close = randombytes_salsa20_random_close
|
||||
};
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ safe_read(const int fd, void * const buf_, size_t count)
|
||||
|
||||
#ifndef _WIN32
|
||||
static int
|
||||
sysrandom_random_dev_open(void)
|
||||
randombytes_sysrandom_random_dev_open(void)
|
||||
{
|
||||
static const char * const devices[] = {
|
||||
# ifndef USE_BLOCKING_RANDOM
|
||||
@ -83,10 +83,10 @@ sysrandom_random_dev_open(void)
|
||||
}
|
||||
|
||||
static void
|
||||
sysrandom_init(void)
|
||||
randombytes_sysrandom_init(void)
|
||||
{
|
||||
if ((stream.random_data_source_fd =
|
||||
sysrandom_random_dev_open()) == -1) {
|
||||
randombytes_sysrandom_random_dev_open()) == -1) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@ -94,7 +94,7 @@ sysrandom_init(void)
|
||||
#else /* _WIN32 */
|
||||
|
||||
static void
|
||||
sysrandom_init(void)
|
||||
randombytes_sysrandom_init(void)
|
||||
{
|
||||
if (! CryptAcquireContext(&stream.hcrypt_prov, NULL, NULL,
|
||||
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
|
||||
@ -104,24 +104,24 @@ sysrandom_init(void)
|
||||
#endif
|
||||
|
||||
void
|
||||
sysrandom_stir(void)
|
||||
randombytes_sysrandom_stir(void)
|
||||
{
|
||||
if (stream.initialized == 0) {
|
||||
sysrandom_init();
|
||||
randombytes_sysrandom_init();
|
||||
stream.initialized = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sysrandom_stir_if_needed(void)
|
||||
randombytes_sysrandom_stir_if_needed(void)
|
||||
{
|
||||
if (stream.initialized == 0) {
|
||||
sysrandom_stir();
|
||||
randombytes_sysrandom_stir();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
sysrandom_close(void)
|
||||
randombytes_sysrandom_close(void)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
@ -143,20 +143,20 @@ sysrandom_close(void)
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sysrandom(void)
|
||||
randombytes_sysrandom(void)
|
||||
{
|
||||
uint32_t r;
|
||||
|
||||
sysrandom_stir_if_needed();
|
||||
sysrandom_buf(&r, sizeof r);
|
||||
randombytes_sysrandom_stir_if_needed();
|
||||
randombytes_sysrandom_buf(&r, sizeof r);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
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
|
||||
assert(size <= ULONG_LONG_MAX);
|
||||
#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>
|
||||
*/
|
||||
|
||||
uint32_t
|
||||
sysrandom_uniform(const uint32_t upper_bound)
|
||||
randombytes_sysrandom_uniform(const uint32_t upper_bound)
|
||||
{
|
||||
uint32_t min;
|
||||
uint32_t r;
|
||||
@ -187,7 +187,7 @@ sysrandom_uniform(const uint32_t upper_bound)
|
||||
}
|
||||
min = (uint32_t) (-upper_bound % upper_bound);
|
||||
for (;;) {
|
||||
r = sysrandom();
|
||||
r = randombytes_sysrandom();
|
||||
if (r >= min) {
|
||||
break;
|
||||
}
|
||||
@ -206,10 +206,10 @@ randombytes_sysrandom_implementation(void)
|
||||
{
|
||||
return (randombytes_implementation) {
|
||||
.implementation_name = randombytes_sysrandom_implementation_name,
|
||||
.random = sysrandom,
|
||||
.stir = sysrandom_stir,
|
||||
.uniform = sysrandom_uniform,
|
||||
.buf = sysrandom_buf,
|
||||
.close = sysrandom_close
|
||||
.random = randombytes_sysrandom,
|
||||
.stir = randombytes_sysrandom_stir,
|
||||
.uniform = randombytes_sysrandom_uniform,
|
||||
.buf = randombytes_sysrandom_buf,
|
||||
.close = randombytes_sysrandom_close
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user