From a1933bd1847951b959016f59ee744d1b18a00142 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 14 Oct 2016 17:07:56 +0000 Subject: Clean up and modernize librandom. Correct random number generation has historically been suprisingly painful to achieve. There was no standard, every system behaved in a subtly different way, and there were a few userland initiatives to get decent randomness, all incompatible of course. The situation is a bit better now, we're heading towards some standardization. The arc4random() series of functions is a good API, and available on a lot of systems - unfortunately not Linux, but on Linux the new getrandom() makes using /dev/random obsolete. So I removed the old crap in librandom, dropped EGD support, dropped dynamic backend selection, made a single API series (random_* instead of goodrandom_* and badrandom_*), added an arc4random backend and a getrandom backend, and defaulted to /dev/urandom backed up by SURF in the worst case. This should be much smaller and logical. However, it's a major API break, so the skarnet.org stack will be changed to adapt. --- src/include/skalibs/random.h | 29 ++++++-------------- src/include/skalibs/rrandom.h | 36 ------------------------ src/include/skalibs/surf.h | 6 +--- src/include/skalibs/unirandom.h | 56 -------------------------------------- src/include/skalibs/unirandomdev.h | 19 ------------- src/include/skalibs/unirandomegd.h | 17 ------------ src/include/skalibs/unisurf.h | 16 ----------- 7 files changed, 9 insertions(+), 170 deletions(-) delete mode 100644 src/include/skalibs/rrandom.h delete mode 100644 src/include/skalibs/unirandom.h delete mode 100644 src/include/skalibs/unirandomdev.h delete mode 100644 src/include/skalibs/unirandomegd.h delete mode 100644 src/include/skalibs/unisurf.h (limited to 'src/include') diff --git a/src/include/skalibs/random.h b/src/include/skalibs/random.h index 90aa0c0..2d1bbcf 100644 --- a/src/include/skalibs/random.h +++ b/src/include/skalibs/random.h @@ -1,33 +1,20 @@ /* ISC license. */ -/* MT-unsafe functions only. Use rrandom for MT-safety. */ - #ifndef RANDOM_H #define RANDOM_H -#include +#include #include -extern int badrandom_init (void) ; -extern unsigned char badrandom_char (void) ; -extern unsigned int badrandom_int (unsigned int) ; -extern unsigned int badrandom_string (char *, unsigned int) ; -extern void badrandom_finish (void) ; - -extern int goodrandom_init (void) ; -extern unsigned char goodrandom_char (void) ; -extern unsigned int goodrandom_int (unsigned int) ; -extern unsigned int goodrandom_string (char *, unsigned int) ; -extern void goodrandom_finish (void) ; +extern void random_makeseed (char *) ; /* fills 160 bytes */ -#define random_init badrandom_init -#define random_char badrandom_char -#define random_int badrandom_int -#define random_string badrandom_string -#define random_finish badrandom_finish +extern int random_init (void) ; +extern void random_finish (void) ; -extern int random_name (char *, unsigned int) ; -extern int random_sauniquename (stralloc *, unsigned int) ; +extern void random_string (char *, unsigned int) ; +extern uint32 random_uint32 (uint32) ; +extern void random_name (char *, unsigned int) ; extern void random_unsort (char *, unsigned int, unsigned int) ; +extern int random_sauniquename (stralloc *, unsigned int) ; #endif diff --git a/src/include/skalibs/rrandom.h b/src/include/skalibs/rrandom.h deleted file mode 100644 index 0678523..0000000 --- a/src/include/skalibs/rrandom.h +++ /dev/null @@ -1,36 +0,0 @@ -/* ISC license. */ - -#ifndef RRANDOM_H -#define RRANDOM_H - -#include - -typedef struct rrandominfo rrandominfo, *rrandominfo_ref, **rrandominfo_ref_ref ; -struct rrandominfo -{ - unirandom it ; - unsigned int ok ; -} ; - -#define RRANDOMINFO_ZERO { .it = UNIRANDOM_ZERO, .ok = 3 } - -typedef struct rrandom rrandom, *rrandom_ref, **rrandom_ref_ref ; -struct rrandom -{ - rrandominfo tries[3] ; - unsigned int n ; -} ; - -#define RRANDOM_ZERO { .tries = { RRANDOMINFO_ZERO, RRANDOMINFO_ZERO, RRANDOMINFO_ZERO }, .n = 0 } - -extern int rrandom_add (rrandom *, int (*) (unirandom *)) ; -extern int rrandom_finish (rrandom *) ; - -extern unsigned int rrandom_read (rrandom *, char *, unsigned int, unsigned int (*) (unirandom *, char *, unsigned int)) ; -extern unsigned int rrandom_readint (rrandom *, unsigned int, unsigned int (*) (unirandom *, char *, unsigned int)) ; -#define rrandom_readb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readb) -#define rrandom_readnb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readnb) - -extern unsigned int rrandom_name (rrandom *, char *, unsigned int, int) ; - -#endif diff --git a/src/include/skalibs/surf.h b/src/include/skalibs/surf.h index aa872e1..aae21e8 100644 --- a/src/include/skalibs/surf.h +++ b/src/include/skalibs/surf.h @@ -16,11 +16,7 @@ struct SURFSchedule #define SURFSCHEDULE_ZERO { .seed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .in = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .out = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", .pos = 32 } -extern void surf_sinit (SURFSchedule *, char const *) ; /* 160 chars */ +extern void surf_init (SURFSchedule *, char const *) ; /* 160 chars */ extern void surf (SURFSchedule *, char *, unsigned int) ; -extern void surf_makeseed (char *) ; /* fills 160 chars */ -extern void surf_init (SURFSchedule *) ; -extern void surf_autoinit (SURFSchedule *, char *, unsigned int) ; - #endif diff --git a/src/include/skalibs/unirandom.h b/src/include/skalibs/unirandom.h deleted file mode 100644 index 818990e..0000000 --- a/src/include/skalibs/unirandom.h +++ /dev/null @@ -1,56 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOM_H -#define UNIRANDOM_H - -#include -#include - -#define RANDOMBUF_BUFSIZE 256 - -struct randombuf_s -{ - char buf[RANDOMBUF_BUFSIZE] ; - buffer b ; - unsigned int nb : 1 ; -} ; -#define RANDOMBUF_ZERO { .buf = "", .b = BUFFER_INIT(0, -1, 0, 0), .nb = 0 } - -struct randomegd_s -{ - int fd ; -} ; - -union unirandominfo -{ - SURFSchedule surf_ctx ; - struct randombuf_s device ; - struct randomegd_s egd ; -} ; - -#define UNIRANDOMINFO_ZERO { .surf_ctx = SURFSCHEDULE_ZERO } - -typedef struct unirandom unirandom, *unirandom_ref, **unirandom_ref_ref ; -struct unirandom -{ - unsigned int (*readb) (union unirandominfo *, char *, unsigned int) ; - unsigned int (*readnb) (union unirandominfo *, char *, unsigned int) ; - int (*init) (union unirandominfo *) ; - int (*finish) (union unirandominfo *) ; - union unirandominfo data ; - unsigned int initted : 1 ; -} ; - -#define UNIRANDOM_ZERO { .readb = 0, .readnb = 0, .init = 0, .finish = 0, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -extern int unirandom_register_devrandom (unirandom *) ; -extern int unirandom_register_devurandom (unirandom *) ; -extern int unirandom_register_hasegd (unirandom *) ; -extern int unirandom_register_surf (unirandom *) ; - -extern int unirandom_init (unirandom *) ; -extern unsigned int unirandom_readb (unirandom *, char *, unsigned int) ; -extern unsigned int unirandom_readnb (unirandom *, char *, unsigned int) ; -extern int unirandom_finish (unirandom *) ; - -#endif diff --git a/src/include/skalibs/unirandomdev.h b/src/include/skalibs/unirandomdev.h deleted file mode 100644 index 589a1c1..0000000 --- a/src/include/skalibs/unirandomdev.h +++ /dev/null @@ -1,19 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOMDEV_H -#define UNIRANDOMDEV_H - -#include - -extern int unirandomdev_sinit (union unirandominfo *, char const *) ; -extern unsigned int unirandomdev_readb (union unirandominfo *, char *, unsigned int) ; -extern unsigned int unirandomdev_readnb (union unirandominfo *, char *, unsigned int) ; -extern int unirandomdev_finish (union unirandominfo *) ; - -extern int unidevrandom_init (union unirandominfo *) ; -extern int unidevurandom_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_DEVRANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevrandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } -#define UNIRANDOM_REGISTER_DEVURANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevurandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif diff --git a/src/include/skalibs/unirandomegd.h b/src/include/skalibs/unirandomegd.h deleted file mode 100644 index 11bb464..0000000 --- a/src/include/skalibs/unirandomegd.h +++ /dev/null @@ -1,17 +0,0 @@ -/* ISC license. */ - -#ifndef UNIRANDOMEGD_H -#define UNIRANDOMEGD_H - -#include - -extern int unirandomegd_sinit (union unirandominfo *, char const *) ; -extern unsigned int unirandomegd_readb (union unirandominfo *, char *, unsigned int) ; -extern unsigned int unirandomegd_readnb (union unirandominfo *, char *, unsigned int) ; -extern int unirandomegd_finish (union unirandominfo *) ; - -extern int unihasegd_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_HASEGD() { .readb = &unirandomegd_readb, .readnb = &unirandomegd_readnb, .init = &unihasegd_init, .finish = &unirandomegd_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif diff --git a/src/include/skalibs/unisurf.h b/src/include/skalibs/unisurf.h deleted file mode 100644 index 5c95c82..0000000 --- a/src/include/skalibs/unisurf.h +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#ifndef UNISURF_H -#define UNISURF_H - -#include - -extern int unisurf_sinit (union unirandominfo *, char const *) ; -extern unsigned int unisurf_read (union unirandominfo *, char *, unsigned int) ; -extern int unisurf_finish (union unirandominfo *) ; - -extern int unisurf_init (union unirandominfo *) ; - -#define UNIRANDOM_REGISTER_SURF() { .readb = &unisurf_read, .readnb = &unisurf_read, .init = &unisurf_init, .finish = &unisurf_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } - -#endif -- cgit v1.2.3