From 253b3ff700a2a06cca6bc163e3473d748bbf5396 Mon Sep 17 00:00:00 2001 From: guenther <> Date: Tue, 15 Dec 2009 18:19:06 +0000 Subject: [PATCH] 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@ --- src/lib/libc/crypt/arc4random.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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();