dhkeyexchange 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ext/extconf.rb +42 -0
- data/ext/main.c +276 -0
- metadata +41 -0
data/ext/extconf.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Copyright (c) 2007, Simon Menke
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
5
|
+
# modification, are permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above copyright notice,
|
8
|
+
# this list of conditions and the following disclaimer.
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer in the
|
11
|
+
# documentation and/or other materials provided with the distribution.
|
12
|
+
# * All advertising materials mentioning features or use of this software
|
13
|
+
# must display the following acknowledgement: This product includes
|
14
|
+
# software developed by 3motions and its contributors.
|
15
|
+
# * Neither the name of 3motions nor the names of its contributors may be
|
16
|
+
# used to endorse or promote products derived from this software without
|
17
|
+
# specific prior written permission.
|
18
|
+
#
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
23
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
24
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
require 'mkmf'
|
32
|
+
|
33
|
+
def check_functions(lib, *functions)
|
34
|
+
functions.each do |f|
|
35
|
+
return false unless have_library lib.to_s, f.to_s
|
36
|
+
end
|
37
|
+
return true
|
38
|
+
end
|
39
|
+
|
40
|
+
if check_functions :crypto, :BN_new, :BN_init, :BN_mod_exp
|
41
|
+
create_makefile("dhkeyexchange")
|
42
|
+
end
|
data/ext/main.c
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
/*
|
2
|
+
|
3
|
+
Copyright (c) 2007, Simon Menke
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
* Redistributions of source code must retain the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer.
|
11
|
+
* Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
* All advertising materials mentioning features or use of this software
|
15
|
+
must display the following acknowledgement: This product includes
|
16
|
+
software developed by 3motions and its contributors.
|
17
|
+
* Neither the name of 3motions nor the names of its contributors may be
|
18
|
+
used to endorse or promote products derived from this software without
|
19
|
+
specific prior written permission.
|
20
|
+
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
25
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
26
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
27
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
29
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
30
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31
|
+
POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
|
33
|
+
*/
|
34
|
+
|
35
|
+
#include "ruby.h"
|
36
|
+
#include <openssl/bn.h>
|
37
|
+
|
38
|
+
static VALUE rb_cDHKey;
|
39
|
+
|
40
|
+
struct dh_DHKey {
|
41
|
+
BN_CTX * ctx;
|
42
|
+
|
43
|
+
BIGNUM * base;
|
44
|
+
BIGNUM * prime;
|
45
|
+
BIGNUM * private_key;
|
46
|
+
BIGNUM * my_public_key;
|
47
|
+
BIGNUM * their_public_key;
|
48
|
+
BIGNUM * shared_key;
|
49
|
+
};
|
50
|
+
|
51
|
+
static VALUE rb_DH_key_new(VALUE self, VALUE args);
|
52
|
+
static VALUE rb_DH_key_initialize(VALUE self, VALUE args);
|
53
|
+
#define def_c_get_attr(n) static VALUE rb_DH_key_get_ ## n (VALUE self);
|
54
|
+
def_c_get_attr(base)
|
55
|
+
def_c_get_attr(prime)
|
56
|
+
def_c_get_attr(private_key)
|
57
|
+
def_c_get_attr(my_public_key)
|
58
|
+
def_c_get_attr(their_public_key)
|
59
|
+
def_c_get_attr(shared_key)
|
60
|
+
|
61
|
+
#define def_c_set_attr(n) static VALUE rb_DH_key_set_ ## n (VALUE self, VALUE v);
|
62
|
+
def_c_set_attr(base)
|
63
|
+
def_c_set_attr(prime)
|
64
|
+
def_c_set_attr(private_key)
|
65
|
+
def_c_set_attr(their_public_key)
|
66
|
+
void dh_DHKey_free(struct dh_DHKey* key);
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
void Init_dhkeyexchange() {
|
71
|
+
rb_cDHKey = rb_define_class("DHKey", rb_cObject);
|
72
|
+
rb_define_singleton_method(rb_cDHKey, "new", rb_DH_key_new, -2);
|
73
|
+
rb_define_method(rb_cDHKey, "initialize", rb_DH_key_initialize, -2);
|
74
|
+
|
75
|
+
#define def_get_attr(n) rb_define_method(rb_cDHKey, #n, rb_DH_key_get_ ## n, 0);
|
76
|
+
def_get_attr(base)
|
77
|
+
def_get_attr(prime)
|
78
|
+
def_get_attr(private_key)
|
79
|
+
def_get_attr(my_public_key)
|
80
|
+
def_get_attr(their_public_key)
|
81
|
+
def_get_attr(shared_key)
|
82
|
+
|
83
|
+
rb_define_method(rb_cDHKey, "base=" , rb_DH_key_set_base, 1);
|
84
|
+
rb_define_method(rb_cDHKey, "prime=" , rb_DH_key_set_prime, 1);
|
85
|
+
rb_define_method(rb_cDHKey, "private_key=" , rb_DH_key_set_private_key, 1);
|
86
|
+
rb_define_method(rb_cDHKey, "their_public_key=" , rb_DH_key_set_their_public_key, 1);
|
87
|
+
|
88
|
+
rb_define_alias(rb_cDHKey, "g", "base");
|
89
|
+
rb_define_alias(rb_cDHKey, "p", "prime");
|
90
|
+
rb_define_alias(rb_cDHKey, "a", "private_key");
|
91
|
+
rb_define_alias(rb_cDHKey, "x", "my_public_key");
|
92
|
+
rb_define_alias(rb_cDHKey, "y", "their_public_key");
|
93
|
+
rb_define_alias(rb_cDHKey, "k", "shared_key");
|
94
|
+
|
95
|
+
rb_define_alias(rb_cDHKey, "g=", "base=");
|
96
|
+
rb_define_alias(rb_cDHKey, "p=", "prime=");
|
97
|
+
rb_define_alias(rb_cDHKey, "a=", "private_key=");
|
98
|
+
rb_define_alias(rb_cDHKey, "y=", "their_public_key=");
|
99
|
+
}
|
100
|
+
|
101
|
+
static VALUE rb_DH_key_new(VALUE self, VALUE args) {
|
102
|
+
VALUE argv[3];
|
103
|
+
struct dh_DHKey * data = ALLOC(struct dh_DHKey);
|
104
|
+
VALUE obj = Data_Wrap_Struct(self, 0, dh_DHKey_free, data);
|
105
|
+
|
106
|
+
data->ctx = BN_CTX_new();
|
107
|
+
data->base = BN_new();
|
108
|
+
data->prime = BN_new();
|
109
|
+
data->private_key = BN_new();
|
110
|
+
data->my_public_key = BN_new();
|
111
|
+
data->their_public_key = BN_new();
|
112
|
+
data->shared_key = BN_new();
|
113
|
+
|
114
|
+
BN_zero(data->base);
|
115
|
+
BN_zero(data->prime);
|
116
|
+
BN_zero(data->private_key);
|
117
|
+
BN_zero(data->my_public_key);
|
118
|
+
BN_zero(data->their_public_key);
|
119
|
+
BN_zero(data->shared_key);
|
120
|
+
|
121
|
+
if (RARRAY(args)->len < 1) rb_DH_key_set_base(obj, Qnil);
|
122
|
+
else rb_DH_key_set_base(obj, rb_ary_entry(args, 0));
|
123
|
+
if (RARRAY(args)->len < 2) rb_DH_key_set_prime(obj, Qnil);
|
124
|
+
else rb_DH_key_set_prime(obj, rb_ary_entry(args,1));
|
125
|
+
if (RARRAY(args)->len < 3) rb_DH_key_set_private_key(obj, Qnil);
|
126
|
+
else rb_DH_key_set_private_key(obj, rb_ary_entry(args, 2));
|
127
|
+
|
128
|
+
argv[0] = (VALUE)rb_DH_key_get_base;
|
129
|
+
argv[1] = (VALUE)rb_DH_key_get_prime;
|
130
|
+
argv[2] = (VALUE)rb_DH_key_get_private_key;
|
131
|
+
rb_obj_call_init(obj, 3, argv);
|
132
|
+
|
133
|
+
return obj;
|
134
|
+
}
|
135
|
+
|
136
|
+
static VALUE rb_DH_key_initialize(VALUE self, VALUE args) {
|
137
|
+
return self;
|
138
|
+
}
|
139
|
+
|
140
|
+
|
141
|
+
static VALUE rb_DH_key_get_base (VALUE self) {
|
142
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
143
|
+
return rb_str_new2(BN_bn2dec(ptr->base));
|
144
|
+
}
|
145
|
+
static VALUE rb_DH_key_get_prime (VALUE self) {
|
146
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
147
|
+
return rb_str_new2(BN_bn2dec(ptr->prime));
|
148
|
+
}
|
149
|
+
static VALUE rb_DH_key_get_private_key (VALUE self) {
|
150
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
151
|
+
return rb_str_new2(BN_bn2dec(ptr->private_key));
|
152
|
+
}
|
153
|
+
static VALUE rb_DH_key_get_my_public_key (VALUE self) {
|
154
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
155
|
+
return rb_str_new2(BN_bn2dec(ptr->my_public_key));
|
156
|
+
}
|
157
|
+
static VALUE rb_DH_key_get_their_public_key (VALUE self) {
|
158
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
159
|
+
return rb_str_new2(BN_bn2dec(ptr->their_public_key));
|
160
|
+
}
|
161
|
+
static VALUE rb_DH_key_get_shared_key (VALUE self) {
|
162
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
163
|
+
return rb_str_new2(BN_bn2dec(ptr->shared_key));
|
164
|
+
}
|
165
|
+
|
166
|
+
#define TO_STR(x) #x
|
167
|
+
#define DH_RAND_RANGE TO_STR(9999999999999999999999999999999999999)
|
168
|
+
static BIGNUM * rb_DH_rand_range = NULL;
|
169
|
+
|
170
|
+
static VALUE rb_DH_key_set_base(VALUE self, VALUE v) {
|
171
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
172
|
+
if (NIL_P(v) == 0) {
|
173
|
+
if (rb_respond_to(v, rb_intern("to_s")) != 0) {
|
174
|
+
BN_dec2bn(&(ptr->base), STR2CSTR(rb_funcall(v, rb_intern("to_s"), 0)));
|
175
|
+
} else {
|
176
|
+
rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "base");
|
177
|
+
return Qnil;
|
178
|
+
}
|
179
|
+
} else {
|
180
|
+
if (rb_DH_rand_range == NULL) BN_dec2bn(&rb_DH_rand_range, DH_RAND_RANGE);
|
181
|
+
BN_rand_range(ptr->base, rb_DH_rand_range);
|
182
|
+
}
|
183
|
+
|
184
|
+
if ((BN_is_zero(ptr->base) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
|
185
|
+
if (BN_mod_exp(ptr->my_public_key, ptr->base, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
|
186
|
+
rb_raise(rb_eRuntimeError, "failed to calulate");
|
187
|
+
return Qnil;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
return rb_DH_key_get_base(self);
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE rb_DH_key_set_prime(VALUE self, VALUE v) {
|
195
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
196
|
+
if (NIL_P(v) == 0) {
|
197
|
+
if (rb_respond_to(v, rb_intern("to_s")) != 0) {
|
198
|
+
BN_dec2bn(&(ptr->prime), STR2CSTR(rb_funcall(v, rb_intern("to_s"), 0)));
|
199
|
+
} else {
|
200
|
+
rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "prime");
|
201
|
+
return Qnil;
|
202
|
+
}
|
203
|
+
} else {
|
204
|
+
BN_dec2bn(&(ptr->prime), "155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443");
|
205
|
+
}
|
206
|
+
|
207
|
+
if ((BN_is_zero(ptr->base) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
|
208
|
+
if (BN_mod_exp(ptr->my_public_key, ptr->base, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
|
209
|
+
rb_raise(rb_eRuntimeError, "failed to calulate");
|
210
|
+
return Qnil;
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
return rb_DH_key_get_prime(self);
|
215
|
+
}
|
216
|
+
|
217
|
+
static VALUE rb_DH_key_set_private_key(VALUE self, VALUE v) {
|
218
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
219
|
+
if (NIL_P(v) == 0) {
|
220
|
+
if (rb_respond_to(v, rb_intern("to_s")) != 0) {
|
221
|
+
BN_dec2bn(&(ptr->private_key), STR2CSTR(rb_funcall(v, rb_intern("to_s"), 0)));
|
222
|
+
} else {
|
223
|
+
rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "private_key");
|
224
|
+
return Qnil;
|
225
|
+
}
|
226
|
+
} else {
|
227
|
+
if (rb_DH_rand_range == NULL) BN_dec2bn(&rb_DH_rand_range, DH_RAND_RANGE);
|
228
|
+
BN_rand_range(ptr->private_key, rb_DH_rand_range);
|
229
|
+
}
|
230
|
+
|
231
|
+
if ((BN_is_zero(ptr->base) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
|
232
|
+
if (BN_mod_exp(ptr->my_public_key, ptr->base, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
|
233
|
+
rb_raise(rb_eRuntimeError, "failed to calulate");
|
234
|
+
return Qnil;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
|
238
|
+
return rb_DH_key_get_private_key(self);
|
239
|
+
}
|
240
|
+
|
241
|
+
static VALUE rb_DH_key_set_their_public_key(VALUE self, VALUE v) {
|
242
|
+
struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
|
243
|
+
if (NIL_P(v) == 0) {
|
244
|
+
if (rb_respond_to(v, rb_intern("to_s")) != 0) {
|
245
|
+
BN_dec2bn(&(ptr->their_public_key), STR2CSTR(rb_funcall(v, rb_intern("to_s"), 0)));
|
246
|
+
} else {
|
247
|
+
rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "their_public_key");
|
248
|
+
return Qnil;
|
249
|
+
}
|
250
|
+
} else {
|
251
|
+
rb_raise(rb_eRuntimeError, "%s must not be nil", "their_public_key");
|
252
|
+
return Qnil;
|
253
|
+
}
|
254
|
+
|
255
|
+
if ((BN_is_zero(ptr->their_public_key) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
|
256
|
+
if (BN_mod_exp(ptr->shared_key, ptr->their_public_key, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
|
257
|
+
rb_raise(rb_eRuntimeError, "failed to calulate");
|
258
|
+
return Qnil;
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
return rb_DH_key_get_their_public_key(self);
|
263
|
+
}
|
264
|
+
|
265
|
+
void dh_DHKey_free(struct dh_DHKey* key) {
|
266
|
+
if (key != NULL) {
|
267
|
+
if (key->base != NULL) BN_clear_free(key->base);
|
268
|
+
if (key->prime != NULL) BN_clear_free(key->prime);
|
269
|
+
if (key->private_key != NULL) BN_clear_free(key->private_key);
|
270
|
+
if (key->my_public_key != NULL) BN_clear_free(key->my_public_key);
|
271
|
+
if (key->their_public_key != NULL) BN_clear_free(key->their_public_key);
|
272
|
+
if (key->shared_key != NULL) BN_clear_free(key->shared_key);
|
273
|
+
if (key->ctx != NULL) BN_CTX_free(key->ctx);
|
274
|
+
free(key);
|
275
|
+
}
|
276
|
+
}
|
metadata
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: dhkeyexchange
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-03-26 00:00:00 +02:00
|
8
|
+
summary: An implementation of the Diffie Hellman key exchange protocol.
|
9
|
+
require_paths:
|
10
|
+
- "."
|
11
|
+
email: simon@3motions.net
|
12
|
+
homepage: http://3motions.net
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
authors:
|
30
|
+
- Simon Menke
|
31
|
+
files:
|
32
|
+
- ext/main.c
|
33
|
+
- ext/extconf.rb
|
34
|
+
test_files: []
|
35
|
+
rdoc_options: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
executables: []
|
38
|
+
extensions:
|
39
|
+
- ext/extconf.rb
|
40
|
+
requirements: []
|
41
|
+
dependencies: []
|