diff options
Diffstat (limited to 'src')
63 files changed, 334 insertions, 992 deletions
diff --git a/src/include/skalibs/random.h b/src/include/skalibs/random.h index 90aa0c0..2d1bbcf 100644 --- a/src/include/skalibs/random.h +++ b/src/include/skalibs/random.h @@ -1,33 +1,20 @@ /* ISC license. */ -/* MT-unsafe functions only. Use rrandom for MT-safety. */ - #ifndef RANDOM_H #define RANDOM_H -#include <skalibs/gccattributes.h> +#include <skalibs/uint32.h> #include <skalibs/stralloc.h> -extern int badrandom_init (void) ; -extern unsigned char badrandom_char (void) ; -extern unsigned int badrandom_int (unsigned int) ; -extern unsigned int badrandom_string (char *, unsigned int) ; -extern void badrandom_finish (void) ; - -extern int goodrandom_init (void) ; -extern unsigned char goodrandom_char (void) ; -extern unsigned int goodrandom_int (unsigned int) ; -extern unsigned int goodrandom_string (char *, unsigned int) ; -extern void goodrandom_finish (void) ; +extern void random_makeseed (char *) ; /* fills 160 bytes */ -#define random_init badrandom_init -#define random_char badrandom_char -#define random_int badrandom_int -#define random_string badrandom_string -#define random_finish badrandom_finish +extern int random_init (void) ; +extern void random_finish (void) ; -extern int random_name (char *, unsigned int) ; -extern int random_sauniquename (stralloc *, unsigned int) ; +extern void random_string (char *, unsigned int) ; +extern uint32 random_uint32 (uint32) ; +extern void random_name (char *, unsigned int) ; extern void random_unsort (char *, unsigned int, unsigned int) ; +extern int random_sauniquename (stralloc *, unsigned int) ; #endif diff --git a/src/include/skalibs/rrandom.h b/src/include/skalibs/rrandom.h deleted file mode 100644 index 0678523..0000000 --- a/src/include/skalibs/rrandom.h +++ /dev/null @@ -1,36 +0,0 @@ -/* ISC license. */ - -#ifndef RRANDOM_H -#define RRANDOM_H - -#include <skalibs/unirandom.h> - -typedef struct rrandominfo rrandominfo, *rrandominfo_ref, **rrandominfo_ref_ref ; -struct rrandominfo -{ - unirandom it ; - unsigned int ok ; -} ; - -#define RRANDOMINFO_ZERO { .it = UNIRANDOM_ZERO, .ok = 3 } - -typedef struct rrandom rrandom, *rrandom_ref, **rrandom_ref_ref ; -struct rrandom -{ - rrandominfo tries[3] ; - unsigned int n ; -} ; - -#define RRANDOM_ZERO { .tries = { RRANDOMINFO_ZERO, RRANDOMINFO_ZERO, RRANDOMINFO_ZERO }, .n = 0 } - -extern int rrandom_add (rrandom *, int (*) (unirandom *)) ; -extern int rrandom_finish (rrandom *) ; - -extern unsigned int rrandom_read (rrandom *, char *, unsigned int, unsigned int (*) (unirandom *, char *, unsigned int)) ; -extern unsigned int rrandom_readint (rrandom *, unsigned int, unsigned int (*) (unirandom *, char *, unsigned int)) ; -#define rrandom_readb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readb) -#define rrandom_readnb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readnb) - -extern unsigned int rrandom_name (rrandom *, char *, unsigned int, int) ; - -#endif diff --git a/src/include/skalibs/surf.h b/src/include/skalibs/surf.h index aa872e1..aae21e8 100644 --- a/src/include/skalibs/surf.h +++ b/src/include/skalibs/surf.h @@ -16,11 +16,7 @@ struct SURFSchedule #define SURFSCHEDULE_ZERO { .seed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .in = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .out = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", .pos = 32 } -extern void surf_sinit (SURFSchedule *, char const *) ; /* 160 chars */ +extern void surf_init (SURFSchedule *, char const *) ; /* 160 chars */ extern void surf (SURFSchedule *, char *, unsigned int) ; -extern void surf_makeseed (char *) ; /* fills 160 chars */ -extern void surf_init (SURFSchedule *) ; -extern void surf_autoinit (SURFSchedule *, char *, unsigned int) ; - #endif diff --git a/src/include/skalibs/unirandom.h b/src/include/skalibs/unirandom.h deleted file mode 100644 index 818990e..0000000 --- a/src/include/skalibs/unirandom.h +++ /dev/null @@ -1,56 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOM_H -#define UNIRANDOM_H - -#include <skalibs/buffer.h> -#include <skalibs/surf.h> - -#define RANDOMBUF_BUFSIZE 256 - -struct randombuf_s -{ - char buf[RANDOMBUF_BUFSIZE] ; - buffer b ; - unsigned int nb : 1 ; -} ; -#define RANDOMBUF_ZERO { .buf = "", .b = BUFFER_INIT(0, -1, 0, 0), .nb = 0 } - -struct randomegd_s -{ - int fd ; -} ; - -union unirandominfo -{ - SURFSchedule surf_ctx ; - struct randombuf_s device ; - struct randomegd_s egd ; -} ; - -#define UNIRANDOMINFO_ZERO { .surf_ctx = SURFSCHEDULE_ZERO } - -typedef struct unirandom unirandom, *unirandom_ref, **unirandom_ref_ref ; -struct unirandom -{ - unsigned int (*readb) (union unirandominfo *, char *, unsigned int) ; - unsigned int (*readnb) (union unirandominfo *, char *, unsigned int) ; - int (*init) (union unirandominfo *) ; - int (*finish) (union unirandominfo *) ; - union unirandominfo data ; - unsigned int initted : 1 ; -} ; - -#define UNIRANDOM_ZERO { .readb = 0, .readnb = 0, .init = 0, .finish = 0, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -extern int unirandom_register_devrandom (unirandom *) ; -extern int unirandom_register_devurandom (unirandom *) ; -extern int unirandom_register_hasegd (unirandom *) ; -extern int unirandom_register_surf (unirandom *) ; - -extern int unirandom_init (unirandom *) ; -extern unsigned int unirandom_readb (unirandom *, char *, unsigned int) ; -extern unsigned int unirandom_readnb (unirandom *, char *, unsigned int) ; -extern int unirandom_finish (unirandom *) ; - -#endif diff --git a/src/include/skalibs/unirandomdev.h b/src/include/skalibs/unirandomdev.h deleted file mode 100644 index 589a1c1..0000000 --- a/src/include/skalibs/unirandomdev.h +++ /dev/null @@ -1,19 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOMDEV_H -#define UNIRANDOMDEV_H - -#include <skalibs/unirandom.h> - -extern int unirandomdev_sinit (union unirandominfo *, char const *) ; -extern unsigned int unirandomdev_readb (union unirandominfo *, char *, unsigned int) ; -extern unsigned int unirandomdev_readnb (union unirandominfo *, char *, unsigned int) ; -extern int unirandomdev_finish (union unirandominfo *) ; - -extern int unidevrandom_init (union unirandominfo *) ; -extern int unidevurandom_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_DEVRANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevrandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } -#define UNIRANDOM_REGISTER_DEVURANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevurandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif diff --git a/src/include/skalibs/unirandomegd.h b/src/include/skalibs/unirandomegd.h deleted file mode 100644 index 11bb464..0000000 --- a/src/include/skalibs/unirandomegd.h +++ /dev/null @@ -1,17 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOMEGD_H -#define UNIRANDOMEGD_H - -#include <skalibs/unirandom.h> - -extern int unirandomegd_sinit (union unirandominfo *, char const *) ; -extern unsigned int unirandomegd_readb (union unirandominfo *, char *, unsigned int) ; -extern unsigned int unirandomegd_readnb (union unirandominfo *, char *, unsigned int) ; -extern int unirandomegd_finish (union unirandominfo *) ; - -extern int unihasegd_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_HASEGD() { .readb = &unirandomegd_readb, .readnb = &unirandomegd_readnb, .init = &unihasegd_init, .finish = &unirandomegd_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif diff --git a/src/include/skalibs/unisurf.h b/src/include/skalibs/unisurf.h deleted file mode 100644 index 5c95c82..0000000 --- a/src/include/skalibs/unisurf.h +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#ifndef UNISURF_H -#define UNISURF_H - -#include <skalibs/unirandom.h> - -extern int unisurf_sinit (union unirandominfo *, char const *) ; -extern unsigned int unisurf_read (union unirandominfo *, char *, unsigned int) ; -extern int unisurf_finish (union unirandominfo *) ; - -extern int unisurf_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_SURF() { .readb = &unisurf_read, .readnb = &unisurf_read, .init = &unisurf_init, .finish = &unisurf_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif 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 ; -} diff --git a/src/libstddjb/sauniquename.c b/src/libstddjb/sauniquename.c index 5a6ad83..ddf47ec 100644 --- a/src/libstddjb/sauniquename.c +++ b/src/libstddjb/sauniquename.c @@ -12,7 +12,7 @@ int sauniquename (stralloc *sa) unsigned int base = sa->len ; int wasnull = !sa->s ; - if (!stralloc_readyplus(sa, TIMESTAMP + UINT_FMT + 131)) return -1 ; + if (!stralloc_readyplus(sa, TIMESTAMP + UINT_FMT + 131)) return 0 ; sa->s[base] = ':' ; timestamp(sa->s + base + 1) ; sa->s[base + 1 + TIMESTAMP] = ':' ; @@ -20,9 +20,9 @@ int sauniquename (stralloc *sa) sa->len += uint_fmt(sa->s + sa->len, getpid()) ; sa->s[sa->len++] = ':' ; if (sagethostname(sa) == -1) goto err ; - return 0 ; + return 1 ; err: if (wasnull) stralloc_free(sa) ; else sa->len = base ; - return -1 ; + return 0 ; } diff --git a/src/libunixonacid/atomic_rm_rf_tmp.c b/src/libunixonacid/atomic_rm_rf_tmp.c index 8be5189..5bba785 100644 --- a/src/libunixonacid/atomic_rm_rf_tmp.c +++ b/src/libunixonacid/atomic_rm_rf_tmp.c @@ -16,7 +16,7 @@ int atomic_rm_rf_tmp (char const *filename, stralloc *tmp) start = tmp->len ; for (;;) { - if (random_sauniquename(tmp, 64) < 0) goto err ; + if (!random_sauniquename(tmp, 64)) goto err ; if (!stralloc_0(tmp)) goto err ; if (!rename(filename, tmp->s + tmpbase)) break ; if (errno != EEXIST && errno != ENOTEMPTY) goto err ; diff --git a/src/libunixonacid/dd_commit.c b/src/libunixonacid/dd_commit.c index 2555d1e..f4a7b06 100644 --- a/src/libunixonacid/dd_commit.c +++ b/src/libunixonacid/dd_commit.c @@ -30,7 +30,7 @@ int dd_commit (dirdescriptor_t *dd) unsigned int lnkbnbase = dd->new.len ; if (errno != EINVAL) goto fail ; if (!stralloc_cats(&dd->new, lnkbn)) goto fail ; - if (random_sauniquename(&dd->new, 8) < 0) goto fail ; + if (!random_sauniquename(&dd->new, 8)) goto fail ; if (!stralloc_0(&dd->new)) goto fail ; if (rename(dd->lnkfn, dd->new.s + oldbase) < 0) goto fail ; /* /!\ race condition right here: there's no lnkfn in the fs */ @@ -45,7 +45,7 @@ int dd_commit (dirdescriptor_t *dd) if (!stralloc_0(&dd->new)) goto fail ; newlnkbase = dd->new.len ; if (!stralloc_catb(&dd->new, dd->lnkfn, len)) goto fail ; - if (random_sauniquename(&dd->new, 8) < 0) goto fail ; + if (!random_sauniquename(&dd->new, 8)) goto fail ; if (!stralloc_0(&dd->new)) goto fail ; if (symlink(dd->new.s, dd->new.s + newlnkbase) < 0) goto fail ; if (rename(dd->new.s + newlnkbase, dd->lnkfn) < 0) diff --git a/src/libunixonacid/dd_open_write.c b/src/libunixonacid/dd_open_write.c index f579c73..90518cb 100644 --- a/src/libunixonacid/dd_open_write.c +++ b/src/libunixonacid/dd_open_write.c @@ -14,7 +14,7 @@ int dd_open_write (dirdescriptor_t *dd, char const *lnkfn, unsigned int mode) dirdescriptor_t d = DIRDESCRIPTOR_ZERO ; d.lnkfn = lnkfn ; if (!stralloc_cats(&d.new, lnkfn)) return 0 ; - if (random_sauniquename(&d.new, 8) < 0) goto fail ; + if (!random_sauniquename(&d.new, 8)) goto fail ; if (!stralloc_0(&d.new)) goto fail ; if (mkdir(d.new.s, mode) < 0) goto fail ; d.fd = open_read(d.new.s) ; diff --git a/src/libunixonacid/mkdir_unique.c b/src/libunixonacid/mkdir_unique.c index efc74cc..1eee4c6 100644 --- a/src/libunixonacid/mkdir_unique.c +++ b/src/libunixonacid/mkdir_unique.c @@ -11,7 +11,7 @@ int mkdir_unique (stralloc *sa, char const *fn, unsigned int mode) int wasnull = !sa->s ; if (!stralloc_cats(sa, fn)) return 0 ; if (!stralloc_cats(sa, "/mkdir_unique:")) goto fail ; - if (random_sauniquename(sa, 64) < 0) goto fail ; + if (!random_sauniquename(sa, 64)) goto fail ; if (!stralloc_0(sa)) goto fail ; if (mkdir(sa->s + base, mode) < 0) goto fail ; sa->len-- ; diff --git a/src/libunixonacid/openwritenclose_devino_tmp.c b/src/libunixonacid/openwritenclose_devino_tmp.c index 343da65..0fe032c 100644 --- a/src/libunixonacid/openwritenclose_devino_tmp.c +++ b/src/libunixonacid/openwritenclose_devino_tmp.c @@ -14,7 +14,7 @@ int openwritenclose_devino_tmp (char const *fn, char const *s, unsigned int len, uint64 tmpdev, tmpino ; unsigned int base = tmp->len ; if (!stralloc_cats(tmp, fn)) return 0 ; - if (random_sauniquename(tmp, 8) < 0) goto fail ; + if (!random_sauniquename(tmp, 8)) goto fail ; if (!stralloc_0(tmp)) goto fail ; if (!openwritenclose_unsafe_devino_sync(tmp->s + base, s, len, &tmpdev, &tmpino)) goto fail ; if (rename(tmp->s + base, fn) < 0) diff --git a/src/sysdeps/tryarc4random.c b/src/sysdeps/tryarc4random.c new file mode 100644 index 0000000..b2f596a --- /dev/null +++ b/src/sysdeps/tryarc4random.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE + +#include <stdlib.h> + +int main (void) +{ + return arc4random_uniform(1) ; +} diff --git a/src/sysdeps/trydevrandom.c b/src/sysdeps/trydevrandom.c deleted file mode 100644 index 395d008..0000000 --- a/src/sysdeps/trydevrandom.c +++ /dev/null @@ -1,54 +0,0 @@ -/* ISC license. */ - -#include <unistd.h> -#include <sys/types.h> -#include <fcntl.h> -#include <errno.h> - -static int fd_read (int fd, char *buf, unsigned int len) -{ - register int r ; - do r = read(fd, buf, len) ; - while ((r == -1) && (errno == EINTR)) ; - return r ; -} - -static unsigned int allread (int fd, register char *buf, register unsigned int len) -{ - register unsigned int written = 0 ; - while (len) - { - register int w = fd_read(fd, buf, len) ; - if (!w) errno = EPIPE ; - if (w <= 0) break ; - written += w ; - buf += w ; - len -= w ; - } - return written ; -} - -static int byte_diff (char *s, unsigned int n, char *t) -{ - for (;;) - { - if (!n) return 0 ; - if (*s != *t) break ; - ++s ; ++t ; --n ; - } - return ((int)(unsigned int)(unsigned char) *s) - - ((int)(unsigned int)(unsigned char) *t); -} - -int main () -{ - char a[64] ; - char b[64] ; - int fd = open("/dev/random", O_RDONLY) ; - if ((fd == -1) || (allread(fd, a, 64) < 64) ) return 111 ; - close(fd) ; - fd = open("/dev/random", O_RDONLY) ; - if ((fd == -1) || (allread(fd, b, 64) < 64) ) return 111 ; - close(fd) ; - return !byte_diff(a, 64, b) ; -} diff --git a/src/sysdeps/trygetrandom.c b/src/sysdeps/trygetrandom.c new file mode 100644 index 0000000..67a623d --- /dev/null +++ b/src/sysdeps/trygetrandom.c @@ -0,0 +1,22 @@ +/* ISC license. */ + +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <sys/types.h> +#include <sys/syscall.h> + +static int getrandom (void *buf, size_t buflen, unsigned int flags) +{ + return syscall(SYS_getrandom, buf, buflen, flags) ; +} + +int main (void) +{ + char buf[4] ; + return getrandom(buf, 4, 0) ; +} |