-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.c
195 lines (160 loc) · 3.62 KB
/
random.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Routines to get randomness/set seeds.
*/
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "shm.h"
#include "params.h" // 'user_set_seed'
#include "log.h"
#include "sanitise.h"
/* The actual seed lives in the shm. This variable is used
* to store what gets passed in from the command line -s argument */
unsigned int seed = 0;
static void syslog_seed(int seedparam)
{
fprintf(stderr, "Randomness reseeded to %u\n", seedparam);
openlog("trinity", LOG_CONS|LOG_PERROR, LOG_USER);
syslog(LOG_CRIT, "Randomness reseeded to %u\n", seedparam);
closelog();
}
unsigned int new_seed(void)
{
int fd;
struct timeval t;
unsigned int r;
if ((fd = open("/dev/urandom", O_RDONLY)) < 0 ||
read(fd, &r, sizeof(r)) != sizeof(r)) {
r = rand();
if (!(rand() % 2)) {
gettimeofday(&t, 0);
r |= t.tv_usec;
}
}
if (fd >= 0)
close(fd);
return r;
}
/*
* If we passed in a seed with -s, use that. Otherwise make one up from time of day.
*/
unsigned int init_seed(unsigned int seedparam)
{
if (user_set_seed == TRUE)
printf("[%d] Using user passed random seed: %u\n", getpid(), seedparam);
else {
seedparam = new_seed();
printf("Initial random seed: %u\n", seedparam);
}
if (do_syslog == TRUE)
syslog_seed(seedparam);
return seedparam;
}
/* Mix in the pidslot so that all children get different randomness.
* we can't use the actual pid or anything else 'random' because otherwise reproducing
* seeds with -s would be much harder to replicate.
*/
void set_seed(unsigned int pidslot)
{
srand(shm->seed + (pidslot + 1));
shm->seeds[pidslot] = shm->seed;
}
/*
* Periodically reseed.
*
* We do this so we can log a new seed every now and again, so we can cut down on the
* amount of time necessary to reproduce a bug.
* Caveat: Not used if we passed in our own seed with -s
*/
void reseed(void)
{
shm->need_reseed = FALSE;
shm->reseed_counter = 0;
if (getpid() != shm->parentpid) {
output(0, "Reseeding should only happen from parent!\n");
exit(EXIT_FAILURE);
}
/* don't change the seed if we passed -s */
if (user_set_seed == TRUE)
return;
/* We are reseeding. */
shm->seed = new_seed();
output(0, "[%d] Random reseed: %u\n", getpid(), shm->seed);
if (do_syslog == TRUE)
syslog_seed(shm->seed);
}
unsigned int rand_bool(void)
{
return rand() % 2;
}
unsigned int rand_single_32bit(void)
{
return (1L << (rand() % 32));
}
unsigned long rand_single_64bit(void)
{
return (1L << (rand() % 64));
}
unsigned int rand32(void)
{
unsigned long r = 0;
switch (rand() % 3) {
/* Just set one bit */
case 0: return rand_single_32bit();
/* 0 .. RAND_MAX */
case 1: r = rand();
if (rand_bool())
r |= (1L << 31);
break;
case 2: return get_interesting_32bit_value();
default:
break;
}
return r;
}
unsigned long rand64(void)
{
unsigned long r = 0;
switch (rand() % 7) {
/* Just set one bit */
case 0: return rand_single_32bit();
case 1: return rand_single_64bit();
/* Sometimes pick a not-so-random number. */
case 2: return get_interesting_value();
/* limit to RAND_MAX (31 bits) */
case 3: r = rand();
break;
/* do some gymnastics here to get > RAND_MAX
* Based on very similar routine stolen from iknowthis. Thanks Tavis.
*/
case 4:
r = rand() & rand();
#if __WORDSIZE == 64
r <<= 32;
r |= rand() & rand();
#endif
break;
case 5:
r = rand() | rand();
#if __WORDSIZE == 64
r <<= 32;
r |= rand() | rand();
#endif
break;
case 6:
r = rand();
#if __WORDSIZE == 64
r <<= 32;
r |= rand();
#endif
break;
default:
break;
}
if (rand_bool())
r |= (1L << (__WORDSIZE - 1));
return r;
}