mirror of
https://github.com/troglobit/finit.git
synced 2026-11-07 16:01:08 +00:00
plugins: urandom: improve entropy gathering in fallback path
Enhance fallback entropy generation by mixing in multiple sources: runtime statistics, PID, and high-resolution timestamps. This provides better initialization for the PRNG when neither saved entropy nor hardware RNG are available during early boot. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 file changed
+16
-1
+16
-1
@@ -26,6 +26,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/time.h> /* gettimeofday() */
|
#include <sys/time.h> /* gettimeofday() */
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/resource.h> /* getrusage() */
|
||||||
#ifdef _LIBITE_LITE
|
#ifdef _LIBITE_LITE
|
||||||
# include <libite/lite.h>
|
# include <libite/lite.h>
|
||||||
#else
|
#else
|
||||||
@@ -43,13 +44,27 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef RANDOMSEED
|
#ifdef RANDOMSEED
|
||||||
|
/*
|
||||||
|
* This is the fallback seed function, please make sure you have drivers
|
||||||
|
* enabling /dev/hwrng instead.
|
||||||
|
*/
|
||||||
static void fallback(FILE *fp)
|
static void fallback(FILE *fp)
|
||||||
{
|
{
|
||||||
|
unsigned long seed;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
struct rusage ru;
|
||||||
int iter = 128;
|
int iter = 128;
|
||||||
|
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
srandom(tv.tv_sec % 3600);
|
getrusage(RUSAGE_SELF, &ru);
|
||||||
|
|
||||||
|
/* Mix multiple sources of "randomness" */
|
||||||
|
seed = tv.tv_sec ^ (tv.tv_usec << 16);
|
||||||
|
seed ^= ru.ru_utime.tv_sec ^ (ru.ru_utime.tv_usec << 16);
|
||||||
|
seed ^= ru.ru_stime.tv_sec ^ (ru.ru_stime.tv_usec << 16);
|
||||||
|
seed ^= getpid() << 8;
|
||||||
|
|
||||||
|
srandom(seed);
|
||||||
while (iter--) {
|
while (iter--) {
|
||||||
uint32_t i, prng = random();
|
uint32_t i, prng = random();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user