dhkeyx 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 980a9cb9e6e95e5808d90c2e8451d22ea4a53af0
4
+ data.tar.gz: d1b256e064ecf5dcf45acad7d947a65a7d3ebdf5
5
+ SHA512:
6
+ metadata.gz: a2045075a80d78880cecd1eba0e5b823ac862ebcb67d79e0af32d0c9c454ccbd7d2a8a2276c6e7921a1f9e120df348742d3d779929b68ecb4219da0bb50735b4
7
+ data.tar.gz: f9d8509134958256aac34a684dcb812f91ad6c859a9d1cf81be30a459b894b01e8e2a036b9312623e6297dd38f044c0f5377f174961386043a18576ce59f233e
@@ -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_mod_exp
41
+ create_makefile("dhkeyx")
42
+ end
data/ext/dhkeyx/main.c ADDED
@@ -0,0 +1,278 @@
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
+ void Init_dhkeyx() {
69
+ rb_cDHKey = rb_define_class("DHKey", rb_cObject);
70
+ rb_define_singleton_method(rb_cDHKey, "new", rb_DH_key_new, -2);
71
+ rb_define_method(rb_cDHKey, "initialize", rb_DH_key_initialize, -2);
72
+
73
+ #define def_get_attr(n) rb_define_method(rb_cDHKey, #n, rb_DH_key_get_ ## n, 0);
74
+ def_get_attr(base)
75
+ def_get_attr(prime)
76
+ def_get_attr(private_key)
77
+ def_get_attr(my_public_key)
78
+ def_get_attr(their_public_key)
79
+ def_get_attr(shared_key)
80
+
81
+ rb_define_method(rb_cDHKey, "base=" , rb_DH_key_set_base, 1);
82
+ rb_define_method(rb_cDHKey, "prime=" , rb_DH_key_set_prime, 1);
83
+ rb_define_method(rb_cDHKey, "private_key=" , rb_DH_key_set_private_key, 1);
84
+ rb_define_method(rb_cDHKey, "their_public_key=" , rb_DH_key_set_their_public_key, 1);
85
+
86
+ rb_define_alias(rb_cDHKey, "g", "base");
87
+ rb_define_alias(rb_cDHKey, "p", "prime");
88
+ rb_define_alias(rb_cDHKey, "a", "private_key");
89
+ rb_define_alias(rb_cDHKey, "x", "my_public_key");
90
+ rb_define_alias(rb_cDHKey, "y", "their_public_key");
91
+ rb_define_alias(rb_cDHKey, "k", "shared_key");
92
+
93
+ rb_define_alias(rb_cDHKey, "g=", "base=");
94
+ rb_define_alias(rb_cDHKey, "p=", "prime=");
95
+ rb_define_alias(rb_cDHKey, "a=", "private_key=");
96
+ rb_define_alias(rb_cDHKey, "y=", "their_public_key=");
97
+ }
98
+
99
+ static VALUE rb_DH_key_new(VALUE self, VALUE args) {
100
+ VALUE argv[3];
101
+ struct dh_DHKey * data = ALLOC(struct dh_DHKey);
102
+ VALUE obj = Data_Wrap_Struct(self, 0, dh_DHKey_free, data);
103
+
104
+ data->ctx = BN_CTX_new();
105
+ data->base = BN_new();
106
+ data->prime = BN_new();
107
+ data->private_key = BN_new();
108
+ data->my_public_key = BN_new();
109
+ data->their_public_key = BN_new();
110
+ data->shared_key = BN_new();
111
+
112
+ BN_zero(data->base);
113
+ BN_zero(data->prime);
114
+ BN_zero(data->private_key);
115
+ BN_zero(data->my_public_key);
116
+ BN_zero(data->their_public_key);
117
+ BN_zero(data->shared_key);
118
+
119
+ if (RARRAY_LEN(args) < 1) rb_DH_key_set_base(obj, Qnil);
120
+ else rb_DH_key_set_base(obj, rb_ary_entry(args, 0));
121
+ if (RARRAY_LEN(args) < 2) rb_DH_key_set_prime(obj, Qnil);
122
+ else rb_DH_key_set_prime(obj, rb_ary_entry(args,1));
123
+ if (RARRAY_LEN(args) < 3) rb_DH_key_set_private_key(obj, Qnil);
124
+ else rb_DH_key_set_private_key(obj, rb_ary_entry(args, 2));
125
+
126
+ argv[0] = (VALUE)rb_DH_key_get_base;
127
+ argv[1] = (VALUE)rb_DH_key_get_prime;
128
+ argv[2] = (VALUE)rb_DH_key_get_private_key;
129
+ rb_obj_call_init(obj, 3, argv);
130
+
131
+ return obj;
132
+ }
133
+
134
+ static VALUE rb_DH_key_initialize(VALUE self, VALUE args) {
135
+ return self;
136
+ }
137
+
138
+
139
+ static VALUE rb_DH_key_get_base (VALUE self) {
140
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
141
+ return rb_str_new2(BN_bn2dec(ptr->base));
142
+ }
143
+ static VALUE rb_DH_key_get_prime (VALUE self) {
144
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
145
+ return rb_str_new2(BN_bn2dec(ptr->prime));
146
+ }
147
+ static VALUE rb_DH_key_get_private_key (VALUE self) {
148
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
149
+ return rb_str_new2(BN_bn2dec(ptr->private_key));
150
+ }
151
+ static VALUE rb_DH_key_get_my_public_key (VALUE self) {
152
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
153
+ return rb_str_new2(BN_bn2dec(ptr->my_public_key));
154
+ }
155
+ static VALUE rb_DH_key_get_their_public_key (VALUE self) {
156
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
157
+ return rb_str_new2(BN_bn2dec(ptr->their_public_key));
158
+ }
159
+ static VALUE rb_DH_key_get_shared_key (VALUE self) {
160
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
161
+ return rb_str_new2(BN_bn2dec(ptr->shared_key));
162
+ }
163
+
164
+ #define TO_STR(x) #x
165
+ #define DH_RAND_RANGE TO_STR(9999999999999999999999999999999999999)
166
+ static BIGNUM * rb_DH_rand_range = NULL;
167
+
168
+ static VALUE rb_DH_key_set_base(VALUE self, VALUE v) {
169
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
170
+ if (NIL_P(v) == 0) {
171
+ if (rb_respond_to(v, rb_intern("to_s")) != 0) {
172
+ VALUE str = rb_funcall( v, rb_intern("to_s"), 0);
173
+ BN_dec2bn( &(ptr->base), StringValueCStr( str ));
174
+ } else {
175
+ rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "base");
176
+ return Qnil;
177
+ }
178
+ } else {
179
+ if (rb_DH_rand_range == NULL) BN_dec2bn(&rb_DH_rand_range, DH_RAND_RANGE);
180
+ BN_rand_range(ptr->base, rb_DH_rand_range);
181
+ }
182
+
183
+ if ((BN_is_zero(ptr->base) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
184
+ if (BN_mod_exp(ptr->my_public_key, ptr->base, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
185
+ rb_raise(rb_eRuntimeError, "failed to calculate");
186
+ return Qnil;
187
+ }
188
+ }
189
+
190
+ return rb_DH_key_get_base(self);
191
+ }
192
+
193
+ static VALUE rb_DH_key_set_prime(VALUE self, VALUE v) {
194
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
195
+ if (NIL_P(v) == 0) {
196
+ if (rb_respond_to(v, rb_intern("to_s")) != 0) {
197
+ VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
198
+ BN_dec2bn(&(ptr->prime), StringValueCStr( str ));
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 calculate");
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
+ VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
222
+ BN_dec2bn(&(ptr->private_key), StringValueCStr( str ));
223
+ } else {
224
+ rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "private_key");
225
+ return Qnil;
226
+ }
227
+ } else {
228
+ if (rb_DH_rand_range == NULL) BN_dec2bn(&rb_DH_rand_range, DH_RAND_RANGE);
229
+ BN_rand_range(ptr->private_key, rb_DH_rand_range);
230
+ }
231
+
232
+ if ((BN_is_zero(ptr->base) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
233
+ if (BN_mod_exp(ptr->my_public_key, ptr->base, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
234
+ rb_raise(rb_eRuntimeError, "failed to calculate");
235
+ return Qnil;
236
+ }
237
+ }
238
+
239
+ return rb_DH_key_get_private_key(self);
240
+ }
241
+
242
+ static VALUE rb_DH_key_set_their_public_key(VALUE self, VALUE v) {
243
+ struct dh_DHKey * ptr; Data_Get_Struct(self, struct dh_DHKey, ptr);
244
+ if (NIL_P(v) == 0) {
245
+ if (rb_respond_to(v, rb_intern("to_s")) != 0) {
246
+ VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
247
+ BN_dec2bn(&(ptr->their_public_key), StringValueCStr( str ));
248
+ } else {
249
+ rb_raise(rb_eRuntimeError, "%s must respond to 'to_s'", "their_public_key");
250
+ return Qnil;
251
+ }
252
+ } else {
253
+ rb_raise(rb_eRuntimeError, "%s must not be nil", "their_public_key");
254
+ return Qnil;
255
+ }
256
+
257
+ if ((BN_is_zero(ptr->their_public_key) == 0) && (BN_is_zero(ptr->prime) == 0) && (BN_is_zero(ptr->private_key) == 0)) {
258
+ if (BN_mod_exp(ptr->shared_key, ptr->their_public_key, ptr->private_key, ptr->prime, ptr->ctx) == 0) {
259
+ rb_raise(rb_eRuntimeError, "failed to calculate");
260
+ return Qnil;
261
+ }
262
+ }
263
+
264
+ return rb_DH_key_get_their_public_key(self);
265
+ }
266
+
267
+ void dh_DHKey_free(struct dh_DHKey* key) {
268
+ if (key != NULL) {
269
+ if (key->base != NULL) BN_clear_free(key->base);
270
+ if (key->prime != NULL) BN_clear_free(key->prime);
271
+ if (key->private_key != NULL) BN_clear_free(key->private_key);
272
+ if (key->my_public_key != NULL) BN_clear_free(key->my_public_key);
273
+ if (key->their_public_key != NULL) BN_clear_free(key->their_public_key);
274
+ if (key->shared_key != NULL) BN_clear_free(key->shared_key);
275
+ if (key->ctx != NULL) BN_CTX_free(key->ctx);
276
+ free(key);
277
+ }
278
+ }
@@ -0,0 +1,3 @@
1
+ module DHKeyX
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dhkeyx
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ - jfrazx
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '10.4'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '10.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake-compiler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0.9'
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.5
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '0.9'
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.5
48
+ description: ''
49
+ email:
50
+ - simon@3motions.net
51
+ - staringblind@gmail.com
52
+ executables: []
53
+ extensions:
54
+ - ext/dhkeyx/extconf.rb
55
+ extra_rdoc_files: []
56
+ files:
57
+ - ext/dhkeyx/extconf.rb
58
+ - ext/dhkeyx/main.c
59
+ - lib/dhkeyx/version.rb
60
+ homepage: https://github.com/jfrazx/dhkeyx
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ - ext
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.9.2
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: An implementation of the Diffie Hellman key exchange protocol
85
+ test_files: []