Class: OpenSSL::BN
- Inherits:
-
Object
- Object
- OpenSSL::BN
- Includes:
- Comparable
- Defined in:
- lib/openssl/bn.rb,
ossl_bn.c
Constant Summary collapse
- CONSTTIME =
INT2NUM(BN_FLG_CONSTTIME)
Class Method Summary collapse
-
.generate_prime(bits, [, safe [, add [, rem]]]) ⇒ Object
Generates a random prime number of bit length bits.
-
.rand ⇒ Object
zero one value_one - DON’T IMPL.
-
.rand_range(range) ⇒ Object
Generates a cryptographically strong pseudo-random number in the range 0…
range
.
Instance Method Summary collapse
- #%(bn2) ⇒ Object
- #*(bn2) ⇒ Object
- #**(bn2) ⇒ Object
- #+(bn2) ⇒ Object
- #+ ⇒ Object
- #-(bn2) ⇒ Object
- #- ⇒ Object
- #/ ⇒ Object
- #<<(bits) ⇒ Object
- #== ⇒ Object (also: #===)
- #>>(bits) ⇒ Object
- #abs ⇒ Object
- #bit_set? ⇒ Boolean
- #clear_bit!(bit) ⇒ self
- #cmp(bn2) ⇒ Integer (also: #<=>)
- #coerce(other) ⇒ Object
- #copy ⇒ Object
-
#eql?(obj) ⇒ Boolean
Returns
true
only if obj is aOpenSSL::BN
with the same value as bn. - #gcd(bn2) ⇒ Object
-
#get_flags(flags) ⇒ Object
Returns the flags on the BN object.
-
#hash ⇒ Integer
Returns a hash code for this object.
-
#initialize(*args) ⇒ Object
constructor
Construct a new OpenSSL BIGNUM object.
- #initialize_copy ⇒ Object
- #lshift!(bits) ⇒ self
- #mask_bits! ⇒ Object
- #mod_add(bn1, bn2) ⇒ Object
- #mod_exp(bn1, bn2) ⇒ Object
- #mod_inverse(bn2) ⇒ Object
- #mod_mul(bn1, bn2) ⇒ Object
- #mod_sqr(bn2) ⇒ Object
- #mod_sqrt(bn2) ⇒ Object
- #mod_sub(bn1, bn2) ⇒ Object
- #negative? ⇒ Boolean
- #num_bits ⇒ Integer
- #num_bytes ⇒ Integer
- #odd? ⇒ Boolean
- #one? ⇒ Boolean
- #pretty_print(q) ⇒ Object
-
#prime?(*args) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test for
bn
. -
#prime_fasttest?(*args) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test for
bn
. - #rshift!(bits) ⇒ self
- #set_bit!(bit) ⇒ self
-
#set_flags(flags) ⇒ nil
Enables the flags on the BN object.
- #sqr ⇒ Object
- #to_bn ⇒ Object
- #to_i ⇒ Integer (also: #to_int)
-
#to_s(base = 10) ⇒ String
Returns the string representation of the bignum.
- #ucmp(bn2) ⇒ Integer
- #zero? ⇒ Boolean
Constructor Details
#OpenSSL::BN.new(bn) ⇒ Object #OpenSSL::BN.new(integer) ⇒ Object #OpenSSL::BN.new(string, base = 10) ⇒ Object
Construct a new OpenSSL BIGNUM object.
If bn
is an Integer or OpenSSL::BN, a new instance of OpenSSL::BN representing the same value is returned. See also Integer#to_bn for the short-hand.
If a String is given, the content will be parsed according to base
.
string
-
The string to be parsed.
base
-
The format. Must be one of the following:
-
0
- MPI format. See the man page BN_mpi2bn(3) for details. -
2
- Variable-length and big-endian binary encoding of a positive number. -
10
- Decimal number representation, with a leading ‘-’ for a negative number. -
16
- Hexadecimal number representation, with a leading ‘-’ for a negative number.
-
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'ossl_bn.c', line 246
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");
}
rb_check_frozen(self);
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_prime(bits, [, safe [, add [, rem]]]) ⇒ Object
Generates a random prime number of bit length bits. If safe is set to true
, generates a safe prime. If add is specified, generates a prime that fulfills condition p % add = rem
.
Parameters
-
bits - integer
-
safe - boolean
-
add - BN
-
rem - BN
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 |
# File 'ossl_bn.c', line 878
static VALUE
ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
{
BIGNUM *add = NULL, *rem = NULL, *result;
int safe = 1, num;
VALUE vnum, vsafe, vadd, vrem, obj;
rb_scan_args(argc, argv, "13", &vnum, &vsafe, &vadd, &vrem);
num = NUM2INT(vnum);
if (vsafe == Qfalse) {
safe = 0;
}
if (!NIL_P(vadd)) {
add = GetBNPtr(vadd);
rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem);
}
obj = NewBN(klass);
if (!(result = BN_new())) {
ossl_raise(eBNError, NULL);
}
if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) {
BN_free(result);
ossl_raise(eBNError, NULL);
}
SetBN(obj, result);
return obj;
}
|
.rand ⇒ Object
zero one value_one - DON’T IMPL. set_word get_word
.rand_range(range) ⇒ Object
Generates a cryptographically strong pseudo-random number in the range 0…range
.
See also the man page BN_rand_range(3).
849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'ossl_bn.c', line 849
static VALUE
ossl_bn_s_rand_range(VALUE klass, VALUE range)
{
BIGNUM *bn = GetBNPtr(range), *result;
VALUE obj = NewBN(klass);
if (!(result = BN_new()))
ossl_raise(eBNError, "BN_new");
if (BN_rand_range(result, bn) <= 0) {
BN_free(result);
ossl_raise(eBNError, "BN_rand_range");
}
SetBN(obj, result);
return obj;
}
|
Instance Method Details
#%(bn2) ⇒ Object
#*(bn2) ⇒ Object
#**(bn2) ⇒ Object
#+(bn2) ⇒ Object
#+ ⇒ Object
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'ossl_bn.c', line 954
static VALUE
ossl_bn_uplus(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);
return obj;
}
|
#-(bn2) ⇒ Object
#- ⇒ Object
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 |
# File 'ossl_bn.c', line 974
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;
}
|
#/ ⇒ Object
#<<(bits) ⇒ Object
#== ⇒ Object Also known as: ===
#>>(bits) ⇒ Object
#abs ⇒ Object
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 |
# File 'ossl_bn.c', line 995
static VALUE
ossl_bn_abs(VALUE self)
{
BIGNUM *bn1;
GetBN(self, bn1);
if (BN_is_negative(bn1)) {
return ossl_bn_uminus(self);
}
else {
return ossl_bn_uplus(self);
}
}
|
#bit_set? ⇒ Boolean
#clear_bit!(bit) ⇒ self
#cmp(bn2) ⇒ Integer Also known as: <=>
#coerce(other) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'ossl_bn.c', line 397
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);
}
|
#copy ⇒ Object
#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.
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
# File 'ossl_bn.c', line 1069
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
#get_flags(flags) ⇒ Object
Returns the flags on the BN object. The argument is used as a bit mask.
Parameters
-
flags - integer
1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'ossl_bn.c', line 1171
static VALUE
ossl_bn_get_flags(VALUE self, VALUE arg)
{
BIGNUM *bn;
GetBN(self, bn);
return INT2NUM(BN_get_flags(bn, NUM2INT(arg)));
}
|
#hash ⇒ Integer
Returns a hash code for this object.
See also Object#hash.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'ossl_bn.c', line 1090
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_copy ⇒ Object
#lshift!(bits) ⇒ self
#mask_bits! ⇒ Object
#mod_add(bn1, bn2) ⇒ Object
#mod_exp(bn1, bn2) ⇒ Object
#mod_inverse(bn2) ⇒ Object
#mod_mul(bn1, bn2) ⇒ Object
#mod_sqr(bn2) ⇒ Object
#mod_sqrt(bn2) ⇒ Object
#mod_sub(bn1, bn2) ⇒ Object
#negative? ⇒ Boolean
#num_bits ⇒ Integer
#num_bytes ⇒ Integer
#odd? ⇒ Boolean
#one? ⇒ 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 for bn
.
checks
parameter is deprecated in version 3.0. It has no effect.
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 |
# File 'ossl_bn.c', line 1121
static VALUE
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
int ret;
rb_check_arity(argc, 0, 1);
GetBN(self, bn);
#ifdef HAVE_BN_CHECK_PRIME
ret = BN_check_prime(bn, ossl_bn_ctx, NULL);
if (ret < 0)
ossl_raise(eBNError, "BN_check_prime");
#else
ret = BN_is_prime_fasttest_ex(bn, BN_prime_checks, ossl_bn_ctx, 1, NULL);
if (ret < 0)
ossl_raise(eBNError, "BN_is_prime_fasttest_ex");
#endif
return ret ? Qtrue : Qfalse;
}
|
#prime_fasttest? ⇒ Boolean #prime_fasttest?(checks) ⇒ Boolean #prime_fasttest?(checks, trial_div) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test for bn
.
Deprecated in version 3.0. Use #prime? instead.
checks
and trial_div
parameters no longer have any effect.
1154 1155 1156 1157 1158 1159 |
# File 'ossl_bn.c', line 1154
static VALUE
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 2);
return ossl_bn_is_prime(0, argv, self);
}
|
#rshift!(bits) ⇒ self
#set_bit!(bit) ⇒ self
#set_flags(flags) ⇒ nil
Enables the flags on the BN object. Currently, the flags argument can contain zero of OpenSSL::BN::CONSTTIME.
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'ossl_bn.c', line 1187
static VALUE
ossl_bn_set_flags(VALUE self, VALUE arg)
{
BIGNUM *bn;
GetBN(self, bn);
rb_check_frozen(self);
BN_set_flags(bn, NUM2INT(arg));
return Qnil;
}
|
#sqr ⇒ Object
#to_bn ⇒ Object
391 392 393 394 395 |
# File 'ossl_bn.c', line 391
static VALUE
ossl_bn_to_bn(VALUE self)
{
return self;
}
|
#to_i ⇒ Integer Also known as: to_int
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'ossl_bn.c', line 373
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_s(base = 10) ⇒ String
Returns the string representation of the bignum.
BN.new can parse the encoded string to convert back into an OpenSSL::BN.
base
-
The format. Must be one of the following:
-
0
- MPI format. See the man page BN_bn2mpi(3) for details. -
2
- Variable-length and big-endian binary encoding. The sign of the bignum is ignored. -
10
- Decimal number representation, with a leading ‘-’ for a negative bignum. -
16
- Hexadecimal number representation, with a leading ‘-’ for a negative bignum.
-
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'ossl_bn.c', line 329
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;
}
|