diff options
66 files changed, 402 insertions, 1104 deletions
@@ -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. </p> - <h3> High quality, cryptographically strong random data </h3> +<h3> Basic functions </h3> <pre> 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() ; </pre> <p> - <tt>goodrandom_init()</tt> becomes optional with skalibs-0.43. + <tt>random_init()</tt> must be called before any other function in +the random library. It returns 0 (and sets errno) on failure, and +nonzero on success. +</p> + +<p> It is recommended that you let the library perform cleanups after you -have used it, by calling <tt>goodrandom_finish()</tt>. +are done using it, by calling <tt>random_finish()</tt> - unless your +process exits right away. </p> <ul> - <li> <tt>goodrandom_char()</tt> returns a random character </li> - <li> <tt>goodrandom_int(<em>max</em>)</tt> returns a random integer -between 0 and <em>max</em>-1 </li> - <li> <tt>goodrandom_string(<em>data</em>, <em>b</em>)</tt> puts -<em>b</em> random bytes in <em>data</em>, which must be preallocated. -It returns <em>b</em> if it succeeds, or a non-negative integer lesser -than <em>b</em> if it fails for any reason. </li> + <li> <tt>random_char()</tt> returns a random character. </li> + <li> <tt>random_uint32(<em>max</em>)</tt> returns a random integer +between 0 and <em>max</em>-1. </li> + <li> <tt>random_string(<em>data</em>, <em>b</em>)</tt> fills +<em>b</em> bytes in <em>data</em> (which must be preallocated) +with random data. </li> </ul> +<h3> Advanced functions </h3> + <p> - If you have neither <tt>/dev/random</tt> nor EGD, a software PRNG is -used. This PRNG is based on the -<a href="http://cr.yp.to/antiforgery.html#surf">SURF</a> function, which -is unpredictable enough for most uses. +<code> void random_unsort (char *s, unsigned int n, unsigned int chunksize) </code> <br /> +Shuffles the array <em>s</em> (of size at least <em>n</em>*<em>chunksize</em>) by +performing a random permutation of the <em>n</em> blocks of <em>chunksize</em> bytes. +Bytes are not permuted inside chunks. </p> - <h3> Lower quality random data </h3> - <p> - It works basically the same, by replacing <tt>goodrandom_*</tt> with -<tt>badrandom_*</tt>. It uses <tt>/dev/urandom</tt> 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. +<code> void random_name (char *s, unsigned int n) </code> <br /> +Writes <em>n</em> random readable ASCII characters into <em>s</em>: +letters, numbers, hyphens or underscores. Does not terminate with a +null character. </p> <p> - The point of <tt>badrandom</tt> is to get random bytes <em>instantly</em>, -even at the expense of quality; whereas <tt>goodrandom</tt> always returns -high-quality random bytes, but may block if entropy is insufficient. In -practice, in spite of its name, <tt>badrandom</tt> will return quite -unpredictable pseudo-random data, so <tt>goodrandom</tt> should be used -only when paranoia is the rule and blocking is an option. +<code> int random_sauniquename (stralloc *sa, unsigned int n) </code> <br /> +Appends a (non-null-terminated) unique, unpredictable ASCII name to the +<a href="../libstddjb/stralloc.html">stralloc</a> <em>*sa</em>. That +name includes <em>n</em> randomly generated ASCII characters. </p> +<h2> Notes </h2> + +<ul> + <li> The <tt>random</tt> library is a thin wrapper around the best +available random generator provided by the underlying system. By +decreasing order of preference, it will use the following +implementations if available: + <ul> + <li> <a href="http://man.openbsd.org/arc4random.3">arc4random()</a> </li> + <li> <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html">getrandom()</a> </li> + <li> <tt>/dev/urandom</tt> </li> + <li> a basic <a href="https://cr.yp.to/papers/surf.pdf">SURF</a> pseudo-random generator</li> + </ul> </li> + <li> The <tt>random_init()</tt> function tries to automatically add some +reasonably random data to the underlying random generator's entropy pool. +However, that pseudorandom data is not 100% unpredictable, so it will not +replace proper seeding of the random generator at boot time. </li> +</ul> + </body> </html> 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 <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) ; +} |