summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPhilip Guenthe <guenther@cvs.openbsd.org>2009-12-15 18:19:07 +0000
committerPhilip Guenthe <guenther@cvs.openbsd.org>2009-12-15 18:19:07 +0000
commit222c2131ec6ee80028e52f896a83c3cd43c0243b (patch)
tree1960f51a4281afb52c064b0a1f28f83ee60a1442 /lib
parent70d564abe741cd1f3082dd0a496e1cc70dc3c34b (diff)
No point in refreshing the pid from inside arc4_stir() when that
doesn't test it, so factor out the two places that test it into a routine and do the refreshing there. With this, arch4random_buf() doesn't trigger superfluous calls to getpid() when filling large buffers. ok deraadt@, "looks nicer indeed" otto@
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/crypt/arc4random.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/libc/crypt/arc4random.c b/lib/libc/crypt/arc4random.c
index e921e4b7c70..7580e39fb54 100644
--- a/lib/libc/crypt/arc4random.c
+++ b/lib/libc/crypt/arc4random.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: arc4random.c,v 1.20 2008/10/03 18:46:04 otto Exp $ */
+/* $OpenBSD: arc4random.c,v 1.21 2009/12/15 18:19:06 guenther Exp $ */
/*
* Copyright (c) 1996, David Mazieres <dm@uun.org>
@@ -109,7 +109,6 @@ arc4_stir(void)
len = sizeof(rnd);
sysctl(mib, 2, rnd, &len, NULL, 0);
- arc4_stir_pid = getpid();
arc4_addrandom(rnd, sizeof(rnd));
/*
@@ -121,6 +120,18 @@ arc4_stir(void)
arc4_count = 1600000;
}
+static void
+arc4_stir_if_needed(void)
+{
+ pid_t pid = getpid();
+
+ if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid)
+ {
+ arc4_stir_pid = pid;
+ arc4_stir();
+ }
+}
+
static inline u_int8_t
arc4_getbyte(void)
{
@@ -170,8 +181,7 @@ arc4random(void)
u_int32_t val;
_ARC4_LOCK();
arc4_count -= 4;
- if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
- arc4_stir();
+ arc4_stir_if_needed();
val = arc4_getword();
_ARC4_UNLOCK();
return val;
@@ -182,8 +192,7 @@ arc4random_buf(void *_buf, size_t n)
{
u_char *buf = (u_char *)_buf;
_ARC4_LOCK();
- if (!rs_initialized || arc4_stir_pid != getpid())
- arc4_stir();
+ arc4_stir_if_needed();
while (n--) {
if (--arc4_count <= 0)
arc4_stir();