summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_rand.c
blob: ec9883d70a4ec3d9afe2802f10cc3151217b079c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * $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
 */
static VALUE
ossl_rand_seed(VALUE self, VALUE str)
{
    StringValue(str);
    RAND_seed(RSTRING(str)->ptr, RSTRING(str)->len);

    return str;
}

static VALUE
ossl_rand_load_file(VALUE self, VALUE filename)
{
    SafeStringValue(filename);
	
    if(!RAND_load_file(RSTRING(filename)->ptr, -1)) {
	ossl_raise(eRandomError, NULL);
    }
    return Qtrue;
}

static VALUE
ossl_rand_write_file(VALUE self, VALUE filename)
{
    SafeStringValue(filename);
    if (RAND_write_file(RSTRING(filename)->ptr) == -1) {
	ossl_raise(eRandomError, NULL);
    }
    return Qtrue;
}

static VALUE
ossl_rand_bytes(VALUE self, VALUE len)
{
    VALUE str;
	
    str = rb_str_new(0, FIX2INT(len));
    if (!RAND_bytes(RSTRING(str)->ptr, FIX2INT(len))) {
	ossl_raise(eRandomError, NULL);
    }

    return str;
}

static VALUE
ossl_rand_pseudo_bytes(VALUE self, VALUE len)
{
    VALUE str;

    str = rb_str_new(0, FIX2INT(len));
    if (!RAND_pseudo_bytes(RSTRING(str)->ptr, FIX2INT(len))) {
	ossl_raise(eRandomError, NULL);
    }

    return str;
}

static VALUE
ossl_rand_egd(VALUE self, VALUE filename)
{
    SafeStringValue(filename);
	
    if(!RAND_egd(RSTRING(filename)->ptr)) {
	ossl_raise(eRandomError, NULL);
    }
    return Qtrue;
}

static VALUE
ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
{
    SafeStringValue(filename);

    if (!RAND_egd_bytes(RSTRING(filename)->ptr, FIX2INT(len))) {
	ossl_raise(eRandomError, NULL);
    }
    return Qtrue;
}

#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()
{
    mRandom = rb_define_module_under(mOSSL, "Random");
	
    eRandomError = rb_define_class_under(mRandom, "RandomError", eOSSLError);
	
    DEFMETH(mRandom, "seed", ossl_rand_seed, 1);
    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);
    DEFMETH(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2);	
}