Skip to content

Commit

Permalink
Update, following David's comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane Beckwith committed Sep 21, 2017
1 parent 3f036c8 commit 6705bcc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
17 changes: 4 additions & 13 deletions include/ecdaa/prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,20 @@ int ecdaa_prng_init(struct ecdaa_prng *prng_in);

#endif // DISABLE_LIBSODIUM_RNG_SEED_FUNCTION

/*
* Type of function used to obtain a cryptographically-secure seed.
*
* Must be able to provide AMCL_SEED_SIZE crpytographically-strong bytes in a single call.
*
* Returns:
* 0 on success
* non-zero on failure
*/
typedef int (*ecdaa_prng_custom_seed_function_type)(void * const buf, const size_t size);

/*
* Properly seed a `ecdaa_prng`.
*
* MUST be called on a `ecdaa_prng` before first use.
*
* No dynamic memory allocation is performed.
*
* `seed_size` MUST be at least AMCL_SEED_SIZE.
*
* Returns:
* 0 on success
* return value of `custom_func`, if non-zero
* -1 if seed_size < AMCL_SEED_SIZE
*/
int ecdaa_prng_init_custom(struct ecdaa_prng *prng_in, ecdaa_prng_custom_seed_function_type custom_func);
int ecdaa_prng_init_custom(struct ecdaa_prng *prng_in, char *seed, size_t seed_size);

#ifdef __cplusplus
}
Expand Down
57 changes: 32 additions & 25 deletions src/prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,32 @@
#include <linux/random.h>
#endif // defined(__linux__)

static int check_entropy();

int ecdaa_prng_init(struct ecdaa_prng *prng_in)
{
int ret = 0;

prng_in->initialized = ECDAA_PRNG_INITIALIZED_NO;

do {
ret = check_entropy();
if (0 != ret)
break;

// Note: We don't have to worry about the race-condition here.
// `sodium_init` can be called multiple times, and from multiple threads.
if (-1 == sodium_init()) {
ret = -1;
break;
}
#if defined(__linux__) && defined(RNDGETENTCNT)
int fd;
if ((fd = open("/dev/random", O_RDONLY)) != -1) {
int c;
if (ioctl(fd, RNDGETENTCNT, &c) == 0 && c < 160) {
ret = -2;
}
(void) close(fd);
if (0 != ret)
break;
}
#endif

RAND_clean(&prng_in->impl);

char seed[AMCL_SEED_SIZE];
randombytes_buf(seed, sizeof(seed));

randombytes_buf(seed, AMCL_SEED_SIZE);

RAND_seed(&prng_in->impl, AMCL_SEED_SIZE, seed);
ret = ecdaa_prng_init_custom(prng_in, seed, sizeof(seed));
if (0 != ret)
break;
} while(0);

if (0 == ret)
Expand All @@ -70,21 +63,35 @@ int ecdaa_prng_init(struct ecdaa_prng *prng_in)
return ret;
}

static int check_entropy()
{
int ret = 0;
#if defined(__linux__) && defined(RNDGETENTCNT)
int fd;
if ((fd = open("/dev/random", O_RDONLY)) != -1) {
int c;
if (ioctl(fd, RNDGETENTCNT, &c) == 0 && c < 160) {
ret = -2;
}
(void) close(fd);
}
#endif

return ret;
}

#endif // DISABLE_LIBSODIUM_RNG_SEED_FUNCTION

int ecdaa_prng_init_custom(struct ecdaa_prng *prng_in, ecdaa_prng_custom_seed_function_type custom_func)
int ecdaa_prng_init_custom(struct ecdaa_prng *prng_in, char *seed, size_t seed_size)
{
prng_in->initialized = ECDAA_PRNG_INITIALIZED_NO;

RAND_clean(&prng_in->impl);
if (AMCL_SEED_SIZE > seed_size)
return -1;

char seed[AMCL_SEED_SIZE];

int ret = custom_func(seed, AMCL_SEED_SIZE);
if (0 != ret)
return ret;
RAND_clean(&prng_in->impl);

RAND_seed(&prng_in->impl, AMCL_SEED_SIZE, seed);
RAND_seed(&prng_in->impl, seed_size, seed);

prng_in->initialized = ECDAA_PRNG_INITIALIZED_YES;

Expand Down
19 changes: 5 additions & 14 deletions test/prng-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include <amcl/amcl.h>

static int deterministic_seed_func(void * const buf, const size_t size);

static void different_rngs_are_different();
static void deterministic_seed_makes_same_rngs();

Expand Down Expand Up @@ -56,8 +54,11 @@ static void different_rngs_are_different()
static void deterministic_seed_makes_same_rngs()
{
struct ecdaa_prng rng1, rng2;
ecdaa_prng_init_custom(&rng1, &deterministic_seed_func);
ecdaa_prng_init_custom(&rng2, &deterministic_seed_func);
char seed[AMCL_SEED_SIZE];
for (size_t i = 0; i < sizeof(seed); i++)
seed[i] = 5;
ecdaa_prng_init_custom(&rng1, seed, sizeof(seed));
ecdaa_prng_init_custom(&rng2, seed, sizeof(seed));

int bytes1[5];
int bytes2[5];
Expand All @@ -72,13 +73,3 @@ static void deterministic_seed_makes_same_rngs()
&& bytes1[4] == bytes2[4]
);
}

static int deterministic_seed_func(void * const buf, const size_t size)
{
uint8_t *buf_ = (uint8_t*)buf;

for (size_t i = 0; i < size; i++)
buf_[i] = 5;

return 0;
}

0 comments on commit 6705bcc

Please sign in to comment.