summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorguenther <>2009-12-15 18:19:06 +0000
committerguenther <>2009-12-15 18:19:06 +0000
commit0ac52e6e4d02749d85405ea08400d4b7e6db2ca1 (patch)
tree4f708f2e07feb88d20f5fc86fd6a5b8aaaa620f7
parente0fd73fcca39e03d57dc0a553e943e84cda61e30 (diff)
downloadopenbsd-0ac52e6e4d02749d85405ea08400d4b7e6db2ca1.tar.gz
openbsd-0ac52e6e4d02749d85405ea08400d4b7e6db2ca1.tar.bz2
openbsd-0ac52e6e4d02749d85405ea08400d4b7e6db2ca1.zip
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@
-rw-r--r--src/lib/libc/crypt/arc4random.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c
index e921e4b7c7..7580e39fb5 100644
--- a/src/lib/libc/crypt/arc4random.c
+++ b/src/lib/libc/crypt/arc4random.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: arc4random.c,v 1.20 2008/10/03 18:46:04 otto Exp $ */ 1/* $OpenBSD: arc4random.c,v 1.21 2009/12/15 18:19:06 guenther Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org> 4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
@@ -109,7 +109,6 @@ arc4_stir(void)
109 len = sizeof(rnd); 109 len = sizeof(rnd);
110 sysctl(mib, 2, rnd, &len, NULL, 0); 110 sysctl(mib, 2, rnd, &len, NULL, 0);
111 111
112 arc4_stir_pid = getpid();
113 arc4_addrandom(rnd, sizeof(rnd)); 112 arc4_addrandom(rnd, sizeof(rnd));
114 113
115 /* 114 /*
@@ -121,6 +120,18 @@ arc4_stir(void)
121 arc4_count = 1600000; 120 arc4_count = 1600000;
122} 121}
123 122
123static void
124arc4_stir_if_needed(void)
125{
126 pid_t pid = getpid();
127
128 if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid)
129 {
130 arc4_stir_pid = pid;
131 arc4_stir();
132 }
133}
134
124static inline u_int8_t 135static inline u_int8_t
125arc4_getbyte(void) 136arc4_getbyte(void)
126{ 137{
@@ -170,8 +181,7 @@ arc4random(void)
170 u_int32_t val; 181 u_int32_t val;
171 _ARC4_LOCK(); 182 _ARC4_LOCK();
172 arc4_count -= 4; 183 arc4_count -= 4;
173 if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid()) 184 arc4_stir_if_needed();
174 arc4_stir();
175 val = arc4_getword(); 185 val = arc4_getword();
176 _ARC4_UNLOCK(); 186 _ARC4_UNLOCK();
177 return val; 187 return val;
@@ -182,8 +192,7 @@ arc4random_buf(void *_buf, size_t n)
182{ 192{
183 u_char *buf = (u_char *)_buf; 193 u_char *buf = (u_char *)_buf;
184 _ARC4_LOCK(); 194 _ARC4_LOCK();
185 if (!rs_initialized || arc4_stir_pid != getpid()) 195 arc4_stir_if_needed();
186 arc4_stir();
187 while (n--) { 196 while (n--) {
188 if (--arc4_count <= 0) 197 if (--arc4_count <= 0)
189 arc4_stir(); 198 arc4_stir();