1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Add randombytes wrappers.

These are real wrappers, not globals or macros, in order to make it easier
to use them in different programming languages.
This commit is contained in:
Frank Denis 2013-01-21 13:50:00 -08:00
parent 3444a46f35
commit 5788f3d6a8
4 changed files with 65 additions and 12 deletions

View File

@ -12,12 +12,12 @@ extern "C" {
#include <stdio.h>
typedef struct randombytes_implementation {
const char (**randombytes_implementation_name)(void);
uint32_t (*randombytes_random)(void);
void (*randombytes_stir)(void);
uint32_t (*randombytes_uniform)(const uint32_t upper_bound);
void (*randombytes_buf)(void * const buf, const size_t size);
int (*randombytes_close)(void);
const char *(*randombytes_implementation_name)(void);
uint32_t (*randombytes_random)(void);
void (*randombytes_stir)(void);
uint32_t (*randombytes_uniform)(const uint32_t upper_bound);
void (*randombytes_buf)(void * const buf, const size_t size);
int (*randombytes_close)(void);
} randombytes_implementation;
int randombytes_set_implementation(randombytes_implementation *impl);

View File

@ -9,11 +9,13 @@
extern "C" {
#endif
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);
const char *salsa20_random_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);
#ifdef __cplusplus
}

View File

@ -7,9 +7,54 @@
#include "randombytes.h"
#include "salsa20_random.h"
static randombytes_implementation implementation = {
.randombytes_implementation_name = salsa20_random_implementation_name,
.randombytes_random = salsa20_random,
.randombytes_stir = salsa20_random_stir,
.randombytes_uniform = salsa20_random_uniform,
.randombytes_buf = salsa20_random_buf,
.randombytes_close = salsa20_random_close
};
const char *
randombytes_implementation_name(void)
{
return implementation.randombytes_implementation_name();
}
uint32_t
randombytes_random(void)
{
return implementation.randombytes_random();
}
void
randombytes_stir(void)
{
return implementation.randombytes_stir();
}
uint32_t
randombytes_uniform(const uint32_t upper_bound)
{
return implementation.randombytes_uniform(upper_bound);
}
void
randombytes_buf(void * const buf, const size_t size)
{
return implementation.randombytes_buf(buf, size);
}
int
randombytes_close(void)
{
return implementation.randombytes_close();
}
void
randombytes(unsigned char * const buf, const unsigned long long buf_len)
{
assert(buf_len <= SIZE_MAX);
salsa20_random_buf(buf, buf_len);
randombytes_buf(buf, (size_t) buf_len);
}

View File

@ -306,3 +306,9 @@ salsa20_random_uniform(const uint32_t upper_bound)
}
return r % upper_bound;
}
const char *
salsa20_random_implementation_name(void)
{
return "salsa20_random";
}