diff options
author | tedu <> | 2005-06-04 05:13:13 +0000 |
---|---|---|
committer | tedu <> | 2005-06-04 05:13:13 +0000 |
commit | 26efa7c2a86f35a1ee66b7060085328402a5ac4d (patch) | |
tree | ead973fcbbe9a6f121e92f0648e3da96d72f1880 /src/lib/libc/crypt | |
parent | 183b6733970efb96d69e51e501575f3da2c096fc (diff) | |
download | openbsd-26efa7c2a86f35a1ee66b7060085328402a5ac4d.tar.gz openbsd-26efa7c2a86f35a1ee66b7060085328402a5ac4d.tar.bz2 openbsd-26efa7c2a86f35a1ee66b7060085328402a5ac4d.zip |
use the new fat random sysctl to get initial state. (fallback to looping).
stir after eating 400000 words. ok + input deraadt
Diffstat (limited to 'src/lib/libc/crypt')
-rw-r--r-- | src/lib/libc/crypt/arc4random.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index 5c768f5494..1bfaca8cf1 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.11 2004/11/02 11:07:13 hshoexer Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.12 2005/06/04 05:13:13 tedu Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Arc4 random number generator for OpenBSD. | 4 | * Arc4 random number generator for OpenBSD. |
@@ -47,6 +47,7 @@ struct arc4_stream { | |||
47 | static int rs_initialized; | 47 | static int rs_initialized; |
48 | static struct arc4_stream rs; | 48 | static struct arc4_stream rs; |
49 | static pid_t arc4_stir_pid; | 49 | static pid_t arc4_stir_pid; |
50 | static int arc4_count; | ||
50 | 51 | ||
51 | static inline u_int8_t arc4_getbyte(struct arc4_stream *); | 52 | static inline u_int8_t arc4_getbyte(struct arc4_stream *); |
52 | 53 | ||
@@ -83,23 +84,23 @@ arc4_stir(struct arc4_stream *as) | |||
83 | { | 84 | { |
84 | int i, mib[2]; | 85 | int i, mib[2]; |
85 | size_t len; | 86 | size_t len; |
86 | struct { | 87 | u_char rnd[128]; |
87 | struct timeval tv; | ||
88 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; | ||
89 | } rdat; | ||
90 | 88 | ||
91 | gettimeofday(&rdat.tv, NULL); | ||
92 | mib[0] = CTL_KERN; | 89 | mib[0] = CTL_KERN; |
93 | mib[1] = KERN_ARND; | 90 | mib[1] = KERN_ARND; |
94 | 91 | ||
95 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | 92 | len = sizeof(rnd); |
96 | len = sizeof(u_int); | 93 | if (sysctl(mib, 2, rnd, &len, NULL, 0) == -1) { |
97 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | 94 | for (i = 0; i < sizeof(rnd) / sizeof(u_int); i ++) { |
98 | break; | 95 | len = sizeof(u_int); |
96 | if (sysctl(mib, 2, &rnd[i * sizeof(u_int)], &len, | ||
97 | NULL, 0) == -1) | ||
98 | break; | ||
99 | } | ||
99 | } | 100 | } |
100 | 101 | ||
101 | arc4_stir_pid = getpid(); | 102 | arc4_stir_pid = getpid(); |
102 | arc4_addrandom(as, (void *)&rdat, sizeof(rdat)); | 103 | arc4_addrandom(as, rnd, sizeof(rnd)); |
103 | 104 | ||
104 | /* | 105 | /* |
105 | * Discard early keystream, as per recommendations in: | 106 | * Discard early keystream, as per recommendations in: |
@@ -107,6 +108,7 @@ arc4_stir(struct arc4_stream *as) | |||
107 | */ | 108 | */ |
108 | for (i = 0; i < 256; i++) | 109 | for (i = 0; i < 256; i++) |
109 | (void)arc4_getbyte(as); | 110 | (void)arc4_getbyte(as); |
111 | arc4_count = 400000; | ||
110 | } | 112 | } |
111 | 113 | ||
112 | static inline u_int8_t | 114 | static inline u_int8_t |
@@ -155,7 +157,7 @@ arc4random_addrandom(u_char *dat, int datlen) | |||
155 | u_int32_t | 157 | u_int32_t |
156 | arc4random(void) | 158 | arc4random(void) |
157 | { | 159 | { |
158 | if (!rs_initialized || arc4_stir_pid != getpid()) | 160 | if (--arc4_count == 0 || !rs_initialized || arc4_stir_pid != getpid()) |
159 | arc4random_stir(); | 161 | arc4random_stir(); |
160 | return arc4_getword(&rs); | 162 | return arc4_getword(&rs); |
161 | } | 163 | } |