diff options
author | tedu <> | 2003-08-16 19:07:40 +0000 |
---|---|---|
committer | tedu <> | 2003-08-16 19:07:40 +0000 |
commit | 374812f5f6500fbcfb87566cf4faf8060b2c4703 (patch) | |
tree | cf250f982b215253fe9500eb5a5162499ba26694 /src/lib/libc/crypt | |
parent | 782252fc8707defaacb647af8795dedac318c7df (diff) | |
download | openbsd-374812f5f6500fbcfb87566cf4faf8060b2c4703.tar.gz openbsd-374812f5f6500fbcfb87566cf4faf8060b2c4703.tar.bz2 openbsd-374812f5f6500fbcfb87566cf4faf8060b2c4703.zip |
just use sysctl for stirring. thread safe and can't fail.
ok deraadt and co.
Diffstat (limited to 'src/lib/libc/crypt')
-rw-r--r-- | src/lib/libc/crypt/arc4random.c | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index b23b1955e4..5e3b2925a6 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.8 2003/06/11 21:03:10 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.9 2003/08/16 19:07:40 tedu Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Arc4 random number generator for OpenBSD. | 4 | * Arc4 random number generator for OpenBSD. |
@@ -79,35 +79,22 @@ arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen) | |||
79 | static void | 79 | static void |
80 | arc4_stir(struct arc4_stream *as) | 80 | arc4_stir(struct arc4_stream *as) |
81 | { | 81 | { |
82 | int fd; | 82 | int i, mib[2]; |
83 | size_t len; | ||
83 | struct { | 84 | struct { |
84 | struct timeval tv; | 85 | struct timeval tv; |
85 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; | 86 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; |
86 | } rdat; | 87 | } rdat; |
87 | 88 | ||
88 | gettimeofday(&rdat.tv, NULL); | 89 | gettimeofday(&rdat.tv, NULL); |
89 | fd = open("/dev/arandom", O_RDONLY); | 90 | mib[0] = CTL_KERN; |
90 | if (fd != -1) { | 91 | mib[1] = KERN_ARND; |
91 | read(fd, rdat.rnd, sizeof(rdat.rnd)); | 92 | |
92 | close(fd); | 93 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { |
93 | } else { | 94 | len = sizeof(u_int); |
94 | int i, mib[2]; | 95 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) |
95 | size_t len; | 96 | break; |
96 | |||
97 | /* Device could not be opened, we might be chrooted, take | ||
98 | * randomness from sysctl. */ | ||
99 | |||
100 | mib[0] = CTL_KERN; | ||
101 | mib[1] = KERN_ARND; | ||
102 | |||
103 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | ||
104 | len = sizeof(u_int); | ||
105 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | ||
106 | break; | ||
107 | } | ||
108 | } | 97 | } |
109 | /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take | ||
110 | * whatever was on the stack... */ | ||
111 | 98 | ||
112 | arc4_stir_pid = getpid(); | 99 | arc4_stir_pid = getpid(); |
113 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); | 100 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); |