read rng seed from random number source device

Linux and BSD system have random number source devices that can be used
as source for a unguessable seed source.

Other platforms fall back to generate the seed with gettime().
This commit is contained in:
Patric Mueller
2019-01-28 10:01:45 +01:00
parent 1083971228
commit 86d694c61b
4 changed files with 56 additions and 13 deletions
+2 -1
View File
@@ -925,7 +925,8 @@ E char *FDECL(strstri, (const char *, const char *));
#endif #endif
E boolean E boolean
FDECL(fuzzymatch, (const char *, const char *, const char *, BOOLEAN_P)); FDECL(fuzzymatch, (const char *, const char *, const char *, BOOLEAN_P));
E void NDECL(setrandom); E void NDECL(init_random);
E void NDECL(reseed_random);
E time_t NDECL(getnow); E time_t NDECL(getnow);
E int NDECL(getyear); E int NDECL(getyear);
#if 0 #if 0
+13
View File
@@ -408,5 +408,18 @@
# define RUNTIME_PASTEBUF_SUPPORT # define RUNTIME_PASTEBUF_SUPPORT
#endif #endif
/*
* /dev/random is blocking on Linux, so there we default to /dev/urandom which
* should still be good enough.
* BSD systems usually have /dev/random that is supposed to be used.
*/
#ifdef LINUX
# define DEV_RANDOM "/dev/urandom"
#else
# ifdef BSD
# define DEV_RANDOM "/dev/random"
# endif
#endif
#endif /* UNIXCONF_H */ #endif /* UNIXCONF_H */
#endif /* UNIX */ #endif /* UNIX */
+32 -3
View File
@@ -848,10 +848,21 @@ extern struct tm *FDECL(localtime, (time_t *));
#endif #endif
STATIC_DCL struct tm *NDECL(getlt); STATIC_DCL struct tm *NDECL(getlt);
void /* Returns a number suitable as seed for the random number generator. */
setrandom() static unsigned long
get_random_seed()
{ {
unsigned long seed = (unsigned long) getnow(); /* time((TIME_type) 0) */ unsigned long seed = 0;
#ifdef DEV_RANDOM
FILE *fptr = NULL;
fptr = fopen(DEV_RANDOM, "r");
if (fptr) {
fread(&seed, sizeof(long), 1, fptr);
}
fclose(fptr);
#else
seed = (unsigned long) getnow(); /* time((TIME_type) 0) */
# if defined(UNIX) || defined(VMS) # if defined(UNIX) || defined(VMS)
{ {
@@ -865,7 +876,14 @@ setrandom()
} }
} }
# endif # endif
#endif
return seed;
}
/* Sets the seed for the random number generator */
static void
set_random(unsigned long seed)
{
/* the types are different enough here that sweeping the different /* the types are different enough here that sweeping the different
* routine names into one via #defines is even more confusing * routine names into one via #defines is even more confusing
*/ */
@@ -888,6 +906,17 @@ setrandom()
#endif #endif
} }
/*
* Initializes the random number generator.
* Only call once.
*/
void
init_random()
{
unsigned long seed = get_random_seed();
set_random(seed);
}
time_t time_t
getnow() getnow()
{ {
+1 -1
View File
@@ -692,7 +692,7 @@ initoptions_init()
reset_commands(TRUE); /* init */ reset_commands(TRUE); /* init */
/* initialize the random number generator */ /* initialize the random number generator */
setrandom(); init_random();
/* for detection of configfile options specified multiple times */ /* for detection of configfile options specified multiple times */
iflags.opt_booldup = iflags.opt_compdup = (int *) 0; iflags.opt_booldup = iflags.opt_compdup = (int *) 0;