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

random.c: separate fill_random_bytes

* random.c (fill_random_bytes): separate into functions by system
  call and by direct read of urandom device.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51181 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-07-07 06:25:55 +00:00
parent 8a0ab36db1
commit e96955e8f6

View file

@ -445,21 +445,10 @@ random_init(int argc, VALUE *argv, VALUE obj)
# define USE_DEV_URANDOM 0
#endif
#if defined(_WIN32)
static void
release_crypt(void *p)
{
HCRYPTPROV prov = (HCRYPTPROV)ATOMIC_PTR_EXCHANGE(*(HCRYPTPROV *)p, INVALID_HANDLE_VALUE);
if (prov && prov != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
CryptReleaseContext(prov, 0);
}
}
#endif
static int
fill_random_bytes(void *seed, size_t size)
{
#if USE_DEV_URANDOM
static int
fill_random_bytes_urandom(void *seed, size_t size)
{
int fd = rb_cloexec_open("/dev/urandom",
# ifdef O_NONBLOCK
O_NONBLOCK|
@ -478,7 +467,25 @@ fill_random_bytes(void *seed, size_t size)
}
close(fd);
if (ret < 0 || (size_t)ret < size) return -1;
#elif defined(_WIN32)
return 0;
}
#else
# define fill_random_bytes_urandom(seed, size) -1
#endif
#if defined(_WIN32)
static void
release_crypt(void *p)
{
HCRYPTPROV prov = (HCRYPTPROV)ATOMIC_PTR_EXCHANGE(*(HCRYPTPROV *)p, INVALID_HANDLE_VALUE);
if (prov && prov != (HCRYPTPROV)INVALID_HANDLE_VALUE) {
CryptReleaseContext(prov, 0);
}
}
static int
fill_random_bytes_syscall(void *seed, size_t size)
{
static HCRYPTPROV perm_prov;
HCRYPTPROV prov = perm_prov, old_prov;
if (!prov) {
@ -500,9 +507,19 @@ fill_random_bytes(void *seed, size_t size)
}
if (prov == (HCRYPTPROV)INVALID_HANDLE_VALUE) return -1;
CryptGenRandom(prov, size, seed);
#endif
return 0;
}
#else
# define fill_random_bytes_syscall(seed, size) -1
#endif
static int
fill_random_bytes(void *seed, size_t size)
{
int ret = fill_random_bytes_syscall(seed, size);
if (ret) return ret;
return fill_random_bytes_urandom(seed, size);
}
static void
fill_random_seed(uint32_t seed[DEFAULT_SEED_CNT])