diff options
Diffstat (limited to 'src/librandom')
47 files changed, 283 insertions, 759 deletions
diff --git a/src/librandom/badrandom_char.c b/src/librandom/badrandom_char.c deleted file mode 100644 index c0ca1de..0000000 --- a/src/librandom/badrandom_char.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned char badrandom_char (void) -{ - unsigned char x ; - badrandom_string((char *)&x, 1) ; - return x ; -} diff --git a/src/librandom/badrandom_finish.c b/src/librandom/badrandom_finish.c deleted file mode 100644 index e0791ee..0000000 --- a/src/librandom/badrandom_finish.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/rrandom.h> -#include "random-internal.h" -#include <skalibs/random.h> - -void badrandom_finish (void) -{ - rrandom_finish(&badrandom_here) ; -} diff --git a/src/librandom/badrandom_here.c b/src/librandom/badrandom_here.c deleted file mode 100644 index c7d2992..0000000 --- a/src/librandom/badrandom_here.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/sysdeps.h> -#include <skalibs/config.h> -#include <skalibs/unisurf.h> -#include "random-internal.h" - -#ifdef SKALIBS_EGD - -# include <skalibs/unirandomegd.h> - -# ifdef SKALIBS_HASDEVURANDOM - -# include <skalibs/unirandomdev.h> - -rrandom badrandom_here = { { { UNIRANDOM_REGISTER_DEVURANDOM(), 3 }, { UNIRANDOM_REGISTER_HASEGD(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 } }, 3 } ; - -# else - -rrandom badrandom_here = { { { UNIRANDOM_REGISTER_HASEGD(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 } }, 2 } ; - -# endif - -#else - -# ifdef SKALIBS_HASDEVURANDOM - -# include <skalibs/unirandomdev.h> - -rrandom badrandom_here = { { { UNIRANDOM_REGISTER_DEVURANDOM(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 } }, 2 } ; - -# else - -rrandom badrandom_here = { { { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 }, { UNIRANDOM_ZERO, 3 } }, 1 } ; - -# endif - -#endif diff --git a/src/librandom/badrandom_init.c b/src/librandom/badrandom_init.c deleted file mode 100644 index a6eb7e3..0000000 --- a/src/librandom/badrandom_init.c +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/random.h> - -int badrandom_init (void) -{ - return 1 ; -} diff --git a/src/librandom/badrandom_int.c b/src/librandom/badrandom_int.c deleted file mode 100644 index ae12db5..0000000 --- a/src/librandom/badrandom_int.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/rrandom.h> -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned int badrandom_int (unsigned int n) -{ - return rrandom_readint(&badrandom_here, n, &unirandom_readnb) ; -} diff --git a/src/librandom/badrandom_string.c b/src/librandom/badrandom_string.c deleted file mode 100644 index ec20f5b..0000000 --- a/src/librandom/badrandom_string.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/rrandom.h> -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned int badrandom_string (char *s, unsigned int n) -{ - return rrandom_readnb(&badrandom_here, s, n) ; -} diff --git a/src/librandom/goodrandom_char.c b/src/librandom/goodrandom_char.c deleted file mode 100644 index 8fbf5e8..0000000 --- a/src/librandom/goodrandom_char.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned char goodrandom_char (void) -{ - unsigned char x ; - goodrandom_string((char *)&x, 1) ; - return x ; -} diff --git a/src/librandom/goodrandom_finish.c b/src/librandom/goodrandom_finish.c deleted file mode 100644 index a764030..0000000 --- a/src/librandom/goodrandom_finish.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include "random-internal.h" -#include <skalibs/rrandom.h> -#include <skalibs/random.h> - -void goodrandom_finish (void) -{ - rrandom_finish(&goodrandom_here) ; -} diff --git a/src/librandom/goodrandom_here.c b/src/librandom/goodrandom_here.c deleted file mode 100644 index 8275ab2..0000000 --- a/src/librandom/goodrandom_here.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/sysdeps.h> -#include <skalibs/config.h> -#include <skalibs/unisurf.h> -#include "random-internal.h" - -#ifdef SKALIBS_EGD - -# include <skalibs/unirandomegd.h> - -# ifdef SKALIBS_HASDEVRANDOM - -# include <skalibs/unirandomdev.h> - -rrandom goodrandom_here = { { { UNIRANDOM_REGISTER_DEVRANDOM(), 3 }, { UNIRANDOM_REGISTER_HASEGD(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 } }, 3 } ; - -# else - -rrandom goodrandom_here = { { { UNIRANDOM_REGISTER_HASEGD(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 } }, 2 } ; - -# endif - -#else - -# ifdef SKALIBS_HASDEVRANDOM - -# include <skalibs/unirandomdev.h> - -rrandom goodrandom_here = { { { UNIRANDOM_REGISTER_DEVRANDOM(), 3 }, { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 } }, 2 } ; - -# else - -rrandom goodrandom_here = { { { UNIRANDOM_REGISTER_SURF(), 3 }, { UNIRANDOM_ZERO, 3 }, { UNIRANDOM_ZERO, 3 } }, 1 } ; - -# endif - -#endif diff --git a/src/librandom/goodrandom_init.c b/src/librandom/goodrandom_init.c deleted file mode 100644 index dea6225..0000000 --- a/src/librandom/goodrandom_init.c +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/random.h> - -int goodrandom_init (void) -{ - return 1 ; -} diff --git a/src/librandom/goodrandom_int.c b/src/librandom/goodrandom_int.c deleted file mode 100644 index 11ef2e1..0000000 --- a/src/librandom/goodrandom_int.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/rrandom.h> -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned int goodrandom_int (unsigned int n) -{ - return rrandom_readint(&goodrandom_here, n, &unirandom_readb) ; -} diff --git a/src/librandom/goodrandom_string.c b/src/librandom/goodrandom_string.c deleted file mode 100644 index 556c41c..0000000 --- a/src/librandom/goodrandom_string.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/rrandom.h> -#include "random-internal.h" -#include <skalibs/random.h> - -unsigned int goodrandom_string (char *s, unsigned int n) -{ - return rrandom_readb(&goodrandom_here, s, n) ; -} diff --git a/src/librandom/random-internal.h b/src/librandom/random-internal.h index a9788fc..c915170 100644 --- a/src/librandom/random-internal.h +++ b/src/librandom/random-internal.h @@ -3,18 +3,9 @@ #ifndef RANDOM_INTERNAL_H #define RANDOM_INTERNAL_H -#include <skalibs/gccattributes.h> #include <skalibs/surf.h> -#include <skalibs/unirandom.h> -#include <skalibs/rrandom.h> - -extern void unirandom_register (unirandom *, int (*) (union unirandominfo *), int (*) (union unirandominfo *), unsigned int (*) (union unirandominfo *, char *, unsigned int), unsigned int (*) (union unirandominfo *, char *, unsigned int)) ; - -extern unsigned int random_mask2 (unsigned int) gccattr_const ; -extern unsigned int random_nchars (unsigned int) gccattr_const ; +extern int random_fd ; extern SURFSchedule surf_here ; -extern rrandom goodrandom_here ; -extern rrandom badrandom_here ; #endif diff --git a/src/librandom/random_char.c b/src/librandom/random_char.c new file mode 100644 index 0000000..2c5a7da --- /dev/null +++ b/src/librandom/random_char.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/random.h> + +unsigned char badrandom_char (void) +{ + unsigned char c ; + random_string((char *)&c, 1) ; + return c ; +} diff --git a/src/librandom/random_finish.c b/src/librandom/random_finish.c new file mode 100644 index 0000000..bcc42da --- /dev/null +++ b/src/librandom/random_finish.c @@ -0,0 +1,45 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASARC4RANDOM + +#include <skalibs/random.h> + +void random_finish () +{ +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include <skalibs/random.h> + +void random_finish () +{ +} + +#else +#ifdef SKALIBS_HASDEVURANDOM + +#include <skalibs/djbunix.h> +#include <skalibs/random.h> +#include "random-internal.h" + +void random_finish () +{ + fd_close(random_fd) ; + random_fd = -1 ; +} + +#else /* default */ + +#include <skalibs/random.h> + +void random_finish () +{ +} + +#endif +#endif +#endif diff --git a/src/librandom/random_init.c b/src/librandom/random_init.c new file mode 100644 index 0000000..e990fb4 --- /dev/null +++ b/src/librandom/random_init.c @@ -0,0 +1,75 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASARC4RANDOM + +#include <stdlib.h> +#include <skalibs/random.h> + +int random_init () +{ + char seed[160] ; + random_makeseed(seed) ; + arc4random_addrandom(seed, 160) ; + return 1 ; +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include <skalibs/djbunix.h> +#include <skalibs/random.h> + +int random_init () +{ + char seed[160] ; + random_makeseed(seed) ; + return openwritenclose_unsafe("/dev/urandom", seed, 160) ; +} + +#else +#ifdef SKALIBS_HASDEVURANDOM + +#include <skalibs/djbunix.h> +#include <skalibs/random.h> +#include "random-internal.h" + +int random_fd = -1 ; +SURFSchedule surf_here = SURFSCHEDULE_ZERO ; + +int random_init () +{ + int fd ; + char seed[160] ; + random_makeseed(seed) ; + surf_init(&surf_here, seed) ; + if (!openwritenclose_unsafe("/dev/urandom", seed, 160)) return 0 ; + if (random_fd < 0) + { + fd = open_readb("/dev/urandom") ; + if (fd < 0) return 0 ; + if (coe(fd) < 0) { fd_close(fd) ; return 0 ; } + random_fd = fd ; + } + return 1 ; +} + +#else /* default */ + +#include <skalibs/random.h> +#include "random-internal.h" + +SURFSchedule surf_here = SURFSCHEDULE_ZERO ; + +int random_init () +{ + char seed[160] ; + random_makeseed(seed) ; + surf_init(&surf_here, seed) ; + return 1 ; +} + +#endif +#endif +#endif diff --git a/src/librandom/surf_makeseed.c b/src/librandom/random_makeseed.c index adae9a4..909e896 100644 --- a/src/librandom/surf_makeseed.c +++ b/src/librandom/random_makeseed.c @@ -4,14 +4,21 @@ #include <skalibs/uint32.h> #include <skalibs/tai.h> #include <skalibs/sha1.h> -#include <skalibs/surf.h> -void surf_makeseed (char *s) +/* + Writes 160 bytes of crap into s. + Certainly not cryptographically secure or 100% unpredictable, + but we're only using this to speed up /dev/urandom + initialization or to init an internal SURF PRNG. + iow: we are CS iff the system's RNG is CS. +*/ + +void random_makeseed (char *s) { SHA1Schedule bak = SHA1_INIT() ; { tain_t now ; - char tmp[20 + TAIN_PACK] ; + char tmp[256] ; uint32 x = getpid() ; uint32_pack(tmp, x) ; x = getppid() ; @@ -19,6 +26,8 @@ void surf_makeseed (char *s) tain_now(&now) ; tain_pack(tmp + 8, &now) ; sha1_update(&bak, tmp, 8 + TAIN_PACK) ; + gethostname(tmp, 256) ; + sha1_update(&bak, tmp, 256) ; sha1_final(&bak, tmp) ; sha1_init(&bak) ; sha1_update(&bak, tmp, 20) ; diff --git a/src/librandom/random_mask2.c b/src/librandom/random_mask2.c deleted file mode 100644 index ab8c8e7..0000000 --- a/src/librandom/random_mask2.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include "random-internal.h" - -unsigned int random_mask2 (register unsigned int n) -{ - for (;;) - { - register unsigned int m = n | (n >> 1) ; - if (m == n) return n ; - n = m ; - } -} - -unsigned int random_nchars (register unsigned int n) -{ - return n <= 0xff ? 1 : -#if SKALIBS_SIZEOFUINT == 2 - 2 -#else - n <= 0xffff ? 2 : - n <= 0xffffffUL ? 3 : -# if SKALIBS_SIZEOFUINT == 4 - 4 -# else - n <= 0xffffffffUL ? 4 : - n <= 0xffffffffffULL ? 5 : - n <= 0xffffffffffffULL ? 6 : - n <= 0xffffffffffffffULL ? 7 : - 8 -# endif -#endif - ; -} diff --git a/src/librandom/random_name.c b/src/librandom/random_name.c index 8736699..83b1626 100644 --- a/src/librandom/random_name.c +++ b/src/librandom/random_name.c @@ -2,12 +2,11 @@ /* MT-unsafe */ -#include "random-internal.h" #include <skalibs/random.h> -int random_name (char *s, unsigned int n) +void random_name (char *s, unsigned int n) { - register unsigned int r = rrandom_name(&badrandom_here, s, n, 1) ; - if (r < n) return -1 ; - return n ; + static char const oklist[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ; + random_string(s, n) ; + while (n--) s[n] = oklist[s[n] & 63] ; } diff --git a/src/librandom/random_sauniquename.c b/src/librandom/random_sauniquename.c index 7c902ef..6d200aa 100644 --- a/src/librandom/random_sauniquename.c +++ b/src/librandom/random_sauniquename.c @@ -10,16 +10,14 @@ int random_sauniquename (stralloc *sa, unsigned int n) { unsigned int base = sa->len ; int wasnull = !sa->s ; - register int r ; - if (sauniquename(sa) == -1) return -1 ; + if (!sauniquename(sa)) return 0 ; if (!stralloc_readyplus(sa, n+1)) goto err ; stralloc_catb(sa, ":", 1) ; - r = random_name(sa->s + sa->len, n) ; - if (r == -1) goto err ; - sa->len += r ; - return r ; + random_name(sa->s + sa->len, n) ; + sa->len += n ; + return 1 ; err: if (wasnull) stralloc_free(sa) ; else sa->len = base ; - return -1 ; + return 0 ; } diff --git a/src/librandom/random_string.c b/src/librandom/random_string.c new file mode 100644 index 0000000..c87c8c2 --- /dev/null +++ b/src/librandom/random_string.c @@ -0,0 +1,66 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASARC4RANDOM + +#include <stdlib.h> +#include <skalibs/random.h> + +void random_string (char *s, unsigned int n) +{ + arc4random_buf(s, n) ; +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include <skalibs/nonposix.h> +#include <sys/types.h> +#include <sys/syscall.h> +#include <skalibs/random.h> + +static int getrandom (void *buf, size_t buflen, unsigned int flags) +{ + return syscall(SYS_getrandom, buf, buflen, flags) ; +} + +void random_string (char *s, unsigned int n) +{ + while (n) + { + register int r = getrandom(s, n, 0) ; + if (r >= 0) + { + s += r ; + n -= r ; + } + } +} + +#else +#ifdef SKALIBS_HASDEVURANDOM + +#include <skalibs/allreadwrite.h> +#include <skalibs/random.h> +#include "random-internal.h" + +void random_string (char *s, unsigned int n) +{ + unsigned int r = allread(random_fd, s, n) ; + if (r < n) surf(&surf_here, s+r, n-r) ; +} + +#else /* default */ + +#include <skalibs/random.h> +#include "random-internal.h" + +void random_string (char *s, unsigned int n) +{ + surf(&surf_here, s, n) ; +} + +#endif +#endif +#endif diff --git a/src/librandom/random_uint32.c b/src/librandom/random_uint32.c new file mode 100644 index 0000000..d011885 --- /dev/null +++ b/src/librandom/random_uint32.c @@ -0,0 +1,56 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASARC4RANDOM + +#include <stdlib.h> +#include <skalibs/random.h> + +uint32 random_uint32 (uint32 n) +{ + return arc4random_uniform(n) ; +} + +#else + +#include <skalibs/uint32.h> +#include <skalibs/random.h> + +static inline uint32 random_mask2 (register uint32 n) +{ + for (;;) + { + register uint32 m = n | (n >> 1) ; + if (m == n) return n ; + n = m ; + } +} + +static inline unsigned int random_nchars (register uint32 n) +{ + return n <= 0xff ? 1 : + n <= 0xffff ? 2 : + n <= 0xffffffUL ? 3 : 4 ; +} + +uint32 random_uint32 (uint32 n) +{ + if (!n) return 0 ; + else + { + uint32 i = n, m = random_mask2(n-1) ; + unsigned int nchars = random_nchars(n) ; + char tmp[4] ; + while (i >= n) + { + random_string(tmp, nchars) ; + byte_zero(tmp + nchars, 4 - nchars) ; + uint32_unpack(tmp, &i) ; + i &= m ; + } + return i ; + } +} + +#endif diff --git a/src/librandom/random_unsort.c b/src/librandom/random_unsort.c index 289ef96..22c4e08 100644 --- a/src/librandom/random_unsort.c +++ b/src/librandom/random_unsort.c @@ -1,6 +1,6 @@ /* ISC license. */ -#include <errno.h> +#include <skalibs/uint32.h> #include <skalibs/bytestr.h> #include <skalibs/random.h> @@ -9,7 +9,7 @@ void random_unsort (char *s, unsigned int n, unsigned int chunksize) char tmp[chunksize] ; while (n--) { - register unsigned int i = badrandom_int(n+1) ; + register uint32 i = random_uint32(n+1) ; byte_copy(tmp, chunksize, s + i * chunksize) ; byte_copy(s + i * chunksize, chunksize, s + n * chunksize) ; byte_copy(s + n * chunksize, chunksize, tmp) ; diff --git a/src/librandom/randomegd_open.c b/src/librandom/randomegd_open.c deleted file mode 100644 index 7c43f0d..0000000 --- a/src/librandom/randomegd_open.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/djbunix.h> -#include <skalibs/webipc.h> -#include <skalibs/randomegd.h> - -int randomegd_open (char const *path) -{ - int s = ipc_stream() ; - if (s < 0) return -1 ; - if (ipc_connect(s, path) == -1) - { - register int e = errno ; - fd_close(s) ; - errno = e ; - return -1 ; - } - return s ; -} diff --git a/src/librandom/randomegd_readb.c b/src/librandom/randomegd_readb.c deleted file mode 100644 index 614f7a8..0000000 --- a/src/librandom/randomegd_readb.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ISC license. */ - -#include <skalibs/allreadwrite.h> -#include <skalibs/randomegd.h> - -unsigned int randomegd_readb (int s, char *x, unsigned int n) -{ - unsigned int w = 0 ; - unsigned int i = 0 ; - for (; i < (n / 255) ; i++) - { - char const c[2] = { 0x02, 0xFF } ; - register unsigned int wtmp ; - if (allwrite(s, c, 2) < 2) return w ; - wtmp = allread(s, x + w, 255) ; - w += wtmp ; - if (wtmp < 255) return w ; - } - if (w < n) - { - char c[2] = "\002" ; - c[1] = n - w ; - if (allwrite(s, c, 2) < 2) return w ; - w += allread(s, x + w, c[1]) ; - } - return w ; -} diff --git a/src/librandom/randomegd_readnb.c b/src/librandom/randomegd_readnb.c deleted file mode 100644 index 29165c4..0000000 --- a/src/librandom/randomegd_readnb.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ISC license. */ - -#include <skalibs/allreadwrite.h> -#include <skalibs/randomegd.h> - -unsigned int randomegd_readnb (int s, char *x, unsigned int n) -{ - unsigned int w = 0 ; - while ((n - w) >= 255) - { - char c[2] = { 0x01, 0xFF } ; - register unsigned char wtmp ; - if (allwrite(s, c, 2) < 2) return w ; - if (sanitize_read(fd_read(s, c+1, 1)) < 1) return w ; - wtmp = allread(s, x + w, c[1]) ; - w += wtmp ; - if ((wtmp < (unsigned char)c[1]) || ((unsigned char)c[1] < 0xFF)) return w ; - } - if (w < n) - { - char c[2] = "\001" ; - c[1] = n - w ; - if (allwrite(s, c, 2) < 2) return w ; - if (sanitize_read(fd_read(s, c+1, 1)) < 1) return w ; - w += allread(s, x + w, c[1]) ; - } - return w ; -} diff --git a/src/librandom/rrandom_add.c b/src/librandom/rrandom_add.c deleted file mode 100644 index 23fa2af..0000000 --- a/src/librandom/rrandom_add.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> -#include <skalibs/rrandom.h> - -int rrandom_add (rrandom *z, int (*f) (unirandom *)) -{ - if (z->n >= 3) return (errno = EBUSY, 0) ; - if (!(*f)(&z->tries[z->n].it)) return 0 ; - z->tries[z->n++].ok = 3 ; - return 1 ; -} diff --git a/src/librandom/rrandom_finish.c b/src/librandom/rrandom_finish.c deleted file mode 100644 index c2d4992..0000000 --- a/src/librandom/rrandom_finish.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <skalibs/unirandom.h> -#include <skalibs/rrandom.h> - -int rrandom_finish (rrandom *z) -{ - rrandom zero = RRANDOM_ZERO ; - unsigned int i = z->n ; - int e = 1 ; - while (i--) e &= unirandom_finish(&z->tries[i].it) ; - if (e) *z = zero ; - return e ; -} diff --git a/src/librandom/rrandom_name.c b/src/librandom/rrandom_name.c deleted file mode 100644 index 2085d17..0000000 --- a/src/librandom/rrandom_name.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -#include <skalibs/unirandom.h> -#include <skalibs/rrandom.h> - -unsigned int rrandom_name (rrandom *z, char *s, unsigned int n, int nb) -{ - static char const *oklist = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ; - register unsigned int r = rrandom_read(z, s, n, nb ? &unirandom_readnb : &unirandom_readb) ; - register unsigned int i = 0 ; - for (; i < r ; i++) s[i] = oklist[s[i] & 63] ; - return r ; -} diff --git a/src/librandom/rrandom_read.c b/src/librandom/rrandom_read.c deleted file mode 100644 index 9d56b0c..0000000 --- a/src/librandom/rrandom_read.c +++ /dev/null @@ -1,21 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/error.h> -#include <skalibs/unirandom.h> -#include <skalibs/rrandom.h> - -unsigned int rrandom_read (rrandom *z, char *s, unsigned int n, unsigned int (*f) (unirandom *, char *, unsigned int)) -{ - unsigned int i = 0 ; - for (; i < 3 ; i++) - { - int r ; - if (!z->tries[i].ok) continue ; - r = sanitize_read((*f)(&z->tries[i].it, s, n)) ; - if (r > 0) return r ; - z->tries[i].ok = error_temp(errno) ? z->tries[i].ok - 1 : 0 ; - } - return (errno = ENOENT, 0) ; -} diff --git a/src/librandom/rrandom_readint.c b/src/librandom/rrandom_readint.c deleted file mode 100644 index 6a34de2..0000000 --- a/src/librandom/rrandom_readint.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ISC license. */ - -#include <skalibs/uint.h> -#include <skalibs/bytestr.h> -#include <skalibs/unirandom.h> -#include "random-internal.h" -#include <skalibs/rrandom.h> - -unsigned int rrandom_readint (rrandom *z, unsigned int n, unsigned int (*f) (unirandom *, char *, unsigned int)) -{ - if (!n) return 0 ; - else - { - unsigned int i = n, nchars = random_nchars(n), m = random_mask2(n-1) ; - char tmp[UINT_PACK] ; - while (i >= n) - { - if (rrandom_read(z, tmp, nchars, f) < nchars) return 0 ; - byte_zero(tmp + nchars, UINT_PACK - nchars) ; - uint_unpack(tmp, &i) ; - i &= m ; - } - return i ; - } -} diff --git a/src/librandom/surf_autoinit.c b/src/librandom/surf_autoinit.c deleted file mode 100644 index b7774c0..0000000 --- a/src/librandom/surf_autoinit.c +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -#include <skalibs/surf.h> - -void surf_autoinit (SURFSchedule *ctx, char *s, unsigned int n) -{ - if (!ctx->in[0] && !ctx->in[1] && !ctx->in[2] && !ctx->in[3]) - surf_init(ctx) ; - surf(ctx, s, n) ; -} diff --git a/src/librandom/surf_here.c b/src/librandom/surf_here.c deleted file mode 100644 index ebbdffb..0000000 --- a/src/librandom/surf_here.c +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -/* MT-unsafe */ - -#include <skalibs/surf.h> -#include "random-internal.h" - -SURFSchedule surf_here = SURFSCHEDULE_ZERO ; diff --git a/src/librandom/surf_init.c b/src/librandom/surf_init.c index 045ec09..d1d9b3c 100644 --- a/src/librandom/surf_init.c +++ b/src/librandom/surf_init.c @@ -1,10 +1,13 @@ /* ISC license. */ +#include <skalibs/uint32.h> #include <skalibs/surf.h> -void surf_init (SURFSchedule *ctx) +void surf_init (SURFSchedule *ctx, char const *s) { - char s[160] ; - surf_makeseed(s) ; - surf_sinit(ctx, s) ; + SURFSchedule zero = SURFSCHEDULE_ZERO ; + register unsigned int i = 4 ; + *ctx = zero ; + for (; i < 12 ; i++) uint32_unpack(s + (i<<2) - 16, ctx->in + i) ; + for (i = 0 ; i < 32 ; i++) uint32_unpack(s + 32 + (i<<2), ctx->seed + i) ; } diff --git a/src/librandom/surf_sinit.c b/src/librandom/surf_sinit.c deleted file mode 100644 index f81a5c8..0000000 --- a/src/librandom/surf_sinit.c +++ /dev/null @@ -1,13 +0,0 @@ -/* ISC license. */ - -#include <skalibs/uint32.h> -#include <skalibs/surf.h> - -void surf_sinit (SURFSchedule *ctx, char const *s) -{ - SURFSchedule zero = SURFSCHEDULE_ZERO ; - register unsigned int i = 4 ; - *ctx = zero ; - for (; i < 12 ; i++) uint32_unpack(s + (i<<2) - 16, ctx->in + i) ; - for (i = 0 ; i < 32 ; i++) uint32_unpack(s + 32 + (i<<2), ctx->seed + i) ; -} diff --git a/src/librandom/unidevrandom.c b/src/librandom/unidevrandom.c deleted file mode 100644 index fb7b6b8..0000000 --- a/src/librandom/unidevrandom.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/unirandomdev.h> -#include <skalibs/unirandom.h> - -#ifdef SKALIBS_HASDEVRANDOM - -#include "random-internal.h" - -int unidevrandom_init (union unirandominfo *u) -{ - return unirandomdev_sinit(u, "/dev/random") ; -} - -int unirandom_register_devrandom (unirandom *u) -{ - unirandom_register(u, &unidevrandom_init, &unirandomdev_finish, &unirandomdev_readb, &unirandomdev_readnb) ; - return 1 ; -} - -#else - -#include <errno.h> - -int unidevrandom_init (union unirandominfo *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -int unirandom_register_devrandom (unirandom *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/librandom/unidevurandom.c b/src/librandom/unidevurandom.c deleted file mode 100644 index d601ea0..0000000 --- a/src/librandom/unidevurandom.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/unirandomdev.h> -#include <skalibs/unirandom.h> - -#ifdef SKALIBS_HASDEVURANDOM - -#include "random-internal.h" - -int unidevurandom_init (union unirandominfo *u) -{ - return unirandomdev_sinit(u, "/dev/urandom") ; -} - -int unirandom_register_devurandom (unirandom *u) -{ - unirandom_register(u, &unidevurandom_init, &unirandomdev_finish, &unirandomdev_readb, &unirandomdev_readnb) ; - return 1 ; -} - -#else - -#include <errno.h> - -int unidevurandom_init (union unirandominfo *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -int unirandom_register_devurandom (unirandom *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/librandom/unihasegd.c b/src/librandom/unihasegd.c deleted file mode 100644 index ab263c7..0000000 --- a/src/librandom/unihasegd.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ISC license. */ - -#include <skalibs/config.h> -#include <skalibs/unirandomegd.h> -#include <skalibs/unirandom.h> - -#ifdef SKALIBS_EGD - -#include "random-internal.h" - -int unihasegd_init (union unirandominfo *u) -{ - return unirandomegd_sinit(u, SKALIBS_EGD) ; -} - -int unirandom_register_hasegd (unirandom *u) -{ - unirandom_register(u, &unihasegd_init, &unirandomegd_finish, &unirandomegd_readb, &unirandomegd_readnb) ; - return 1 ; -} - -#else - -#include <errno.h> - -int unihasegd_init (union unirandominfo *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -int unirandom_register_hasegd (unirandom *u) -{ - (void)u ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/librandom/unirandom_finish.c b/src/librandom/unirandom_finish.c deleted file mode 100644 index bb54eb5..0000000 --- a/src/librandom/unirandom_finish.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> - -int unirandom_finish (unirandom *u) -{ - if (!u->initted) return 1 ; - if (!(*u->finish)(&u->data)) return 0 ; - u->initted = 0 ; - return 1 ; -} diff --git a/src/librandom/unirandom_init.c b/src/librandom/unirandom_init.c deleted file mode 100644 index 6324d71..0000000 --- a/src/librandom/unirandom_init.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> - -int unirandom_init (unirandom *u) -{ - if (u->initted) return 1 ; - if (!(*u->init)(&u->data)) return 0 ; - u->initted = 1 ; - return 1 ; -} diff --git a/src/librandom/unirandom_readb.c b/src/librandom/unirandom_readb.c deleted file mode 100644 index 605e467..0000000 --- a/src/librandom/unirandom_readb.c +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> - -unsigned int unirandom_readb (unirandom *u, char *s, unsigned int n) -{ - if (!u->initted && !unirandom_init(u)) return 0 ; - return (*u->readb)(&u->data, s, n) ; -} diff --git a/src/librandom/unirandom_readnb.c b/src/librandom/unirandom_readnb.c deleted file mode 100644 index 5ccc6bc..0000000 --- a/src/librandom/unirandom_readnb.c +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> - -unsigned int unirandom_readnb (unirandom *u, char *s, unsigned int n) -{ - if (!u->initted && !unirandom_init(u)) return 0 ; - return (*u->readnb)(&u->data, s, n) ; -} diff --git a/src/librandom/unirandom_register.c b/src/librandom/unirandom_register.c deleted file mode 100644 index e174909..0000000 --- a/src/librandom/unirandom_register.c +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/unirandom.h> -#include "random-internal.h" - -void unirandom_register (unirandom *u, int (*init) (union unirandominfo *), int (*finish) (union unirandominfo *), unsigned int (*readb) (union unirandominfo *, char *, unsigned int), unsigned int (*readnb) (union unirandominfo *, char *, unsigned int)) -{ - unirandom zero = UNIRANDOM_ZERO ; - *u = zero ; - u->init = init ; - u->finish = finish ; - u->readb = readb ; - u->readnb = readnb ; - u->initted = 0 ; -} diff --git a/src/librandom/unirandomdev.c b/src/librandom/unirandomdev.c deleted file mode 100644 index 0f42ae3..0000000 --- a/src/librandom/unirandomdev.c +++ /dev/null @@ -1,51 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/buffer.h> -#include <skalibs/djbunix.h> -#include <skalibs/unirandom.h> -#include <skalibs/unirandomdev.h> - -int unirandomdev_sinit (union unirandominfo *u, char const *file) -{ - register int fd = open_read(file) ; - if (fd < 0) return 0 ; - if (coe(fd) < 0) - { - register int e = errno ; - fd_close(fd) ; - errno = e ; - return 0 ; - } - buffer_init(&u->device.b, &fd_readsv, fd, u->device.buf, RANDOMBUF_BUFSIZE) ; - u->device.nb = 1 ; - return 1 ; -} - -int unirandomdev_finish (union unirandominfo *u) -{ - return fd_close(buffer_fd(&u->device.b)) < 0 ; -} - -static unsigned int unirandomdev_read (union unirandominfo *u, char *s, unsigned int n, unsigned int h) -{ - unsigned int w = 0 ; - if (u->device.nb != h) - { - if ((h ? ndelay_on(buffer_fd(&u->device.b)) : ndelay_off(buffer_fd(&u->device.b))) < 0) return 0 ; - u->device.nb = h ; - } - buffer_getall(&u->device.b, s, n, &w) ; - return w ; -} - -unsigned int unirandomdev_readb (union unirandominfo *u, char *s, unsigned int n) -{ - return unirandomdev_read(u, s, n, 0) ; -} - -unsigned int unirandomdev_readnb (union unirandominfo *u, char *s, unsigned int n) -{ - return unirandomdev_read(u, s, n, 1) ; -} diff --git a/src/librandom/unirandomegd.c b/src/librandom/unirandomegd.c deleted file mode 100644 index a340f06..0000000 --- a/src/librandom/unirandomegd.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/djbunix.h> -#include <skalibs/randomegd.h> -#include <skalibs/unirandom.h> -#include <skalibs/unirandomegd.h> - -int unirandomegd_sinit (union unirandominfo *u, char const *path) -{ - register int s = randomegd_open(path) ; - if (s == -1) return 0 ; - if (coe(s) == -1) - { - register int e = errno ; - fd_close(s) ; - errno = e ; - return 0 ; - } - u->egd.fd = s ; - return 1 ; -} - -int unirandomegd_finish (union unirandominfo *u) -{ - return !fd_close(u->egd.fd) ; -} - -unsigned int unirandomegd_readb (union unirandominfo *u, char *s, unsigned int n) -{ - return randomegd_readb(u->egd.fd, s, n) ; -} - -unsigned int unirandomegd_readnb (union unirandominfo *u, char *s, unsigned int n) -{ - return randomegd_readnb(u->egd.fd, s, n) ; -} diff --git a/src/librandom/unisurf.c b/src/librandom/unisurf.c deleted file mode 100644 index 334d044..0000000 --- a/src/librandom/unisurf.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ISC license. */ - -#include <skalibs/surf.h> -#include <skalibs/unirandom.h> -#include <skalibs/unisurf.h> - -int unisurf_sinit (union unirandominfo *u, char const *s) -{ - surf_sinit(&u->surf_ctx, s) ; - return 1 ; -} - -int unisurf_finish (union unirandominfo *u) -{ - (void)u ; - return 1 ; -} - -unsigned int unisurf_read (union unirandominfo *u, char *s, unsigned int n) -{ - surf(&u->surf_ctx, s, n) ; - return n ; -} diff --git a/src/librandom/unisurf_init.c b/src/librandom/unisurf_init.c deleted file mode 100644 index d5a1f73..0000000 --- a/src/librandom/unisurf_init.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ISC license. */ - -#include <skalibs/surf.h> -#include "random-internal.h" -#include <skalibs/unirandom.h> -#include <skalibs/unisurf.h> - -int unisurf_init (union unirandominfo *u) -{ - surf_init(&u->surf_ctx) ; - return 1 ; -} - -int unirandom_register_surf (unirandom *u) -{ - unirandom_register(u, &unisurf_init, &unisurf_finish, &unisurf_read, &unisurf_read) ; - return 1 ; -} |