ruby-changes:39100
From: nobu <ko1@a...>
Date: Tue, 7 Jul 2015 15:26:11 +0900 (JST)
Subject: [ruby-changes:39100] nobu:r51181 (trunk): random.c: separate fill_random_bytes
nobu 2015-07-07 15:25:55 +0900 (Tue, 07 Jul 2015) New Revision: 51181 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51181 Log: random.c: separate fill_random_bytes * random.c (fill_random_bytes): separate into functions by system call and by direct read of urandom device. Modified files: trunk/random.c Index: random.c =================================================================== --- random.c (revision 51180) +++ random.c (revision 51181) @@ -445,21 +445,10 @@ random_init(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/random.c#L445 # 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 - +#if USE_DEV_URANDOM static int -fill_random_bytes(void *seed, size_t size) +fill_random_bytes_urandom(void *seed, size_t size) { -#if USE_DEV_URANDOM int fd = rb_cloexec_open("/dev/urandom", # ifdef O_NONBLOCK O_NONBLOCK| @@ -478,7 +467,25 @@ fill_random_bytes(void *seed, size_t siz https://github.com/ruby/ruby/blob/trunk/random.c#L467 } 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 siz https://github.com/ruby/ruby/blob/trunk/random.c#L507 } 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]) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/