Skip to content

Commit

Permalink
plugins: add support for hwrng and check if seed file is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Mar 12, 2024
1 parent 90a4df8 commit 49218a6
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions plugins/urandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static void setup(void *arg)
#ifdef RANDOMSEED
struct rand_pool_info *rpi;
ssize_t len = 0;
struct stat st;
int rc = -1;
int fd, err;

Expand All @@ -55,7 +56,7 @@ static void setup(void *arg)
return;
}

if (!fexist(RANDOMSEED)) {
if (!stat(RANDOMSEED, &st) || st.st_size < 512) {
int ret = 1;
mode_t prev;
FILE *fp;
Expand All @@ -64,16 +65,34 @@ static void setup(void *arg)
prev = umask(077);
fp = fopen(RANDOMSEED, "w");
if (fp) {
int iter = 128;
struct timeval tv;

gettimeofday(&tv, NULL);
srandom(tv.tv_sec % 3600);
while (iter--) {
uint32_t i, prng = random();

for (i = 0; i < sizeof(prng); i++)
fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp);
const char *hwrng = "/dev/hwrng";
FILE *hw;

hw = fopen(hwrng, "r");
if (hw) {
char buf[512];
size_t len;

len = fread(buf, sizeof(buf[0]), sizeof(buf), hw);
if (len == 0) {
fclose(hw);
goto no_hwrng;
}

len = fwrite(buf, sizeof(buf[0]), len, fp);
fclose(hw);
} else {
struct timeval tv;
int iter = 128;
no_hwrng:
gettimeofday(&tv, NULL);
srandom(tv.tv_sec % 3600);
while (iter--) {
uint32_t i, prng = random();

for (i = 0; i < sizeof(prng); i++)
fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp);
}
}
ret = fclose(fp);
}
Expand Down

0 comments on commit 49218a6

Please sign in to comment.