Class: OpenSSL::BN

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/openssl/bn.rb,
ext/openssl/ossl_bn.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#OpenSSL::BN.new(bn) ⇒ Object #OpenSSL::BN.new(integer) ⇒ Object #OpenSSL::BN.new(string) ⇒ Object #OpenSSL::BN.new(string, 0|2|10|16) ⇒ Object

Construct a new OpenSSL BIGNUM object.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/openssl/ossl_bn.c', line 233

static VALUE
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
{
    BIGNUM *bn;
    VALUE str, bs;
    int base = 10;
    char *ptr;

    if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
	base = NUM2INT(bs);
    }

    if (NIL_P(str)) {
        ossl_raise(rb_eArgError, "invalid argument");
    }

    if (RB_INTEGER_TYPE_P(str)) {
	GetBN(self, bn);
	integer_to_bnptr(str, bn);

	return self;
    }

    if (RTEST(rb_obj_is_kind_of(str, cBN))) {
	BIGNUM *other;

	GetBN(self, bn);
	GetBN(str, other); /* Safe - we checked kind_of? above */
	if (!BN_copy(bn, other)) {
	    ossl_raise(eBNError, NULL);
	}
	return self;
    }

    GetBN(self, bn);
    switch (base) {
    case 0:
        ptr = StringValuePtr(str);
        if (!BN_mpi2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
	    ossl_raise(eBNError, NULL);
	}
	break;
    case 2:
        ptr = StringValuePtr(str);
        if (!BN_bin2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
	    ossl_raise(eBNError, NULL);
	}
	break;
    case 10:
	if (!BN_dec2bn(&bn, StringValueCStr(str))) {
	    ossl_raise(eBNError, NULL);
	}
	break;
    case 16:
	if (!BN_hex2bn(&bn, StringValueCStr(str))) {
	    ossl_raise(eBNError, NULL);
	}
	break;
    default:
	ossl_raise(rb_eArgError, "invalid radix %d", base);
    }
    return self;
}

Class Method Details

.generate_primeObject

.pseudo_randObject

BN.pseudo_rand(bits [, fill [, odd]]) -> aBN

.pseudo_rand_range(range) ⇒ Object

.randObject

BN.rand(bits [, fill [, odd]]) -> aBN

.rand_range(range) ⇒ Object

Instance Method Details

#%(bn2) ⇒ Object

#*(bn2) ⇒ Object

#**(bn2) ⇒ Object

#+(bn2) ⇒ Object

#+Object



936
937
938
939
940
# File 'ext/openssl/ossl_bn.c', line 936

static VALUE
ossl_bn_uplus(VALUE self)
{
    return self;
}

#-(bn2) ⇒ Object

#-Object



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'ext/openssl/ossl_bn.c', line 946

static VALUE
ossl_bn_uminus(VALUE self)
{
    VALUE obj;
    BIGNUM *bn1, *bn2;

    GetBN(self, bn1);
    obj = NewBN(cBN);
    bn2 = BN_dup(bn1);
    if (!bn2)
	ossl_raise(eBNError, "BN_dup");
    SetBN(obj, bn2);
    BN_set_negative(bn2, !BN_is_negative(bn2));

    return obj;
}

#/(bn2) ⇒ Array

Division of OpenSSL::BN instances

Returns:

  • (Array)


581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'ext/openssl/ossl_bn.c', line 581

static VALUE
ossl_bn_div(VALUE self, VALUE other)
{
    BIGNUM *bn1, *bn2 = GetBNPtr(other), *r1, *r2;
    VALUE klass, obj1, obj2;

    GetBN(self, bn1);

    klass = rb_obj_class(self);
    obj1 = NewBN(klass);
    obj2 = NewBN(klass);
    if (!(r1 = BN_new())) {
	ossl_raise(eBNError, NULL);
    }
    if (!(r2 = BN_new())) {
	BN_free(r1);
	ossl_raise(eBNError, NULL);
    }
    if (!BN_div(r1, r2, bn1, bn2, ossl_bn_ctx)) {
	BN_free(r1);
	BN_free(r2);
	ossl_raise(eBNError, NULL);
    }
    SetBN(obj1, r1);
    SetBN(obj2, r2);

    return rb_ary_new3(2, obj1, obj2);
}

#<<(bits) ⇒ Object

#==Object Also known as: ===

#>>(bits) ⇒ Object

#bit_set?Boolean

Returns:

  • (Boolean)

#clear_bit!(bit) ⇒ self

Returns:

  • (self)

#cmp(bn2) ⇒ Integer Also known as: <=>

Returns:

#coerce(other) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'ext/openssl/ossl_bn.c', line 378

static VALUE
ossl_bn_coerce(VALUE self, VALUE other)
{
    switch(TYPE(other)) {
    case T_STRING:
	self = ossl_bn_to_s(0, NULL, self);
	break;
    case T_FIXNUM:
    case T_BIGNUM:
	self = ossl_bn_to_i(self);
	break;
    default:
	if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
	    ossl_raise(rb_eTypeError, "Don't know how to coerce");
	}
    }
    return rb_assoc_new(other, self);
}

#copyObject

#eql?(obj) ⇒ Boolean

Returns true only if obj is a OpenSSL::BN with the same value as bn. Contrast this with OpenSSL::BN#==, which performs type conversions.

Returns:

  • (Boolean)


1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'ext/openssl/ossl_bn.c', line 1023

static VALUE
ossl_bn_eql(VALUE self, VALUE other)
{
    BIGNUM *bn1, *bn2;

    if (!rb_obj_is_kind_of(other, cBN))
	return Qfalse;
    GetBN(self, bn1);
    GetBN(other, bn2);

    return BN_cmp(bn1, bn2) ? Qfalse : Qtrue;
}

