summaryrefslogtreecommitdiff
path: root/src/librandom/random_buf_early.c
blob: d14cd24a0936eb6fe9756548129e49feba4b1a91 (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
/* ISC license. */

#include <skalibs/sysdeps.h>

#if defined(SKALIBS_HASARC4RANDOM) && !defined(__linux__)

#include <skalibs/nonposix.h>
#include <stdlib.h>
#include <skalibs/random.h>

void random_buf_early (char *s, size_t n)
{
  arc4random_buf(s, n) ;
}

#elif defined(SKALIBS_HASGETRANDOM) && defined(SKALIBS_HASGRNDINSECURE)

#include <errno.h>
#include <sys/random.h>

#include <skalibs/random.h>

void random_buf_early (char *s, size_t n)
{
  static int broken = 0 ;
  if (broken) goto bleh ;
  while (n)
  {
    ssize_t r = getrandom(s, n, GRND_INSECURE) ;
    if (r == -1)
    {
      if (errno != EINTR) goto breakit ;
      else continue ;
    }
    s += r ;
    n -= r ;
  }
  return ;
 breakit:
  broken = 1 ;
 bleh:
  random_devurandom(s, n) ;
}

#elif defined(SKALIBS_HASDEVURANDOM)

#include <skalibs/random.h>

void random_buf_early (char *s, size_t n)
{
  random_devurandom(s, n) ;
}

#else

#include <skalibs/surf.h>
#include <skalibs/random.h>

void random_buf_early (char *s, size_t n)
{
  autosurf(s, n) ;
}

#endif