summaryrefslogtreecommitdiff
path: root/src/librandom
diff options
context:
space:
mode:
Diffstat (limited to 'src/librandom')
-rw-r--r--src/librandom/autosurf.c48
-rw-r--r--src/librandom/autosurf_name.c10
-rw-r--r--src/librandom/random-internal.h10
-rw-r--r--src/librandom/random_buf.c55
-rw-r--r--src/librandom/random_buf_early.c54
-rw-r--r--src/librandom/random_char.c10
-rw-r--r--src/librandom/random_devurandom.c22
-rw-r--r--src/librandom/random_finish.c45
-rw-r--r--src/librandom/random_init.c72
-rw-r--r--src/librandom/random_makeseed.c43
-rw-r--r--src/librandom/random_name.c10
-rw-r--r--src/librandom/random_name_from.c11
-rw-r--r--src/librandom/random_oklist.c6
-rw-r--r--src/librandom/random_sauniquename_from.c (renamed from src/librandom/random_sauniquename.c)4
-rw-r--r--src/librandom/random_string.c60
-rw-r--r--src/librandom/random_uint32.c37
-rw-r--r--src/librandom/random_uint32_from.c20
-rw-r--r--src/librandom/random_unsort_from.c (renamed from src/librandom/random_unsort.c)5
18 files changed, 216 insertions, 306 deletions
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 <unistd.h>
+#include <stdint.h>
+
+#include <skalibs/uint32.h>
+#include <skalibs/tai.h>
+#include <skalibs/sha1.h>
#include <skalibs/random.h>
#include <skalibs/surf.h>
+/*
+ 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 <skalibs/surf.h>
-#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 <skalibs/surf.h>
+#include <stddef.h>
-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 <skalibs/sysdeps.h>
+
+#if defined(SKALIBS_HASARC4RANDOM)
+
+#include <skalibs/nonposix.h>
+#include <stdlib.h>
+
+#include <skalibs/random.h>
+
+void random_buf (char *s, size_t n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#elif defined(SKALIBS_HASGETRANDOM)
+
+#include <sys/random.h>
+
+#include <skalibs/random.h>
+
+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 <skalibs/surf.h>
+#include <skalibs/random.h>
+
+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 <skalibs/sysdeps.h>
+
+#if defined(SKALIBS_HASARC4RANDOM)
+
+#include <skalibs/nonposix.h>
+#include <stdlib.h>
+#include <skalibs/random.h>
+
+void random_buf_early (char *s, size_t n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#elif defined(SKALIBS_HASGETRANDOM) && defined(SKALIBS_HASGRNDINSECURE)
+
+#include <sys/random.h>
+
+#include <skalibs/random.h>
+
+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 <skalibs/surf.h>
+#include <skalibs/random.h>
+
+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 <skalibs/random.h>
-
-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 <stddef.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/random.h>
+
+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 <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
deleted file mode 100644
index cd880e8..0000000
--- a/src/librandom/random_init.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include <skalibs/nonposix.h>
-#include <stdlib.h>
-
-#include <skalibs/random.h>
-
-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 <skalibs/random.h>
-
-int random_init ()
-{
- return 1 ;
-}
-
-#else
-
-#include <skalibs/surf.h>
-#include <skalibs/random.h>
-#include "random-internal.h"
-
-SURFSchedule surf_here = SURFSCHEDULE_ZERO ;
-
-#ifdef SKALIBS_HASDEVURANDOM
-
-#include <errno.h>
-
-#include <skalibs/djbunix.h>
-
-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 <unistd.h>
-#include <stdint.h>
-#include <skalibs/uint32.h>
-#include <skalibs/tai.h>
-#include <skalibs/sha1.h>
-
-/*
- 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 <skalibs/random.h>
-#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 <skalibs/functypes.h>
+#include <skalibs/random.h>
+
+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_from.c
index e44e7ba..132d303 100644
--- a/src/librandom/random_sauniquename.c
+++ b/src/librandom/random_sauniquename_from.c
@@ -4,14 +4,14 @@
#include <skalibs/skamisc.h>
#include <skalibs/random.h>
-int random_sauniquename (stralloc *sa, size_t n)
+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(sa->s + sa->len, n) ;
+ random_name_from(sa->s + sa->len, n, f) ;
sa->len += n ;
return 1 ;
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 <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include <skalibs/nonposix.h>
-#include <stdlib.h>
-#include <skalibs/random.h>
-
-void random_string (char *s, size_t n)
-{
- arc4random_buf(s, n) ;
-}
-
-#else
-#ifdef SKALIBS_HASGETRANDOM
-
-#include <sys/random.h>
-#include <skalibs/random.h>
-
-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 <skalibs/allreadwrite.h>
-#include <skalibs/random.h>
-#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 <skalibs/random.h>
-#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 <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASARC4RANDOM
-
-#include <skalibs/nonposix.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <skalibs/random.h>
-
-uint32_t random_uint32 (uint32_t n)
-{
- return arc4random_uniform(n) ;
-}
-
-#else
-
-#include <skalibs/uint32.h>
-#include <skalibs/random.h>
-
-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 <skalibs/uint32.h>
+#include <skalibs/functypes.h>
+#include <skalibs/random.h>
+
+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_from.c
index 4596136..179cab9 100644
--- a/src/librandom/random_unsort.c
+++ b/src/librandom/random_unsort_from.c
@@ -2,14 +2,15 @@
#include <stdint.h>
#include <string.h>
+
#include <skalibs/random.h>
-void random_unsort (char *s, size_t n, size_t chunksize)
+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(n+1) ;
+ 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) ;