2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is licenced under the same licence as Ruby.
|
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE mRandom;
|
|
|
|
VALUE eRandomError;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Struct
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
2007-12-25 06:31:51 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* seed(str) -> str
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_seed(VALUE self, VALUE str)
|
|
|
|
{
|
|
|
|
StringValue(str);
|
2006-08-31 06:30:33 -04:00
|
|
|
RAND_seed(RSTRING_PTR(str), RSTRING_LEN(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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
|
|
|
|
{
|
|
|
|
StringValue(str);
|
|
|
|
RAND_add(RSTRING_PTR(str), RSTRING_LEN(str), NUM2DBL(entropy));
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* load_random_file(filename) -> true
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_load_file(VALUE self, VALUE filename)
|
|
|
|
{
|
|
|
|
SafeStringValue(filename);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
if(!RAND_load_file(RSTRING_PTR(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
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_write_file(VALUE self, VALUE filename)
|
|
|
|
{
|
|
|
|
SafeStringValue(filename);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RAND_write_file(RSTRING_PTR(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:
|
|
|
|
* random_bytes(length) -> aString
|
|
|
|
*
|
|
|
|
*/
|
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);
|
2008-05-29 13:41:56 -04:00
|
|
|
|
|
|
|
str = rb_str_new(0, n);
|
2008-07-22 11:34:23 -04:00
|
|
|
if (!RAND_bytes((unsigned char *)RSTRING_PTR(str), n)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* pseudo_bytes(length) -> aString
|
|
|
|
*
|
|
|
|
*/
|
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);
|
2008-07-22 11:34:23 -04:00
|
|
|
if (!RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str), n)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* egd(filename) -> true
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_egd(VALUE self, VALUE filename)
|
|
|
|
{
|
|
|
|
SafeStringValue(filename);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
if(!RAND_egd(RSTRING_PTR(filename))) {
|
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
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
|
|
|
|
{
|
2008-05-29 13:41:56 -04:00
|
|
|
long n = NUM2INT(len);
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
SafeStringValue(filename);
|
|
|
|
|
2008-05-29 13:41:56 -04:00
|
|
|
if (!RAND_egd_bytes(RSTRING_PTR(filename), n)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eRandomError, NULL);
|
|
|
|
}
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
#define DEFMETH(class, name, func, argc) \
|
|
|
|
rb_define_method(class, name, func, argc); \
|
|
|
|
rb_define_singleton_method(class, name, func, argc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Init_ossl_rand()
|
|
|
|
{
|
2007-03-11 22:01:19 -04:00
|
|
|
#if 0 /* let rdoc know about mOSSL */
|
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
#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
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
DEFMETH(mRandom, "seed", ossl_rand_seed, 1);
|
2007-12-25 06:31:51 -05:00
|
|
|
DEFMETH(mRandom, "random_add", ossl_rand_add, 2);
|
2003-07-23 12:12:24 -04:00
|
|
|
DEFMETH(mRandom, "load_random_file", ossl_rand_load_file, 1);
|
|
|
|
DEFMETH(mRandom, "write_random_file", ossl_rand_write_file, 1);
|
|
|
|
DEFMETH(mRandom, "random_bytes", ossl_rand_bytes, 1);
|
|
|
|
DEFMETH(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1);
|
|
|
|
DEFMETH(mRandom, "egd", ossl_rand_egd, 1);
|
2010-04-22 04:21:01 -04:00
|
|
|
DEFMETH(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2);
|
2007-06-18 04:56:21 -04:00
|
|
|
DEFMETH(mRandom, "status?", ossl_rand_status, 0)
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|