diff options
Diffstat (limited to 'libc/stdlib/arc4random.c')
-rw-r--r-- | libc/stdlib/arc4random.c | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/libc/stdlib/arc4random.c b/libc/stdlib/arc4random.c index ebdd7d561..836b79f6c 100644 --- a/libc/stdlib/arc4random.c +++ b/libc/stdlib/arc4random.c @@ -27,8 +27,6 @@ */ #include <features.h> -#ifdef __UCLIBC_HAS_ARC4RANDOM__ - #include <fcntl.h> #include <stdlib.h> #include <unistd.h> @@ -37,12 +35,18 @@ #include <sys/time.h> #ifdef __ARC4RANDOM_USE_ERANDOM__ #include <sys/sysctl.h> +//libc_hidden_proto(sysctl) #endif +libc_hidden_proto(open) +libc_hidden_proto(read) +libc_hidden_proto(close) +libc_hidden_proto(gettimeofday) + struct arc4_stream { - u_int8_t i; - u_int8_t j; - u_int8_t s[256]; + uint8_t i; + uint8_t j; + uint8_t s[256]; }; static int rs_initialized; @@ -51,8 +55,8 @@ static struct arc4_stream rs; static inline void arc4_init(struct arc4_stream *); static inline void arc4_addrandom(struct arc4_stream *, u_char *, int); static void arc4_stir(struct arc4_stream *); -static inline u_int8_t arc4_getbyte(struct arc4_stream *); -static inline u_int32_t arc4_getword(struct arc4_stream *); +static inline uint8_t arc4_getbyte(struct arc4_stream *); +static inline uint32_t arc4_getword(struct arc4_stream *); static inline void arc4_init(as) @@ -73,7 +77,7 @@ arc4_addrandom(as, dat, datlen) int datlen; { int n; - u_int8_t si; + uint8_t si; as->i--; for (n = 0; n < 256; n++) { @@ -93,20 +97,20 @@ arc4_stir(as) int fd; struct { struct timeval tv; - u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; + uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)]; } rdat; int n; gettimeofday(&rdat.tv, NULL); - fd = __open("/dev/urandom", O_RDONLY); + fd = open("/dev/urandom", O_RDONLY); if (fd != -1) { - __read(fd, rdat.rnd, sizeof(rdat.rnd)); - __close(fd); + read(fd, rdat.rnd, sizeof(rdat.rnd)); + close(fd); } #ifdef __ARC4RANDOM_USE_ERANDOM__ else { int mib[3]; - u_int i; + uint i; size_t len; /* Device could not be opened, we might be chrooted, take @@ -116,8 +120,8 @@ arc4_stir(as) mib[1] = KERN_RANDOM; mib[2] = RANDOM_ERANDOM; - for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i++) { - len = sizeof(u_int); + for (i = 0; i < sizeof(rdat.rnd) / sizeof(uint); i++) { + len = sizeof(uint); if (sysctl(mib, 3, &rdat.rnd[i], &len, NULL, 0) == -1) break; } @@ -137,11 +141,11 @@ arc4_stir(as) arc4_getbyte(as); } -static inline u_int8_t +static inline uint8_t arc4_getbyte(as) struct arc4_stream *as; { - u_int8_t si, sj; + uint8_t si, sj; as->i = (as->i + 1); si = as->s[as->i]; @@ -152,11 +156,11 @@ arc4_getbyte(as) return (as->s[(si + sj) & 0xff]); } -static inline u_int32_t +static inline uint32_t arc4_getword(as) struct arc4_stream *as; { - u_int32_t val; + uint32_t val; val = arc4_getbyte(as) << 24; val |= arc4_getbyte(as) << 16; val |= arc4_getbyte(as) << 8; @@ -164,8 +168,9 @@ arc4_getword(as) return val; } +libc_hidden_proto(arc4random_stir) void -arc4random_stir() +arc4random_stir(void) { if (!rs_initialized) { arc4_init(&rs); @@ -173,19 +178,18 @@ arc4random_stir() } arc4_stir(&rs); } +libc_hidden_def(arc4random_stir) void -arc4random_addrandom(dat, datlen) - u_char *dat; - int datlen; +arc4random_addrandom(u_char *dat, int datlen) { if (!rs_initialized) arc4random_stir(); arc4_addrandom(&rs, dat, datlen); } -u_int32_t -arc4random() +uint32_t +arc4random(void) { if (!rs_initialized) arc4random_stir(); @@ -204,5 +208,3 @@ int main(void) { return 0; } #endif - -#endif /* __UCLIBC_HAS_ARC4RANDOM__ */ |