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

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASARC4RANDOM

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

#include <skalibs/random.h>

int random_init ()
{
#ifdef SKALIBS_HASARC4RANDOM_ADDRANDOM
  char seed[160] ;
  random_makeseed(seed) ;
  arc4random_addrandom((unsigned char *)seed, 160) ;
#endif
  return 1 ;
}

#else
#ifdef SKALIBS_HASGETRANDOM

#include <skalibs/random.h>

int random_init ()
{
  return 1 ;
}

#else

#include <skalibs/surf.h>
#include <skalibs/random.h>
#include "random-internal.h"

SURFSchedule surf_here = SURFSCHEDULE_ZERO ;

#ifdef SKALIBS_HASDEVURANDOM

#include <errno.h>

#include <skalibs/djbunix.h>

int random_fd = -1 ;

int random_init ()
{
  char seed[160] ;
  if (random_fd >= 0) return 1 ;
  random_makeseed(seed) ;
  surf_init(&surf_here, seed) ;
  openwritenclose_unsafe("/dev/urandom", seed, 160) ;
  random_fd = openc_readb("/dev/urandom") ;
  return random_fd >= 0 ;
}

#else /* default */

int random_init ()
{
  static int initted = 0 ;
  if (!initted)
  {
    char seed[160] ;
    initted = 1 ;
    random_makeseed(seed) ;
    surf_init(&surf_here, seed) ;
  }
  return 1 ;
}

#endif
#endif
#endif