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
|
#define CONFIG_HAS_ARC4RANDOM_BUF 0
|
||||||
#endif
|
#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.
|
// GLIBC before 2.25.
|
||||||
#if defined(__linux__) && \
|
#if defined(__linux__) && \
|
||||||
(!defined(__GLIBC__) || \
|
(!defined(__GLIBC__) || \
|
||||||
__GLIBC__ > 2 || \
|
__GLIBC__ > 2 || \
|
||||||
(__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
|
(__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
|
||||||
#define CONFIG_HAS_GETENTROPY 1
|
#define CONFIG_HAS_GETRANDOM 1
|
||||||
#else
|
#else
|
||||||
#define CONFIG_HAS_GETENTROPY 0
|
#define CONFIG_HAS_GETRANDOM 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__CloudABI__)
|
#if defined(__CloudABI__)
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
@@ -25,10 +27,24 @@ void random_buf(void *buf, size_t len) {
|
|||||||
arc4random_buf(buf, 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) {
|
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
|
#else
|
||||||
|
|||||||
Reference in New Issue
Block a user