aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2017-01-26 15:13:50 +0900
committerKazuki Yamaguchi <k@rhe.jp>2017-02-14 22:58:33 +0900
commit13d973b32258532f933745ed9741cabd3e80ba4b (patch)
tree8ff7b67ebf137b3d17fd8ea79f72f8c8242bb73e
parent732318548f7c0e58f48c1baed3ed63b49a23e121 (diff)
downloadruby-openssl-ky/rand-poll.tar.gz
random: add OpenSSL::Random.pollky/rand-poll
As a direct wrapper of RAND_poll() which seeds OpenSSL's CSPRNG using a system-provided entropy source.
-rw-r--r--ext/openssl/ossl_rand.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/ext/openssl/ossl_rand.c b/ext/openssl/ossl_rand.c
index 688c525a..b9b2ba97 100644
--- a/ext/openssl/ossl_rand.c
+++ b/ext/openssl/ossl_rand.c
@@ -60,6 +60,24 @@ ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
/*
* call-seq:
+ * poll -> self
+ *
+ * Seeds the random number generator using a system-provided entropy source.
+ *
+ * This is automatically called through OpenSSL::Random.random_bytes on the
+ * first call, but manual reseeding is required after forking.
+ */
+static VALUE
+ossl_rand_poll(VALUE self)
+{
+ if (!RAND_poll())
+ ossl_raise(eRandomError, "RAND_poll");
+
+ return self;
+}
+
+/*
+ * call-seq:
* load_random_file(filename) -> true
*
* Reads bytes from +filename+ and adds them to the PRNG.
@@ -224,6 +242,7 @@ Init_ossl_rand(void)
rb_define_module_function(mRandom, "seed", ossl_rand_seed, 1);
rb_define_module_function(mRandom, "random_add", ossl_rand_add, 2);
+ rb_define_module_function(mRandom, "poll", ossl_rand_poll, 0);
rb_define_module_function(mRandom, "load_random_file", ossl_rand_load_file, 1);
rb_define_module_function(mRandom, "write_random_file", ossl_rand_write_file, 1);
rb_define_module_function(mRandom, "random_bytes", ossl_rand_bytes, 1);