summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/bsd-arc4random.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/openbsd-compat/bsd-arc4random.c b/openbsd-compat/bsd-arc4random.c
index 87c7da5f7..2f3130700 100644
--- a/openbsd-compat/bsd-arc4random.c
+++ b/openbsd-compat/bsd-arc4random.c
@@ -24,7 +24,7 @@
24 24
25#include "includes.h" 25#include "includes.h"
26 26
27RCSID("$Id: bsd-arc4random.c,v 1.2 2001/02/09 01:55:36 djm Exp $"); 27RCSID("$Id: bsd-arc4random.c,v 1.3 2001/03/18 22:38:16 djm Exp $");
28 28
29#ifndef HAVE_ARC4RANDOM 29#ifndef HAVE_ARC4RANDOM
30 30
@@ -43,10 +43,15 @@ static RC4_KEY rc4;
43unsigned int arc4random(void) 43unsigned int arc4random(void)
44{ 44{
45 unsigned int r = 0; 45 unsigned int r = 0;
46 static int first_time = 1;
46 47
47 if (rc4_ready <= 0) 48 if (rc4_ready <= 0) {
49 if (!first_time)
50 seed_rng();
51 first_time = 0;
48 arc4random_stir(); 52 arc4random_stir();
49 53 }
54
50 RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r); 55 RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
51 56
52 rc4_ready -= sizeof(r); 57 rc4_ready -= sizeof(r);
@@ -57,17 +62,14 @@ unsigned int arc4random(void)
57void arc4random_stir(void) 62void arc4random_stir(void)
58{ 63{
59 unsigned char rand_buf[SEED_SIZE]; 64 unsigned char rand_buf[SEED_SIZE];
60
61 memset(&rc4, 0, sizeof(rc4));
62
63 seed_rng();
64 65
65 RAND_bytes(rand_buf, sizeof(rand_buf)); 66 memset(&rc4, 0, sizeof(rc4));
66 67 if (!RAND_bytes(rand_buf, sizeof(rand_buf)))
68 fatal("Couldn't obtain random bytes (error %ld)",
69 ERR_get_error());
67 RC4_set_key(&rc4, sizeof(rand_buf), rand_buf); 70 RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
68
69 memset(rand_buf, 0, sizeof(rand_buf)); 71 memset(rand_buf, 0, sizeof(rand_buf));
70 72
71 rc4_ready = REKEY_BYTES; 73 rc4_ready = REKEY_BYTES;
72} 74}
73#endif /* !HAVE_ARC4RANDOM */ 75#endif /* !HAVE_ARC4RANDOM */