diff options
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/stdlib/random.c | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c index 737d4d27b6..a71402ace5 100644 --- a/src/lib/libc/stdlib/random.c +++ b/src/lib/libc/stdlib/random.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: random.c,v 1.22 2014/06/15 05:10:58 deraadt Exp $ */ | 1 | /* $OpenBSD: random.c,v 1.23 2014/07/13 14:21:14 tedu Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 1983 Regents of the University of California. | 3 | * Copyright (c) 1983 Regents of the University of California. |
| 4 | * All rights reserved. | 4 | * All rights reserved. |
| @@ -176,6 +176,8 @@ static int rand_type = TYPE_3; | |||
| 176 | static int rand_deg = DEG_3; | 176 | static int rand_deg = DEG_3; |
| 177 | static int rand_sep = SEP_3; | 177 | static int rand_sep = SEP_3; |
| 178 | 178 | ||
| 179 | static int use_arc4random; | ||
| 180 | |||
| 179 | _THREAD_PRIVATE_MUTEX(random); | 181 | _THREAD_PRIVATE_MUTEX(random); |
| 180 | static long random_l(void); | 182 | static long random_l(void); |
| 181 | 183 | ||
| @@ -201,6 +203,7 @@ srandom_l(unsigned int x) | |||
| 201 | int32_t test; | 203 | int32_t test; |
| 202 | div_t val; | 204 | div_t val; |
| 203 | 205 | ||
| 206 | use_arc4random = 0; | ||
| 204 | if (rand_type == TYPE_0) | 207 | if (rand_type == TYPE_0) |
| 205 | state[0] = x; | 208 | state[0] = x; |
| 206 | else { | 209 | else { |
| @@ -254,17 +257,7 @@ srandomdev(void) | |||
| 254 | size_t len; | 257 | size_t len; |
| 255 | 258 | ||
| 256 | LOCK(); | 259 | LOCK(); |
| 257 | if (rand_type == TYPE_0) | 260 | use_arc4random = 1; |
| 258 | len = sizeof(state[0]); | ||
| 259 | else | ||
| 260 | len = rand_deg * sizeof(state[0]); | ||
| 261 | |||
| 262 | arc4random_buf(state, len); | ||
| 263 | |||
| 264 | if (rand_type != TYPE_0) { | ||
| 265 | fptr = &state[rand_sep]; | ||
| 266 | rptr = &state[0]; | ||
| 267 | } | ||
| 268 | UNLOCK(); | 261 | UNLOCK(); |
| 269 | } | 262 | } |
| 270 | 263 | ||
| @@ -298,6 +291,7 @@ initstate(u_int seed, char *arg_state, size_t n) | |||
| 298 | char *ostate = (char *)(&state[-1]); | 291 | char *ostate = (char *)(&state[-1]); |
| 299 | 292 | ||
| 300 | LOCK(); | 293 | LOCK(); |
| 294 | use_arc4random = 0; | ||
| 301 | if (rand_type == TYPE_0) | 295 | if (rand_type == TYPE_0) |
| 302 | state[-1] = rand_type; | 296 | state[-1] = rand_type; |
| 303 | else | 297 | else |
| @@ -362,6 +356,7 @@ setstate(char *arg_state) | |||
| 362 | char *ostate = (char *)(&state[-1]); | 356 | char *ostate = (char *)(&state[-1]); |
| 363 | 357 | ||
| 364 | LOCK(); | 358 | LOCK(); |
| 359 | use_arc4random = 0; | ||
| 365 | if (rand_type == TYPE_0) | 360 | if (rand_type == TYPE_0) |
| 366 | state[-1] = rand_type; | 361 | state[-1] = rand_type; |
| 367 | else | 362 | else |
| @@ -412,6 +407,9 @@ random_l(void) | |||
| 412 | { | 407 | { |
| 413 | int32_t i; | 408 | int32_t i; |
| 414 | 409 | ||
| 410 | if (use_arc4random) | ||
| 411 | return arc4random() & 0x7fffffff; | ||
| 412 | |||
| 415 | if (rand_type == TYPE_0) | 413 | if (rand_type == TYPE_0) |
| 416 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | 414 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; |
| 417 | else { | 415 | else { |
