Skip to content

Commit

Permalink
core: cleanup code for fortuna and random implementation
Browse files Browse the repository at this point in the history
- fix indention for a few functions
- use types from sys/types.h consistently
- get rid of redundant wrapper function, we implement a wrapper in core anyway
  • Loading branch information
henningw committed Oct 3, 2019
1 parent 6f66bd2 commit 752f3b4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 57 deletions.
1 change: 0 additions & 1 deletion src/core/rand/fortuna/fortuna.c
Expand Up @@ -31,7 +31,6 @@

#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <string.h>

#include "../../crypto/rijndael.h"
Expand Down
6 changes: 4 additions & 2 deletions src/core/rand/fortuna/fortuna.h
Expand Up @@ -32,7 +32,9 @@
#ifndef __FORTUNA_H
#define __FORTUNA_H

void fortuna_get_bytes(unsigned len, unsigned char *dst);
void fortuna_add_entropy(const unsigned char *data, unsigned len);
#include <sys/types.h>

void fortuna_get_bytes(unsigned len, u_int8_t *dst);
void fortuna_add_entropy(const u_int8_t *data, unsigned len);

#endif
99 changes: 47 additions & 52 deletions src/core/rand/fortuna/random.c
Expand Up @@ -58,15 +58,15 @@
static time_t seed_time = 0;
static time_t check_time = 0;

static int get_random_bytes_int(u_int8_t *dst, unsigned count);
int sr_get_pseudo_random_bytes(u_int8_t *dst, unsigned count);

/* private functions */

static int safe_read(int fd, void *buf, size_t count)
{
int done = 0;
char *p = buf;
int res;
int done = 0;
char *p = buf;
int res;

while (count)
{
Expand All @@ -86,8 +86,8 @@ static int safe_read(int fd, void *buf, size_t count)

static u_int8_t * try_dev_random(u_int8_t *dst)
{
int fd;
int res;
int fd;
int res;

fd = open("/dev/urandom", O_RDONLY, 0);
if (fd == -1)
Expand All @@ -110,69 +110,64 @@ static u_int8_t * try_dev_random(u_int8_t *dst)
*/
static unsigned acquire_system_randomness(u_int8_t *dst)
{
u_int8_t *p = dst;
u_int8_t *p = dst;

p = try_dev_random(p);
return p - dst;
}

static void system_reseed(void)
{
u_int8_t buf[1024];
int n;
time_t t;
int skip = 1;

t = time(NULL);

if (seed_time == 0)
skip = 0;
else if ((t - seed_time) < SYSTEM_RESEED_MIN)
skip = 1;
else if ((t - seed_time) > SYSTEM_RESEED_MAX)
skip = 0;
else if (check_time == 0 ||
(t - check_time) > SYSTEM_RESEED_CHECK_TIME)
{
check_time = t;

/* roll dice */
get_random_bytes_int(buf, 1);
skip = buf[0] >= SYSTEM_RESEED_CHANCE;
}
/* clear 1 byte */
memset(buf, 0, sizeof(buf));

if (skip)
return;

n = acquire_system_randomness(buf);
if (n > 0)
fortuna_add_entropy(buf, n);

seed_time = t;
memset(buf, 0, sizeof(buf));
}
u_int8_t buf[1024];
int n;
time_t t;
int skip = 1;

t = time(NULL);

if (seed_time == 0)
skip = 0;
else if ((t - seed_time) < SYSTEM_RESEED_MIN)
skip = 1;
else if ((t - seed_time) > SYSTEM_RESEED_MAX)
skip = 0;
else if (check_time == 0 ||
(t - check_time) > SYSTEM_RESEED_CHECK_TIME)
{
check_time = t;

static int get_random_bytes_int(u_int8_t *dst, unsigned count)
{
system_reseed();
fortuna_get_bytes(count, dst);
return 0;
/* roll dice */
sr_get_pseudo_random_bytes(buf, 1);
skip = buf[0] >= SYSTEM_RESEED_CHANCE;
}
/* clear 1 byte */
memset(buf, 0, sizeof(buf));

if (skip)
return;

n = acquire_system_randomness(buf);
if (n > 0)
fortuna_add_entropy(buf, n);

seed_time = t;
memset(buf, 0, sizeof(buf));
}


/* public functions */

int sr_get_pseudo_random_bytes(u_int8_t *dst, unsigned count)
{
return get_random_bytes_int(dst, count);
system_reseed();
fortuna_get_bytes(count, dst);
return 0;
}



int sr_add_entropy(const u_int8_t *data, unsigned count)
{
system_reseed();
fortuna_add_entropy(data, count);
return 0;
system_reseed();
fortuna_add_entropy(data, count);
return 0;
}
5 changes: 3 additions & 2 deletions src/core/rand/fortuna/random.h
Expand Up @@ -37,6 +37,7 @@
#ifndef _RANDOM_H_
#define _RANDOM_H_

#include <sys/types.h>

/*
* System reseeds should be separated at least this much.
Expand All @@ -56,9 +57,9 @@
*/
#define SYSTEM_RESEED_MAX (12*60*60) /* 12h */

int sr_get_pseudo_random_bytes(unsigned char *dst, unsigned count);
int sr_get_pseudo_random_bytes(u_int8_t *dst, unsigned count);

int sr_add_entropy(const unsigned char *data, unsigned count);
int sr_add_entropy(const u_int8_t *data, unsigned count);


#endif

0 comments on commit 752f3b4

Please sign in to comment.