zig_example 0.3.2 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/mkmf.rb +5 -2
- data/lib/zig_example/version.rb +1 -1
- metadata +3 -56
- data/ext/openssl/openssl_missing.c +0 -40
- data/ext/openssl/openssl_missing.h +0 -238
- data/ext/openssl/ossl.c +0 -1295
- data/ext/openssl/ossl.h +0 -201
- data/ext/openssl/ossl_asn1.c +0 -1891
- data/ext/openssl/ossl_asn1.h +0 -62
- data/ext/openssl/ossl_bio.c +0 -42
- data/ext/openssl/ossl_bio.h +0 -16
- data/ext/openssl/ossl_bn.c +0 -1344
- data/ext/openssl/ossl_bn.h +0 -26
- data/ext/openssl/ossl_cipher.c +0 -1074
- data/ext/openssl/ossl_cipher.h +0 -20
- data/ext/openssl/ossl_config.c +0 -460
- data/ext/openssl/ossl_config.h +0 -16
- data/ext/openssl/ossl_digest.c +0 -425
- data/ext/openssl/ossl_digest.h +0 -20
- data/ext/openssl/ossl_engine.c +0 -568
- data/ext/openssl/ossl_engine.h +0 -19
- data/ext/openssl/ossl_hmac.c +0 -310
- data/ext/openssl/ossl_hmac.h +0 -18
- data/ext/openssl/ossl_kdf.c +0 -311
- data/ext/openssl/ossl_kdf.h +0 -6
- data/ext/openssl/ossl_ns_spki.c +0 -405
- data/ext/openssl/ossl_ns_spki.h +0 -19
- data/ext/openssl/ossl_ocsp.c +0 -1965
- data/ext/openssl/ossl_ocsp.h +0 -23
- data/ext/openssl/ossl_pkcs12.c +0 -275
- data/ext/openssl/ossl_pkcs12.h +0 -13
- data/ext/openssl/ossl_pkcs7.c +0 -1081
- data/ext/openssl/ossl_pkcs7.h +0 -36
- data/ext/openssl/ossl_pkey.c +0 -1624
- data/ext/openssl/ossl_pkey.h +0 -204
- data/ext/openssl/ossl_pkey_dh.c +0 -440
- data/ext/openssl/ossl_pkey_dsa.c +0 -359
- data/ext/openssl/ossl_pkey_ec.c +0 -1655
- data/ext/openssl/ossl_pkey_rsa.c +0 -579
- data/ext/openssl/ossl_rand.c +0 -200
- data/ext/openssl/ossl_rand.h +0 -18
- data/ext/openssl/ossl_ssl.c +0 -3142
- data/ext/openssl/ossl_ssl.h +0 -36
- data/ext/openssl/ossl_ssl_session.c +0 -331
- data/ext/openssl/ossl_ts.c +0 -1539
- data/ext/openssl/ossl_ts.h +0 -16
- data/ext/openssl/ossl_x509.c +0 -256
- data/ext/openssl/ossl_x509.h +0 -115
- data/ext/openssl/ossl_x509attr.c +0 -324
- data/ext/openssl/ossl_x509cert.c +0 -1002
- data/ext/openssl/ossl_x509crl.c +0 -545
- data/ext/openssl/ossl_x509ext.c +0 -490
- data/ext/openssl/ossl_x509name.c +0 -597
- data/ext/openssl/ossl_x509req.c +0 -444
- data/ext/openssl/ossl_x509revoked.c +0 -300
- data/ext/openssl/ossl_x509store.c +0 -986
data/ext/openssl/ossl_bn.c
DELETED
@@ -1,1344 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* 'OpenSSL for Ruby' project
|
3
|
-
* Copyright (C) 2001-2002 Technorama team <oss-ruby@technorama.net>
|
4
|
-
* All rights reserved.
|
5
|
-
*/
|
6
|
-
/*
|
7
|
-
* This program is licensed under the same licence as Ruby.
|
8
|
-
* (See the file 'LICENCE'.)
|
9
|
-
*/
|
10
|
-
/* modified by Michal Rokos <m.rokos@sh.cvut.cz> */
|
11
|
-
#include "ossl.h"
|
12
|
-
|
13
|
-
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
14
|
-
#include <ruby/ractor.h>
|
15
|
-
#endif
|
16
|
-
|
17
|
-
#define NewBN(klass) \
|
18
|
-
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
|
19
|
-
#define SetBN(obj, bn) do { \
|
20
|
-
if (!(bn)) { \
|
21
|
-
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
22
|
-
} \
|
23
|
-
RTYPEDDATA_DATA(obj) = (bn); \
|
24
|
-
} while (0)
|
25
|
-
|
26
|
-
#define GetBN(obj, bn) do { \
|
27
|
-
TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
|
28
|
-
if (!(bn)) { \
|
29
|
-
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
30
|
-
} \
|
31
|
-
} while (0)
|
32
|
-
|
33
|
-
static void
|
34
|
-
ossl_bn_free(void *ptr)
|
35
|
-
{
|
36
|
-
BN_clear_free(ptr);
|
37
|
-
}
|
38
|
-
|
39
|
-
static const rb_data_type_t ossl_bn_type = {
|
40
|
-
"OpenSSL/BN",
|
41
|
-
{
|
42
|
-
0, ossl_bn_free,
|
43
|
-
},
|
44
|
-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
45
|
-
};
|
46
|
-
|
47
|
-
/*
|
48
|
-
* Classes
|
49
|
-
*/
|
50
|
-
VALUE cBN;
|
51
|
-
|
52
|
-
/* Document-class: OpenSSL::BNError
|
53
|
-
*
|
54
|
-
* Generic Error for all of OpenSSL::BN (big num)
|
55
|
-
*/
|
56
|
-
VALUE eBNError;
|
57
|
-
|
58
|
-
/*
|
59
|
-
* Public
|
60
|
-
*/
|
61
|
-
VALUE
|
62
|
-
ossl_bn_new(const BIGNUM *bn)
|
63
|
-
{
|
64
|
-
BIGNUM *newbn;
|
65
|
-
VALUE obj;
|
66
|
-
|
67
|
-
obj = NewBN(cBN);
|
68
|
-
newbn = bn ? BN_dup(bn) : BN_new();
|
69
|
-
if (!newbn) {
|
70
|
-
ossl_raise(eBNError, NULL);
|
71
|
-
}
|
72
|
-
SetBN(obj, newbn);
|
73
|
-
|
74
|
-
return obj;
|
75
|
-
}
|
76
|
-
|
77
|
-
static BIGNUM *
|
78
|
-
integer_to_bnptr(VALUE obj, BIGNUM *orig)
|
79
|
-
{
|
80
|
-
BIGNUM *bn;
|
81
|
-
|
82
|
-
if (FIXNUM_P(obj)) {
|
83
|
-
long i;
|
84
|
-
unsigned char bin[sizeof(long)];
|
85
|
-
long n = FIX2LONG(obj);
|
86
|
-
unsigned long un = labs(n);
|
87
|
-
|
88
|
-
for (i = sizeof(long) - 1; 0 <= i; i--) {
|
89
|
-
bin[i] = un & 0xff;
|
90
|
-
un >>= 8;
|
91
|
-
}
|
92
|
-
|
93
|
-
bn = BN_bin2bn(bin, sizeof(bin), orig);
|
94
|
-
if (!bn)
|
95
|
-
ossl_raise(eBNError, "BN_bin2bn");
|
96
|
-
if (n < 0)
|
97
|
-
BN_set_negative(bn, 1);
|
98
|
-
}
|
99
|
-
else { /* assuming Bignum */
|
100
|
-
size_t len = rb_absint_size(obj, NULL);
|
101
|
-
unsigned char *bin;
|
102
|
-
VALUE buf;
|
103
|
-
int sign;
|
104
|
-
|
105
|
-
if (INT_MAX < len) {
|
106
|
-
rb_raise(eBNError, "bignum too long");
|
107
|
-
}
|
108
|
-
bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len);
|
109
|
-
sign = rb_integer_pack(obj, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN);
|
110
|
-
|
111
|
-
bn = BN_bin2bn(bin, (int)len, orig);
|
112
|
-
ALLOCV_END(buf);
|
113
|
-
if (!bn)
|
114
|
-
ossl_raise(eBNError, "BN_bin2bn");
|
115
|
-
if (sign < 0)
|
116
|
-
BN_set_negative(bn, 1);
|
117
|
-
}
|
118
|
-
|
119
|
-
return bn;
|
120
|
-
}
|
121
|
-
|
122
|
-
static VALUE
|
123
|
-
try_convert_to_bn(VALUE obj)
|
124
|
-
{
|
125
|
-
BIGNUM *bn;
|
126
|
-
VALUE newobj = Qnil;
|
127
|
-
|
128
|
-
if (rb_obj_is_kind_of(obj, cBN))
|
129
|
-
return obj;
|
130
|
-
if (RB_INTEGER_TYPE_P(obj)) {
|
131
|
-
newobj = NewBN(cBN); /* Handle potential mem leaks */
|
132
|
-
bn = integer_to_bnptr(obj, NULL);
|
133
|
-
SetBN(newobj, bn);
|
134
|
-
}
|
135
|
-
|
136
|
-
return newobj;
|
137
|
-
}
|
138
|
-
|
139
|
-
BIGNUM *
|
140
|
-
ossl_bn_value_ptr(volatile VALUE *ptr)
|
141
|
-
{
|
142
|
-
VALUE tmp;
|
143
|
-
BIGNUM *bn;
|
144
|
-
|
145
|
-
tmp = try_convert_to_bn(*ptr);
|
146
|
-
if (NIL_P(tmp))
|
147
|
-
ossl_raise(rb_eTypeError, "Cannot convert into OpenSSL::BN");
|
148
|
-
GetBN(tmp, bn);
|
149
|
-
*ptr = tmp;
|
150
|
-
|
151
|
-
return bn;
|
152
|
-
}
|
153
|
-
|
154
|
-
/*
|
155
|
-
* Private
|
156
|
-
*/
|
157
|
-
|
158
|
-
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
159
|
-
void
|
160
|
-
ossl_bn_ctx_free(void *ptr)
|
161
|
-
{
|
162
|
-
BN_CTX *ctx = (BN_CTX *)ptr;
|
163
|
-
BN_CTX_free(ctx);
|
164
|
-
}
|
165
|
-
|
166
|
-
struct rb_ractor_local_storage_type ossl_bn_ctx_key_type = {
|
167
|
-
NULL, // mark
|
168
|
-
ossl_bn_ctx_free,
|
169
|
-
};
|
170
|
-
|
171
|
-
rb_ractor_local_key_t ossl_bn_ctx_key;
|
172
|
-
|
173
|
-
BN_CTX *
|
174
|
-
ossl_bn_ctx_get(void)
|
175
|
-
{
|
176
|
-
// stored in ractor local storage
|
177
|
-
|
178
|
-
BN_CTX *ctx = rb_ractor_local_storage_ptr(ossl_bn_ctx_key);
|
179
|
-
if (!ctx) {
|
180
|
-
if (!(ctx = BN_CTX_new())) {
|
181
|
-
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
|
182
|
-
}
|
183
|
-
rb_ractor_local_storage_ptr_set(ossl_bn_ctx_key, ctx);
|
184
|
-
}
|
185
|
-
return ctx;
|
186
|
-
}
|
187
|
-
#else
|
188
|
-
// for ruby 2.x
|
189
|
-
static BN_CTX *gv_ossl_bn_ctx;
|
190
|
-
|
191
|
-
BN_CTX *
|
192
|
-
ossl_bn_ctx_get(void)
|
193
|
-
{
|
194
|
-
if (gv_ossl_bn_ctx == NULL) {
|
195
|
-
if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
|
196
|
-
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
|
197
|
-
}
|
198
|
-
}
|
199
|
-
return gv_ossl_bn_ctx;
|
200
|
-
}
|
201
|
-
|
202
|
-
void
|
203
|
-
ossl_bn_ctx_free(void)
|
204
|
-
{
|
205
|
-
BN_CTX_free(gv_ossl_bn_ctx);
|
206
|
-
gv_ossl_bn_ctx = NULL;
|
207
|
-
}
|
208
|
-
#endif
|
209
|
-
|
210
|
-
static VALUE
|
211
|
-
ossl_bn_alloc(VALUE klass)
|
212
|
-
{
|
213
|
-
BIGNUM *bn;
|
214
|
-
VALUE obj = NewBN(klass);
|
215
|
-
|
216
|
-
if (!(bn = BN_new())) {
|
217
|
-
ossl_raise(eBNError, NULL);
|
218
|
-
}
|
219
|
-
SetBN(obj, bn);
|
220
|
-
|
221
|
-
return obj;
|
222
|
-
}
|
223
|
-
|
224
|
-
/*
|
225
|
-
* call-seq:
|
226
|
-
* OpenSSL::BN.new(bn) -> aBN
|
227
|
-
* OpenSSL::BN.new(integer) -> aBN
|
228
|
-
* OpenSSL::BN.new(string, base = 10) -> aBN
|
229
|
-
*
|
230
|
-
* Construct a new \OpenSSL BIGNUM object.
|
231
|
-
*
|
232
|
-
* If +bn+ is an Integer or OpenSSL::BN, a new instance of OpenSSL::BN
|
233
|
-
* representing the same value is returned. See also Integer#to_bn for the
|
234
|
-
* short-hand.
|
235
|
-
*
|
236
|
-
* If a String is given, the content will be parsed according to +base+.
|
237
|
-
*
|
238
|
-
* +string+::
|
239
|
-
* The string to be parsed.
|
240
|
-
* +base+::
|
241
|
-
* The format. Must be one of the following:
|
242
|
-
* - +0+ - MPI format. See the man page BN_mpi2bn(3) for details.
|
243
|
-
* - +2+ - Variable-length and big-endian binary encoding of a positive
|
244
|
-
* number.
|
245
|
-
* - +10+ - Decimal number representation, with a leading '-' for a negative
|
246
|
-
* number.
|
247
|
-
* - +16+ - Hexadeciaml number representation, with a leading '-' for a
|
248
|
-
* negative number.
|
249
|
-
*/
|
250
|
-
static VALUE
|
251
|
-
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
|
252
|
-
{
|
253
|
-
BIGNUM *bn;
|
254
|
-
VALUE str, bs;
|
255
|
-
int base = 10;
|
256
|
-
char *ptr;
|
257
|
-
|
258
|
-
if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
|
259
|
-
base = NUM2INT(bs);
|
260
|
-
}
|
261
|
-
|
262
|
-
if (NIL_P(str)) {
|
263
|
-
ossl_raise(rb_eArgError, "invalid argument");
|
264
|
-
}
|
265
|
-
|
266
|
-
if (RB_INTEGER_TYPE_P(str)) {
|
267
|
-
GetBN(self, bn);
|
268
|
-
integer_to_bnptr(str, bn);
|
269
|
-
|
270
|
-
return self;
|
271
|
-
}
|
272
|
-
|
273
|
-
if (RTEST(rb_obj_is_kind_of(str, cBN))) {
|
274
|
-
BIGNUM *other;
|
275
|
-
|
276
|
-
GetBN(self, bn);
|
277
|
-
GetBN(str, other); /* Safe - we checked kind_of? above */
|
278
|
-
if (!BN_copy(bn, other)) {
|
279
|
-
ossl_raise(eBNError, NULL);
|
280
|
-
}
|
281
|
-
return self;
|
282
|
-
}
|
283
|
-
|
284
|
-
GetBN(self, bn);
|
285
|
-
switch (base) {
|
286
|
-
case 0:
|
287
|
-
ptr = StringValuePtr(str);
|
288
|
-
if (!BN_mpi2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
289
|
-
ossl_raise(eBNError, NULL);
|
290
|
-
}
|
291
|
-
break;
|
292
|
-
case 2:
|
293
|
-
ptr = StringValuePtr(str);
|
294
|
-
if (!BN_bin2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
295
|
-
ossl_raise(eBNError, NULL);
|
296
|
-
}
|
297
|
-
break;
|
298
|
-
case 10:
|
299
|
-
if (!BN_dec2bn(&bn, StringValueCStr(str))) {
|
300
|
-
ossl_raise(eBNError, NULL);
|
301
|
-
}
|
302
|
-
break;
|
303
|
-
case 16:
|
304
|
-
if (!BN_hex2bn(&bn, StringValueCStr(str))) {
|
305
|
-
ossl_raise(eBNError, NULL);
|
306
|
-
}
|
307
|
-
break;
|
308
|
-
default:
|
309
|
-
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
310
|
-
}
|
311
|
-
return self;
|
312
|
-
}
|
313
|
-
|
314
|
-
/*
|
315
|
-
* call-seq:
|
316
|
-
* bn.to_s(base = 10) -> string
|
317
|
-
*
|
318
|
-
* Returns the string representation of the bignum.
|
319
|
-
*
|
320
|
-
* BN.new can parse the encoded string to convert back into an OpenSSL::BN.
|
321
|
-
*
|
322
|
-
* +base+::
|
323
|
-
* The format. Must be one of the following:
|
324
|
-
* - +0+ - MPI format. See the man page BN_bn2mpi(3) for details.
|
325
|
-
* - +2+ - Variable-length and big-endian binary encoding. The sign of
|
326
|
-
* the bignum is ignored.
|
327
|
-
* - +10+ - Decimal number representation, with a leading '-' for a negative
|
328
|
-
* bignum.
|
329
|
-
* - +16+ - Hexadeciaml number representation, with a leading '-' for a
|
330
|
-
* negative bignum.
|
331
|
-
*/
|
332
|
-
static VALUE
|
333
|
-
ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
|
334
|
-
{
|
335
|
-
BIGNUM *bn;
|
336
|
-
VALUE str, bs;
|
337
|
-
int base = 10, len;
|
338
|
-
char *buf;
|
339
|
-
|
340
|
-
if (rb_scan_args(argc, argv, "01", &bs) == 1) {
|
341
|
-
base = NUM2INT(bs);
|
342
|
-
}
|
343
|
-
GetBN(self, bn);
|
344
|
-
switch (base) {
|
345
|
-
case 0:
|
346
|
-
len = BN_bn2mpi(bn, NULL);
|
347
|
-
str = rb_str_new(0, len);
|
348
|
-
if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
349
|
-
ossl_raise(eBNError, NULL);
|
350
|
-
break;
|
351
|
-
case 2:
|
352
|
-
len = BN_num_bytes(bn);
|
353
|
-
str = rb_str_new(0, len);
|
354
|
-
if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
355
|
-
ossl_raise(eBNError, NULL);
|
356
|
-
break;
|
357
|
-
case 10:
|
358
|
-
if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
|
359
|
-
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
360
|
-
break;
|
361
|
-
case 16:
|
362
|
-
if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
|
363
|
-
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
364
|
-
break;
|
365
|
-
default:
|
366
|
-
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
367
|
-
}
|
368
|
-
|
369
|
-
return str;
|
370
|
-
}
|
371
|
-
|
372
|
-
/*
|
373
|
-
* call-seq:
|
374
|
-
* bn.to_i => integer
|
375
|
-
*/
|
376
|
-
static VALUE
|
377
|
-
ossl_bn_to_i(VALUE self)
|
378
|
-
{
|
379
|
-
BIGNUM *bn;
|
380
|
-
char *txt;
|
381
|
-
VALUE num;
|
382
|
-
|
383
|
-
GetBN(self, bn);
|
384
|
-
|
385
|
-
if (!(txt = BN_bn2hex(bn))) {
|
386
|
-
ossl_raise(eBNError, NULL);
|
387
|
-
}
|
388
|
-
num = rb_cstr_to_inum(txt, 16, Qtrue);
|
389
|
-
OPENSSL_free(txt);
|
390
|
-
|
391
|
-
return num;
|
392
|
-
}
|
393
|
-
|
394
|
-
static VALUE
|
395
|
-
ossl_bn_to_bn(VALUE self)
|
396
|
-
{
|
397
|
-
return self;
|
398
|
-
}
|
399
|
-
|
400
|
-
static VALUE
|
401
|
-
ossl_bn_coerce(VALUE self, VALUE other)
|
402
|
-
{
|
403
|
-
switch(TYPE(other)) {
|
404
|
-
case T_STRING:
|
405
|
-
self = ossl_bn_to_s(0, NULL, self);
|
406
|
-
break;
|
407
|
-
case T_FIXNUM:
|
408
|
-
case T_BIGNUM:
|
409
|
-
self = ossl_bn_to_i(self);
|
410
|
-
break;
|
411
|
-
default:
|
412
|
-
if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
|
413
|
-
ossl_raise(rb_eTypeError, "Don't know how to coerce");
|
414
|
-
}
|
415
|
-
}
|
416
|
-
return rb_assoc_new(other, self);
|
417
|
-
}
|
418
|
-
|
419
|
-
#define BIGNUM_BOOL1(func) \
|
420
|
-
static VALUE \
|
421
|
-
ossl_bn_##func(VALUE self) \
|
422
|
-
{ \
|
423
|
-
BIGNUM *bn; \
|
424
|
-
GetBN(self, bn); \
|
425
|
-
if (BN_##func(bn)) { \
|
426
|
-
return Qtrue; \
|
427
|
-
} \
|
428
|
-
return Qfalse; \
|
429
|
-
}
|
430
|
-
|
431
|
-
/*
|
432
|
-
* Document-method: OpenSSL::BN#zero?
|
433
|
-
* call-seq:
|
434
|
-
* bn.zero? => true | false
|
435
|
-
*/
|
436
|
-
BIGNUM_BOOL1(is_zero)
|
437
|
-
|
438
|
-
/*
|
439
|
-
* Document-method: OpenSSL::BN#one?
|
440
|
-
* call-seq:
|
441
|
-
* bn.one? => true | false
|
442
|
-
*/
|
443
|
-
BIGNUM_BOOL1(is_one)
|
444
|
-
|
445
|
-
/*
|
446
|
-
* Document-method: OpenSSL::BN#odd?
|
447
|
-
* call-seq:
|
448
|
-
* bn.odd? => true | false
|
449
|
-
*/
|
450
|
-
BIGNUM_BOOL1(is_odd)
|
451
|
-
|
452
|
-
/*
|
453
|
-
* call-seq:
|
454
|
-
* bn.negative? => true | false
|
455
|
-
*/
|
456
|
-
static VALUE
|
457
|
-
ossl_bn_is_negative(VALUE self)
|
458
|
-
{
|
459
|
-
BIGNUM *bn;
|
460
|
-
|
461
|
-
GetBN(self, bn);
|
462
|
-
if (BN_is_zero(bn))
|
463
|
-
return Qfalse;
|
464
|
-
return BN_is_negative(bn) ? Qtrue : Qfalse;
|
465
|
-
}
|
466
|
-
|
467
|
-
#define BIGNUM_1c(func) \
|
468
|
-
static VALUE \
|
469
|
-
ossl_bn_##func(VALUE self) \
|
470
|
-
{ \
|
471
|
-
BIGNUM *bn, *result; \
|
472
|
-
VALUE obj; \
|
473
|
-
GetBN(self, bn); \
|
474
|
-
obj = NewBN(rb_obj_class(self)); \
|
475
|
-
if (!(result = BN_new())) { \
|
476
|
-
ossl_raise(eBNError, NULL); \
|
477
|
-
} \
|
478
|
-
if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
|
479
|
-
BN_free(result); \
|
480
|
-
ossl_raise(eBNError, NULL); \
|
481
|
-
} \
|
482
|
-
SetBN(obj, result); \
|
483
|
-
return obj; \
|
484
|
-
}
|
485
|
-
|
486
|
-
/*
|
487
|
-
* Document-method: OpenSSL::BN#sqr
|
488
|
-
* call-seq:
|
489
|
-
* bn.sqr => aBN
|
490
|
-
*/
|
491
|
-
BIGNUM_1c(sqr)
|
492
|
-
|
493
|
-
#define BIGNUM_2(func) \
|
494
|
-
static VALUE \
|
495
|
-
ossl_bn_##func(VALUE self, VALUE other) \
|
496
|
-
{ \
|
497
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
498
|
-
VALUE obj; \
|
499
|
-
GetBN(self, bn1); \
|
500
|
-
obj = NewBN(rb_obj_class(self)); \
|
501
|
-
if (!(result = BN_new())) { \
|
502
|
-
ossl_raise(eBNError, NULL); \
|
503
|
-
} \
|
504
|
-
if (BN_##func(result, bn1, bn2) <= 0) { \
|
505
|
-
BN_free(result); \
|
506
|
-
ossl_raise(eBNError, NULL); \
|
507
|
-
} \
|
508
|
-
SetBN(obj, result); \
|
509
|
-
return obj; \
|
510
|
-
}
|
511
|
-
|
512
|
-
/*
|
513
|
-
* Document-method: OpenSSL::BN#+
|
514
|
-
* call-seq:
|
515
|
-
* bn + bn2 => aBN
|
516
|
-
*/
|
517
|
-
BIGNUM_2(add)
|
518
|
-
|
519
|
-
/*
|
520
|
-
* Document-method: OpenSSL::BN#-
|
521
|
-
* call-seq:
|
522
|
-
* bn - bn2 => aBN
|
523
|
-
*/
|
524
|
-
BIGNUM_2(sub)
|
525
|
-
|
526
|
-
#define BIGNUM_2c(func) \
|
527
|
-
static VALUE \
|
528
|
-
ossl_bn_##func(VALUE self, VALUE other) \
|
529
|
-
{ \
|
530
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
531
|
-
VALUE obj; \
|
532
|
-
GetBN(self, bn1); \
|
533
|
-
obj = NewBN(rb_obj_class(self)); \
|
534
|
-
if (!(result = BN_new())) { \
|
535
|
-
ossl_raise(eBNError, NULL); \
|
536
|
-
} \
|
537
|
-
if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
|
538
|
-
BN_free(result); \
|
539
|
-
ossl_raise(eBNError, NULL); \
|
540
|
-
} \
|
541
|
-
SetBN(obj, result); \
|
542
|
-
return obj; \
|
543
|
-
}
|
544
|
-
|
545
|
-
/*
|
546
|
-
* Document-method: OpenSSL::BN#*
|
547
|
-
* call-seq:
|
548
|
-
* bn * bn2 => aBN
|
549
|
-
*/
|
550
|
-
BIGNUM_2c(mul)
|
551
|
-
|
552
|
-
/*
|
553
|
-
* Document-method: OpenSSL::BN#%
|
554
|
-
* call-seq:
|
555
|
-
* bn % bn2 => aBN
|
556
|
-
*/
|
557
|
-
BIGNUM_2c(mod)
|
558
|
-
|
559
|
-
/*
|
560
|
-
* Document-method: OpenSSL::BN#**
|
561
|
-
* call-seq:
|
562
|
-
* bn ** bn2 => aBN
|
563
|
-
*/
|
564
|
-
BIGNUM_2c(exp)
|
565
|
-
|
566
|
-
/*
|
567
|
-
* Document-method: OpenSSL::BN#gcd
|
568
|
-
* call-seq:
|
569
|
-
* bn.gcd(bn2) => aBN
|
570
|
-
*/
|
571
|
-
BIGNUM_2c(gcd)
|
572
|
-
|
573
|
-
/*
|
574
|
-
* Document-method: OpenSSL::BN#mod_sqr
|
575
|
-
* call-seq:
|
576
|
-
* bn.mod_sqr(bn2) => aBN
|
577
|
-
*/
|
578
|
-
BIGNUM_2c(mod_sqr)
|
579
|
-
|
580
|
-
#define BIGNUM_2cr(func) \
|
581
|
-
static VALUE \
|
582
|
-
ossl_bn_##func(VALUE self, VALUE other) \
|
583
|
-
{ \
|
584
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
585
|
-
VALUE obj; \
|
586
|
-
GetBN(self, bn1); \
|
587
|
-
obj = NewBN(rb_obj_class(self)); \
|
588
|
-
if (!(result = BN_##func(NULL, bn1, bn2, ossl_bn_ctx))) \
|
589
|
-
ossl_raise(eBNError, NULL); \
|
590
|
-
SetBN(obj, result); \
|
591
|
-
return obj; \
|
592
|
-
}
|
593
|
-
|
594
|
-
/*
|
595
|
-
* Document-method: OpenSSL::BN#mod_sqrt
|
596
|
-
* call-seq:
|
597
|
-
* bn.mod_sqrt(bn2) => aBN
|
598
|
-
*/
|
599
|
-
BIGNUM_2cr(mod_sqrt)
|
600
|
-
|
601
|
-
/*
|
602
|
-
* Document-method: OpenSSL::BN#mod_inverse
|
603
|
-
* call-seq:
|
604
|
-
* bn.mod_inverse(bn2) => aBN
|
605
|
-
*/
|
606
|
-
BIGNUM_2cr(mod_inverse)
|
607
|
-
|
608
|
-
/*
|
609
|
-
* call-seq:
|
610
|
-
* bn1 / bn2 => [result, remainder]
|
611
|
-
*
|
612
|
-
* Division of OpenSSL::BN instances
|
613
|
-
*/
|
614
|
-
static VALUE
|
615
|
-
ossl_bn_div(VALUE self, VALUE other)
|
616
|
-
{
|
617
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other), *r1, *r2;
|
618
|
-
VALUE klass, obj1, obj2;
|
619
|
-
|
620
|
-
GetBN(self, bn1);
|
621
|
-
|
622
|
-
klass = rb_obj_class(self);
|
623
|
-
obj1 = NewBN(klass);
|
624
|
-
obj2 = NewBN(klass);
|
625
|
-
if (!(r1 = BN_new())) {
|
626
|
-
ossl_raise(eBNError, NULL);
|
627
|
-
}
|
628
|
-
if (!(r2 = BN_new())) {
|
629
|
-
BN_free(r1);
|
630
|
-
ossl_raise(eBNError, NULL);
|
631
|
-
}
|
632
|
-
if (!BN_div(r1, r2, bn1, bn2, ossl_bn_ctx)) {
|
633
|
-
BN_free(r1);
|
634
|
-
BN_free(r2);
|
635
|
-
ossl_raise(eBNError, NULL);
|
636
|
-
}
|
637
|
-
SetBN(obj1, r1);
|
638
|
-
SetBN(obj2, r2);
|
639
|
-
|
640
|
-
return rb_ary_new3(2, obj1, obj2);
|
641
|
-
}
|
642
|
-
|
643
|
-
#define BIGNUM_3c(func) \
|
644
|
-
static VALUE \
|
645
|
-
ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
|
646
|
-
{ \
|
647
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
|
648
|
-
BIGNUM *bn3 = GetBNPtr(other2), *result; \
|
649
|
-
VALUE obj; \
|
650
|
-
GetBN(self, bn1); \
|
651
|
-
obj = NewBN(rb_obj_class(self)); \
|
652
|
-
if (!(result = BN_new())) { \
|
653
|
-
ossl_raise(eBNError, NULL); \
|
654
|
-
} \
|
655
|
-
if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
|
656
|
-
BN_free(result); \
|
657
|
-
ossl_raise(eBNError, NULL); \
|
658
|
-
} \
|
659
|
-
SetBN(obj, result); \
|
660
|
-
return obj; \
|
661
|
-
}
|
662
|
-
|
663
|
-
/*
|
664
|
-
* Document-method: OpenSSL::BN#mod_add
|
665
|
-
* call-seq:
|
666
|
-
* bn.mod_add(bn1, bn2) -> aBN
|
667
|
-
*/
|
668
|
-
BIGNUM_3c(mod_add)
|
669
|
-
|
670
|
-
/*
|
671
|
-
* Document-method: OpenSSL::BN#mod_sub
|
672
|
-
* call-seq:
|
673
|
-
* bn.mod_sub(bn1, bn2) -> aBN
|
674
|
-
*/
|
675
|
-
BIGNUM_3c(mod_sub)
|
676
|
-
|
677
|
-
/*
|
678
|
-
* Document-method: OpenSSL::BN#mod_mul
|
679
|
-
* call-seq:
|
680
|
-
* bn.mod_mul(bn1, bn2) -> aBN
|
681
|
-
*/
|
682
|
-
BIGNUM_3c(mod_mul)
|
683
|
-
|
684
|
-
/*
|
685
|
-
* Document-method: OpenSSL::BN#mod_exp
|
686
|
-
* call-seq:
|
687
|
-
* bn.mod_exp(bn1, bn2) -> aBN
|
688
|
-
*/
|
689
|
-
BIGNUM_3c(mod_exp)
|
690
|
-
|
691
|
-
#define BIGNUM_BIT(func) \
|
692
|
-
static VALUE \
|
693
|
-
ossl_bn_##func(VALUE self, VALUE bit) \
|
694
|
-
{ \
|
695
|
-
BIGNUM *bn; \
|
696
|
-
GetBN(self, bn); \
|
697
|
-
if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
|
698
|
-
ossl_raise(eBNError, NULL); \
|
699
|
-
} \
|
700
|
-
return self; \
|
701
|
-
}
|
702
|
-
|
703
|
-
/*
|
704
|
-
* Document-method: OpenSSL::BN#set_bit!
|
705
|
-
* call-seq:
|
706
|
-
* bn.set_bit!(bit) -> self
|
707
|
-
*/
|
708
|
-
BIGNUM_BIT(set_bit)
|
709
|
-
|
710
|
-
/*
|
711
|
-
* Document-method: OpenSSL::BN#clear_bit!
|
712
|
-
* call-seq:
|
713
|
-
* bn.clear_bit!(bit) -> self
|
714
|
-
*/
|
715
|
-
BIGNUM_BIT(clear_bit)
|
716
|
-
|
717
|
-
/*
|
718
|
-
* Document-method: OpenSSL::BN#mask_bit!
|
719
|
-
* call-seq:
|
720
|
-
* bn.mask_bit!(bit) -> self
|
721
|
-
*/
|
722
|
-
BIGNUM_BIT(mask_bits)
|
723
|
-
|
724
|
-
/*
|
725
|
-
* call-seq:
|
726
|
-
* bn.bit_set?(bit) => true | false
|
727
|
-
*
|
728
|
-
* Tests bit _bit_ in _bn_ and returns +true+ if set, +false+ if not set.
|
729
|
-
*/
|
730
|
-
static VALUE
|
731
|
-
ossl_bn_is_bit_set(VALUE self, VALUE bit)
|
732
|
-
{
|
733
|
-
int b;
|
734
|
-
BIGNUM *bn;
|
735
|
-
|
736
|
-
b = NUM2INT(bit);
|
737
|
-
GetBN(self, bn);
|
738
|
-
if (BN_is_bit_set(bn, b)) {
|
739
|
-
return Qtrue;
|
740
|
-
}
|
741
|
-
return Qfalse;
|
742
|
-
}
|
743
|
-
|
744
|
-
#define BIGNUM_SHIFT(func) \
|
745
|
-
static VALUE \
|
746
|
-
ossl_bn_##func(VALUE self, VALUE bits) \
|
747
|
-
{ \
|
748
|
-
BIGNUM *bn, *result; \
|
749
|
-
int b; \
|
750
|
-
VALUE obj; \
|
751
|
-
b = NUM2INT(bits); \
|
752
|
-
GetBN(self, bn); \
|
753
|
-
obj = NewBN(rb_obj_class(self)); \
|
754
|
-
if (!(result = BN_new())) { \
|
755
|
-
ossl_raise(eBNError, NULL); \
|
756
|
-
} \
|
757
|
-
if (BN_##func(result, bn, b) <= 0) { \
|
758
|
-
BN_free(result); \
|
759
|
-
ossl_raise(eBNError, NULL); \
|
760
|
-
} \
|
761
|
-
SetBN(obj, result); \
|
762
|
-
return obj; \
|
763
|
-
}
|
764
|
-
|
765
|
-
/*
|
766
|
-
* Document-method: OpenSSL::BN#<<
|
767
|
-
* call-seq:
|
768
|
-
* bn << bits -> aBN
|
769
|
-
*/
|
770
|
-
BIGNUM_SHIFT(lshift)
|
771
|
-
|
772
|
-
/*
|
773
|
-
* Document-method: OpenSSL::BN#>>
|
774
|
-
* call-seq:
|
775
|
-
* bn >> bits -> aBN
|
776
|
-
*/
|
777
|
-
BIGNUM_SHIFT(rshift)
|
778
|
-
|
779
|
-
#define BIGNUM_SELF_SHIFT(func) \
|
780
|
-
static VALUE \
|
781
|
-
ossl_bn_self_##func(VALUE self, VALUE bits) \
|
782
|
-
{ \
|
783
|
-
BIGNUM *bn; \
|
784
|
-
int b; \
|
785
|
-
b = NUM2INT(bits); \
|
786
|
-
GetBN(self, bn); \
|
787
|
-
if (BN_##func(bn, bn, b) <= 0) \
|
788
|
-
ossl_raise(eBNError, NULL); \
|
789
|
-
return self; \
|
790
|
-
}
|
791
|
-
|
792
|
-
/*
|
793
|
-
* Document-method: OpenSSL::BN#lshift!
|
794
|
-
* call-seq:
|
795
|
-
* bn.lshift!(bits) -> self
|
796
|
-
*/
|
797
|
-
BIGNUM_SELF_SHIFT(lshift)
|
798
|
-
|
799
|
-
/*
|
800
|
-
* Document-method: OpenSSL::BN#rshift!
|
801
|
-
* call-seq:
|
802
|
-
* bn.rshift!(bits) -> self
|
803
|
-
*/
|
804
|
-
BIGNUM_SELF_SHIFT(rshift)
|
805
|
-
|
806
|
-
/*
|
807
|
-
* call-seq:
|
808
|
-
* BN.rand(bits [, fill [, odd]]) -> aBN
|
809
|
-
*
|
810
|
-
* Generates a cryptographically strong pseudo-random number of +bits+.
|
811
|
-
*
|
812
|
-
* See also the man page BN_rand(3).
|
813
|
-
*/
|
814
|
-
static VALUE
|
815
|
-
ossl_bn_s_rand(int argc, VALUE *argv, VALUE klass)
|
816
|
-
{
|
817
|
-
BIGNUM *result;
|
818
|
-
int bottom = 0, top = 0, b;
|
819
|
-
VALUE bits, fill, odd, obj;
|
820
|
-
|
821
|
-
switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) {
|
822
|
-
case 3:
|
823
|
-
bottom = (odd == Qtrue) ? 1 : 0;
|
824
|
-
/* FALLTHROUGH */
|
825
|
-
case 2:
|
826
|
-
top = NUM2INT(fill);
|
827
|
-
}
|
828
|
-
b = NUM2INT(bits);
|
829
|
-
obj = NewBN(klass);
|
830
|
-
if (!(result = BN_new())) {
|
831
|
-
ossl_raise(eBNError, "BN_new");
|
832
|
-
}
|
833
|
-
if (BN_rand(result, b, top, bottom) <= 0) {
|
834
|
-
BN_free(result);
|
835
|
-
ossl_raise(eBNError, "BN_rand");
|
836
|
-
}
|
837
|
-
SetBN(obj, result);
|
838
|
-
return obj;
|
839
|
-
}
|
840
|
-
|
841
|
-
/*
|
842
|
-
* call-seq:
|
843
|
-
* BN.rand_range(range) -> aBN
|
844
|
-
*
|
845
|
-
* Generates a cryptographically strong pseudo-random number in the range
|
846
|
-
* 0...+range+.
|
847
|
-
*
|
848
|
-
* See also the man page BN_rand_range(3).
|
849
|
-
*/
|
850
|
-
static VALUE
|
851
|
-
ossl_bn_s_rand_range(VALUE klass, VALUE range)
|
852
|
-
{
|
853
|
-
BIGNUM *bn = GetBNPtr(range), *result;
|
854
|
-
VALUE obj = NewBN(klass);
|
855
|
-
if (!(result = BN_new()))
|
856
|
-
ossl_raise(eBNError, "BN_new");
|
857
|
-
if (BN_rand_range(result, bn) <= 0) {
|
858
|
-
BN_free(result);
|
859
|
-
ossl_raise(eBNError, "BN_rand_range");
|
860
|
-
}
|
861
|
-
SetBN(obj, result);
|
862
|
-
return obj;
|
863
|
-
}
|
864
|
-
|
865
|
-
/*
|
866
|
-
* call-seq:
|
867
|
-
* BN.generate_prime(bits, [, safe [, add [, rem]]]) => bn
|
868
|
-
*
|
869
|
-
* Generates a random prime number of bit length _bits_. If _safe_ is set to
|
870
|
-
* +true+, generates a safe prime. If _add_ is specified, generates a prime that
|
871
|
-
* fulfills condition <tt>p % add = rem</tt>.
|
872
|
-
*
|
873
|
-
* === Parameters
|
874
|
-
* * _bits_ - integer
|
875
|
-
* * _safe_ - boolean
|
876
|
-
* * _add_ - BN
|
877
|
-
* * _rem_ - BN
|
878
|
-
*/
|
879
|
-
static VALUE
|
880
|
-
ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
|
881
|
-
{
|
882
|
-
BIGNUM *add = NULL, *rem = NULL, *result;
|
883
|
-
int safe = 1, num;
|
884
|
-
VALUE vnum, vsafe, vadd, vrem, obj;
|
885
|
-
|
886
|
-
rb_scan_args(argc, argv, "13", &vnum, &vsafe, &vadd, &vrem);
|
887
|
-
|
888
|
-
num = NUM2INT(vnum);
|
889
|
-
|
890
|
-
if (vsafe == Qfalse) {
|
891
|
-
safe = 0;
|
892
|
-
}
|
893
|
-
if (!NIL_P(vadd)) {
|
894
|
-
add = GetBNPtr(vadd);
|
895
|
-
rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem);
|
896
|
-
}
|
897
|
-
obj = NewBN(klass);
|
898
|
-
if (!(result = BN_new())) {
|
899
|
-
ossl_raise(eBNError, NULL);
|
900
|
-
}
|
901
|
-
if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) {
|
902
|
-
BN_free(result);
|
903
|
-
ossl_raise(eBNError, NULL);
|
904
|
-
}
|
905
|
-
SetBN(obj, result);
|
906
|
-
|
907
|
-
return obj;
|
908
|
-
}
|
909
|
-
|
910
|
-
#define BIGNUM_NUM(func) \
|
911
|
-
static VALUE \
|
912
|
-
ossl_bn_##func(VALUE self) \
|
913
|
-
{ \
|
914
|
-
BIGNUM *bn; \
|
915
|
-
GetBN(self, bn); \
|
916
|
-
return INT2NUM(BN_##func(bn)); \
|
917
|
-
}
|
918
|
-
|
919
|
-
/*
|
920
|
-
* Document-method: OpenSSL::BN#num_bytes
|
921
|
-
* call-seq:
|
922
|
-
* bn.num_bytes => integer
|
923
|
-
*/
|
924
|
-
BIGNUM_NUM(num_bytes)
|
925
|
-
|
926
|
-
/*
|
927
|
-
* Document-method: OpenSSL::BN#num_bits
|
928
|
-
* call-seq:
|
929
|
-
* bn.num_bits => integer
|
930
|
-
*/
|
931
|
-
BIGNUM_NUM(num_bits)
|
932
|
-
|
933
|
-
static VALUE
|
934
|
-
ossl_bn_copy(VALUE self, VALUE other)
|
935
|
-
{
|
936
|
-
BIGNUM *bn1, *bn2;
|
937
|
-
|
938
|
-
rb_check_frozen(self);
|
939
|
-
|
940
|
-
if (self == other) return self;
|
941
|
-
|
942
|
-
GetBN(self, bn1);
|
943
|
-
bn2 = GetBNPtr(other);
|
944
|
-
|
945
|
-
if (!BN_copy(bn1, bn2)) {
|
946
|
-
ossl_raise(eBNError, NULL);
|
947
|
-
}
|
948
|
-
return self;
|
949
|
-
}
|
950
|
-
|
951
|
-
/*
|
952
|
-
* call-seq:
|
953
|
-
* +bn -> aBN
|
954
|
-
*/
|
955
|
-
static VALUE
|
956
|
-
ossl_bn_uplus(VALUE self)
|
957
|
-
{
|
958
|
-
VALUE obj;
|
959
|
-
BIGNUM *bn1, *bn2;
|
960
|
-
|
961
|
-
GetBN(self, bn1);
|
962
|
-
obj = NewBN(cBN);
|
963
|
-
bn2 = BN_dup(bn1);
|
964
|
-
if (!bn2)
|
965
|
-
ossl_raise(eBNError, "BN_dup");
|
966
|
-
SetBN(obj, bn2);
|
967
|
-
|
968
|
-
return obj;
|
969
|
-
}
|
970
|
-
|
971
|
-
/*
|
972
|
-
* call-seq:
|
973
|
-
* -bn -> aBN
|
974
|
-
*/
|
975
|
-
static VALUE
|
976
|
-
ossl_bn_uminus(VALUE self)
|
977
|
-
{
|
978
|
-
VALUE obj;
|
979
|
-
BIGNUM *bn1, *bn2;
|
980
|
-
|
981
|
-
GetBN(self, bn1);
|
982
|
-
obj = NewBN(cBN);
|
983
|
-
bn2 = BN_dup(bn1);
|
984
|
-
if (!bn2)
|
985
|
-
ossl_raise(eBNError, "BN_dup");
|
986
|
-
SetBN(obj, bn2);
|
987
|
-
BN_set_negative(bn2, !BN_is_negative(bn2));
|
988
|
-
|
989
|
-
return obj;
|
990
|
-
}
|
991
|
-
|
992
|
-
/*
|
993
|
-
* call-seq:
|
994
|
-
* bn.abs -> aBN
|
995
|
-
*/
|
996
|
-
static VALUE
|
997
|
-
ossl_bn_abs(VALUE self)
|
998
|
-
{
|
999
|
-
BIGNUM *bn1;
|
1000
|
-
|
1001
|
-
GetBN(self, bn1);
|
1002
|
-
if (BN_is_negative(bn1)) {
|
1003
|
-
return ossl_bn_uminus(self);
|
1004
|
-
}
|
1005
|
-
else {
|
1006
|
-
return ossl_bn_uplus(self);
|
1007
|
-
}
|
1008
|
-
}
|
1009
|
-
|
1010
|
-
#define BIGNUM_CMP(func) \
|
1011
|
-
static VALUE \
|
1012
|
-
ossl_bn_##func(VALUE self, VALUE other) \
|
1013
|
-
{ \
|
1014
|
-
BIGNUM *bn1, *bn2 = GetBNPtr(other); \
|
1015
|
-
GetBN(self, bn1); \
|
1016
|
-
return INT2NUM(BN_##func(bn1, bn2)); \
|
1017
|
-
}
|
1018
|
-
|
1019
|
-
/*
|
1020
|
-
* Document-method: OpenSSL::BN#cmp
|
1021
|
-
* call-seq:
|
1022
|
-
* bn.cmp(bn2) => integer
|
1023
|
-
*/
|
1024
|
-
/*
|
1025
|
-
* Document-method: OpenSSL::BN#<=>
|
1026
|
-
* call-seq:
|
1027
|
-
* bn <=> bn2 => integer
|
1028
|
-
*/
|
1029
|
-
BIGNUM_CMP(cmp)
|
1030
|
-
|
1031
|
-
/*
|
1032
|
-
* Document-method: OpenSSL::BN#ucmp
|
1033
|
-
* call-seq:
|
1034
|
-
* bn.ucmp(bn2) => integer
|
1035
|
-
*/
|
1036
|
-
BIGNUM_CMP(ucmp)
|
1037
|
-
|
1038
|
-
/*
|
1039
|
-
* call-seq:
|
1040
|
-
* bn == obj => true or false
|
1041
|
-
*
|
1042
|
-
* Returns +true+ only if _obj_ has the same value as _bn_. Contrast this
|
1043
|
-
* with OpenSSL::BN#eql?, which requires obj to be OpenSSL::BN.
|
1044
|
-
*/
|
1045
|
-
static VALUE
|
1046
|
-
ossl_bn_eq(VALUE self, VALUE other)
|
1047
|
-
{
|
1048
|
-
BIGNUM *bn1, *bn2;
|
1049
|
-
|
1050
|
-
GetBN(self, bn1);
|
1051
|
-
other = try_convert_to_bn(other);
|
1052
|
-
if (NIL_P(other))
|
1053
|
-
return Qfalse;
|
1054
|
-
GetBN(other, bn2);
|
1055
|
-
|
1056
|
-
if (!BN_cmp(bn1, bn2)) {
|
1057
|
-
return Qtrue;
|
1058
|
-
}
|
1059
|
-
return Qfalse;
|
1060
|
-
}
|
1061
|
-
|
1062
|
-
/*
|
1063
|
-
* call-seq:
|
1064
|
-
* bn.eql?(obj) => true or false
|
1065
|
-
*
|
1066
|
-
* Returns <code>true</code> only if <i>obj</i> is a
|
1067
|
-
* <code>OpenSSL::BN</code> with the same value as <i>bn</i>. Contrast this
|
1068
|
-
* with OpenSSL::BN#==, which performs type conversions.
|
1069
|
-
*/
|
1070
|
-
static VALUE
|
1071
|
-
ossl_bn_eql(VALUE self, VALUE other)
|
1072
|
-
{
|
1073
|
-
BIGNUM *bn1, *bn2;
|
1074
|
-
|
1075
|
-
if (!rb_obj_is_kind_of(other, cBN))
|
1076
|
-
return Qfalse;
|
1077
|
-
GetBN(self, bn1);
|
1078
|
-
GetBN(other, bn2);
|
1079
|
-
|
1080
|
-
return BN_cmp(bn1, bn2) ? Qfalse : Qtrue;
|
1081
|
-
}
|
1082
|
-
|
1083
|
-
/*
|
1084
|
-
* call-seq:
|
1085
|
-
* bn.hash => Integer
|
1086
|
-
*
|
1087
|
-
* Returns a hash code for this object.
|
1088
|
-
*
|
1089
|
-
* See also Object#hash.
|
1090
|
-
*/
|
1091
|
-
static VALUE
|
1092
|
-
ossl_bn_hash(VALUE self)
|
1093
|
-
{
|
1094
|
-
BIGNUM *bn;
|
1095
|
-
VALUE tmp, hash;
|
1096
|
-
unsigned char *buf;
|
1097
|
-
int len;
|
1098
|
-
|
1099
|
-
GetBN(self, bn);
|
1100
|
-
len = BN_num_bytes(bn);
|
1101
|
-
buf = ALLOCV(tmp, len);
|
1102
|
-
if (BN_bn2bin(bn, buf) != len) {
|
1103
|
-
ALLOCV_END(tmp);
|
1104
|
-
ossl_raise(eBNError, "BN_bn2bin");
|
1105
|
-
}
|
1106
|
-
|
1107
|
-
hash = ST2FIX(rb_memhash(buf, len));
|
1108
|
-
ALLOCV_END(tmp);
|
1109
|
-
|
1110
|
-
return hash;
|
1111
|
-
}
|
1112
|
-
|
1113
|
-
/*
|
1114
|
-
* call-seq:
|
1115
|
-
* bn.prime? => true | false
|
1116
|
-
* bn.prime?(checks) => true | false
|
1117
|
-
*
|
1118
|
-
* Performs a Miller-Rabin probabilistic primality test for +bn+.
|
1119
|
-
*
|
1120
|
-
* <b>+checks+ parameter is deprecated in version 3.0.</b> It has no effect.
|
1121
|
-
*/
|
1122
|
-
static VALUE
|
1123
|
-
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
|
1124
|
-
{
|
1125
|
-
BIGNUM *bn;
|
1126
|
-
int ret;
|
1127
|
-
|
1128
|
-
rb_check_arity(argc, 0, 1);
|
1129
|
-
GetBN(self, bn);
|
1130
|
-
|
1131
|
-
#ifdef HAVE_BN_CHECK_PRIME
|
1132
|
-
ret = BN_check_prime(bn, ossl_bn_ctx, NULL);
|
1133
|
-
if (ret < 0)
|
1134
|
-
ossl_raise(eBNError, "BN_check_prime");
|
1135
|
-
#else
|
1136
|
-
ret = BN_is_prime_fasttest_ex(bn, BN_prime_checks, ossl_bn_ctx, 1, NULL);
|
1137
|
-
if (ret < 0)
|
1138
|
-
ossl_raise(eBNError, "BN_is_prime_fasttest_ex");
|
1139
|
-
#endif
|
1140
|
-
return ret ? Qtrue : Qfalse;
|
1141
|
-
}
|
1142
|
-
|
1143
|
-
/*
|
1144
|
-
* call-seq:
|
1145
|
-
* bn.prime_fasttest? => true | false
|
1146
|
-
* bn.prime_fasttest?(checks) => true | false
|
1147
|
-
* bn.prime_fasttest?(checks, trial_div) => true | false
|
1148
|
-
*
|
1149
|
-
* Performs a Miller-Rabin probabilistic primality test for +bn+.
|
1150
|
-
*
|
1151
|
-
* <b>Deprecated in version 3.0.</b> Use #prime? instead.
|
1152
|
-
*
|
1153
|
-
* +checks+ and +trial_div+ parameters no longer have any effect.
|
1154
|
-
*/
|
1155
|
-
static VALUE
|
1156
|
-
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
|
1157
|
-
{
|
1158
|
-
rb_check_arity(argc, 0, 2);
|
1159
|
-
return ossl_bn_is_prime(0, argv, self);
|
1160
|
-
}
|
1161
|
-
|
1162
|
-
/*
|
1163
|
-
* call-seq:
|
1164
|
-
* bn.get_flags(flags) => flags
|
1165
|
-
*
|
1166
|
-
* Returns the flags on the BN object.
|
1167
|
-
* The argument is used as a bit mask.
|
1168
|
-
*
|
1169
|
-
* === Parameters
|
1170
|
-
* * _flags_ - integer
|
1171
|
-
*/
|
1172
|
-
static VALUE
|
1173
|
-
ossl_bn_get_flags(VALUE self, VALUE arg)
|
1174
|
-
{
|
1175
|
-
BIGNUM *bn;
|
1176
|
-
GetBN(self, bn);
|
1177
|
-
|
1178
|
-
return INT2NUM(BN_get_flags(bn, NUM2INT(arg)));
|
1179
|
-
}
|
1180
|
-
|
1181
|
-
/*
|
1182
|
-
* call-seq:
|
1183
|
-
* bn.set_flags(flags) => nil
|
1184
|
-
*
|
1185
|
-
* Enables the flags on the BN object.
|
1186
|
-
* Currently, the flags argument can contain zero of OpenSSL::BN::CONSTTIME.
|
1187
|
-
*/
|
1188
|
-
static VALUE
|
1189
|
-
ossl_bn_set_flags(VALUE self, VALUE arg)
|
1190
|
-
{
|
1191
|
-
BIGNUM *bn;
|
1192
|
-
GetBN(self, bn);
|
1193
|
-
|
1194
|
-
BN_set_flags(bn, NUM2INT(arg));
|
1195
|
-
return Qnil;
|
1196
|
-
}
|
1197
|
-
|
1198
|
-
/*
|
1199
|
-
* INIT
|
1200
|
-
* (NOTE: ordering of methods is the same as in 'man bn')
|
1201
|
-
*/
|
1202
|
-
void
|
1203
|
-
Init_ossl_bn(void)
|
1204
|
-
{
|
1205
|
-
#if 0
|
1206
|
-
mOSSL = rb_define_module("OpenSSL");
|
1207
|
-
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
1208
|
-
#endif
|
1209
|
-
|
1210
|
-
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
1211
|
-
ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
|
1212
|
-
#else
|
1213
|
-
ossl_bn_ctx_get();
|
1214
|
-
#endif
|
1215
|
-
|
1216
|
-
eBNError = rb_define_class_under(mOSSL, "BNError", eOSSLError);
|
1217
|
-
|
1218
|
-
cBN = rb_define_class_under(mOSSL, "BN", rb_cObject);
|
1219
|
-
|
1220
|
-
rb_define_alloc_func(cBN, ossl_bn_alloc);
|
1221
|
-
rb_define_method(cBN, "initialize", ossl_bn_initialize, -1);
|
1222
|
-
|
1223
|
-
rb_define_method(cBN, "initialize_copy", ossl_bn_copy, 1);
|
1224
|
-
rb_define_method(cBN, "copy", ossl_bn_copy, 1);
|
1225
|
-
|
1226
|
-
/* swap (=coerce?) */
|
1227
|
-
|
1228
|
-
rb_define_method(cBN, "num_bytes", ossl_bn_num_bytes, 0);
|
1229
|
-
rb_define_method(cBN, "num_bits", ossl_bn_num_bits, 0);
|
1230
|
-
/* num_bits_word */
|
1231
|
-
|
1232
|
-
rb_define_method(cBN, "+@", ossl_bn_uplus, 0);
|
1233
|
-
rb_define_method(cBN, "-@", ossl_bn_uminus, 0);
|
1234
|
-
rb_define_method(cBN, "abs", ossl_bn_abs, 0);
|
1235
|
-
|
1236
|
-
rb_define_method(cBN, "+", ossl_bn_add, 1);
|
1237
|
-
rb_define_method(cBN, "-", ossl_bn_sub, 1);
|
1238
|
-
rb_define_method(cBN, "*", ossl_bn_mul, 1);
|
1239
|
-
rb_define_method(cBN, "sqr", ossl_bn_sqr, 0);
|
1240
|
-
rb_define_method(cBN, "/", ossl_bn_div, 1);
|
1241
|
-
rb_define_method(cBN, "%", ossl_bn_mod, 1);
|
1242
|
-
/* nnmod */
|
1243
|
-
|
1244
|
-
rb_define_method(cBN, "mod_add", ossl_bn_mod_add, 2);
|
1245
|
-
rb_define_method(cBN, "mod_sub", ossl_bn_mod_sub, 2);
|
1246
|
-
rb_define_method(cBN, "mod_mul", ossl_bn_mod_mul, 2);
|
1247
|
-
rb_define_method(cBN, "mod_sqr", ossl_bn_mod_sqr, 1);
|
1248
|
-
rb_define_method(cBN, "mod_sqrt", ossl_bn_mod_sqrt, 1);
|
1249
|
-
rb_define_method(cBN, "**", ossl_bn_exp, 1);
|
1250
|
-
rb_define_method(cBN, "mod_exp", ossl_bn_mod_exp, 2);
|
1251
|
-
rb_define_method(cBN, "gcd", ossl_bn_gcd, 1);
|
1252
|
-
|
1253
|
-
/* add_word
|
1254
|
-
* sub_word
|
1255
|
-
* mul_word
|
1256
|
-
* div_word
|
1257
|
-
* mod_word */
|
1258
|
-
|
1259
|
-
rb_define_method(cBN, "cmp", ossl_bn_cmp, 1);
|
1260
|
-
rb_define_alias(cBN, "<=>", "cmp");
|
1261
|
-
rb_define_method(cBN, "ucmp", ossl_bn_ucmp, 1);
|
1262
|
-
rb_define_method(cBN, "eql?", ossl_bn_eql, 1);
|
1263
|
-
rb_define_method(cBN, "hash", ossl_bn_hash, 0);
|
1264
|
-
rb_define_method(cBN, "==", ossl_bn_eq, 1);
|
1265
|
-
rb_define_alias(cBN, "===", "==");
|
1266
|
-
rb_define_method(cBN, "zero?", ossl_bn_is_zero, 0);
|
1267
|
-
rb_define_method(cBN, "one?", ossl_bn_is_one, 0);
|
1268
|
-
/* is_word */
|
1269
|
-
rb_define_method(cBN, "odd?", ossl_bn_is_odd, 0);
|
1270
|
-
rb_define_method(cBN, "negative?", ossl_bn_is_negative, 0);
|
1271
|
-
|
1272
|
-
/* zero
|
1273
|
-
* one
|
1274
|
-
* value_one - DON'T IMPL.
|
1275
|
-
* set_word
|
1276
|
-
* get_word */
|
1277
|
-
|
1278
|
-
rb_define_singleton_method(cBN, "rand", ossl_bn_s_rand, -1);
|
1279
|
-
rb_define_singleton_method(cBN, "rand_range", ossl_bn_s_rand_range, 1);
|
1280
|
-
rb_define_alias(rb_singleton_class(cBN), "pseudo_rand", "rand");
|
1281
|
-
rb_define_alias(rb_singleton_class(cBN), "pseudo_rand_range", "rand_range");
|
1282
|
-
|
1283
|
-
rb_define_singleton_method(cBN, "generate_prime", ossl_bn_s_generate_prime, -1);
|
1284
|
-
rb_define_method(cBN, "prime?", ossl_bn_is_prime, -1);
|
1285
|
-
rb_define_method(cBN, "prime_fasttest?", ossl_bn_is_prime_fasttest, -1);
|
1286
|
-
|
1287
|
-
rb_define_method(cBN, "set_bit!", ossl_bn_set_bit, 1);
|
1288
|
-
rb_define_method(cBN, "clear_bit!", ossl_bn_clear_bit, 1);
|
1289
|
-
rb_define_method(cBN, "bit_set?", ossl_bn_is_bit_set, 1);
|
1290
|
-
rb_define_method(cBN, "mask_bits!", ossl_bn_mask_bits, 1);
|
1291
|
-
rb_define_method(cBN, "<<", ossl_bn_lshift, 1);
|
1292
|
-
rb_define_method(cBN, ">>", ossl_bn_rshift, 1);
|
1293
|
-
rb_define_method(cBN, "lshift!", ossl_bn_self_lshift, 1);
|
1294
|
-
rb_define_method(cBN, "rshift!", ossl_bn_self_rshift, 1);
|
1295
|
-
/* lshift1 - DON'T IMPL. */
|
1296
|
-
/* rshift1 - DON'T IMPL. */
|
1297
|
-
|
1298
|
-
rb_define_method(cBN, "get_flags", ossl_bn_get_flags, 1);
|
1299
|
-
rb_define_method(cBN, "set_flags", ossl_bn_set_flags, 1);
|
1300
|
-
|
1301
|
-
#ifdef BN_FLG_CONSTTIME
|
1302
|
-
rb_define_const(cBN, "CONSTTIME", INT2NUM(BN_FLG_CONSTTIME));
|
1303
|
-
#endif
|
1304
|
-
/* BN_FLG_MALLOCED and BN_FLG_STATIC_DATA seems for C programming.
|
1305
|
-
* Allowing them leads to memory leak.
|
1306
|
-
* So, for now, they are not exported
|
1307
|
-
#ifdef BN_FLG_MALLOCED
|
1308
|
-
rb_define_const(cBN, "MALLOCED", INT2NUM(BN_FLG_MALLOCED));
|
1309
|
-
#endif
|
1310
|
-
#ifdef BN_FLG_STATIC_DATA
|
1311
|
-
rb_define_const(cBN, "STATIC_DATA", INT2NUM(BN_FLG_STATIC_DATA));
|
1312
|
-
#endif
|
1313
|
-
*/
|
1314
|
-
|
1315
|
-
/*
|
1316
|
-
* bn2bin
|
1317
|
-
* bin2bn
|
1318
|
-
* bn2hex
|
1319
|
-
* bn2dec
|
1320
|
-
* hex2bn
|
1321
|
-
* dec2bn - all these are implemented in ossl_bn_initialize, and ossl_bn_to_s
|
1322
|
-
* print - NOT IMPL.
|
1323
|
-
* print_fp - NOT IMPL.
|
1324
|
-
* bn2mpi
|
1325
|
-
* mpi2bn
|
1326
|
-
*/
|
1327
|
-
rb_define_method(cBN, "to_s", ossl_bn_to_s, -1);
|
1328
|
-
rb_define_method(cBN, "to_i", ossl_bn_to_i, 0);
|
1329
|
-
rb_define_alias(cBN, "to_int", "to_i");
|
1330
|
-
rb_define_method(cBN, "to_bn", ossl_bn_to_bn, 0);
|
1331
|
-
rb_define_method(cBN, "coerce", ossl_bn_coerce, 1);
|
1332
|
-
|
1333
|
-
/*
|
1334
|
-
* TODO:
|
1335
|
-
* But how to: from_bin, from_mpi? PACK?
|
1336
|
-
* to_bin
|
1337
|
-
* to_mpi
|
1338
|
-
*/
|
1339
|
-
|
1340
|
-
rb_define_method(cBN, "mod_inverse", ossl_bn_mod_inverse, 1);
|
1341
|
-
|
1342
|
-
/* RECiProcal
|
1343
|
-
* MONTgomery */
|
1344
|
-
}
|