2000-06-05 19:46:40 +08:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Random Numbers
|
|
|
|
*
|
|
|
|
* (c) 2000 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2021-04-02 01:20:13 +08:00
|
|
|
#include <fcntl.h>
|
2021-04-08 07:15:17 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2021-04-02 01:20:13 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "sysdep/config.h"
|
2021-04-08 07:15:17 +08:00
|
|
|
#include "nest/bird.h"
|
2021-04-02 01:20:13 +08:00
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
#ifdef HAVE_GETRANDOM
|
|
|
|
#include <sys/random.h>
|
2021-04-02 01:20:13 +08:00
|
|
|
#endif
|
2000-06-05 19:46:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
u32
|
|
|
|
random_u32(void)
|
|
|
|
{
|
|
|
|
long int rand_low, rand_high;
|
|
|
|
|
|
|
|
rand_low = random();
|
|
|
|
rand_high = random();
|
|
|
|
return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
|
|
|
|
}
|
2021-04-02 01:20:13 +08:00
|
|
|
|
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
/* If there is no getrandom() / getentropy(), use /dev/urandom */
|
|
|
|
#if !defined(HAVE_GETRANDOM) && !defined(HAVE_GETENTROPY)
|
|
|
|
|
|
|
|
#define HAVE_URANDOM_FD 1
|
|
|
|
static int urandom_fd = -1;
|
|
|
|
|
2021-04-02 01:20:13 +08:00
|
|
|
int
|
2021-04-08 07:15:17 +08:00
|
|
|
read_urandom_fd(void *buf, uint count)
|
2021-04-02 01:20:13 +08:00
|
|
|
{
|
2021-04-08 07:15:17 +08:00
|
|
|
if (urandom_fd < 0)
|
|
|
|
{
|
|
|
|
urandom_fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (urandom_fd < 0)
|
|
|
|
die("Cannot open /dev/urandom: %m");
|
2021-04-02 01:20:13 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
return read(urandom_fd, buf, count);
|
2021-04-02 01:20:13 +08:00
|
|
|
}
|
2021-04-08 07:15:17 +08:00
|
|
|
#endif
|
2021-04-02 01:20:13 +08:00
|
|
|
|
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
void
|
|
|
|
random_init(void)
|
|
|
|
{
|
|
|
|
uint seed;
|
2021-04-02 01:20:13 +08:00
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
/* Get random bytes to trip any errors early and to seed random() */
|
|
|
|
random_bytes(&seed, sizeof(seed));
|
|
|
|
|
|
|
|
srandom(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
random_bytes(void *buf, size_t count)
|
2021-04-02 01:20:13 +08:00
|
|
|
{
|
2021-04-08 07:15:17 +08:00
|
|
|
ASSERT(count <= 256);
|
2021-04-02 01:20:13 +08:00
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
while (count > 0)
|
2021-04-02 01:20:13 +08:00
|
|
|
{
|
2021-04-08 07:15:17 +08:00
|
|
|
int n = -1;
|
2021-04-02 01:20:13 +08:00
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
#if defined(HAVE_GETRANDOM)
|
|
|
|
n = getrandom(buf, count, 0);
|
|
|
|
#elif defined(HAVE_GETENTROPY)
|
|
|
|
n = getentropy(buf, count);
|
|
|
|
n = !n ? (int) count : n;
|
|
|
|
#elif defined(HAVE_URANDOM_FD)
|
|
|
|
n = read_urandom_fd(buf, count);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
{
|
2021-04-02 01:20:13 +08:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2021-04-08 07:15:17 +08:00
|
|
|
die("Cannot get random bytes: %m");
|
2021-04-02 01:20:13 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 07:15:17 +08:00
|
|
|
buf += n;
|
|
|
|
count -= n;
|
2021-04-02 01:20:13 +08:00
|
|
|
}
|
|
|
|
}
|