Skip to content

Commit

Permalink
plugins: urandom: improve entropy gathering in fallback path
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
troglobit committed Feb 23, 2025
1 parent 1f2c1d2 commit b75b107
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion plugins/urandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/stat.h>
#include <sys/time.h> /* gettimeofday() */
#include <sys/types.h>
#include <sys/resource.h> /* getrusage() */
#ifdef _LIBITE_LITE
# include <libite/lite.h>
#else
Expand All @@ -43,13 +44,27 @@
#endif

#ifdef RANDOMSEED
/*
* This is the fallback seed function, please make sure you have drivers
* enabling /dev/hwrng instead.
*/
static void fallback(FILE *fp)
{
unsigned long seed;
struct timeval tv;
struct rusage ru;
int iter = 128;

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--) {
uint32_t i, prng = random();

Expand Down

0 comments on commit b75b107

Please sign in to comment.