summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-07 06:25:55 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-07 06:25:55 (GMT)
commite96955e8f63be2ae5a4999487c00c3237f922371 (patch)
treeb701d924fe8ac49be991651191c76850a243d90f
parent8a0ab36db160331190299a5b6e5483b8b47e98e2 (diff)
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
-rw-r--r--random.c47
1 files changed, 32 insertions, 15 deletions
diff --git a/random.c b/random.c
index 344798a..a5a6362 100644
--- a/random.c
+++ b/random.c
@@ -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
-
+#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 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])