summaryrefslogtreecommitdiff
path: root/src/librandom/random_init.c
blob: e780cfa68452a464625e74cfb83ad3c92a743f24 (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
/* 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/djbunix.h>
#include <skalibs/random.h>

int random_init ()
{
#ifdef SKALIBS_HASDEVURANDOM
  char seed[160] ;
  random_makeseed(seed) ;
  openwritenclose_unsafe("/dev/urandom", seed, 160) ;
#endif
  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 ()
{
  int fd ;
  char seed[160] ;
  random_makeseed(seed) ;
  surf_init(&surf_here, seed) ;
  openwritenclose_unsafe("/dev/urandom", seed, 160) ;
  if (random_fd < 0)
  {
    fd = open_readb("/dev/urandom") ;
    if (fd < 0) return 0 ;
    if (coe(fd) < 0)
    {
      fd_close(fd) ;
      return 0 ;
    }
    random_fd = fd ;
  }
  return 1 ;
}

#else /* default */

int random_init ()
{
  char seed[160] ;
  random_makeseed(seed) ;
  surf_init(&surf_here, seed) ;
  return 1 ;
}

#endif
#endif
#endif