summaryrefslogtreecommitdiff
path: root/src/librandom/random_init.c
blob: a26217f4241989db913cdc5c26276af8f310218a (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 ()
{
  char seed[160] ;
  random_makeseed(seed) ;
  arc4random_addrandom(seed, 160) ;
  return 1 ;
}

#else
#ifdef SKALIBS_HASGETRANDOM

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

int random_init ()
{
  char seed[160] ;
  random_makeseed(seed) ;
  return openwritenclose_unsafe("/dev/urandom", seed, 160) ;
}

#else
#ifdef SKALIBS_HASDEVURANDOM

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

int random_fd = -1 ;
SURFSchedule surf_here = SURFSCHEDULE_ZERO ;

int random_init ()
{
  int fd ;
  char seed[160] ;
  random_makeseed(seed) ;
  surf_init(&surf_here, seed) ;
  if (!openwritenclose_unsafe("/dev/urandom", seed, 160)) return 0 ;
  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 */

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

SURFSchedule surf_here = SURFSCHEDULE_ZERO ;

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

#endif
#endif
#endif