diff options
Diffstat (limited to 'src/lib/libc/crypt/arc4random.c')
| -rw-r--r-- | src/lib/libc/crypt/arc4random.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index 5279c21518..b3d65abe68 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.3 1998/03/22 19:01:16 niklas Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.4 1999/06/29 23:54:05 provos Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Arc4 random number generator for OpenBSD. | 4 | * Arc4 random number generator for OpenBSD. |
| @@ -29,7 +29,9 @@ | |||
| 29 | #include <stdlib.h> | 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> | 30 | #include <unistd.h> |
| 31 | #include <sys/types.h> | 31 | #include <sys/types.h> |
| 32 | #include <sys/param.h> | ||
| 32 | #include <sys/time.h> | 33 | #include <sys/time.h> |
| 34 | #include <sys/sysctl.h> | ||
| 33 | 35 | ||
| 34 | #ifdef __GNUC__ | 36 | #ifdef __GNUC__ |
| 35 | #define inline __inline | 37 | #define inline __inline |
| @@ -84,17 +86,32 @@ arc4_stir(as) | |||
| 84 | int fd; | 86 | int fd; |
| 85 | struct { | 87 | struct { |
| 86 | struct timeval tv; | 88 | struct timeval tv; |
| 87 | u_int8_t rnd[128 - sizeof(struct timeval)]; | 89 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; |
| 88 | } rdat; | 90 | } rdat; |
| 89 | 91 | ||
| 90 | gettimeofday(&rdat.tv, NULL); | 92 | gettimeofday(&rdat.tv, NULL); |
| 91 | fd = open("/dev/arandom", O_RDONLY); | 93 | fd = open("/dev/arandom", O_RDONLY); |
| 92 | if (fd >= 0) { | 94 | if (fd != -1) { |
| 93 | read(fd, rdat.rnd, sizeof(rdat.rnd)); | 95 | read(fd, rdat.rnd, sizeof(rdat.rnd)); |
| 94 | close(fd); | 96 | close(fd); |
| 97 | } else { | ||
| 98 | int i, mib[2]; | ||
| 99 | size_t len; | ||
| 100 | |||
| 101 | /* Device could not be opened, we might be chrooted, take | ||
| 102 | * randomness from sysctl. */ | ||
| 103 | |||
| 104 | mib[0] = CTL_KERN; | ||
| 105 | mib[1] = KERN_ARND; | ||
| 106 | |||
| 107 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | ||
| 108 | len = sizeof(u_int); | ||
| 109 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | ||
| 110 | break; | ||
| 111 | } | ||
| 95 | } | 112 | } |
| 96 | /* fd < 0? Ah, what the heck. We'll just take whatever was on the | 113 | /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take |
| 97 | * stack... */ | 114 | * whatever was on the stack... */ |
| 98 | 115 | ||
| 99 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); | 116 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); |
| 100 | } | 117 | } |
