14 #define WrapBN(klass, obj, bn) do { \ 16 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ 18 (obj) = Data_Wrap_Struct((klass), 0, BN_clear_free, (bn)); \ 21 #define GetBN(obj, bn) do { \ 22 Data_Get_Struct((obj), BIGNUM, (bn)); \ 24 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ 28 #define SafeGetBN(obj, bn) do { \ 29 OSSL_Check_Kind((obj), cBN); \ 48 newbn = bn ? BN_dup(bn) : BN_new();
64 }
else switch (
TYPE(obj)) {
97 if (!(bn = BN_new())) {
126 unsigned char bin[
sizeof(long)];
128 unsigned long un =
labs(n);
130 for (i =
sizeof(
long) - 1; 0 <= i; i--) {
136 if (!BN_bin2bn(
bin,
sizeof(
bin), bn)) {
139 if (n < 0) BN_set_negative(bn, 1);
148 for (i = 0; len > i; i++) {
170 if (!BN_copy(bn, other)) {
232 len = BN_bn2mpi(bn,
NULL);
234 if (BN_bn2mpi(bn, (
unsigned char *)
RSTRING_PTR(str)) != len)
238 len = BN_num_bytes(bn);
240 if (BN_bn2bin(bn, (
unsigned char *)
RSTRING_PTR(str)) != len)
271 if (!(txt = BN_bn2hex(bn))) {
289 switch(
TYPE(other)) {
305 #define BIGNUM_BOOL1(func) \ 312 ossl_bn_##func(VALUE self) \ 316 if (BN_##func(bn)) { \ 325 #define BIGNUM_1c(func) \ 332 ossl_bn_##func(VALUE self) \ 334 BIGNUM *bn, *result; \ 337 if (!(result = BN_new())) { \ 338 ossl_raise(eBNError, NULL); \ 340 if (!BN_##func(result, bn, ossl_bn_ctx)) { \ 342 ossl_raise(eBNError, NULL); \ 344 WrapBN(CLASS_OF(self), obj, result); \ 349 #define BIGNUM_2(func) \ 356 ossl_bn_##func(VALUE self, VALUE other) \ 358 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 361 if (!(result = BN_new())) { \ 362 ossl_raise(eBNError, NULL); \ 364 if (!BN_##func(result, bn1, bn2)) { \ 366 ossl_raise(eBNError, NULL); \ 368 WrapBN(CLASS_OF(self), obj, result); \ 374 #define BIGNUM_2c(func) \ 381 ossl_bn_##func(VALUE self, VALUE other) \ 383 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 386 if (!(result = BN_new())) { \ 387 ossl_raise(eBNError, NULL); \ 389 if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \ 391 ossl_raise(eBNError, NULL); \ 393 WrapBN(CLASS_OF(self), obj, result); \ 410 BIGNUM *bn1, *bn2 =
GetBNPtr(other), *r1, *r2;
415 if (!(r1 = BN_new())) {
418 if (!(r2 = BN_new())) {
433 #define BIGNUM_3c(func) \ 440 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \ 442 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \ 443 BIGNUM *bn3 = GetBNPtr(other2), *result; \ 446 if (!(result = BN_new())) { \ 447 ossl_raise(eBNError, NULL); \ 449 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \ 451 ossl_raise(eBNError, NULL); \ 453 WrapBN(CLASS_OF(self), obj, result); \ 461 #define BIGNUM_BIT(func) \ 468 ossl_bn_##func(VALUE self, VALUE bit) \ 472 if (!BN_##func(bn, NUM2INT(bit))) { \ 473 ossl_raise(eBNError, NULL); \ 493 if (BN_is_bit_set(bn, b)) {
499 #define BIGNUM_SHIFT(func) \ 506 ossl_bn_##func(VALUE self, VALUE bits) \ 508 BIGNUM *bn, *result; \ 513 if (!(result = BN_new())) { \ 514 ossl_raise(eBNError, NULL); \ 516 if (!BN_##func(result, bn, b)) { \ 518 ossl_raise(eBNError, NULL); \ 520 WrapBN(CLASS_OF(self), obj, result); \ 526 #define BIGNUM_SELF_SHIFT(func) \ 533 ossl_bn_self_##func(VALUE self, VALUE bits) \ 539 if (!BN_##func(bn, bn, b)) \ 540 ossl_raise(eBNError, NULL); \ 546 #define BIGNUM_RAND(func) \ 553 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \ 556 int bottom = 0, top = 0, b; \ 557 VALUE bits, fill, odd, obj; \ 559 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \ 561 bottom = (odd == Qtrue) ? 1 : 0; \ 564 top = NUM2INT(fill); \ 567 if (!(result = BN_new())) { \ 568 ossl_raise(eBNError, NULL); \ 570 if (!BN_##func(result, b, top, bottom)) { \ 572 ossl_raise(eBNError, NULL); \ 574 WrapBN(klass, obj, result); \ 580 #define BIGNUM_RAND_RANGE(func) \ 587 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ 589 BIGNUM *bn = GetBNPtr(range), *result; \ 591 if (!(result = BN_new())) { \ 592 ossl_raise(eBNError, NULL); \ 594 if (!BN_##func##_range(result, bn)) { \ 596 ossl_raise(eBNError, NULL); \ 598 WrapBN(klass, obj, result); \ 619 VALUE vnum, vsafe, vadd, vrem, obj;
632 if (!(
result = BN_new())) {
644 #define BIGNUM_NUM(func) \ 651 ossl_bn_##func(VALUE self) \ 655 return INT2FIX(BN_##func(bn)); \ 667 if (
self == other)
return self;
672 if (!BN_copy(bn1, bn2)) {
678 #define BIGNUM_CMP(func) \ 685 ossl_bn_##func(VALUE self, VALUE other) \ 687 BIGNUM *bn1, *bn2 = GetBNPtr(other); \ 689 return INT2FIX(BN_##func(bn1, bn2)); \ 697 if (ossl_bn_cmp(
self, other) ==
INT2FIX(0)) {
716 int checks = BN_prime_checks;
748 VALUE vchecks, vtrivdiv;
749 int checks = BN_prime_checks, do_trial_division = 1;
753 if (!
NIL_P(vchecks)) {
759 do_trial_division = 0;
#define BIGNUM_RAND_RANGE(func)
size_t strlen(const char *)
static VALUE ossl_bn_alloc(VALUE klass)
static int is_zero(VALUE x)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
#define BIGNUM_RAND(func)
static long labs(long const x)
static VALUE ossl_bn_to_bn(VALUE self)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static int is_one(VALUE x)
static VALUE ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
#define RB_TYPE_P(obj, type)
#define BIGNUM_SHIFT(func)
#define rb_define_copy_func(klass, func)
#define BIGNUM_BOOL1(func)
RUBY_EXTERN VALUE rb_cObject
#define ALLOCV_N(type, v, n)
static VALUE ossl_bn_to_i(VALUE self)
#define RBIGNUM_DIGITS(b)
static VALUE ossl_bn_eql(VALUE self, VALUE other)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
static VALUE ossl_bn_coerce(VALUE self, VALUE other)
static VALUE ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
static VALUE ossl_bn_copy(VALUE self, VALUE other)
VALUE ossl_buf2str(char *buf, int len)
void ossl_raise(VALUE exc, const char *fmt,...)
VALUE ossl_bn_new(const BIGNUM *bn)
#define BIGNUM_SELF_SHIFT(func)
#define StringValuePtr(v)
#define RSTRING_LENINT(str)
#define rb_check_frozen(obj)
BIGNUM * GetBNPtr(VALUE obj)
VALUE rb_define_module(const char *name)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
#define RBIGNUM_LENINT(b)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static VALUE ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
static VALUE ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
#define WrapBN(klass, obj, bn)
VALUE rb_str_new(const char *, long)