1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.
This commit is contained in:
David CARLIER 2021-01-16 12:47:33 +00:00 committed by Nobuyoshi Nakada
parent 8d099aa040
commit 54c91185c9
Notes: git 2021-01-17 18:49:18 +09:00
2 changed files with 19 additions and 2 deletions

View file

@ -1876,6 +1876,7 @@ AC_CHECK_FUNCS(ftruncate)
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)

View file

@ -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)
{