Skip to content

Commit 49218a6

Browse files
committed
plugins: add support for hwrng and check if seed file is empty
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent 90a4df8 commit 49218a6

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

plugins/urandom.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static void setup(void *arg)
4747
#ifdef RANDOMSEED
4848
struct rand_pool_info *rpi;
4949
ssize_t len = 0;
50+
struct stat st;
5051
int rc = -1;
5152
int fd, err;
5253

@@ -55,7 +56,7 @@ static void setup(void *arg)
5556
return;
5657
}
5758

58-
if (!fexist(RANDOMSEED)) {
59+
if (!stat(RANDOMSEED, &st) || st.st_size < 512) {
5960
int ret = 1;
6061
mode_t prev;
6162
FILE *fp;
@@ -64,16 +65,34 @@ static void setup(void *arg)
6465
prev = umask(077);
6566
fp = fopen(RANDOMSEED, "w");
6667
if (fp) {
67-
int iter = 128;
68-
struct timeval tv;
69-
70-
gettimeofday(&tv, NULL);
71-
srandom(tv.tv_sec % 3600);
72-
while (iter--) {
73-
uint32_t i, prng = random();
74-
75-
for (i = 0; i < sizeof(prng); i++)
76-
fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp);
68+
const char *hwrng = "/dev/hwrng";
69+
FILE *hw;
70+
71+
hw = fopen(hwrng, "r");
72+
if (hw) {
73+
char buf[512];
74+
size_t len;
75+
76+
len = fread(buf, sizeof(buf[0]), sizeof(buf), hw);
77+
if (len == 0) {
78+
fclose(hw);
79+
goto no_hwrng;
80+
}
81+
82+
len = fwrite(buf, sizeof(buf[0]), len, fp);
83+
fclose(hw);
84+
} else {
85+
struct timeval tv;
86+
int iter = 128;
87+
no_hwrng:
88+
gettimeofday(&tv, NULL);
89+
srandom(tv.tv_sec % 3600);
90+
while (iter--) {
91+
uint32_t i, prng = random();
92+
93+
for (i = 0; i < sizeof(prng); i++)
94+
fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp);
95+
}
7796
}
7897
ret = fclose(fp);
7998
}

0 commit comments

Comments
 (0)