summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Unangst <tedu@cvs.openbsd.org>2005-06-04 05:13:14 +0000
committerTed Unangst <tedu@cvs.openbsd.org>2005-06-04 05:13:14 +0000
commitb5bb1b375dfb06afc0eb3d1e7eebd3afcee454e0 (patch)
tree4dbbdc71ea919a217483f2f0efefa738e485abe9
parentb17d8df3c47c47915a02540d63ad29fd7f59595a (diff)
use the new fat random sysctl to get initial state. (fallback to looping).
stir after eating 400000 words. ok + input deraadt
-rw-r--r--lib/libc/crypt/arc4random.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/lib/libc/crypt/arc4random.c b/lib/libc/crypt/arc4random.c
index 5c768f54944..1bfaca8cf1b 100644
--- a/lib/libc/crypt/arc4random.c
+++ b/lib/libc/crypt/arc4random.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: arc4random.c,v 1.11 2004/11/02 11:07:13 hshoexer Exp $ */
+/* $OpenBSD: arc4random.c,v 1.12 2005/06/04 05:13:13 tedu Exp $ */
/*
* Arc4 random number generator for OpenBSD.
@@ -47,6 +47,7 @@ struct arc4_stream {
static int rs_initialized;
static struct arc4_stream rs;
static pid_t arc4_stir_pid;
+static int arc4_count;
static inline u_int8_t arc4_getbyte(struct arc4_stream *);
@@ -83,23 +84,23 @@ arc4_stir(struct arc4_stream *as)
{
int i, mib[2];
size_t len;
- struct {
- struct timeval tv;
- u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)];
- } rdat;
+ u_char rnd[128];
- gettimeofday(&rdat.tv, NULL);
mib[0] = CTL_KERN;
mib[1] = KERN_ARND;
- for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) {
- len = sizeof(u_int);
- if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1)
- break;
+ len = sizeof(rnd);
+ if (sysctl(mib, 2, rnd, &len, NULL, 0) == -1) {
+ for (i = 0; i < sizeof(rnd) / sizeof(u_int); i ++) {
+ len = sizeof(u_int);
+ if (sysctl(mib, 2, &rnd[i * sizeof(u_int)], &len,
+ NULL, 0) == -1)
+ break;
+ }
}
arc4_stir_pid = getpid();
- arc4_addrandom(as, (void *)&rdat, sizeof(rdat));
+ arc4_addrandom(as, rnd, sizeof(rnd));
/*
* Discard early keystream, as per recommendations in:
@@ -107,6 +108,7 @@ arc4_stir(struct arc4_stream *as)
*/
for (i = 0; i < 256; i++)
(void)arc4_getbyte(as);
+ arc4_count = 400000;
}
static inline u_int8_t
@@ -155,7 +157,7 @@ arc4random_addrandom(u_char *dat, int datlen)
u_int32_t
arc4random(void)
{
- if (!rs_initialized || arc4_stir_pid != getpid())
+ if (--arc4_count == 0 || !rs_initialized || arc4_stir_pid != getpid())
arc4random_stir();
return arc4_getword(&rs);
}