13 #define NewBN(klass) \ 14 TypedData_Wrap_Struct((klass), &ossl_bn_type, 0) 15 #define SetBN(obj, bn) do { \ 17 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ 19 RTYPEDDATA_DATA(obj) = (bn); \ 22 #define GetBN(obj, bn) do { \ 23 TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \ 25 ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ 29 #define SafeGetBN(obj, bn) do { \ 30 OSSL_Check_Kind((obj), cBN); \ 69 newbn = bn ? BN_dup(bn) : BN_new();
85 unsigned char bin[
sizeof(long)];
87 unsigned long un =
labs(n);
89 for (i =
sizeof(
long) - 1; 0 <= i; i--) {
94 bn = BN_bin2bn(
bin,
sizeof(
bin), orig);
98 BN_set_negative(bn, 1);
109 bin = (
unsigned char*)
ALLOCV_N(
unsigned char, buf, len);
112 bn = BN_bin2bn(bin, (
int)len, orig);
117 BN_set_negative(bn, 1);
171 if (!(bn = BN_new())) {
213 if (!BN_copy(bn, other)) {
280 len = BN_num_bytes(bn);
313 if (!(txt = BN_bn2hex(bn))) {
331 switch(
TYPE(other)) {
347 #define BIGNUM_BOOL1(func) \ 349 ossl_bn_##func(VALUE self) \ 353 if (BN_##func(bn)) { \ 380 #define BIGNUM_1c(func) \ 382 ossl_bn_##func(VALUE self) \ 384 BIGNUM *bn, *result; \ 387 obj = NewBN(rb_obj_class(self)); \ 388 if (!(result = BN_new())) { \ 389 ossl_raise(eBNError, NULL); \ 391 if (!BN_##func(result, bn, ossl_bn_ctx)) { \ 393 ossl_raise(eBNError, NULL); \ 395 SetBN(obj, result); \ 406 #define BIGNUM_2(func) \ 408 ossl_bn_##func(VALUE self, VALUE other) \ 410 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 413 obj = NewBN(rb_obj_class(self)); \ 414 if (!(result = BN_new())) { \ 415 ossl_raise(eBNError, NULL); \ 417 if (!BN_##func(result, bn1, bn2)) { \ 419 ossl_raise(eBNError, NULL); \ 421 SetBN(obj, result); \ 439 #define BIGNUM_2c(func) \ 441 ossl_bn_##func(VALUE self, VALUE other) \ 443 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 446 obj = NewBN(rb_obj_class(self)); \ 447 if (!(result = BN_new())) { \ 448 ossl_raise(eBNError, NULL); \ 450 if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \ 452 ossl_raise(eBNError, NULL); \ 454 SetBN(obj, result); \ 511 VALUE klass, obj1, obj2;
518 if (!(
r1 = BN_new())) {
521 if (!(r2 = BN_new())) {
536 #define BIGNUM_3c(func) \ 538 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \ 540 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \ 541 BIGNUM *bn3 = GetBNPtr(other2), *result; \ 544 obj = NewBN(rb_obj_class(self)); \ 545 if (!(result = BN_new())) { \ 546 ossl_raise(eBNError, NULL); \ 548 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \ 550 ossl_raise(eBNError, NULL); \ 552 SetBN(obj, result); \ 584 #define BIGNUM_BIT(func) \ 586 ossl_bn_##func(VALUE self, VALUE bit) \ 590 if (!BN_##func(bn, NUM2INT(bit))) { \ 591 ossl_raise(eBNError, NULL); \ 632 if (BN_is_bit_set(bn, b)) {
638 #define BIGNUM_SHIFT(func) \ 640 ossl_bn_##func(VALUE self, VALUE bits) \ 642 BIGNUM *bn, *result; \ 647 obj = NewBN(rb_obj_class(self)); \ 648 if (!(result = BN_new())) { \ 649 ossl_raise(eBNError, NULL); \ 651 if (!BN_##func(result, bn, b)) { \ 653 ossl_raise(eBNError, NULL); \ 655 SetBN(obj, result); \ 673 #define BIGNUM_SELF_SHIFT(func) \ 675 ossl_bn_self_##func(VALUE self, VALUE bits) \ 681 if (!BN_##func(bn, bn, b)) \ 682 ossl_raise(eBNError, NULL); \ 700 #define BIGNUM_RAND(func) \ 702 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \ 705 int bottom = 0, top = 0, b; \ 706 VALUE bits, fill, odd, obj; \ 708 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \ 710 bottom = (odd == Qtrue) ? 1 : 0; \ 713 top = NUM2INT(fill); \ 716 obj = NewBN(klass); \ 717 if (!(result = BN_new())) { \ 718 ossl_raise(eBNError, NULL); \ 720 if (!BN_##func(result, b, top, bottom)) { \ 722 ossl_raise(eBNError, NULL); \ 724 SetBN(obj, result); \ 740 #define BIGNUM_RAND_RANGE(func) \ 742 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ 744 BIGNUM *bn = GetBNPtr(range), *result; \ 745 VALUE obj = NewBN(klass); \ 746 if (!(result = BN_new())) { \ 747 ossl_raise(eBNError, NULL); \ 749 if (!BN_##func##_range(result, bn)) { \ 751 ossl_raise(eBNError, NULL); \ 753 SetBN(obj, result); \ 792 VALUE vnum, vsafe, vadd, vrem, obj;
794 rb_scan_args(argc, argv,
"13", &vnum, &vsafe, &vadd, &vrem);
806 if (!(result = BN_new())) {
809 if (!BN_generate_prime_ex(result, num, safe, add, rem,
NULL)) {
818 #define BIGNUM_NUM(func) \ 820 ossl_bn_##func(VALUE self) \ 824 return INT2NUM(BN_##func(bn)); \ 848 if (
self == other)
return self;
853 if (!BN_copy(bn1, bn2)) {
859 #define BIGNUM_CMP(func) \ 861 ossl_bn_##func(VALUE self, VALUE other) \ 863 BIGNUM *bn1, *bn2 = GetBNPtr(other); \ 865 return INT2NUM(BN_##func(bn1, bn2)); \ 905 if (!BN_cmp(bn1, bn2)) {
949 len = BN_num_bytes(bn);
951 if (BN_bn2bin(bn, buf) != len) {
979 int checks = BN_prime_checks;
1014 VALUE vchecks, vtrivdiv;
1015 int checks = BN_prime_checks, do_trial_division = 1;
1019 if (!
NIL_P(vchecks)) {
1024 if (vtrivdiv ==
Qfalse) {
1025 do_trial_division = 0;
1027 switch (BN_is_prime_fasttest_ex(bn, checks,
ossl_bn_ctx, do_trial_division,
NULL)) {
static int cmp(VALUE x, VALUE y)
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
static VALUE ossl_bn_hash(VALUE self)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define BIGNUM_RAND_RANGE(func)
size_t strlen(const char *)
static VALUE ossl_bn_alloc(VALUE klass)
static int is_zero(VALUE x)
static unsigned int hash(str, len) register const char *str
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)
st_index_t rb_memhash(const void *ptr, long len)
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_raise(VALUE exc, const char *fmt,...)
static const rb_data_type_t ossl_bn_type
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 Bigint * lshift(Bigint *b, int k)
static VALUE ossl_bn_eq(VALUE self, VALUE other)
static VALUE ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
static BIGNUM * integer_to_bnptr(VALUE obj, BIGNUM *orig)
#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)
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)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
#define INTEGER_PACK_BIG_ENDIAN
register unsigned int len
#define StringValueCStr(v)
static VALUE try_convert_to_bn(VALUE obj)
static void ossl_bn_free(void *ptr)
VALUE ossl_buf2str(char *buf, int len)
BIGNUM * ossl_bn_value_ptr(volatile VALUE *ptr)
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)
VALUE rb_define_module(const char *name)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
#define RB_INTEGER_TYPE_P(obj)
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)
VALUE rb_obj_class(VALUE)
VALUE rb_str_new(const char *, long)