1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 04:25:10 -07:00

Add randombytes_buf_deterministic()

This commit is contained in:
Frank Denis 2017-02-19 18:40:29 +01:00
parent 70c2796ae5
commit cafb0a695b
6 changed files with 47 additions and 4 deletions

View File

@ -5,6 +5,8 @@
#include <stdlib.h>
#include <string.h>
#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
#define LOAD64_LE(SRC) load64_le(SRC)
static inline uint64_t
load64_le(const uint8_t src[8])

View File

@ -25,9 +25,17 @@ typedef struct randombytes_implementation {
int (*close)(void); /* optional */
} randombytes_implementation;
#define randombytes_SEEDBYTES 32U
SODIUM_EXPORT
size_t randombytes_seedbytes(void);
SODIUM_EXPORT
void randombytes_buf(void * const buf, const size_t size);
SODIUM_EXPORT
void randombytes_buf_deterministic(void * const buf, const size_t size,
const unsigned char seed[randombytes_SEEDBYTES]);
SODIUM_EXPORT
uint32_t randombytes_random(void);

View File

@ -10,6 +10,7 @@
# include <emscripten.h>
#endif
#include "crypto_stream_chacha20.h"
#include "randombytes.h"
#ifdef RANDOMBYTES_DEFAULT_IMPLEMENTATION
# include "randombytes_default.h"
@ -20,6 +21,7 @@
# include "randombytes_sysrandom.h"
# endif
#endif
#include "private/common.h"
/* C++Builder defines a "random" macro */
#undef random
@ -163,6 +165,22 @@ randombytes_buf(void * const buf, const size_t size)
#endif
}
void
randombytes_buf_deterministic(void * const buf, const size_t size,
const unsigned char seed[randombytes_SEEDBYTES])
{
static const unsigned char zero[crypto_stream_chacha20_NONCEBYTES];
COMPILER_ASSERT(randombytes_SEEDBYTES == crypto_stream_chacha20_KEYBYTES);
crypto_stream_chacha20((unsigned char *) buf, size, zero, seed);
}
size_t
randombytes_seedbytes(void)
{
return randombytes_SEEDBYTES;
}
int
randombytes_close(void)
{

View File

@ -31,6 +31,7 @@
#include "randombytes.h"
#include "randombytes_salsa20_random.h"
#include "utils.h"
#include "private/common.h"
#ifdef _WIN32
# include <windows.h>
@ -49,7 +50,6 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
#define SALSA20_RANDOM_BLOCK_SIZE crypto_core_salsa20_OUTPUTBYTES
#define HASH_BLOCK_SIZE 128U
#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
#if defined(__OpenBSD__) || defined(__CloudABI__)
# define HAVE_SAFE_ARC4RANDOM 1

View File

@ -27,9 +27,16 @@ static int compat_tests(void)
static int randombytes_tests(void)
{
unsigned int f = 0U;
unsigned int i;
uint32_t n;
const static unsigned char seed[randombytes_SEEDBYTES] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
unsigned char out[100];
unsigned int f = 0U;
unsigned int i;
uint32_t n;
#ifdef __EMSCRIPTEN__
assert(strcmp(randombytes_implementation_name(), "js") == 0);
@ -92,6 +99,13 @@ static int randombytes_tests(void)
}
}
assert(randombytes_uniform(1U) == 0U);
randombytes_buf_deterministic(out, sizeof out, seed);
for (i = 0; i < sizeof out; ++i) {
printf("%02x", out[i]);
}
printf(" (deterministic)\n");
randombytes_close();
randombytes(x, 1U);

View File

@ -1 +1,2 @@
39fd2b7dd9c5196a8dbd0377b8dc4a498a35d86fbcde6accb2cc7d4cd8ea24922b23cce7a26023ab3f0eef693ac87f64258235eab1f7a32dc22762a0485b410c18b84231ade6a6d113615c61af434e27f8b1f3f5e1ad5b5cecf8fc122a35755c7208086d (deterministic)
OK