summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
Diffstat (limited to 'random.c')
-rw-r--r--random.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/random.c b/random.c
index 28b7a9f..7b78efe 100644
--- a/random.c
+++ b/random.c
@@ -49,7 +49,7 @@
# include <sys/param.h>
#endif
-#if defined HAVE_GETRANDOM
+#if defined HAVE_GETRANDOM || defined HAVE_GETENTROPY
# include <sys/random.h>
#elif defined __linux__ && defined __NR_getrandom
# include <linux/random.h>
@@ -425,7 +425,23 @@ random_init(int argc, VALUE *argv, VALUE obj)
# define USE_DEV_URANDOM 0
#endif
-#if USE_DEV_URANDOM
+#if HAVE_GETENTROPY
+# define MAX_SEED_LEN_PER_READ 256
+static int
+fill_random_bytes_urandom(void *seed, size_t size)
+{
+ unsigned char *p = (unsigned char *)seed;
+ while (size) {
+ size_t len = size < MAX_SEED_LEN_PER_READ ? size : MAX_SEED_LEN_PER_READ;
+ if (getentropy(p, len) != 0) {
+ return -1;
+ }
+ p += len;
+ size -= len;
+ }
+ return 0;
+}
+#elif USE_DEV_URANDOM
static int
fill_random_bytes_urandom(void *seed, size_t size)
{