diff options
author | tedu <> | 2014-07-13 14:21:14 +0000 |
---|---|---|
committer | tedu <> | 2014-07-13 14:21:14 +0000 |
commit | 1de48d58864d96a54f8725bf948fd19f0b9026d5 (patch) | |
tree | 764f1697ba2a79b4b7b7ad967e325a96dd7106a3 | |
parent | 93015607b39f25ca4df49ca2c6da58ae67994035 (diff) | |
download | openbsd-1de48d58864d96a54f8725bf948fd19f0b9026d5.tar.gz openbsd-1de48d58864d96a54f8725bf948fd19f0b9026d5.tar.bz2 openbsd-1de48d58864d96a54f8725bf948fd19f0b9026d5.zip |
once srandomdev() is called, switch to using arc4random() but mask off the
high bit as required by posix. wouldn't want to break any standards.
idea and ok deraadt
-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 { |