Browse Source

use the new fat random sysctl to get initial state. (fallback to looping).

stir after eating 400000 words.  ok + input deraadt
OPENBSD_3_8
tedu 19 years ago
parent
commit
64393fc9d6
1 changed files with 14 additions and 12 deletions
  1. +14
    -12
      src/lib/libc/crypt/arc4random.c

+ 14
- 12
src/lib/libc/crypt/arc4random.c View File

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


Loading…
Cancel
Save