}
void random_bytes(void *p, size_t n) {
- static bool have_grndinsecure = true;
-
assert(p || n == 0);
if (n == 0)
for (;;) {
ssize_t l;
- l = getrandom(p, n, have_grndinsecure ? GRND_INSECURE : GRND_NONBLOCK);
- if (l < 0 && errno == EINVAL && have_grndinsecure) {
- /* No GRND_INSECURE; fallback to GRND_NONBLOCK. */
- have_grndinsecure = false;
- continue;
- }
+ l = getrandom(p, n, GRND_INSECURE);
if (l <= 0)
- break; /* Will block (with GRND_NONBLOCK), or unexpected error. Give up and fallback
- to /dev/urandom. */
+ break; /* Unexpected error. Give up and fallback to /dev/urandom. */
if ((size_t) l == n)
return; /* Done reading, success. */