2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
2014-10-03 20:01:07 -04:00
|
|
|
*
|
2003-07-23 12:12:24 -04:00
|
|
|
* All rights reserved.
|
2014-10-03 20:01:07 -04:00
|
|
|
*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 12:12:24 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
VALUE mRandom;
|
|
|
|
VALUE eRandomError;
|
|
|
|
|
2007-12-25 06:31:51 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* seed(str) -> str
|
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* ::seed is equivalent to ::add where +entropy+ is length of +str+.
|
2007-12-25 06:31:51 -05:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_seed(VALUE self, VALUE str)
|
|
|
|
{
|
|
|
|
StringValue(str);
|
2011-03-24 03:29:21 -04:00
|
|
|
RAND_seed(RSTRING_PTR(str), RSTRING_LENINT(str));
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-12-25 06:31:51 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* add(str, entropy) -> self
|
|
|
|
*
|
2014-10-03 20:01:07 -04:00
|
|
|
* Mixes the bytes from +str+ into the Pseudo Random Number Generator(PRNG)
|
|
|
|
* state.
|
|
|
|
*
|
|
|
|
* Thus, if the data from +str+ are unpredictable to an adversary, this
|
|
|
|
* increases the uncertainty about the state and makes the PRNG output less
|
|
|
|
* predictable.
|
|
|
|
*
|
|
|
|
* The +entropy+ argument is (the lower bound of) an estimate of how much
|
|
|
|
* randomness is contained in +str+, measured in bytes.
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
2016-08-29 01:47:09 -04:00
|
|
|
* === Example
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
|
|
|
* pid = $$
|
|
|
|
* now = Time.now
|
|
|
|
* ary = [now.to_i, now.nsec, 1000, pid]
|
2016-08-29 01:47:09 -04:00
|
|
|
* OpenSSL::Random.add(ary.join, 0.0)
|
|
|
|
* OpenSSL::Random.seed(ary.join)
|
2007-12-25 06:31:51 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
|
|
|
|
{
|
|
|
|
StringValue(str);
|
2011-03-24 03:29:21 -04:00
|
|
|
RAND_add(RSTRING_PTR(str), RSTRING_LENINT(str), NUM2DBL(entropy));
|
2007-12-25 06:31:51 -05:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* load_random_file(filename) -> true
|
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* Reads bytes from +filename+ and adds them to the PRNG.
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_load_file(VALUE self, VALUE filename)
|
|
|
|
{
|
2016-05-23 07:40:07 -04:00
|
|
|
rb_check_safe_obj(filename);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
if(!RAND_load_file(StringValueCStr(filename), -1)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* write_random_file(filename) -> true
|
|
|
|
*
|
2014-10-03 20:01:07 -04:00
|
|
|
* Writes a number of random generated bytes (currently 1024) to +filename+
|
|
|
|
* which can be used to initialize the PRNG by calling ::load_random_file in a
|
|
|
|
* later session.
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_write_file(VALUE self, VALUE filename)
|
|
|
|
{
|
2016-05-23 07:40:07 -04:00
|
|
|
rb_check_safe_obj(filename);
|
|
|
|
|
|
|
|
if (RAND_write_file(StringValueCStr(filename)) == -1) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-10-03 20:01:07 -04:00
|
|
|
* random_bytes(length) -> string
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
2014-10-03 20:01:07 -04:00
|
|
|
* Generates +string+ with +length+ number of cryptographically strong
|
|
|
|
* pseudo-random bytes.
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
2016-08-29 01:47:09 -04:00
|
|
|
* === Example
|
2007-03-29 13:29:03 -04:00
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* OpenSSL::Random.random_bytes(12)
|
2016-08-29 01:47:09 -04:00
|
|
|
* #=> "..."
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_bytes(VALUE self, VALUE len)
|
|
|
|
{
|
|
|
|
VALUE str;
|
2008-05-29 14:15:50 -04:00
|
|
|
int n = NUM2INT(len);
|
2015-11-30 15:29:02 -05:00
|
|
|
int ret;
|
2008-05-29 13:41:56 -04:00
|
|
|
|
|
|
|
str = rb_str_new(0, n);
|
2015-11-30 15:29:02 -05:00
|
|
|
ret = RAND_bytes((unsigned char *)RSTRING_PTR(str), n);
|
2016-05-23 06:47:37 -04:00
|
|
|
if (ret == 0) {
|
|
|
|
ossl_raise(eRandomError, "RAND_bytes");
|
2015-11-30 15:29:02 -05:00
|
|
|
} else if (ret == -1) {
|
|
|
|
ossl_raise(eRandomError, "RAND_bytes is not supported");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-06-05 08:38:34 -04:00
|
|
|
#if defined(HAVE_RAND_PSEUDO_BYTES)
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-10-03 20:01:07 -04:00
|
|
|
* pseudo_bytes(length) -> string
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
|
|
|
* Generates +string+ with +length+ number of pseudo-random bytes.
|
|
|
|
*
|
2014-10-03 20:01:07 -04:00
|
|
|
* Pseudo-random byte sequences generated by ::pseudo_bytes will be unique if
|
|
|
|
* they are of sufficient length, but are not necessarily unpredictable.
|
2014-10-03 19:51:09 -04:00
|
|
|
*
|
2016-08-29 01:47:09 -04:00
|
|
|
* === Example
|
2007-03-29 13:29:03 -04:00
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* OpenSSL::Random.pseudo_bytes(12)
|
2016-08-29 01:47:09 -04:00
|
|
|
* #=> "..."
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_pseudo_bytes(VALUE self, VALUE len)
|
|
|
|
{
|
|
|
|
VALUE str;
|
2008-05-29 14:15:50 -04:00
|
|
|
int n = NUM2INT(len);
|
2003-09-17 05:05:02 -04:00
|
|
|
|
2008-05-29 13:41:56 -04:00
|
|
|
str = rb_str_new(0, n);
|
2016-05-23 06:47:37 -04:00
|
|
|
if (RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str), n) < 1) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2016-06-05 08:38:34 -04:00
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2015-02-12 04:34:02 -05:00
|
|
|
#ifdef HAVE_RAND_EGD
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* egd(filename) -> true
|
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* Same as ::egd_bytes but queries 255 bytes by default.
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_egd(VALUE self, VALUE filename)
|
|
|
|
{
|
2016-05-23 07:40:07 -04:00
|
|
|
rb_check_safe_obj(filename);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
if (RAND_egd(StringValueCStr(filename)) == -1) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* egd_bytes(filename, length) -> true
|
|
|
|
*
|
2014-10-03 19:51:09 -04:00
|
|
|
* Queries the entropy gathering daemon EGD on socket path given by +filename+.
|
2014-10-03 20:01:07 -04:00
|
|
|
*
|
|
|
|
* Fetches +length+ number of bytes and uses ::add to seed the OpenSSL built-in
|
|
|
|
* PRNG.
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
|
|
|
|
{
|
2011-03-24 00:49:18 -04:00
|
|
|
int n = NUM2INT(len);
|
2008-05-29 13:41:56 -04:00
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
rb_check_safe_obj(filename);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
if (RAND_egd_bytes(StringValueCStr(filename), n) == -1) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2015-02-12 04:34:02 -05:00
|
|
|
#endif /* HAVE_RAND_EGD */
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-06-18 04:56:21 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* status? => true | false
|
|
|
|
*
|
|
|
|
* Return true if the PRNG has been seeded with enough data, false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_rand_status(VALUE self)
|
|
|
|
{
|
|
|
|
return RAND_status() ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_rand(void)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2010-12-05 19:54:44 -05:00
|
|
|
#if 0
|
2016-08-29 01:47:09 -04:00
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
2007-03-11 22:01:19 -04:00
|
|
|
#endif
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
mRandom = rb_define_module_under(mOSSL, "Random");
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
eRandomError = rb_define_class_under(mRandom, "RandomError", eOSSLError);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2014-10-03 19:24:50 -04:00
|
|
|
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, "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);
|
2016-06-05 08:38:34 -04:00
|
|
|
#if defined(HAVE_RAND_PSEUDO_BYTES)
|
2014-10-03 19:24:50 -04:00
|
|
|
rb_define_module_function(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1);
|
2016-06-05 08:38:34 -04:00
|
|
|
#endif
|
2015-02-12 04:34:02 -05:00
|
|
|
#ifdef HAVE_RAND_EGD
|
2014-10-03 19:24:50 -04:00
|
|
|
rb_define_module_function(mRandom, "egd", ossl_rand_egd, 1);
|
|
|
|
rb_define_module_function(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2);
|
2015-02-12 04:34:02 -05:00
|
|
|
#endif /* HAVE_RAND_EGD */
|
2014-10-03 19:24:50 -04:00
|
|
|
rb_define_module_function(mRandom, "status?", ossl_rand_status, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|