diff options
Diffstat (limited to 'src/lib/libcrypto/rand/rand_unix.c')
-rw-r--r-- | src/lib/libcrypto/rand/rand_unix.c | 81 |
1 files changed, 7 insertions, 74 deletions
diff --git a/src/lib/libcrypto/rand/rand_unix.c b/src/lib/libcrypto/rand/rand_unix.c index e9ead3a529..58c6173094 100644 --- a/src/lib/libcrypto/rand/rand_unix.c +++ b/src/lib/libcrypto/rand/rand_unix.c | |||
@@ -133,62 +133,13 @@ | |||
133 | # define FD_SETSIZE (8*sizeof(fd_set)) | 133 | # define FD_SETSIZE (8*sizeof(fd_set)) |
134 | #endif | 134 | #endif |
135 | 135 | ||
136 | #ifdef __VOS__ | 136 | #ifdef __OpenBSD__ |
137 | int RAND_poll(void) | 137 | int RAND_poll(void) |
138 | { | 138 | { |
139 | unsigned char buf[ENTROPY_NEEDED]; | 139 | unsigned char buf[ENTROPY_NEEDED]; |
140 | pid_t curr_pid; | ||
141 | uid_t curr_uid; | ||
142 | static int first=1; | ||
143 | int i; | ||
144 | long rnd = 0; | ||
145 | struct timespec ts; | ||
146 | unsigned seed; | ||
147 | 140 | ||
148 | /* The VOS random() function starts from a static seed so its | 141 | arc4random_buf(buf, sizeof(buf)); |
149 | initial value is predictable. If random() returns the | 142 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
150 | initial value, reseed it with dynamic data. The VOS | ||
151 | real-time clock has a granularity of 1 nsec so it should be | ||
152 | reasonably difficult to predict its exact value. Do not | ||
153 | gratuitously reseed the PRNG because other code in this | ||
154 | process or thread may be using it. */ | ||
155 | |||
156 | if (first) { | ||
157 | first = 0; | ||
158 | rnd = random (); | ||
159 | if (rnd == 1804289383) { | ||
160 | clock_gettime (CLOCK_REALTIME, &ts); | ||
161 | curr_pid = getpid(); | ||
162 | curr_uid = getuid(); | ||
163 | seed = ts.tv_sec ^ ts.tv_nsec ^ curr_pid ^ curr_uid; | ||
164 | srandom (seed); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | for (i = 0; i < sizeof(buf); i++) { | ||
169 | if (i % 4 == 0) | ||
170 | rnd = random(); | ||
171 | buf[i] = rnd; | ||
172 | rnd >>= 8; | ||
173 | } | ||
174 | RAND_add(buf, sizeof(buf), ENTROPY_NEEDED); | ||
175 | memset(buf, 0, sizeof(buf)); | ||
176 | |||
177 | return 1; | ||
178 | } | ||
179 | #elif defined __OpenBSD__ | ||
180 | int RAND_poll(void) | ||
181 | { | ||
182 | u_int32_t rnd = 0, i; | ||
183 | unsigned char buf[ENTROPY_NEEDED]; | ||
184 | |||
185 | for (i = 0; i < sizeof(buf); i++) { | ||
186 | if (i % 4 == 0) | ||
187 | rnd = arc4random(); | ||
188 | buf[i] = rnd; | ||
189 | rnd >>= 8; | ||
190 | } | ||
191 | RAND_add(buf, sizeof(buf), ENTROPY_NEEDED); | ||
192 | memset(buf, 0, sizeof(buf)); | 143 | memset(buf, 0, sizeof(buf)); |
193 | 144 | ||
194 | return 1; | 145 | return 1; |
@@ -206,7 +157,7 @@ int RAND_poll(void) | |||
206 | static const char *randomfiles[] = { DEVRANDOM }; | 157 | static const char *randomfiles[] = { DEVRANDOM }; |
207 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; | 158 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; |
208 | int fd; | 159 | int fd; |
209 | unsigned int i; | 160 | size_t i; |
210 | #endif | 161 | #endif |
211 | #ifdef DEVRANDOM_EGD | 162 | #ifdef DEVRANDOM_EGD |
212 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; | 163 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; |
@@ -219,8 +170,7 @@ int RAND_poll(void) | |||
219 | * have this. Use /dev/urandom if you can as /dev/random may block | 170 | * have this. Use /dev/urandom if you can as /dev/random may block |
220 | * if it runs out of random entries. */ | 171 | * if it runs out of random entries. */ |
221 | 172 | ||
222 | for (i = 0; (i < sizeof(randomfiles)/sizeof(randomfiles[0])) && | 173 | for (i=0; i<sizeof(randomfiles)/sizeof(randomfiles[0]) && n < ENTROPY_NEEDED; i++) |
223 | (n < ENTROPY_NEEDED); i++) | ||
224 | { | 174 | { |
225 | if ((fd = open(randomfiles[i], O_RDONLY | 175 | if ((fd = open(randomfiles[i], O_RDONLY |
226 | #ifdef O_NONBLOCK | 176 | #ifdef O_NONBLOCK |
@@ -237,7 +187,7 @@ int RAND_poll(void) | |||
237 | { | 187 | { |
238 | int usec = 10*1000; /* spend 10ms on each file */ | 188 | int usec = 10*1000; /* spend 10ms on each file */ |
239 | int r; | 189 | int r; |
240 | unsigned int j; | 190 | size_t j; |
241 | struct stat *st=&randomstats[i]; | 191 | struct stat *st=&randomstats[i]; |
242 | 192 | ||
243 | /* Avoid using same input... Used to be O_NOFOLLOW | 193 | /* Avoid using same input... Used to be O_NOFOLLOW |
@@ -255,12 +205,7 @@ int RAND_poll(void) | |||
255 | { | 205 | { |
256 | int try_read = 0; | 206 | int try_read = 0; |
257 | 207 | ||
258 | #if defined(OPENSSL_SYS_BEOS_R5) | 208 | #if defined(OPENSSL_SYS_LINUX) |
259 | /* select() is broken in BeOS R5, so we simply | ||
260 | * try to read something and snooze if we couldn't */ | ||
261 | try_read = 1; | ||
262 | |||
263 | #elif defined(OPENSSL_SYS_LINUX) | ||
264 | /* use poll() */ | 209 | /* use poll() */ |
265 | struct pollfd pset; | 210 | struct pollfd pset; |
266 | 211 | ||
@@ -307,10 +252,6 @@ int RAND_poll(void) | |||
307 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); | 252 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); |
308 | if (r > 0) | 253 | if (r > 0) |
309 | n += r; | 254 | n += r; |
310 | #if defined(OPENSSL_SYS_BEOS_R5) | ||
311 | if (r == 0) | ||
312 | snooze(t.tv_usec); | ||
313 | #endif | ||
314 | } | 255 | } |
315 | else | 256 | else |
316 | r = -1; | 257 | r = -1; |
@@ -364,14 +305,6 @@ int RAND_poll(void) | |||
364 | l=time(NULL); | 305 | l=time(NULL); |
365 | RAND_add(&l,sizeof(l),0.0); | 306 | RAND_add(&l,sizeof(l),0.0); |
366 | 307 | ||
367 | #if defined(OPENSSL_SYS_BEOS) | ||
368 | { | ||
369 | system_info sysInfo; | ||
370 | get_system_info(&sysInfo); | ||
371 | RAND_add(&sysInfo,sizeof(sysInfo),0); | ||
372 | } | ||
373 | #endif | ||
374 | |||
375 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) | 308 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) |
376 | return 1; | 309 | return 1; |
377 | #else | 310 | #else |