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!"); \ 30 ossl_bn_free(
void *ptr)
64 newbn = bn ? BN_dup(bn) : BN_new();
74 integer_to_bnptr(
VALUE obj, BIGNUM *orig)
80 unsigned char bin[
sizeof(long)];
82 unsigned long un = labs(n);
84 for (i =
sizeof(
long) - 1; 0 <= i; i--) {
89 bn = BN_bin2bn(
bin,
sizeof(
bin), orig);
93 BN_set_negative(bn, 1);
104 bin = (
unsigned char*)
ALLOCV_N(
unsigned char, buf, len);
107 bn = BN_bin2bn(bin, (
int)len, orig);
112 BN_set_negative(bn, 1);
119 try_convert_to_bn(
VALUE obj)
128 bn = integer_to_bnptr(obj,
NULL);
141 tmp = try_convert_to_bn(*ptr);
161 ossl_bn_alloc(
VALUE klass)
166 if (!(bn = BN_new())) {
197 integer_to_bnptr(str, bn);
207 if (!BN_copy(bn, other)) {
274 len = BN_num_bytes(bn);
299 ossl_bn_to_i(
VALUE self)
307 if (!(txt = BN_bn2hex(bn))) {
317 ossl_bn_to_bn(
VALUE self)
325 switch(
TYPE(other)) {
327 self = ossl_bn_to_s(0,
NULL,
self);
331 self = ossl_bn_to_i(
self);
341 #define BIGNUM_BOOL1(func) \ 343 ossl_bn_##func(VALUE self) \ 347 if (BN_##func(bn)) { \ 379 ossl_bn_is_negative(
VALUE self)
389 #define BIGNUM_1c(func) \ 391 ossl_bn_##func(VALUE self) \ 393 BIGNUM *bn, *result; \ 396 obj = NewBN(rb_obj_class(self)); \ 397 if (!(result = BN_new())) { \ 398 ossl_raise(eBNError, NULL); \ 400 if (!BN_##func(result, bn, ossl_bn_ctx)) { \ 402 ossl_raise(eBNError, NULL); \ 404 SetBN(obj, result); \ 415 #define BIGNUM_2(func) \ 417 ossl_bn_##func(VALUE self, VALUE other) \ 419 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 422 obj = NewBN(rb_obj_class(self)); \ 423 if (!(result = BN_new())) { \ 424 ossl_raise(eBNError, NULL); \ 426 if (!BN_##func(result, bn1, bn2)) { \ 428 ossl_raise(eBNError, NULL); \ 430 SetBN(obj, result); \ 448 #define BIGNUM_2c(func) \ 450 ossl_bn_##func(VALUE self, VALUE other) \ 452 BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ 455 obj = NewBN(rb_obj_class(self)); \ 456 if (!(result = BN_new())) { \ 457 ossl_raise(eBNError, NULL); \ 459 if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \ 461 ossl_raise(eBNError, NULL); \ 463 SetBN(obj, result); \ 519 VALUE klass, obj1, obj2;
526 if (!(
r1 = BN_new())) {
529 if (!(r2 = BN_new())) {
544 #define BIGNUM_3c(func) \ 546 ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \ 548 BIGNUM *bn1, *bn2 = GetBNPtr(other1); \ 549 BIGNUM *bn3 = GetBNPtr(other2), *result; \ 552 obj = NewBN(rb_obj_class(self)); \ 553 if (!(result = BN_new())) { \ 554 ossl_raise(eBNError, NULL); \ 556 if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \ 558 ossl_raise(eBNError, NULL); \ 560 SetBN(obj, result); \ 592 #define BIGNUM_BIT(func) \ 594 ossl_bn_##func(VALUE self, VALUE bit) \ 598 if (!BN_##func(bn, NUM2INT(bit))) { \ 599 ossl_raise(eBNError, NULL); \ 639 if (BN_is_bit_set(bn, b)) {
645 #define BIGNUM_SHIFT(func) \ 647 ossl_bn_##func(VALUE self, VALUE bits) \ 649 BIGNUM *bn, *result; \ 654 obj = NewBN(rb_obj_class(self)); \ 655 if (!(result = BN_new())) { \ 656 ossl_raise(eBNError, NULL); \ 658 if (!BN_##func(result, bn, b)) { \ 660 ossl_raise(eBNError, NULL); \ 662 SetBN(obj, result); \ 680 #define BIGNUM_SELF_SHIFT(func) \ 682 ossl_bn_self_##func(VALUE self, VALUE bits) \ 688 if (!BN_##func(bn, bn, b)) \ 689 ossl_raise(eBNError, NULL); \ 707 #define BIGNUM_RAND(func) \ 709 ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \ 712 int bottom = 0, top = 0, b; \ 713 VALUE bits, fill, odd, obj; \ 715 switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \ 717 bottom = (odd == Qtrue) ? 1 : 0; \ 720 top = NUM2INT(fill); \ 723 obj = NewBN(klass); \ 724 if (!(result = BN_new())) { \ 725 ossl_raise(eBNError, NULL); \ 727 if (!BN_##func(result, b, top, bottom)) { \ 729 ossl_raise(eBNError, NULL); \ 731 SetBN(obj, result); \ 747 #define BIGNUM_RAND_RANGE(func) \ 749 ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ 751 BIGNUM *bn = GetBNPtr(range), *result; \ 752 VALUE obj = NewBN(klass); \ 753 if (!(result = BN_new())) { \ 754 ossl_raise(eBNError, NULL); \ 756 if (!BN_##func##_range(result, bn)) { \ 758 ossl_raise(eBNError, NULL); \ 760 SetBN(obj, result); \ 799 VALUE vnum, vsafe, vadd, vrem, obj;
801 rb_scan_args(argc, argv,
"13", &vnum, &vsafe, &vadd, &vrem);
813 if (!(result = BN_new())) {
816 if (!BN_generate_prime_ex(result, num, safe, add, rem,
NULL)) {
825 #define BIGNUM_NUM(func) \ 827 ossl_bn_##func(VALUE self) \ 831 return INT2NUM(BN_##func(bn)); \ 855 if (
self == other)
return self;
860 if (!BN_copy(bn1, bn2)) {
871 ossl_bn_uplus(
VALUE self)
881 ossl_bn_uminus(
VALUE self)
892 BN_set_negative(bn2, !BN_is_negative(bn2));
897 #define BIGNUM_CMP(func) \ 899 ossl_bn_##func(VALUE self, VALUE other) \ 901 BIGNUM *bn1, *bn2 = GetBNPtr(other); \ 903 return INT2NUM(BN_##func(bn1, bn2)); \ 938 other = try_convert_to_bn(other);
943 if (!BN_cmp(bn1, bn2)) {
979 ossl_bn_hash(
VALUE self)
987 len = BN_num_bytes(bn);
989 if (BN_bn2bin(bn, buf) != len) {
1017 int checks = BN_prime_checks;
1049 ossl_bn_is_prime_fasttest(
int argc,
VALUE *argv,
VALUE self)
1052 VALUE vchecks, vtrivdiv;
1053 int checks = BN_prime_checks, do_trial_division = 1;
1057 if (!
NIL_P(vchecks)) {
1062 if (vtrivdiv ==
Qfalse) {
1063 do_trial_division = 0;
1065 switch (BN_is_prime_fasttest_ex(bn, checks,
ossl_bn_ctx, do_trial_division,
NULL)) {
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define BIGNUM_RAND_RANGE(func)
size_t strlen(const char *)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
void rb_raise(VALUE exc, const char *fmt,...)
#define BIGNUM_RAND(func)
st_index_t rb_memhash(const void *ptr, long len)
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_class(VALUE)
call-seq: obj.class -> class
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
#define BIGNUM_SHIFT(func)
#define BIGNUM_BOOL1(func)
RUBY_EXTERN VALUE rb_cObject
#define ALLOCV_N(type, v, n)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
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)
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
#define INTEGER_PACK_BIG_ENDIAN
register unsigned int len
#define StringValueCStr(v)
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)
VALUE rb_str_new(const char *, long)