From 4e75b40d8a96c5d51490ddea43566fe8e6d4168c Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Sat, 9 Apr 2022 02:28:05 +0000
Subject: Prepare for 2.12.0.0. librandom revamp.
Signed-off-by: Laurent Bercot
---
AUTHORS | 3 +-
COPYING | 2 +-
NEWS | 10 +++++
doc/index.html | 2 +-
doc/license.html | 2 +-
doc/upgrade.html | 11 +++++
package/deps.mak | 25 +++++------
package/info | 2 +-
src/include/skalibs/functypes.h | 6 +++
src/include/skalibs/random.h | 28 ++++++++-----
src/libposixplz/mkfiletemp.c | 4 +-
src/librandom/autosurf.c | 48 +++++++++++++++++++--
src/librandom/autosurf_name.c | 10 -----
src/librandom/random-internal.h | 10 ++---
src/librandom/random_buf.c | 55 ++++++++++++++++++++++++
src/librandom/random_buf_early.c | 54 ++++++++++++++++++++++++
src/librandom/random_char.c | 10 -----
src/librandom/random_devurandom.c | 22 ++++++++++
src/librandom/random_finish.c | 45 --------------------
src/librandom/random_init.c | 72 --------------------------------
src/librandom/random_makeseed.c | 43 -------------------
src/librandom/random_name.c | 10 -----
src/librandom/random_name_from.c | 11 +++++
src/librandom/random_oklist.c | 6 ---
src/librandom/random_sauniquename.c | 21 ----------
src/librandom/random_sauniquename_from.c | 21 ++++++++++
src/librandom/random_string.c | 60 --------------------------
src/librandom/random_uint32.c | 37 ----------------
src/librandom/random_uint32_from.c | 20 +++++++++
src/librandom/random_unsort.c | 17 --------
src/librandom/random_unsort_from.c | 18 ++++++++
31 files changed, 313 insertions(+), 372 deletions(-)
delete mode 100644 src/librandom/autosurf_name.c
create mode 100644 src/librandom/random_buf.c
create mode 100644 src/librandom/random_buf_early.c
delete mode 100644 src/librandom/random_char.c
create mode 100644 src/librandom/random_devurandom.c
delete mode 100644 src/librandom/random_finish.c
delete mode 100644 src/librandom/random_init.c
delete mode 100644 src/librandom/random_makeseed.c
delete mode 100644 src/librandom/random_name.c
create mode 100644 src/librandom/random_name_from.c
delete mode 100644 src/librandom/random_oklist.c
delete mode 100644 src/librandom/random_sauniquename.c
create mode 100644 src/librandom/random_sauniquename_from.c
delete mode 100644 src/librandom/random_string.c
delete mode 100644 src/librandom/random_uint32.c
create mode 100644 src/librandom/random_uint32_from.c
delete mode 100644 src/librandom/random_unsort.c
create mode 100644 src/librandom/random_unsort_from.c
diff --git a/AUTHORS b/AUTHORS
index c0410d2..da242b2 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -12,7 +12,7 @@ Contributors:
John Regan
Shengjing Zhu
Colin Booth
- Petr Vaněk
+ Petr Vanek
Thanks to:
Jean Marot
@@ -41,3 +41,4 @@ Thanks to:
Alex Raschi
Josuah Demangeon
Xavier Stonestreet
+ Jason Donenfeld
diff --git a/COPYING b/COPYING
index 9e95f25..0dfd4b1 100644
--- a/COPYING
+++ b/COPYING
@@ -1,4 +1,4 @@
-Copyright (c) 2011-2021 Laurent Bercot
+Copyright (c) 2011-2022 Laurent Bercot
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
diff --git a/NEWS b/NEWS
index f810b7c..273c0bd 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,15 @@
Changelog for skalibs.
+In 2.12.0.0
+-----------
+
+ - librandom revamped (again).
+ * random_init() and random_finish() removed.
+ * random_char() removed.
+ * random_string() renamed to random_buf().
+ * early versions added.
+
+
In 2.11.2.0
-----------
diff --git a/doc/index.html b/doc/index.html
index ac4bbb7..5fb2643 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -60,7 +60,7 @@ with a standard C development environment
Download
- - The current released version of skalibs is 2.11.2.0.
+ - The current released version of skalibs is 2.12.0.0.
- Alternatively, you can checkout a copy of the
skalibs
git repository:
diff --git a/doc/license.html b/doc/license.html
index 1ced372..f3700c3 100644
--- a/doc/license.html
+++ b/doc/license.html
@@ -74,7 +74,7 @@ color, or different text font.
I am aware that the previous restrictions sound completely
ridiculous while the official skalibs documentation is incomplete.
-As of 2.11.0.0, I'm not going to enforce those restrictions, but if you're
+As of 2.12.0.0, I'm not going to enforce those restrictions, but if you're
going to provide documentation for skalibs, don't keep it to yourself,
please send it to me instead. :-)
diff --git a/doc/upgrade.html b/doc/upgrade.html
index 8ac3b8b..9e6c272 100644
--- a/doc/upgrade.html
+++ b/doc/upgrade.html
@@ -16,6 +16,17 @@
skarnet.org
+ in 2.12.0.0
+
+
+ - random_init() and random_finish() removed,
+as well as random_char().
+ - random_string() renamed to random_buf().
+ - random functions now have an early version, to use when
+randomness is required before the generator can be seeded (i.e. in
+programs running in the very early boot).
+
+
in 2.11.2.0
diff --git a/package/deps.mak b/package/deps.mak
index 739a594..6cc5846 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -34,7 +34,7 @@ src/include/skalibs/lolstdio.h: src/include/skalibs/bufalloc.h src/include/skali
src/include/skalibs/netstring.h: src/include/skalibs/buffer.h src/include/skalibs/stralloc.h
src/include/skalibs/posixishard.h: src/include/skalibs/gccattributes.h
src/include/skalibs/posixplz.h: src/include/skalibs/functypes.h src/include/skalibs/gccattributes.h
-src/include/skalibs/random.h: src/include/skalibs/stralloc.h
+src/include/skalibs/random.h: src/include/skalibs/functypes.h src/include/skalibs/stralloc.h
src/include/skalibs/setgroups.h: src/include/skalibs/sysdeps.h
src/include/skalibs/sha512.h: src/include/skalibs/uint64.h
src/include/skalibs/sig.h: src/include/skalibs/gccattributes.h
@@ -58,7 +58,6 @@ src/include/skalibs/unixonacid.h: src/include/skalibs/ancil.h src/include/skalib
src/libdatastruct/avlnode-internal.h: src/include/skalibs/avlnode.h
src/libdatastruct/genqdyn-internal.h: src/include/skalibs/genqdyn.h
src/libenvexec/envdir-internal.h: src/include/skalibs/stralloc.h
-src/librandom/random-internal.h: src/include/skalibs/surf.h
src/libstdcrypto/sha1-internal.h: src/include/skalibs/sha1.h
src/libstdcrypto/sha256-internal.h: src/include/skalibs/sha256.h
src/libstdcrypto/sha512-internal.h: src/include/skalibs/sha512.h
@@ -157,7 +156,7 @@ src/libposixplz/getpeereid.o src/libposixplz/getpeereid.lo: src/libposixplz/getp
src/libposixplz/memmem.o src/libposixplz/memmem.lo: src/libposixplz/memmem.c src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
src/libposixplz/mkbtemp.o src/libposixplz/mkbtemp.lo: src/libposixplz/mkbtemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libposixplz/mkctemp.o src/libposixplz/mkctemp.lo: src/libposixplz/mkctemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
-src/libposixplz/mkfiletemp.o src/libposixplz/mkfiletemp.lo: src/libposixplz/mkfiletemp.c src/include/skalibs/posixplz.h src/include/skalibs/surf.h
+src/libposixplz/mkfiletemp.o src/libposixplz/mkfiletemp.lo: src/libposixplz/mkfiletemp.c src/include/skalibs/posixplz.h src/include/skalibs/random.h
src/libposixplz/mkhtemp.o src/libposixplz/mkhtemp.lo: src/libposixplz/mkhtemp.c src/include/skalibs/posixplz.h
src/libposixplz/mklinktemp.o src/libposixplz/mklinktemp.lo: src/libposixplz/mklinktemp.c src/include/skalibs/djbunix.h src/libposixplz/posixplz-internal.h src/include/skalibs/posixplz.h
src/libposixplz/mkltemp.o src/libposixplz/mkltemp.lo: src/libposixplz/mkltemp.c src/include/skalibs/posixplz.h
@@ -168,18 +167,14 @@ src/libposixplz/setgroups.o src/libposixplz/setgroups.lo: src/libposixplz/setgro
src/libposixplz/strnlen.o src/libposixplz/strnlen.lo: src/libposixplz/strnlen.c src/include/skalibs/bytestr.h src/include/skalibs/posixishard.h src/include/skalibs/sysdeps.h
src/libposixplz/touch.o src/libposixplz/touch.lo: src/libposixplz/touch.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
src/libposixplz/unlink_void.o src/libposixplz/unlink_void.lo: src/libposixplz/unlink_void.c src/include/skalibs/posixplz.h
-src/librandom/autosurf.o src/librandom/autosurf.lo: src/librandom/autosurf.c src/include/skalibs/random.h src/include/skalibs/surf.h
-src/librandom/autosurf_name.o src/librandom/autosurf_name.lo: src/librandom/autosurf_name.c src/librandom/random-internal.h src/include/skalibs/surf.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/include/skalibs/nonposix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/surf.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/librandom/random-internal.h src/include/skalibs/random.h
-src/librandom/random_oklist.o src/librandom/random_oklist.lo: src/librandom/random_oklist.c src/librandom/random-internal.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_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/nonposix.h 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/random.h
+src/librandom/autosurf.o src/librandom/autosurf.lo: src/librandom/autosurf.c src/include/skalibs/random.h src/include/skalibs/sha1.h src/include/skalibs/surf.h src/include/skalibs/tai.h src/include/skalibs/uint32.h
+src/librandom/random_buf.o src/librandom/random_buf.lo: src/librandom/random_buf.c src/include/skalibs/nonposix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/surf.h src/include/skalibs/sysdeps.h
+src/librandom/random_buf_early.o src/librandom/random_buf_early.lo: src/librandom/random_buf_early.c src/include/skalibs/nonposix.h src/librandom/random-internal.h src/include/skalibs/random.h src/include/skalibs/surf.h src/include/skalibs/sysdeps.h
+src/librandom/random_devurandom.o src/librandom/random_devurandom.lo: src/librandom/random_devurandom.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/random.h src/include/skalibs/strerr2.h
+src/librandom/random_name_from.o src/librandom/random_name_from.lo: src/librandom/random_name_from.c src/include/skalibs/functypes.h src/include/skalibs/random.h
+src/librandom/random_sauniquename_from.o src/librandom/random_sauniquename_from.lo: src/librandom/random_sauniquename_from.c src/include/skalibs/random.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
+src/librandom/random_uint32_from.o src/librandom/random_uint32_from.lo: src/librandom/random_uint32_from.c src/include/skalibs/functypes.h src/include/skalibs/random.h src/include/skalibs/uint32.h
+src/librandom/random_unsort_from.o src/librandom/random_unsort_from.lo: src/librandom/random_unsort_from.c src/include/skalibs/random.h
src/librandom/surf.o src/librandom/surf.lo: src/librandom/surf.c src/include/skalibs/surf.h src/include/skalibs/uint32.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/sha1_feed.o src/libstdcrypto/sha1_feed.lo: src/libstdcrypto/sha1_feed.c src/include/skalibs/bytestr.h src/libstdcrypto/sha1-internal.h src/include/skalibs/sha1.h
diff --git a/package/info b/package/info
index de40a07..6caec2b 100644
--- a/package/info
+++ b/package/info
@@ -1,4 +1,4 @@
package=skalibs
-version=2.11.2.0
+version=2.12.0.0
category=prog
package_macro_name=SKALIBS
diff --git a/src/include/skalibs/functypes.h b/src/include/skalibs/functypes.h
index f839877..0c94532 100644
--- a/src/include/skalibs/functypes.h
+++ b/src/include/skalibs/functypes.h
@@ -58,4 +58,10 @@ typedef create_func *create_func_ref ;
typedef int link_func (char const *, char const *) ;
typedef link_func *link_func_ref ;
+typedef void randomgen_func (char *, size_t) ;
+typedef randomgen_func *randomgen_func_ref ;
+
+typedef uint32_t uint32_func (uint32_t) ;
+typedef uint32_func *uint32_func_ref ;
+
#endif
diff --git a/src/include/skalibs/random.h b/src/include/skalibs/random.h
index 6c9dee0..0391d21 100644
--- a/src/include/skalibs/random.h
+++ b/src/include/skalibs/random.h
@@ -3,21 +3,29 @@
#ifndef SKALIBS_RANDOM_H
#define SKALIBS_RANDOM_H
-#include
+#include
#include
+#include
#include
-extern void random_makeseed (char *) ; /* fills 160 bytes */
+extern void random_buf (char *, size_t) ;
+extern void random_buf_early (char *, size_t) ;
-extern int random_init (void) ;
-extern void random_finish (void) ;
+extern uint32_t random_uint32_from (uint32_t, randomgen_func_ref) ;
+#define random_uint32(n) random_uint32_from((n), &random_buf)
+#define random_uint32_early(n) random_uint32_from((n), &random_buf_early)
-extern unsigned char random_char (void) ;
-extern void random_string (char *, size_t) ;
-extern uint32_t random_uint32 (uint32_t) ;
-extern void random_name (char *, size_t) ;
-extern void random_unsort (char *, size_t, size_t) ;
-extern int random_sauniquename (stralloc *, size_t) ;
+extern void random_name_from (char *, size_t, randomgen_func_ref) ;
+#define random_name(s, n) random_name_from(s, (n), &random_buf)
+#define random_name_early(s, n) random_name_from(s, (n), &random_buf_early)
+
+extern void random_unsort_from (char *, size_t, size_t, randomgen_func_ref) ;
+#define random_unsort(s, n, c) random_unsort_from(s, n, (c), &random_buf)
+#define random_unsort_early(s, n, c) random_unsort_from(s, n, (c), &random_buf_early)
+
+extern int random_sauniquename_from (stralloc *, size_t, randomgen_func_ref) ;
+#define random_sauniquename(sa, n) random_sauniquename_from(sa, (n), &random_buf)
+#define random_sauniquename_early(sa, n) random_sauniquename_from(sa, (n), &random_buf_early)
#endif
diff --git a/src/libposixplz/mkfiletemp.c b/src/libposixplz/mkfiletemp.c
index ac8426c..9db5d0b 100644
--- a/src/libposixplz/mkfiletemp.c
+++ b/src/libposixplz/mkfiletemp.c
@@ -3,7 +3,7 @@
#include
#include
-#include
+#include
#include
int mkfiletemp (char *s, create_func_ref f, mode_t mode, void *data)
@@ -15,7 +15,7 @@ int mkfiletemp (char *s, create_func_ref f, mode_t mode, void *data)
if (xlen < 6) return (errno = EINVAL, -1) ;
do
{
- autosurf_name(s + len - xlen, xlen) ;
+ random_name_early(s + len - xlen, xlen) ;
r = (*f)(s, mode, data) ;
} while (r == -1 && errno == EEXIST) ;
return r ;
diff --git a/src/librandom/autosurf.c b/src/librandom/autosurf.c
index 04a47c2..0a219b8 100644
--- a/src/librandom/autosurf.c
+++ b/src/librandom/autosurf.c
@@ -1,17 +1,59 @@
/* ISC license. */
+#include
+#include
+
+#include
+#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 seed a fallback internal PRNG.
+*/
+
+static void makeseed (char *s)
+{
+ SHA1Schedule bak = SHA1_INIT() ;
+ {
+ tain now ;
+ char tmp[256] ;
+ uint32_t x = getpid() ;
+ uint32_pack(tmp, x) ; /* if valgrind sends you here, tell it to stfu */
+ 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) ;
+ }
+ }
+}
+
void autosurf (char *s, size_t n)
{
static SURFSchedule ctx = SURFSCHEDULE_ZERO ;
static int need4seed = 1 ;
if (need4seed)
{
- char tmp[160] ;
- random_makeseed(tmp) ;
- surf_init(&ctx, tmp) ;
+ char seed[160] ;
+ makeseed(seed) ;
+ surf_init(&ctx, seed) ;
need4seed = 0 ;
}
surf(&ctx, s, n) ;
diff --git a/src/librandom/autosurf_name.c b/src/librandom/autosurf_name.c
deleted file mode 100644
index d80e1f8..0000000
--- a/src/librandom/autosurf_name.c
+++ /dev/null
@@ -1,10 +0,0 @@
-/* ISC license. */
-
-#include
-#include "random-internal.h"
-
-void autosurf_name (char *s, size_t n)
-{
- autosurf(s, n) ;
- while (n--) s[n] = random_oklist[s[n] & 63] ;
-}
diff --git a/src/librandom/random-internal.h b/src/librandom/random-internal.h
index a8f8be9..136c0d8 100644
--- a/src/librandom/random-internal.h
+++ b/src/librandom/random-internal.h
@@ -1,12 +1,10 @@
/* ISC license. */
-#ifndef RANDOM_INTERNAL_H
-#define RANDOM_INTERNAL_H
+#ifndef SKALIBS_RANDOM_INTERNAL_H
+#define SKALIBS_RANDOM_INTERNAL_H
-#include
+#include
-extern char const *random_oklist ;
-extern int random_fd ;
-extern SURFSchedule surf_here ;
+extern void random_devurandom (char *, size_t) ;
#endif
diff --git a/src/librandom/random_buf.c b/src/librandom/random_buf.c
new file mode 100644
index 0000000..99cce38
--- /dev/null
+++ b/src/librandom/random_buf.c
@@ -0,0 +1,55 @@
+/* ISC license. */
+
+#include
+
+#if defined(SKALIBS_HASARC4RANDOM)
+
+#include
+#include
+
+#include
+
+void random_buf (char *s, size_t n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#elif defined(SKALIBS_HASGETRANDOM)
+
+#include
+
+#include
+
+void random_buf (char *s, size_t n)
+{
+ while (n)
+ {
+ ssize_t r = getrandom(s, n, 0) ;
+ if (r >= 0)
+ {
+ s += r ;
+ n -= r ;
+ }
+ }
+}
+
+#elif defined(SKALIBS_HASDEVURANDOM)
+
+#include "random-internal.h"
+
+void random_buf (char *s, size_t n)
+{
+ random_devurandom(s, n) ;
+}
+
+#else
+
+#include
+#include
+
+void random_buf (char *s, size_t n)
+{
+ autosurf(s, n) ;
+}
+
+#endif
diff --git a/src/librandom/random_buf_early.c b/src/librandom/random_buf_early.c
new file mode 100644
index 0000000..a306239
--- /dev/null
+++ b/src/librandom/random_buf_early.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include
+
+#if defined(SKALIBS_HASARC4RANDOM)
+
+#include
+#include
+#include
+
+void random_buf_early (char *s, size_t n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#elif defined(SKALIBS_HASGETRANDOM) && defined(SKALIBS_HASGRNDINSECURE)
+
+#include
+
+#include
+
+void random_buf_early (char *s, size_t n)
+{
+ while (n)
+ {
+ ssize_t r = getrandom(s, n, GRND_INSECURE) ;
+ if (r >= 0)
+ {
+ s += r ;
+ n -= r ;
+ }
+ }
+}
+
+#elif defined(SKALIBS_HASDEVURANDOM)
+
+#include "random-internal.h"
+
+void random_buf_early (char *s, size_t n)
+{
+ random_devurandom(s, n) ;
+}
+
+#else
+
+#include
+#include
+
+void random_buf_early (char *s, size_t n)
+{
+ autosurf(s, n) ;
+}
+
+#endif
diff --git a/src/librandom/random_char.c b/src/librandom/random_char.c
deleted file mode 100644
index f55443e..0000000
--- a/src/librandom/random_char.c
+++ /dev/null
@@ -1,10 +0,0 @@
-/* ISC license. */
-
-#include
-
-unsigned char random_char (void)
-{
- unsigned char c ;
- random_string((char *)&c, 1) ;
- return c ;
-}
diff --git a/src/librandom/random_devurandom.c b/src/librandom/random_devurandom.c
new file mode 100644
index 0000000..0ad752b
--- /dev/null
+++ b/src/librandom/random_devurandom.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include
+
+#include
+#include
+#include
+#include
+
+void random_devurandom (char *s, size_t n)
+{
+ static int random_fd = -1 ;
+ size_t r ;
+ if (random_fd < 0)
+ {
+ random_fd = openbc_read("/dev/urandom") ;
+ if (random_fd < 0)
+ strerr_diefu2sys(111, "open ", "/dev/urandom") ;
+ }
+ r = allread(random_fd, s, n) ;
+ if (r < n) strerr_diefu2sys(111, "read from ", "/dev/urandom") ;
+}
diff --git a/src/librandom/random_finish.c b/src/librandom/random_finish.c
deleted file mode 100644
index bcc42da..0000000
--- a/src/librandom/random_finish.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/* 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
deleted file mode 100644
index cd880e8..0000000
--- a/src/librandom/random_init.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include
-#include
-
-#include
-
-int random_init ()
-{
-#ifdef SKALIBS_HASARC4RANDOM_ADDRANDOM
- char seed[160] ;
- random_makeseed(seed) ;
- arc4random_addrandom((unsigned char *)seed, 160) ;
-#endif
- return 1 ;
-}
-
-#else
-#ifdef SKALIBS_HASGETRANDOM
-
-#include
-
-int random_init ()
-{
- return 1 ;
-}
-
-#else
-
-#include
-#include
-#include "random-internal.h"
-
-SURFSchedule surf_here = SURFSCHEDULE_ZERO ;
-
-#ifdef SKALIBS_HASDEVURANDOM
-
-#include
-
-#include
-
-int random_fd = -1 ;
-
-int random_init ()
-{
- if (random_fd >= 0) return 1 ;
- random_fd = openc_readb("/dev/urandom") ;
- return random_fd >= 0 ;
-}
-
-#else /* default */
-
-int random_init ()
-{
- static int initted = 0 ;
- if (!initted)
- {
- char seed[160] ;
- initted = 1 ;
- 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
deleted file mode 100644
index 9c518cf..0000000
--- a/src/librandom/random_makeseed.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/* ISC license. */
-
-#include
-#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 seed an internal PRNG.
-*/
-
-void random_makeseed (char *s)
-{
- SHA1Schedule bak = SHA1_INIT() ;
- {
- tain now ;
- char tmp[256] ;
- uint32_t 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_name.c b/src/librandom/random_name.c
deleted file mode 100644
index 057a5db..0000000
--- a/src/librandom/random_name.c
+++ /dev/null
@@ -1,10 +0,0 @@
-/* ISC license. */
-
-#include
-#include "random-internal.h"
-
-void random_name (char *s, size_t n)
-{
- random_string(s, n) ;
- while (n--) s[n] = random_oklist[s[n] & 63] ;
-}
diff --git a/src/librandom/random_name_from.c b/src/librandom/random_name_from.c
new file mode 100644
index 0000000..c9df29e
--- /dev/null
+++ b/src/librandom/random_name_from.c
@@ -0,0 +1,11 @@
+/* ISC license. */
+
+#include
+#include
+
+void random_name_from (char *s, size_t n, randomgen_func_ref f)
+{
+ static char const random_oklist[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ;
+ (*f)(s, n) ;
+ while (n--) s[n] = random_oklist[s[n] & 63] ;
+}
diff --git a/src/librandom/random_oklist.c b/src/librandom/random_oklist.c
deleted file mode 100644
index d79f745..0000000
--- a/src/librandom/random_oklist.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "random-internal.h"
-
-static char const random_oklist_[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ;
-char const *random_oklist = random_oklist_ ;
diff --git a/src/librandom/random_sauniquename.c b/src/librandom/random_sauniquename.c
deleted file mode 100644
index e44e7ba..0000000
--- a/src/librandom/random_sauniquename.c
+++ /dev/null
@@ -1,21 +0,0 @@
-/* ISC license. */
-
-#include
-#include
-#include
-
-int random_sauniquename (stralloc *sa, size_t n)
-{
- size_t base = sa->len ;
- int wasnull = !sa->s ;
- if (!sauniquename(sa)) return 0 ;
- if (!stralloc_readyplus(sa, n+1)) goto err ;
- stralloc_catb(sa, ":", 1) ;
- random_name(sa->s + sa->len, n) ;
- sa->len += n ;
- return 1 ;
-
-err:
- if (wasnull) stralloc_free(sa) ; else sa->len = base ;
- return 0 ;
-}
diff --git a/src/librandom/random_sauniquename_from.c b/src/librandom/random_sauniquename_from.c
new file mode 100644
index 0000000..132d303
--- /dev/null
+++ b/src/librandom/random_sauniquename_from.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include
+#include
+#include
+
+int random_sauniquename_from (stralloc *sa, size_t n, randomgen_func_ref f)
+{
+ size_t base = sa->len ;
+ int wasnull = !sa->s ;
+ if (!sauniquename(sa)) return 0 ;
+ if (!stralloc_readyplus(sa, n+1)) goto err ;
+ stralloc_catb(sa, ":", 1) ;
+ random_name_from(sa->s + sa->len, n, f) ;
+ sa->len += n ;
+ return 1 ;
+
+err:
+ if (wasnull) stralloc_free(sa) ; else sa->len = base ;
+ return 0 ;
+}
diff --git a/src/librandom/random_string.c b/src/librandom/random_string.c
deleted file mode 100644
index acdec69..0000000
--- a/src/librandom/random_string.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include
-#include
-#include
-
-void random_string (char *s, size_t n)
-{
- arc4random_buf(s, n) ;
-}
-
-#else
-#ifdef SKALIBS_HASGETRANDOM
-
-#include
-#include
-
-void random_string (char *s, size_t n)
-{
- while (n)
- {
- ssize_t 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, size_t n)
-{
- size_t 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, size_t n)
-{
- surf(&surf_here, s, n) ;
-}
-
-#endif
-#endif
-#endif
diff --git a/src/librandom/random_uint32.c b/src/librandom/random_uint32.c
deleted file mode 100644
index 6700879..0000000
--- a/src/librandom/random_uint32.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include
-#include
-#include
-#include
-
-uint32_t random_uint32 (uint32_t n)
-{
- return arc4random_uniform(n) ;
-}
-
-#else
-
-#include
-#include
-
-uint32_t random_uint32 (uint32_t n)
-{
- uint32_t min, x ;
- if (n < 2) return 0 ;
- min = -n % n ;
- for (;;)
- {
- char tmp[4] ;
- random_string(tmp, 4) ;
- uint32_unpack_big(tmp, &x) ;
- if (x >= min) break ;
- }
- return x % n ;
-}
-
-#endif
diff --git a/src/librandom/random_uint32_from.c b/src/librandom/random_uint32_from.c
new file mode 100644
index 0000000..621c438
--- /dev/null
+++ b/src/librandom/random_uint32_from.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include
+#include
+#include
+
+uint32_t random_uint32_from (uint32_t n, randomgen_func_ref f)
+{
+ uint32_t min, x ;
+ if (n < 2) return 0 ;
+ min = -n % n ;
+ for (;;)
+ {
+ char tmp[4] ;
+ (*f)(tmp, 4) ;
+ uint32_unpack_big(tmp, &x) ;
+ if (x >= min) break ;
+ }
+ return x % n ;
+}
diff --git a/src/librandom/random_unsort.c b/src/librandom/random_unsort.c
deleted file mode 100644
index 4596136..0000000
--- a/src/librandom/random_unsort.c
+++ /dev/null
@@ -1,17 +0,0 @@
-/* ISC license. */
-
-#include
-#include
-#include
-
-void random_unsort (char *s, size_t n, size_t chunksize)
-{
- char tmp[chunksize] ;
- while (n--)
- {
- uint32_t i = random_uint32(n+1) ;
- memcpy(tmp, s + i * chunksize, chunksize) ;
- memcpy(s + i * chunksize, s + n * chunksize, chunksize) ;
- memcpy(s + n * chunksize, tmp, chunksize) ;
- }
-}
diff --git a/src/librandom/random_unsort_from.c b/src/librandom/random_unsort_from.c
new file mode 100644
index 0000000..179cab9
--- /dev/null
+++ b/src/librandom/random_unsort_from.c
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#include
+#include
+
+#include
+
+void random_unsort_from (char *s, size_t n, size_t chunksize, randomgen_func_ref f)
+{
+ char tmp[chunksize] ;
+ while (n--)
+ {
+ uint32_t i = random_uint32_from(n+1, f) ;
+ memcpy(tmp, s + i * chunksize, chunksize) ;
+ memcpy(s + i * chunksize, s + n * chunksize, chunksize) ;
+ memcpy(s + n * chunksize, tmp, chunksize) ;
+ }
+}
--
cgit v1.2.3