diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index e921e4b7..7580e39f 100644 --- a/src/lib/libc/crypt/arc4random.c +++ b/src/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 @@ -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();