[前][次][番号順一覧][スレッド一覧]

ruby-changes:64918

From: David <ko1@a...>
Date: Sun, 17 Jan 2021 18:49:14 +0900 (JST)
Subject: [ruby-changes:64918] 54c91185c9 (master): random generator update for Mac proposal

https://git.ruby-lang.org/ruby.git/commit/?id=54c91185c9

From 54c91185c9273b9699693910fa95383c86f2af22 Mon Sep 17 00:00:00 2001
From: David CARLIER <devnexen@g...>
Date: Sat, 16 Jan 2021 12:47:33 +0000
Subject: random generator update for Mac proposal

using getentropy for seeding, reading 256 bytes at a time to avoid
 the EIO errno since this is the maximum.

diff --git a/configure.ac b/configure.ac
index 3d1c5b6..f672f6d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1876,6 +1876,7 @@ AC_CHECK_FUNCS(ftruncate) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1876
 AC_CHECK_FUNCS(ftruncate64)		# used for Win32 platform
 AC_CHECK_FUNCS(getattrlist)
 AC_CHECK_FUNCS(getcwd)
+AC_CHECK_FUNCS(getentropy)
 AC_CHECK_FUNCS(getgidx)
 AC_CHECK_FUNCS(getgrnam)
 AC_CHECK_FUNCS(getgrnam_r)
diff --git a/random.c b/random.c
index 28b7a9f..7b78efe 100644
--- a/random.c
+++ b/random.c
@@ -49,7 +49,7 @@ https://github.com/ruby/ruby/blob/trunk/random.c#L49
 # 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) https://github.com/ruby/ruby/blob/trunk/random.c#L425
 # 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)
 {
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]