Use getrandom rather than getentropy on Linux for random_get.
getentropy is limited to 256 bytes, so switch to getrandom.
This commit is contained in:
@@ -20,15 +20,15 @@
|
||||
#define CONFIG_HAS_ARC4RANDOM_BUF 0
|
||||
#endif
|
||||
|
||||
// On Linux, prefer to use getentropy, though it isn't available in
|
||||
// On Linux, prefer to use getrandom, though it isn't available in
|
||||
// GLIBC before 2.25.
|
||||
#if defined(__linux__) && \
|
||||
(!defined(__GLIBC__) || \
|
||||
__GLIBC__ > 2 || \
|
||||
(__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
|
||||
#define CONFIG_HAS_GETENTROPY 1
|
||||
#define CONFIG_HAS_GETRANDOM 1
|
||||
#else
|
||||
#define CONFIG_HAS_GETENTROPY 0
|
||||
#define CONFIG_HAS_GETRANDOM 0
|
||||
#endif
|
||||
|
||||
#if defined(__CloudABI__)
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "random.h"
|
||||
@@ -25,10 +27,24 @@ void random_buf(void *buf, size_t len) {
|
||||
arc4random_buf(buf, len);
|
||||
}
|
||||
|
||||
#elif CONFIG_HAS_GETENTROPY
|
||||
#elif CONFIG_HAS_GETRANDOM
|
||||
|
||||
#include <sys/random.h>
|
||||
|
||||
void random_buf(void *buf, size_t len) {
|
||||
getentropy(buf, len);
|
||||
for (;;) {
|
||||
ssize_t x = getrandom(buf, len, 0);
|
||||
if (x < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
fprintf(stderr, "getrandom failed: %s", strerror(errno));
|
||||
abort();
|
||||
}
|
||||
if (x == len)
|
||||
return;
|
||||
buf = (void *)((unsigned char *)buf + x);
|
||||
len -= x;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user