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 71b98ec212..e9ead3a529 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 | u_int32_t rnd = 0, i; | 182 | u_int32_t rnd = 0, i; |
| @@ -163,7 +206,7 @@ int RAND_poll(void) | |||
| 163 | static const char *randomfiles[] = { DEVRANDOM }; | 206 | static const char *randomfiles[] = { DEVRANDOM }; |
| 164 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; | 207 | struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])]; |
| 165 | int fd; | 208 | int fd; |
| 166 | size_t i; | 209 | unsigned int i; |
| 167 | #endif | 210 | #endif |
| 168 | #ifdef DEVRANDOM_EGD | 211 | #ifdef DEVRANDOM_EGD |
| 169 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; | 212 | static const char *egdsockets[] = { DEVRANDOM_EGD, NULL }; |
| @@ -176,7 +219,8 @@ int RAND_poll(void) | |||
| 176 | * have this. Use /dev/urandom if you can as /dev/random may block | 219 | * have this. Use /dev/urandom if you can as /dev/random may block |
| 177 | * if it runs out of random entries. */ | 220 | * if it runs out of random entries. */ |
| 178 | 221 | ||
| 179 | for (i=0; i<sizeof(randomfiles)/sizeof(randomfiles[0]) && n < ENTROPY_NEEDED; i++) | 222 | for (i = 0; (i < sizeof(randomfiles)/sizeof(randomfiles[0])) && |
| 223 | (n < ENTROPY_NEEDED); i++) | ||
| 180 | { | 224 | { |
| 181 | if ((fd = open(randomfiles[i], O_RDONLY | 225 | if ((fd = open(randomfiles[i], O_RDONLY |
| 182 | #ifdef O_NONBLOCK | 226 | #ifdef O_NONBLOCK |
| @@ -193,7 +237,7 @@ int RAND_poll(void) | |||
| 193 | { | 237 | { |
| 194 | int usec = 10*1000; /* spend 10ms on each file */ | 238 | int usec = 10*1000; /* spend 10ms on each file */ |
| 195 | int r; | 239 | int r; |
| 196 | size_t j; | 240 | unsigned int j; |
| 197 | struct stat *st=&randomstats[i]; | 241 | struct stat *st=&randomstats[i]; |
| 198 | 242 | ||
| 199 | /* Avoid using same input... Used to be O_NOFOLLOW | 243 | /* Avoid using same input... Used to be O_NOFOLLOW |
| @@ -211,7 +255,12 @@ int RAND_poll(void) | |||
| 211 | { | 255 | { |
| 212 | int try_read = 0; | 256 | int try_read = 0; |
| 213 | 257 | ||
| 214 | #if defined(OPENSSL_SYS_LINUX) | 258 | #if defined(OPENSSL_SYS_BEOS_R5) |
| 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) | ||
| 215 | /* use poll() */ | 264 | /* use poll() */ |
| 216 | struct pollfd pset; | 265 | struct pollfd pset; |
| 217 | 266 | ||
| @@ -258,6 +307,10 @@ int RAND_poll(void) | |||
| 258 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); | 307 | r = read(fd,(unsigned char *)tmpbuf+n, ENTROPY_NEEDED-n); |
| 259 | if (r > 0) | 308 | if (r > 0) |
| 260 | n += r; | 309 | n += r; |
| 310 | #if defined(OPENSSL_SYS_BEOS_R5) | ||
| 311 | if (r == 0) | ||
| 312 | snooze(t.tv_usec); | ||
| 313 | #endif | ||
| 261 | } | 314 | } |
| 262 | else | 315 | else |
| 263 | r = -1; | 316 | r = -1; |
| @@ -311,6 +364,14 @@ int RAND_poll(void) | |||
| 311 | l=time(NULL); | 364 | l=time(NULL); |
| 312 | RAND_add(&l,sizeof(l),0.0); | 365 | RAND_add(&l,sizeof(l),0.0); |
| 313 | 366 | ||
| 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 | |||
| 314 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) | 375 | #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD) |
| 315 | return 1; | 376 | return 1; |
| 316 | #else | 377 | #else |
