From a1933bd1847951b959016f59ee744d1b18a00142 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 14 Oct 2016 17:07:56 +0000 Subject: Clean up and modernize librandom. Correct random number generation has historically been suprisingly painful to achieve. There was no standard, every system behaved in a subtly different way, and there were a few userland initiatives to get decent randomness, all incompatible of course. The situation is a bit better now, we're heading towards some standardization. The arc4random() series of functions is a good API, and available on a lot of systems - unfortunately not Linux, but on Linux the new getrandom() makes using /dev/random obsolete. So I removed the old crap in librandom, dropped EGD support, dropped dynamic backend selection, made a single API series (random_* instead of goodrandom_* and badrandom_*), added an arc4random backend and a getrandom backend, and defaulted to /dev/urandom backed up by SURF in the worst case. This should be much smaller and logical. However, it's a major API break, so the skarnet.org stack will be changed to adapt. --- configure | 33 +--------- doc/librandom/index.html | 89 ++++++++++++++++---------- package/deps.mak | 58 ++++------------- src/include/skalibs/random.h | 29 +++------ src/include/skalibs/rrandom.h | 36 ----------- src/include/skalibs/surf.h | 6 +- src/include/skalibs/unirandom.h | 56 ---------------- src/include/skalibs/unirandomdev.h | 19 ------ src/include/skalibs/unirandomegd.h | 17 ----- src/include/skalibs/unisurf.h | 16 ----- src/librandom/badrandom_char.c | 13 ---- src/librandom/badrandom_finish.c | 12 ---- src/librandom/badrandom_here.c | 40 ------------ src/librandom/badrandom_init.c | 10 --- src/librandom/badrandom_int.c | 12 ---- src/librandom/badrandom_string.c | 12 ---- src/librandom/goodrandom_char.c | 13 ---- src/librandom/goodrandom_finish.c | 12 ---- src/librandom/goodrandom_here.c | 40 ------------ src/librandom/goodrandom_init.c | 10 --- src/librandom/goodrandom_int.c | 12 ---- src/librandom/goodrandom_string.c | 12 ---- src/librandom/random-internal.h | 11 +--- src/librandom/random_char.c | 10 +++ src/librandom/random_finish.c | 45 +++++++++++++ src/librandom/random_init.c | 75 ++++++++++++++++++++++ src/librandom/random_makeseed.c | 44 +++++++++++++ src/librandom/random_mask2.c | 35 ---------- src/librandom/random_name.c | 9 ++- src/librandom/random_sauniquename.c | 12 ++-- src/librandom/random_string.c | 66 +++++++++++++++++++ src/librandom/random_uint32.c | 56 ++++++++++++++++ src/librandom/random_unsort.c | 4 +- src/librandom/randomegd_open.c | 20 ------ src/librandom/randomegd_readb.c | 27 -------- src/librandom/randomegd_readnb.c | 28 -------- src/librandom/rrandom_add.c | 13 ---- src/librandom/rrandom_finish.c | 14 ---- src/librandom/rrandom_name.c | 13 ---- src/librandom/rrandom_read.c | 21 ------ src/librandom/rrandom_readint.c | 25 -------- src/librandom/surf_autoinit.c | 10 --- src/librandom/surf_here.c | 8 --- src/librandom/surf_init.c | 11 ++-- src/librandom/surf_makeseed.c | 35 ---------- src/librandom/surf_sinit.c | 13 ---- src/librandom/unidevrandom.c | 38 ----------- src/librandom/unidevurandom.c | 38 ----------- src/librandom/unihasegd.c | 38 ----------- src/librandom/unirandom_finish.c | 12 ---- src/librandom/unirandom_init.c | 12 ---- src/librandom/unirandom_readb.c | 10 --- src/librandom/unirandom_readnb.c | 10 --- src/librandom/unirandom_register.c | 16 ----- src/librandom/unirandomdev.c | 51 --------------- src/librandom/unirandomegd.c | 37 ----------- src/librandom/unisurf.c | 23 ------- src/librandom/unisurf_init.c | 18 ------ src/libstddjb/sauniquename.c | 6 +- src/libunixonacid/atomic_rm_rf_tmp.c | 2 +- src/libunixonacid/dd_commit.c | 4 +- src/libunixonacid/dd_open_write.c | 2 +- src/libunixonacid/mkdir_unique.c | 2 +- src/libunixonacid/openwritenclose_devino_tmp.c | 2 +- src/sysdeps/tryarc4random.c | 11 ++++ src/sysdeps/trydevrandom.c | 54 ---------------- src/sysdeps/trygetrandom.c | 22 +++++++ 67 files changed, 434 insertions(+), 1136 deletions(-) delete mode 100644 src/include/skalibs/rrandom.h delete mode 100644 src/include/skalibs/unirandom.h delete mode 100644 src/include/skalibs/unirandomdev.h delete mode 100644 src/include/skalibs/unirandomegd.h delete mode 100644 src/include/skalibs/unisurf.h delete mode 100644 src/librandom/badrandom_char.c delete mode 100644 src/librandom/badrandom_finish.c delete mode 100644 src/librandom/badrandom_here.c delete mode 100644 src/librandom/badrandom_init.c delete mode 100644 src/librandom/badrandom_int.c delete mode 100644 src/librandom/badrandom_string.c delete mode 100644 src/librandom/goodrandom_char.c delete mode 100644 src/librandom/goodrandom_finish.c delete mode 100644 src/librandom/goodrandom_here.c delete mode 100644 src/librandom/goodrandom_init.c delete mode 100644 src/librandom/goodrandom_int.c delete mode 100644 src/librandom/goodrandom_string.c create mode 100644 src/librandom/random_char.c create mode 100644 src/librandom/random_finish.c create mode 100644 src/librandom/random_init.c create mode 100644 src/librandom/random_makeseed.c delete mode 100644 src/librandom/random_mask2.c create mode 100644 src/librandom/random_string.c create mode 100644 src/librandom/random_uint32.c delete mode 100644 src/librandom/randomegd_open.c delete mode 100644 src/librandom/randomegd_readb.c delete mode 100644 src/librandom/randomegd_readnb.c delete mode 100644 src/librandom/rrandom_add.c delete mode 100644 src/librandom/rrandom_finish.c delete mode 100644 src/librandom/rrandom_name.c delete mode 100644 src/librandom/rrandom_read.c delete mode 100644 src/librandom/rrandom_readint.c delete mode 100644 src/librandom/surf_autoinit.c delete mode 100644 src/librandom/surf_here.c delete mode 100644 src/librandom/surf_makeseed.c delete mode 100644 src/librandom/surf_sinit.c delete mode 100644 src/librandom/unidevrandom.c delete mode 100644 src/librandom/unidevurandom.c delete mode 100644 src/librandom/unihasegd.c delete mode 100644 src/librandom/unirandom_finish.c delete mode 100644 src/librandom/unirandom_init.c delete mode 100644 src/librandom/unirandom_readb.c delete mode 100644 src/librandom/unirandom_readnb.c delete mode 100644 src/librandom/unirandom_register.c delete mode 100644 src/librandom/unirandomdev.c delete mode 100644 src/librandom/unirandomegd.c delete mode 100644 src/librandom/unisurf.c delete mode 100644 src/librandom/unisurf_init.c create mode 100644 src/sysdeps/tryarc4random.c delete mode 100644 src/sysdeps/trydevrandom.c create mode 100644 src/sysdeps/trygetrandom.c diff --git a/configure b/configure index 55136cc..122eb42 100755 --- a/configure +++ b/configure @@ -38,13 +38,11 @@ Optional features: $package options: --enable-libc-replacements use independent low-level primitives [disabled] - --enable-egd=PATH support an EGD daemon listening on PATH as RNG [disabled] --disable-ipv6 do not build IPv6 support [enabled] --enable-iopause-select prefer select() over poll() for iopause implementation [disabled] --enable-tai-clock assume the system clock is TAI-10 instead of UTC [disabled] --enable-clock use clock_gettime() instead of gettimeofday() [disabled] --enable-monotonic count time with CLOCK_MONOTONIC instead of CLOCK_REALTIME - --enable-force-devr assume /dev/random exists and is valid [autodetection takes time] --with-default-path=PATH default executable search path [/usr/bin:/usr/sbin:/bin:/sbin] EOF @@ -214,13 +212,11 @@ shared=true static=true slashpackage=false replacements=false -egd= ipv6=true select=false taiclock=false clockrt=false clockmon=false -forcedevr=false ddefaultpath=/usr/bin:/usr/sbin:/bin:/sbin defaultpath=$ddefaultpath dpathorig=true @@ -257,8 +253,6 @@ for arg ; do --disable-slashpackage) sproot= ; slashpackage=false ;; --enable-libc-replacements|--enable-libc-replacements=yes) replacements=true ;; --disable-libc-replacements|--enable-libc-replacements=no) replacements=false ;; - --enable-egd=*) egd=${arg#*=} ;; - --disable-egd) egd= ;; --enable-ipv6|--enable-ipv6=yes) ipv6=true ;; --disable-ipv6|--enable-ipv6=no) ipv6=false ;; --enable-iopause-select|--enable-iopause-select=yes) select=true ;; @@ -269,8 +263,6 @@ for arg ; do --disable-clock|--enable-clock=no) clockrt=false ;; --enable-monotonic|--enable-monotonic=yes) clockmon=true ;; --disable-monotonic|--enable-monotonic=no) clockmon=false ;; - --enable-force-devr|--enable-force-devr=yes) forcedevr=true ;; - --disable-force-devr|--enable-force-devr=no) forcedevr=false ;; --with-default-path=*) defaultpath=${arg#*=} ; dpathorig=false ;; --without-default-path) defaultpath=$ddefaultpath ; dpathorig=true ;; --enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;; @@ -403,10 +395,6 @@ if test -n "$sysdeps" ; then sysclock_lib=$(cat $sysdeps/sysclock.lib) tainnow_lib=$(cat $sysdeps/tainnow.lib) util_lib=$(cat $sysdeps/util.lib) - if test -n "$egd" ; then - egd=$(grep -F egd: $sysdeps/sysdeps | cut -d' ' -f2-) - echo "warning: --enable-egd option ignored, using sysdeps-provided value instead: $egd" - fi else sysdeps=sysdeps.cfg mkdir -p $sysdeps @@ -500,21 +488,9 @@ EOF choose c uint64t UINT64T 'uint64_t' choose cl futimens FUTIMENS 'futimens()' choose cl futimes FUTIMES 'futimes()' + choose cl arc4random ARC4RANDOM 'arc4random()' + choose cl getrandom GETRANDOM 'getrandom()' - if $forcedevr ; then - echo "/dev/random detection override required, assuming it exists and is working." - echo "devrandom: yes" >> $sysdeps/sysdeps - echo "#define ${package_macro_name}_HASDEVRANDOM" >> $sysdeps/sysdeps.h - else - choose clr devrandom DEVRANDOM /dev/random - fi - if test -n "$egd" ; then - if echo "$egd" | grep -q '[^[:alnum:]/_-]' ; then - fail "$0: invalid EGD path: $egd" - fi - echo "egd: $egd" >> $sysdeps/sysdeps - echo "#define ${package_macro_name}_HASEGD \"$egd\"" >> $sysdeps/sysdeps.h - fi echo '#endif' >> $sysdeps/sysdeps.h fi @@ -626,11 +602,6 @@ if $select ; then else echo "#undef ${package_macro_name}_FLAG_PREFERSELECT" fi -if test -n "$egd" ; then - echo "#define ${package_macro_name}_EGD $egd" -else - echo "#undef ${package_macro_name}_EGD" -fi echo echo "#endif" diff --git a/doc/librandom/index.html b/doc/librandom/index.html index 8001811..183086a 100644 --- a/doc/librandom/index.html +++ b/doc/librandom/index.html @@ -51,64 +51,85 @@ If the EGD connection fails, a SURF PRNG is used. function prototypes.

-

High quality, cryptographically strong random data

+

Basic functions

   unsigned char c ;
-  unsigned int max ;
-  unsigned int n ;
+  uint32_t max ;
+  uint32_t n ;
   unsigned int b ;
   char data[at least b] ;
   int r ;
 
-  goodrandom_init() ;
-  c = goodrandom_char() ;
-  n = goodrandom_int(max) ;
-  r = goodrandom_string(data, b) ;
-  goodrandom_finish() ;
+  r = random_init() ;
+  c = random_char() ;
+  n = random_uint32(max) ;
+  random_string(data, b) ;
+  random_finish() ;
 

- goodrandom_init() becomes optional with skalibs-0.43. + random_init() must be called before any other function in +the random library. It returns 0 (and sets errno) on failure, and +nonzero on success. +

+ +

It is recommended that you let the library perform cleanups after you -have used it, by calling goodrandom_finish(). +are done using it, by calling random_finish() - unless your +process exits right away.

+

Advanced functions

+

- If you have neither /dev/random nor EGD, a software PRNG is -used. This PRNG is based on the -SURF function, which -is unpredictable enough for most uses. + void random_unsort (char *s, unsigned int n, unsigned int chunksize)
+Shuffles the array s (of size at least n*chunksize) by +performing a random permutation of the n blocks of chunksize bytes. +Bytes are not permuted inside chunks.

-

Lower quality random data

-

- It works basically the same, by replacing goodrandom_* with -badrandom_*. It uses /dev/urandom on systems that -support it; on systems that do not, but support EGD, non-blocking calls -to EGD are made ; if that is not enough, or EGD is not supported, -the SURF generator is used. + void random_name (char *s, unsigned int n)
+Writes n random readable ASCII characters into s: +letters, numbers, hyphens or underscores. Does not terminate with a +null character.

- The point of badrandom is to get random bytes instantly, -even at the expense of quality; whereas goodrandom always returns -high-quality random bytes, but may block if entropy is insufficient. In -practice, in spite of its name, badrandom will return quite -unpredictable pseudo-random data, so goodrandom should be used -only when paranoia is the rule and blocking is an option. + int random_sauniquename (stralloc *sa, unsigned int n)
+Appends a (non-null-terminated) unique, unpredictable ASCII name to the +stralloc *sa. That +name includes n randomly generated ASCII characters.

+

Notes

+ + + diff --git a/package/deps.mak b/package/deps.mak index e4752f1..a37b685 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -33,8 +33,7 @@ src/include/skalibs/lolstdio.h: src/include/skalibs/bufalloc.h src/include/skali src/include/skalibs/md5.h: src/include/skalibs/uint32.h src/include/skalibs/mininetstring.h: src/include/skalibs/stralloc.h src/include/skalibs/uint16.h src/include/skalibs/uint32.h src/include/skalibs/netstring.h: src/include/skalibs/buffer.h src/include/skalibs/siovec.h src/include/skalibs/stralloc.h -src/include/skalibs/random.h: src/include/skalibs/gccattributes.h src/include/skalibs/stralloc.h -src/include/skalibs/rrandom.h: src/include/skalibs/unirandom.h +src/include/skalibs/random.h: src/include/skalibs/stralloc.h src/include/skalibs/uint32.h src/include/skalibs/sha1.h: src/include/skalibs/uint32.h src/include/skalibs/sha256.h: src/include/skalibs/uint32.h src/include/skalibs/sha512.h: src/include/skalibs/uint64.h @@ -51,10 +50,6 @@ src/include/skalibs/strerr.h: src/include/skalibs/gccattributes.h src/include/skalibs/strerr2.h: src/include/skalibs/strerr.h src/include/skalibs/surf.h: src/include/skalibs/uint32.h src/include/skalibs/tai.h: src/include/skalibs/gccattributes.h src/include/skalibs/uint32.h src/include/skalibs/uint64.h -src/include/skalibs/unirandom.h: src/include/skalibs/buffer.h src/include/skalibs/surf.h -src/include/skalibs/unirandomdev.h: src/include/skalibs/unirandom.h -src/include/skalibs/unirandomegd.h: src/include/skalibs/unirandom.h -src/include/skalibs/unisurf.h: src/include/skalibs/unirandom.h src/include/skalibs/unix-timed.h: src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/unix-transactional.h: src/include/skalibs/siovec.h src/include/skalibs/stralloc.h src/include/skalibs/uint64.h src/include/skalibs/unixconnection.h: src/include/skalibs/unixmessage.h @@ -62,7 +57,7 @@ src/include/skalibs/unixmessage.h: src/include/skalibs/buffer.h src/include/skal src/include/skalibs/unixonacid.h: src/include/skalibs/kolbak.h src/include/skalibs/skaclient.h src/include/skalibs/unix-timed.h src/include/skalibs/unix-transactional.h src/include/skalibs/unixconnection.h src/include/skalibs/unixmessage.h src/include/skalibs/webipc.h: src/include/skalibs/djbunix.h src/include/skalibs/tai.h src/libdatastruct/avlnode-internal.h: src/include/skalibs/avlnode.h -src/librandom/random-internal.h: src/include/skalibs/gccattributes.h src/include/skalibs/rrandom.h src/include/skalibs/surf.h src/include/skalibs/unirandom.h +src/librandom/random-internal.h: src/include/skalibs/surf.h src/libstdcrypto/md5-internal.h: src/include/skalibs/md5.h src/include/skalibs/uint32.h src/libstdcrypto/sha1-internal.h: src/include/skalibs/sha1.h src/include/skalibs/uint32.h src/libstdcrypto/sha256-internal.h: src/include/skalibs/sha256.h src/include/skalibs/uint32.h @@ -133,48 +128,17 @@ src/libdatastruct/gensetdyn_iter_withcancel.o src/libdatastruct/gensetdyn_iter_w src/libdatastruct/gensetdyn_new.o src/libdatastruct/gensetdyn_new.lo: src/libdatastruct/gensetdyn_new.c src/include/skalibs/genalloc.h src/include/skalibs/gensetdyn.h src/libdatastruct/gensetdyn_ready.o src/libdatastruct/gensetdyn_ready.lo: src/libdatastruct/gensetdyn_ready.c src/include/skalibs/genalloc.h src/include/skalibs/gensetdyn.h src/include/skalibs/stralloc.h src/libdatastruct/gensetdyn_zero.o src/libdatastruct/gensetdyn_zero.lo: src/libdatastruct/gensetdyn_zero.c src/include/skalibs/gensetdyn.h -src/librandom/badrandom_char.o src/librandom/badrandom_char.lo: src/librandom/badrandom_char.c src/librandom/random-internal.h src/include/skalibs/random.h -src/librandom/badrandom_finish.o src/librandom/badrandom_finish.lo: src/librandom/badrandom_finish.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/badrandom_here.o src/librandom/badrandom_here.lo: src/librandom/badrandom_here.c src/include/skalibs/config.h src/librandom/random-internal.h src/include/skalibs/sysdeps.h src/include/skalibs/unisurf.h -src/librandom/badrandom_init.o src/librandom/badrandom_init.lo: src/librandom/badrandom_init.c src/include/skalibs/random.h -src/librandom/badrandom_int.o src/librandom/badrandom_int.lo: src/librandom/badrandom_int.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/badrandom_string.o src/librandom/badrandom_string.lo: src/librandom/badrandom_string.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/goodrandom_char.o src/librandom/goodrandom_char.lo: src/librandom/goodrandom_char.c src/librandom/random-internal.h src/include/skalibs/random.h -src/librandom/goodrandom_finish.o src/librandom/goodrandom_finish.lo: src/librandom/goodrandom_finish.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/goodrandom_here.o src/librandom/goodrandom_here.lo: src/librandom/goodrandom_here.c src/include/skalibs/config.h src/librandom/random-internal.h src/include/skalibs/sysdeps.h src/include/skalibs/unisurf.h -src/librandom/goodrandom_init.o src/librandom/goodrandom_init.lo: src/librandom/goodrandom_init.c src/include/skalibs/random.h -src/librandom/goodrandom_int.o src/librandom/goodrandom_int.lo: src/librandom/goodrandom_int.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/goodrandom_string.o src/librandom/goodrandom_string.lo: src/librandom/goodrandom_string.c src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/rrandom.h -src/librandom/random_mask2.o src/librandom/random_mask2.lo: src/librandom/random_mask2.c src/librandom/random-internal.h src/include/skalibs/sysdeps.h -src/librandom/random_name.o src/librandom/random_name.lo: src/librandom/random_name.c src/librandom/random-internal.h src/include/skalibs/random.h +src/librandom/random_char.o src/librandom/random_char.lo: src/librandom/random_char.c src/include/skalibs/random.h +src/librandom/random_finish.o src/librandom/random_finish.lo: src/librandom/random_finish.c src/include/skalibs/djbunix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/sysdeps.h +src/librandom/random_init.o src/librandom/random_init.lo: src/librandom/random_init.c src/include/skalibs/djbunix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/sysdeps.h +src/librandom/random_makeseed.o src/librandom/random_makeseed.lo: src/librandom/random_makeseed.c src/include/skalibs/sha1.h src/include/skalibs/tai.h src/include/skalibs/uint32.h +src/librandom/random_name.o src/librandom/random_name.lo: src/librandom/random_name.c src/include/skalibs/random.h src/librandom/random_sauniquename.o src/librandom/random_sauniquename.lo: src/librandom/random_sauniquename.c src/include/skalibs/random.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h -src/librandom/random_unsort.o src/librandom/random_unsort.lo: src/librandom/random_unsort.c src/include/skalibs/bytestr.h src/include/skalibs/random.h -src/librandom/randomegd_open.o src/librandom/randomegd_open.lo: src/librandom/randomegd_open.c src/include/skalibs/djbunix.h src/include/skalibs/randomegd.h src/include/skalibs/webipc.h -src/librandom/randomegd_readb.o src/librandom/randomegd_readb.lo: src/librandom/randomegd_readb.c src/include/skalibs/allreadwrite.h src/include/skalibs/randomegd.h -src/librandom/randomegd_readnb.o src/librandom/randomegd_readnb.lo: src/librandom/randomegd_readnb.c src/include/skalibs/allreadwrite.h src/include/skalibs/randomegd.h -src/librandom/rrandom_add.o src/librandom/rrandom_add.lo: src/librandom/rrandom_add.c src/include/skalibs/rrandom.h src/include/skalibs/unirandom.h -src/librandom/rrandom_finish.o src/librandom/rrandom_finish.lo: src/librandom/rrandom_finish.c src/include/skalibs/rrandom.h src/include/skalibs/unirandom.h -src/librandom/rrandom_name.o src/librandom/rrandom_name.lo: src/librandom/rrandom_name.c src/include/skalibs/rrandom.h src/include/skalibs/unirandom.h -src/librandom/rrandom_read.o src/librandom/rrandom_read.lo: src/librandom/rrandom_read.c src/include/skalibs/allreadwrite.h src/include/skalibs/error.h src/include/skalibs/rrandom.h src/include/skalibs/unirandom.h -src/librandom/rrandom_readint.o src/librandom/rrandom_readint.lo: src/librandom/rrandom_readint.c src/include/skalibs/bytestr.h src/librandom/random-internal.h src/include/skalibs/rrandom.h src/include/skalibs/uint.h src/include/skalibs/unirandom.h +src/librandom/random_string.o src/librandom/random_string.lo: src/librandom/random_string.c src/include/skalibs/allreadwrite.h src/include/skalibs/nonposix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/sysdeps.h +src/librandom/random_uint32.o src/librandom/random_uint32.lo: src/librandom/random_uint32.c src/include/skalibs/random.h src/include/skalibs/sysdeps.h src/include/skalibs/uint32.h +src/librandom/random_unsort.o src/librandom/random_unsort.lo: src/librandom/random_unsort.c src/include/skalibs/bytestr.h src/include/skalibs/random.h src/include/skalibs/uint32.h src/librandom/surf.o src/librandom/surf.lo: src/librandom/surf.c src/include/skalibs/bytestr.h src/include/skalibs/surf.h src/include/skalibs/uint32.h -src/librandom/surf_autoinit.o src/librandom/surf_autoinit.lo: src/librandom/surf_autoinit.c src/include/skalibs/surf.h -src/librandom/surf_here.o src/librandom/surf_here.lo: src/librandom/surf_here.c src/librandom/random-internal.h src/include/skalibs/surf.h -src/librandom/surf_init.o src/librandom/surf_init.lo: src/librandom/surf_init.c src/include/skalibs/surf.h -src/librandom/surf_makeseed.o src/librandom/surf_makeseed.lo: src/librandom/surf_makeseed.c src/include/skalibs/sha1.h src/include/skalibs/surf.h src/include/skalibs/tai.h src/include/skalibs/uint32.h -src/librandom/surf_sinit.o src/librandom/surf_sinit.lo: src/librandom/surf_sinit.c src/include/skalibs/surf.h src/include/skalibs/uint32.h -src/librandom/unidevrandom.o src/librandom/unidevrandom.lo: src/librandom/unidevrandom.c src/librandom/random-internal.h src/include/skalibs/sysdeps.h src/include/skalibs/unirandom.h src/include/skalibs/unirandomdev.h -src/librandom/unidevurandom.o src/librandom/unidevurandom.lo: src/librandom/unidevurandom.c src/librandom/random-internal.h src/include/skalibs/sysdeps.h src/include/skalibs/unirandom.h src/include/skalibs/unirandomdev.h -src/librandom/unihasegd.o src/librandom/unihasegd.lo: src/librandom/unihasegd.c src/include/skalibs/config.h src/librandom/random-internal.h src/include/skalibs/unirandom.h src/include/skalibs/unirandomegd.h -src/librandom/unirandom_finish.o src/librandom/unirandom_finish.lo: src/librandom/unirandom_finish.c src/include/skalibs/unirandom.h -src/librandom/unirandom_init.o src/librandom/unirandom_init.lo: src/librandom/unirandom_init.c src/include/skalibs/unirandom.h -src/librandom/unirandom_readb.o src/librandom/unirandom_readb.lo: src/librandom/unirandom_readb.c src/include/skalibs/unirandom.h -src/librandom/unirandom_readnb.o src/librandom/unirandom_readnb.lo: src/librandom/unirandom_readnb.c src/include/skalibs/unirandom.h -src/librandom/unirandom_register.o src/librandom/unirandom_register.lo: src/librandom/unirandom_register.c src/librandom/random-internal.h src/include/skalibs/unirandom.h -src/librandom/unirandomdev.o src/librandom/unirandomdev.lo: src/librandom/unirandomdev.c src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/djbunix.h src/include/skalibs/unirandom.h src/include/skalibs/unirandomdev.h -src/librandom/unirandomegd.o src/librandom/unirandomegd.lo: src/librandom/unirandomegd.c src/include/skalibs/djbunix.h src/include/skalibs/randomegd.h src/include/skalibs/unirandom.h src/include/skalibs/unirandomegd.h -src/librandom/unisurf.o src/librandom/unisurf.lo: src/librandom/unisurf.c src/include/skalibs/surf.h src/include/skalibs/unirandom.h src/include/skalibs/unisurf.h -src/librandom/unisurf_init.o src/librandom/unisurf_init.lo: src/librandom/unisurf_init.c src/librandom/random-internal.h src/include/skalibs/surf.h src/include/skalibs/unirandom.h src/include/skalibs/unisurf.h +src/librandom/surf_init.o src/librandom/surf_init.lo: src/librandom/surf_init.c src/include/skalibs/surf.h src/include/skalibs/uint32.h src/libstdcrypto/md5_final.o src/libstdcrypto/md5_final.lo: src/libstdcrypto/md5_final.c src/include/skalibs/bytestr.h src/libstdcrypto/md5-internal.h src/include/skalibs/md5.h src/include/skalibs/uint32.h src/libstdcrypto/md5_init.o src/libstdcrypto/md5_init.lo: src/libstdcrypto/md5_init.c src/include/skalibs/md5.h src/libstdcrypto/md5_transform.o src/libstdcrypto/md5_transform.lo: src/libstdcrypto/md5_transform.c src/libstdcrypto/md5-internal.h src/include/skalibs/uint32.h 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 +#include #include -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 - -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 -#include - -#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 - -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 - -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 - -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 - -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 -#include "random-internal.h" -#include - -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 -#include -#include -#include "random-internal.h" - -#ifdef SKALIBS_EGD - -# include - -# ifdef SKALIBS_HASDEVURANDOM - -# include - -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 - -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 - -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 -#include "random-internal.h" -#include - -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 -#include "random-internal.h" -#include - -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 - -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 -#include - -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 -#include -#include -#include "random-internal.h" - -#ifdef SKALIBS_EGD - -# include - -# ifdef SKALIBS_HASDEVRANDOM - -# include - -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 - -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 - -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 -#include "random-internal.h" -#include - -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 -#include "random-internal.h" -#include - -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 #include -#include -#include - -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 + +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 + +#ifdef SKALIBS_HASARC4RANDOM + +#include + +void random_finish () +{ +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include + +void random_finish () +{ +} + +#else +#ifdef SKALIBS_HASDEVURANDOM + +#include +#include +#include "random-internal.h" + +void random_finish () +{ + fd_close(random_fd) ; + random_fd = -1 ; +} + +#else /* default */ + +#include + +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 + +#ifdef SKALIBS_HASARC4RANDOM + +#include +#include + +int random_init () +{ + char seed[160] ; + random_makeseed(seed) ; + arc4random_addrandom(seed, 160) ; + return 1 ; +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include +#include + +int random_init () +{ + char seed[160] ; + random_makeseed(seed) ; + return openwritenclose_unsafe("/dev/urandom", seed, 160) ; +} + +#else +#ifdef SKALIBS_HASDEVURANDOM + +#include +#include +#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 +#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/random_makeseed.c b/src/librandom/random_makeseed.c new file mode 100644 index 0000000..909e896 --- /dev/null +++ b/src/librandom/random_makeseed.c @@ -0,0 +1,44 @@ +/* ISC license. */ + +#include +#include +#include +#include + +/* + 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[256] ; + uint32 x = getpid() ; + uint32_pack(tmp, x) ; + x = getppid() ; + uint32_pack(tmp + 4, x) ; + 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) ; + } + { + char i = 0 ; + for (; i < 8 ; i++) + { + SHA1Schedule ctx = bak ; + sha1_update(&ctx, &i, 1) ; + sha1_final(&ctx, s + 20*i) ; + } + } +} 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 -#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 -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 + +#ifdef SKALIBS_HASARC4RANDOM + +#include +#include + +void random_string (char *s, unsigned int n) +{ + arc4random_buf(s, n) ; +} + +#else +#ifdef SKALIBS_HASGETRANDOM + +#include +#include +#include +#include + +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 +#include +#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 +#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 + +#ifdef SKALIBS_HASARC4RANDOM + +#include +#include + +uint32 random_uint32 (uint32 n) +{ + return arc4random_uniform(n) ; +} + +#else + +#include +#include + +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 +#include #include #include @@ -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 -#include -#include -#include - -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 -#include - -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 -#include - -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 -#include -#include - -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 -#include - -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 -#include - -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 -#include -#include -#include -#include - -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 -#include -#include -#include "random-internal.h" -#include - -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 - -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 -#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 #include -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_makeseed.c b/src/librandom/surf_makeseed.c deleted file mode 100644 index adae9a4..0000000 --- a/src/librandom/surf_makeseed.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include - -void surf_makeseed (char *s) -{ - SHA1Schedule bak = SHA1_INIT() ; - { - tain_t now ; - char tmp[20 + TAIN_PACK] ; - uint32 x = getpid() ; - uint32_pack(tmp, x) ; - x = getppid() ; - uint32_pack(tmp + 4, x) ; - tain_now(&now) ; - tain_pack(tmp + 8, &now) ; - sha1_update(&bak, tmp, 8 + TAIN_PACK) ; - sha1_final(&bak, tmp) ; - sha1_init(&bak) ; - sha1_update(&bak, tmp, 20) ; - } - { - char i = 0 ; - for (; i < 8 ; i++) - { - SHA1Schedule ctx = bak ; - sha1_update(&ctx, &i, 1) ; - sha1_final(&ctx, s + 20*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 -#include - -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 -#include -#include - -#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 - -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 -#include -#include - -#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 - -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 -#include -#include - -#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 - -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 -#include - -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 -#include - -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 -#include - -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 -#include - -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 -#include -#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 -#include -#include -#include -#include -#include - -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 -#include -#include -#include -#include - -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 -#include -#include - -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 -#include "random-internal.h" -#include -#include - -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 + +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 -#include -#include -#include - -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 +#include + +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) ; +} -- cgit v1.2.3