Browse Source

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@
OPENBSD_4_7
guenther 14 years ago
parent
commit
253b3ff700
1 changed files with 15 additions and 6 deletions
  1. +15
    -6
      src/lib/libc/crypt/arc4random.c

+ 15
- 6
src/lib/libc/crypt/arc4random.c View File

@ -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();


Loading…
Cancel
Save