#gcd(bn2) ⇒ Object

#hashInteger

Returns a hash code for this object.

See also Object#hash.

Returns:



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'ext/openssl/ossl_bn.c', line 1044

static VALUE
ossl_bn_hash(VALUE self)
{
    BIGNUM *bn;
    VALUE tmp, hash;
    unsigned char *buf;
    int len;

    GetBN(self, bn);
    len = BN_num_bytes(bn);
    buf = ALLOCV(tmp, len);
    if (BN_bn2bin(bn, buf) != len) {
	ALLOCV_END(tmp);
	ossl_raise(eBNError, "BN_bn2bin");
    }

    hash = ST2FIX(rb_memhash(buf, len));
    ALLOCV_END(tmp);

    return hash;
}

#initialize_copyObject

#lshift!(bits) ⇒ self

Returns:

  • (self)

#mask_bits!Object

#mod_add(bn1, bn2) ⇒ Object

#mod_exp(bn1, bn2) ⇒ Object

#mod_inverseObject

TODO: But how to: from_bin, from_mpi? PACK? to_bin to_mpi

#mod_mul(bn1, bn2) ⇒ Object

#mod_sqr(bn2) ⇒ Object

#mod_sub(bn1, bn2) ⇒ Object

#negative?Boolean

Returns:

  • (Boolean)

#num_bitsInteger

Returns:

#num_bytesInteger

Returns:

#odd?Boolean

Returns:

  • (Boolean)

#one?Boolean

Returns:

  • (Boolean)

#pretty_print(q) ⇒ Object



20
21
22
23
24
25
# File 'lib/openssl/bn.rb', line 20

def pretty_print(q)
  q.object_group(self) {
    q.text ' '
    q.text to_i.to_s
  }
end

#prime?Boolean #prime?(checks) ⇒ Boolean

Performs a Miller-Rabin probabilistic primality test with checks iterations. If checks is not specified, a number of iterations is used that yields a false positive rate of at most 2^-80 for random input.

Parameters

  • checks - integer

Returns:

  • (Boolean)


1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'ext/openssl/ossl_bn.c', line 1078

static VALUE
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
{
    BIGNUM *bn;
    VALUE vchecks;
    int checks = BN_prime_checks;

    if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
	checks = NUM2INT(vchecks);
    }
    GetBN(self, bn);
    switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
    case 1:
	return Qtrue;
    case 0:
	return Qfalse;
    default:
	ossl_raise(eBNError, NULL);
    }
    /* not reachable */
    return Qnil;
}

#prime_fasttest?Boolean #prime_fasttest?(checks) ⇒ Boolean #prime_fasttest?(checks, trial_div) ⇒ Boolean

Performs a Miller-Rabin primality test. This is same as #prime? except this first attempts trial divisions with some small primes.

Parameters

  • checks - integer

  • trial_div - boolean

Returns:

  • (Boolean)


1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'ext/openssl/ossl_bn.c', line 1114

static VALUE
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
{
    BIGNUM *bn;
    VALUE vchecks, vtrivdiv;
    int checks = BN_prime_checks, do_trial_division = 1;

    rb_scan_args(argc, argv, "02", &vchecks, &vtrivdiv);

    if (!NIL_P(vchecks)) {
	checks = NUM2INT(vchecks);
    }
    GetBN(self, bn);
    /* handle true/false */
    if (vtrivdiv == Qfalse) {
	do_trial_division = 0;
    }
    switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
    case 1:
	return Qtrue;
    case 0:
	return Qfalse;
    default:
	ossl_raise(eBNError, NULL);
    }
    /* not reachable */
    return Qnil;
}

#rshift!(bits) ⇒ self

Returns:

  • (self)

#set_bit!(bit) ⇒ self

Returns:

  • (self)

#sqrObject

#to_bnObject



372
373
374
375
376
# File 'ext/openssl/ossl_bn.c', line 372

static VALUE
ossl_bn_to_bn(VALUE self)
{
    return self;
}

#to_iInteger Also known as: to_int

Returns:



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'ext/openssl/ossl_bn.c', line 354

static VALUE
ossl_bn_to_i(VALUE self)
{
    BIGNUM *bn;
    char *txt;
    VALUE num;

    GetBN(self, bn);

    if (!(txt = BN_bn2hex(bn))) {
	ossl_raise(eBNError, NULL);
    }
    num = rb_cstr_to_inum(txt, 16, Qtrue);
    OPENSSL_free(txt);

    return num;
}

#to_sString #to_s(base) ⇒ String

Parameters

  • base - Integer Valid values:

    • 0 - MPI

    • 2 - binary

    • 10 - the default

    • 16 - hex

Overloads:

  • #to_sString

    Returns:

    • (String)
  • #to_s(base) ⇒ String

    Returns:

    • (String)


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'ext/openssl/ossl_bn.c', line 310

static VALUE
ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
{
    BIGNUM *bn;
    VALUE str, bs;
    int base = 10, len;
    char *buf;

    if (rb_scan_args(argc, argv, "01", &bs) == 1) {
	base = NUM2INT(bs);
    }
    GetBN(self, bn);
    switch (base) {
    case 0:
	len = BN_bn2mpi(bn, NULL);
        str = rb_str_new(0, len);
	if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
	    ossl_raise(eBNError, NULL);
	break;
    case 2:
	len = BN_num_bytes(bn);
        str = rb_str_new(0, len);
	if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
	    ossl_raise(eBNError, NULL);
	break;
    case 10:
	if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
	str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
	break;
    case 16:
	if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
	str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
	break;
    default:
	ossl_raise(rb_eArgError, "invalid radix %d", base);
    }

    return str;
}

#ucmp(bn2) ⇒ Integer

Returns:

#zero?Boolean

Returns:

  • (Boolean)