libmisc: use /dev/urandom as a generic fallback for read_random_bytes()
On systems with Linux kernel < 3.17, getentropy() and getrandom() may exist but return ENOSYS. Use /dev/urandom as a fallback to avoid a hard requirement on Linux kernel version. Fixes #512. Signed-off-by: Xi Ruoyao <xry111@xry111.site>
This commit is contained in:
parent
6688f1c1eb
commit
274e786be9
@ -156,24 +156,23 @@ static long read_random_bytes (void)
|
|||||||
/* arc4random_buf, if it exists, can never fail. */
|
/* arc4random_buf, if it exists, can never fail. */
|
||||||
arc4random_buf (&randval, sizeof (randval));
|
arc4random_buf (&randval, sizeof (randval));
|
||||||
goto end;
|
goto end;
|
||||||
|
#endif
|
||||||
|
|
||||||
#elif defined(HAVE_GETENTROPY)
|
#ifdef HAVE_GETENTROPY
|
||||||
/* getentropy may exist but lack kernel support. */
|
/* getentropy may exist but lack kernel support. */
|
||||||
if (getentropy (&randval, sizeof (randval))) {
|
if (getentropy (&randval, sizeof (randval)) == 0) {
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
goto end;
|
goto end;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#elif defined(HAVE_GETRANDOM)
|
#ifdef HAVE_GETRANDOM
|
||||||
/* Likewise getrandom. */
|
/* Likewise getrandom. */
|
||||||
if ((size_t) getrandom (&randval, sizeof (randval), 0) != sizeof (randval)) {
|
if ((size_t) getrandom (&randval, sizeof (randval), 0) == sizeof (randval)) {
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
goto end;
|
goto end;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#else
|
/* Use /dev/urandom as a last resort. */
|
||||||
FILE *f = fopen ("/dev/urandom", "r");
|
FILE *f = fopen ("/dev/urandom", "r");
|
||||||
if (NULL == f) {
|
if (NULL == f) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@ -186,7 +185,6 @@ static long read_random_bytes (void)
|
|||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
goto end;
|
goto end;
|
||||||
#endif
|
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
fprintf (log_get_logfd(),
|
fprintf (log_get_logfd(),
|
||||||
|
Loading…
Reference in New Issue
Block a user