diff options
Diffstat (limited to 'src/lib/libcrypto/rand/rand_unix.c')
-rw-r--r-- | src/lib/libcrypto/rand/rand_unix.c | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/src/lib/libcrypto/rand/rand_unix.c b/src/lib/libcrypto/rand/rand_unix.c index 58c6173094..4bb9666e49 100644 --- a/src/lib/libcrypto/rand/rand_unix.c +++ b/src/lib/libcrypto/rand/rand_unix.c | |||
@@ -133,7 +133,50 @@ | |||
133 | # define FD_SETSIZE (8*sizeof(fd_set)) | 133 | # define FD_SETSIZE (8*sizeof(fd_set)) |
134 | #endif | 134 | #endif |
135 | 135 | ||
136 | #ifdef __OpenBSD__ | 136 | #ifdef __VOS__ |
137 | int RAND_poll(void) | ||
138 | { | ||
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 | |||
148 | /* The VOS random() function starts from a static seed so its | ||
149 | initial value is predictable. If random() returns the | ||
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__ | ||
137 | int RAND_poll(void) | 180 | int RAND_poll(void) |
138 | { | 181 | { |
139 | unsigned char buf[ENTROPY_NEEDED]; | 182 | unsigned char buf[ENTROPY_NEEDED]; |
@@ -157,7 +200,7 @@ int RAND_poll(void) | |||
157 | static const char *randomfiles[] = { DEVRANDOM }; | 200 | static const char *randomfiles[] = { DEVRANDOM }; |
158 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; | 201 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; |
159 | int fd; | 202 | int fd; |
160 | size_t i; | 203 | unsigned int i; |
161 | #endif | 204 | #endif |
162 | #ifdef DEVRANDOM_EGD | 205 | #ifdef DEVRANDOM_EGD |
163 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; | 206 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; |
@@ -170,7 +213,8 @@ int RAND_poll(void) | |||
170 | * have this. Use /dev/urandom if you can as /dev/random may block | 213 | * have this. Use /dev/urandom if you can as /dev/random may block |
171 | * if it runs out of random entries. */ | 214 | * if it runs out of random entries. */ |
172 | 215 | ||
173 | for (i=0; i<sizeof(randomfiles)/sizeof(randomfiles[0]) && n < ENTROPY_NEEDED; i++) | 216 | for (i = 0; (i < sizeof(randomfiles)/sizeof(randomfiles[0])) && |
217 | (n < ENTROPY_NEEDED); i++) | ||
174 | { | 218 | { |
175 | if ((fd = open(randomfiles[i], O_RDONLY | 219 | if ((fd = open(randomfiles[i], O_RDONLY |
176 | #ifdef O_NONBLOCK | 220 | #ifdef O_NONBLOCK |
@@ -187,7 +231,7 @@ int RAND_poll(void) | |||
187 | { | 231 | { |
188 | int usec = 10*1000; /* spend 10ms on each file */ | 232 | int usec = 10*1000; /* spend 10ms on each file */ |
189 | int r; | 233 | int r; |
190 | size_t j; | 234 | unsigned int j; |
191 | struct stat *st=&randomstats[i]; | 235 | struct stat *st=&randomstats[i]; |
192 | 236 | ||
193 | /* Avoid using same input... Used to be O_NOFOLLOW | 237 | /* Avoid using same input... Used to be O_NOFOLLOW |
@@ -205,7 +249,12 @@ int RAND_poll(void) | |||
205 | { | 249 | { |
206 | int try_read = 0; | 250 | int try_read = 0; |
207 | 251 | ||
208 | #if defined(OPENSSL_SYS_LINUX) | 252 | #if defined(OPENSSL_SYS_BEOS_R5) |
253 | /* select() is broken in BeOS R5, so we simply | ||
254 | * try to read something and snooze if we couldn't */ | ||
255 | try_read = 1; | ||
256 | |||
257 | #elif defined(OPENSSL_SYS_LINUX) | ||
209 | /* use poll() */ | 258 | /* use poll() */ |
210 | struct pollfd pset; | 259 | struct pollfd pset; |
211 | 260 | ||
@@ -252,6 +301,10 @@ int RAND_poll(void) | |||
252 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); | 301 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); |
253 | if (r > 0) | 302 | if (r > 0) |
254 | n += r; | 303 | n += r; |
304 | #if defined(OPENSSL_SYS_BEOS_R5) | ||
305 | if (r == 0) | ||
306 | snooze(t.tv_usec); | ||
307 | #endif | ||
255 | } | 308 | } |
256 | else | 309 | else |
257 | r = -1; | 310 | r = -1; |
@@ -305,6 +358,14 @@ int RAND_poll(void) | |||
305 | l=time(NULL); | 358 | l=time(NULL); |
306 | RAND_add(&l,sizeof(l),0.0); | 359 | RAND_add(&l,sizeof(l),0.0); |
307 | 360 | ||
361 | #if defined(OPENSSL_SYS_BEOS) | ||
362 | { | ||
363 | system_info sysInfo; | ||
364 | get_system_info(&sysInfo); | ||
365 | RAND_add(&sysInfo,sizeof(sysInfo),0); | ||
366 | } | ||
367 | #endif | ||
368 | |||
308 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) | 369 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) |
309 | return 1; | 370 | return 1; |
310 | #else | 371 | #else |