summaryrefslogtreecommitdiff
path: root/sysdep/unix/random.c
blob: 4e64e56b8a9c0a2468196ca709cc073d1e6ef3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 *	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>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include "sysdep/config.h"
#include "nest/bird.h"

#ifdef HAVE_GETRANDOM
#include <sys/random.h>
#endif


u32
random_u32(void)
{
  long int rand_low, rand_high;

  rand_low = random();
  rand_high = random();
  return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
}


/* 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;

int
read_urandom_fd(void *buf, uint count)
{
  if (urandom_fd < 0)
  {
    urandom_fd = open("/dev/urandom", O_RDONLY);
    if (urandom_fd < 0)
      die("Cannot open /dev/urandom: %m");
  }

  return read(urandom_fd, buf, count);
}
#endif


void
random_init(void)
{
  uint seed;

  /* 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)
{
  ASSERT(count <= 256);

  while (count > 0)
  {
    int n = -1;

#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)
    {
      if (errno == EINTR)
        continue;
      die("Cannot get random bytes: %m");
    }

    buf += n;
    count -= n;
  }
}