summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib/random.c
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2014-07-13 14:21:14 +0000
committertedu <tedu@openbsd.org>2014-07-13 14:21:14 +0000
commit00694a1db96e730a0942f4d6f001b002663a4f55 (patch)
tree820fad5a37ff60e387f489c4b38342f37e945e81 /lib/libc/stdlib/random.c
parentAdd support for media types (aka. MIME types): the types section is (diff)
downloadwireguard-openbsd-00694a1db96e730a0942f4d6f001b002663a4f55.tar.xz
wireguard-openbsd-00694a1db96e730a0942f4d6f001b002663a4f55.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
Diffstat (limited to 'lib/libc/stdlib/random.c')
-rw-r--r--lib/libc/stdlib/random.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c
index 737d4d27b60..a71402ace56 100644
--- a/lib/libc/stdlib/random.c
+++ b/lib/libc/stdlib/random.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: random.c,v 1.22 2014/06/15 05:10:58 deraadt Exp $ */
+/* $OpenBSD: random.c,v 1.23 2014/07/13 14:21:14 tedu Exp $ */
/*
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved.
@@ -176,6 +176,8 @@ static int rand_type = TYPE_3;
static int rand_deg = DEG_3;
static int rand_sep = SEP_3;
+static int use_arc4random;
+
_THREAD_PRIVATE_MUTEX(random);
static long random_l(void);
@@ -201,6 +203,7 @@ srandom_l(unsigned int x)
int32_t test;
div_t val;
+ use_arc4random = 0;
if (rand_type == TYPE_0)
state[0] = x;
else {
@@ -254,17 +257,7 @@ srandomdev(void)
size_t len;
LOCK();
- if (rand_type == TYPE_0)
- len = sizeof(state[0]);
- else
- len = rand_deg * sizeof(state[0]);
-
- arc4random_buf(state, len);
-
- if (rand_type != TYPE_0) {
- fptr = &state[rand_sep];
- rptr = &state[0];
- }
+ use_arc4random = 1;
UNLOCK();
}
@@ -298,6 +291,7 @@ initstate(u_int seed, char *arg_state, size_t n)
char *ostate = (char *)(&state[-1]);
LOCK();
+ use_arc4random = 0;
if (rand_type == TYPE_0)
state[-1] = rand_type;
else
@@ -362,6 +356,7 @@ setstate(char *arg_state)
char *ostate = (char *)(&state[-1]);
LOCK();
+ use_arc4random = 0;
if (rand_type == TYPE_0)
state[-1] = rand_type;
else
@@ -412,6 +407,9 @@ random_l(void)
{
int32_t i;
+ if (use_arc4random)
+ return arc4random() & 0x7fffffff;
+
if (rand_type == TYPE_0)
i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
else {