Ruby  2.0.0p648(2015-12-16revision53162)
ossl_rand.c
Go to the documentation of this file.
1 /*
2  * $Id: ossl_rand.c 31166 2011-03-24 07:29:21Z naruse $
3  * 'OpenSSL for Ruby' project
4  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5  * All rights reserved.
6  */
7 /*
8  * This program is licenced under the same licence as Ruby.
9  * (See the file 'LICENCE'.)
10  */
11 #include "ossl.h"
12 
13 /*
14  * Classes
15  */
18 
19 /*
20  * Struct
21  */
22 
23 /*
24  * Public
25  */
26 
27 /*
28  * Private
29  */
30 
31 /*
32  * call-seq:
33  * seed(str) -> str
34  *
35  */
36 static VALUE
38 {
39  StringValue(str);
40  RAND_seed(RSTRING_PTR(str), RSTRING_LENINT(str));
41 
42  return str;
43 }
44 
45 /*
46  * call-seq:
47  * add(str, entropy) -> self
48  *
49  */
50 static VALUE
51 ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
52 {
53  StringValue(str);
54  RAND_add(RSTRING_PTR(str), RSTRING_LENINT(str), NUM2DBL(entropy));
55 
56  return self;
57 }
58 
59 /*
60  * call-seq:
61  * load_random_file(filename) -> true
62  *
63  */
64 static VALUE
66 {
67  SafeStringValue(filename);
68 
69  if(!RAND_load_file(RSTRING_PTR(filename), -1)) {
71  }
72  return Qtrue;
73 }
74 
75 /*
76  * call-seq:
77  * write_random_file(filename) -> true
78  *
79  */
80 static VALUE
82 {
83  SafeStringValue(filename);
84  if (RAND_write_file(RSTRING_PTR(filename)) == -1) {
86  }
87  return Qtrue;
88 }
89 
90 /*
91  * call-seq:
92  * random_bytes(length) -> aString
93  *
94  */
95 static VALUE
97 {
98  VALUE str;
99  int n = NUM2INT(len);
100 
101  str = rb_str_new(0, n);
102  if (!RAND_bytes((unsigned char *)RSTRING_PTR(str), n)) {
104  }
105 
106  return str;
107 }
108 
109 /*
110  * call-seq:
111  * pseudo_bytes(length) -> aString
112  *
113  */
114 static VALUE
116 {
117  VALUE str;
118  int n = NUM2INT(len);
119 
120  str = rb_str_new(0, n);
121  if (!RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str), n)) {
123  }
124 
125  return str;
126 }
127 
128 /*
129  * call-seq:
130  * egd(filename) -> true
131  *
132  */
133 static VALUE
134 ossl_rand_egd(VALUE self, VALUE filename)
135 {
136  SafeStringValue(filename);
137 
138  if(!RAND_egd(RSTRING_PTR(filename))) {
140  }
141  return Qtrue;
142 }
143 
144 /*
145  * call-seq:
146  * egd_bytes(filename, length) -> true
147  *
148  */
149 static VALUE
150 ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
151 {
152  int n = NUM2INT(len);
153 
154  SafeStringValue(filename);
155 
156  if (!RAND_egd_bytes(RSTRING_PTR(filename), n)) {
158  }
159  return Qtrue;
160 }
161 
162 /*
163  * call-seq:
164  * status? => true | false
165  *
166  * Return true if the PRNG has been seeded with enough data, false otherwise.
167  */
168 static VALUE
170 {
171  return RAND_status() ? Qtrue : Qfalse;
172 }
173 
174 #define DEFMETH(class, name, func, argc) \
175  rb_define_method((class), (name), (func), (argc)); \
176  rb_define_singleton_method((class), (name), (func), (argc));
177 
178 /*
179  * INIT
180  */
181 void
183 {
184 #if 0
185  mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
186 #endif
187 
188  mRandom = rb_define_module_under(mOSSL, "Random");
189 
191 
192  DEFMETH(mRandom, "seed", ossl_rand_seed, 1);
193  DEFMETH(mRandom, "random_add", ossl_rand_add, 2);
194  DEFMETH(mRandom, "load_random_file", ossl_rand_load_file, 1);
195  DEFMETH(mRandom, "write_random_file", ossl_rand_write_file, 1);
196  DEFMETH(mRandom, "random_bytes", ossl_rand_bytes, 1);
197  DEFMETH(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1);
198  DEFMETH(mRandom, "egd", ossl_rand_egd, 1);
199  DEFMETH(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2);
200  DEFMETH(mRandom, "status?", ossl_rand_status, 0)
201 }
202 
VALUE mOSSL
Definition: ossl.c:259
static VALUE ossl_rand_pseudo_bytes(VALUE self, VALUE len)
Definition: ossl_rand.c:115
#define NUM2INT(x)
Definition: ruby.h:622
#define Qtrue
Definition: ruby.h:434
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:534
#define NUM2DBL(x)
Definition: ruby.h:675
static VALUE ossl_rand_seed(VALUE self, VALUE str)
Definition: ossl_rand.c:37
VALUE eOSSLError
Definition: ossl.c:264
#define Qfalse
Definition: ruby.h:433
void Init_ossl_rand()
Definition: ossl_rand.c:182
static VALUE ossl_rand_bytes(VALUE self, VALUE len)
Definition: ossl_rand.c:96
static VALUE ossl_rand_status(VALUE self)
Definition: ossl_rand.c:169
static VALUE ossl_rand_load_file(VALUE self, VALUE filename)
Definition: ossl_rand.c:65
static VALUE ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
Definition: ossl_rand.c:51
static VALUE ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
Definition: ossl_rand.c:150
unsigned long VALUE
Definition: ruby.h:104
static VALUE ossl_rand_write_file(VALUE self, VALUE filename)
Definition: ossl_rand.c:81
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:626
#define RSTRING_PTR(str)
Definition: ruby.h:866
VALUE eRandomError
Definition: ossl_rand.c:17
VALUE mRandom
Definition: ossl_rand.c:16
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:333
#define SafeStringValue(v)
Definition: ruby.h:552
#define RSTRING_LENINT(str)
Definition: ruby.h:874
static VALUE ossl_rand_egd(VALUE self, VALUE filename)
Definition: ossl_rand.c:134
VALUE rb_define_module(const char *name)
Definition: class.c:606
#define NULL
Definition: _sdbm.c:102
#define DEFMETH(class, name, func, argc)
Definition: ossl_rand.c:174
#define StringValue(v)
Definition: ruby.h:546
VALUE rb_str_new(const char *, long)
Definition: string.c:425