Class: OpenSSL::BN

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#newObject #new(bn) ⇒ Object #new(string) ⇒ Object #new(string, 0|2|10|16) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ossl_bn.c', line 112

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

    if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
	base = NUM2INT(bs);
    }
    StringValue(str);
    GetBN(self, bn);
    if (RTEST(rb_obj_is_kind_of(str, cBN))) {
	BIGNUM *other;

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

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

Class Method Details

.generate_primeObject

.pseudo_randObject

.pseudo_rand_rangeObject

.randObject

zero one value_one - DON'T IMPL. set_word get_word

.rand_rangeObject

Instance Method Details

#%Object

#*Object

#**Object

#+Object

num_bits_word

#-Object

#/Object

#<<Object

#>>Object

#bit_set?Boolean

Returns:

  • (Boolean)

#clear_bit!Object

#cmpObject Also known as: <=>

add_word sub_word mul_word div_word mod_word

#coerceObject


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ossl_bn.c', line 242

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?Boolean Also known as: ==, ===

Returns:

  • (Boolean)

#gcdObject

#lshift!Object

#mask_bits!Object

#mod_addObject

nnmod

#mod_expObject

#mod_inverseObject

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

#mod_mulObject

#mod_sqrObject

#mod_subObject

#num_bitsObject

#num_bytesObject

swap (=coerce?)

#odd?Boolean

is_word

Returns:

  • (Boolean)

#one?Boolean

Returns:

  • (Boolean)

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

Parameters

  • checks - integer

Returns:

  • (Boolean)

667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'ossl_bn.c', line 667

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(bn, checks, NULL, 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

Parameters

  • checks - integer

  • trial_div - boolean

Returns:

  • (Boolean)

700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'ossl_bn.c', line 700

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(bn, checks, NULL, ossl_bn_ctx, NULL, do_trial_division)) {
    case 1:
	return Qtrue;
    case 0:
	return Qfalse;
    default:
	ossl_raise(eBNError, NULL);
    }
    /* not reachable */
    return Qnil;
}

#rshift!Object

#set_bit!Object

#sqrObject

#to_bnObject


236
237
238
239
240
# File 'ossl_bn.c', line 236

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

#to_iInteger Also known as: to_int

Returns:


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'ossl_bn.c', line 218

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

    GetBN(self, bn);

    if (!(txt = BN_bn2dec(bn))) {
	ossl_raise(eBNError, NULL);
    }
    num = rb_cstr_to_inum(txt, 10, 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)

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'ossl_bn.c', line 174

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;
}

#ucmpObject

#zero?Boolean

Returns:

  • (Boolean)