2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Technorama team <oss-ruby@technorama.net>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 12:12:24 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
/* modified by Michal Rokos <m.rokos@sh.cvut.cz> */
|
|
|
|
#include "ossl.h"
|
2020-12-18 12:04:34 -05:00
|
|
|
|
2021-03-25 13:42:59 -04:00
|
|
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
2020-12-18 04:19:33 -05:00
|
|
|
#include <ruby/ractor.h>
|
2020-12-18 12:04:34 -05:00
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
#define NewBN(klass) \
|
|
|
|
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
|
|
|
|
#define SetBN(obj, bn) do { \
|
2011-02-19 10:47:21 -05:00
|
|
|
if (!(bn)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (bn); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GetBN(obj, bn) do { \
|
2014-10-04 19:33:15 -04:00
|
|
|
TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
|
2011-02-19 10:47:21 -05:00
|
|
|
if (!(bn)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2014-10-04 19:33:15 -04:00
|
|
|
static void
|
|
|
|
ossl_bn_free(void *ptr)
|
|
|
|
{
|
|
|
|
BN_clear_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_bn_type = {
|
|
|
|
"OpenSSL/BN",
|
2016-06-05 11:35:12 -04:00
|
|
|
{
|
|
|
|
0, ossl_bn_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
2014-10-04 19:33:15 -04:00
|
|
|
};
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cBN;
|
2015-02-21 14:06:17 -05:00
|
|
|
|
|
|
|
/* Document-class: OpenSSL::BNError
|
|
|
|
*
|
|
|
|
* Generic Error for all of OpenSSL::BN (big num)
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE eBNError;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
|
|
|
VALUE
|
2007-04-03 03:02:44 -04:00
|
|
|
ossl_bn_new(const BIGNUM *bn)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
BIGNUM *newbn;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewBN(cBN);
|
2003-07-23 12:12:24 -04:00
|
|
|
newbn = bn ? BN_dup(bn) : BN_new();
|
|
|
|
if (!newbn) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, newbn);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
static BIGNUM *
|
|
|
|
integer_to_bnptr(VALUE obj, BIGNUM *orig)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
|
|
|
|
if (FIXNUM_P(obj)) {
|
|
|
|
long i;
|
|
|
|
unsigned char bin[sizeof(long)];
|
|
|
|
long n = FIX2LONG(obj);
|
|
|
|
unsigned long un = labs(n);
|
|
|
|
|
|
|
|
for (i = sizeof(long) - 1; 0 <= i; i--) {
|
|
|
|
bin[i] = un & 0xff;
|
|
|
|
un >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
bn = BN_bin2bn(bin, sizeof(bin), orig);
|
|
|
|
if (!bn)
|
|
|
|
ossl_raise(eBNError, "BN_bin2bn");
|
|
|
|
if (n < 0)
|
|
|
|
BN_set_negative(bn, 1);
|
|
|
|
}
|
|
|
|
else { /* assuming Bignum */
|
|
|
|
size_t len = rb_absint_size(obj, NULL);
|
|
|
|
unsigned char *bin;
|
|
|
|
VALUE buf;
|
|
|
|
int sign;
|
|
|
|
|
|
|
|
if (INT_MAX < len) {
|
|
|
|
rb_raise(eBNError, "bignum too long");
|
|
|
|
}
|
|
|
|
bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len);
|
|
|
|
sign = rb_integer_pack(obj, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN);
|
|
|
|
|
|
|
|
bn = BN_bin2bn(bin, (int)len, orig);
|
|
|
|
ALLOCV_END(buf);
|
|
|
|
if (!bn)
|
|
|
|
ossl_raise(eBNError, "BN_bin2bn");
|
|
|
|
if (sign < 0)
|
|
|
|
BN_set_negative(bn, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bn;
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:12:02 -05:00
|
|
|
static VALUE
|
|
|
|
try_convert_to_bn(VALUE obj)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2016-12-10 03:12:02 -05:00
|
|
|
BIGNUM *bn;
|
|
|
|
VALUE newobj = Qnil;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-12-10 03:12:02 -05:00
|
|
|
if (rb_obj_is_kind_of(obj, cBN))
|
|
|
|
return obj;
|
|
|
|
if (RB_INTEGER_TYPE_P(obj)) {
|
2017-06-14 05:49:09 -04:00
|
|
|
newobj = NewBN(cBN); /* Handle potential mem leaks */
|
2016-08-29 01:47:09 -04:00
|
|
|
bn = integer_to_bnptr(obj, NULL);
|
|
|
|
SetBN(newobj, bn);
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:12:02 -05:00
|
|
|
return newobj;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:55:45 -04:00
|
|
|
BIGNUM *
|
2016-12-10 03:12:02 -05:00
|
|
|
ossl_bn_value_ptr(volatile VALUE *ptr)
|
2016-05-17 22:55:45 -04:00
|
|
|
{
|
2016-12-10 03:12:02 -05:00
|
|
|
VALUE tmp;
|
|
|
|
BIGNUM *bn;
|
|
|
|
|
|
|
|
tmp = try_convert_to_bn(*ptr);
|
|
|
|
if (NIL_P(tmp))
|
2016-05-17 22:55:45 -04:00
|
|
|
ossl_raise(rb_eTypeError, "Cannot convert into OpenSSL::BN");
|
2016-12-10 03:12:02 -05:00
|
|
|
GetBN(tmp, bn);
|
|
|
|
*ptr = tmp;
|
2016-05-17 22:55:45 -04:00
|
|
|
|
|
|
|
return bn;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
2020-12-18 04:19:33 -05:00
|
|
|
|
2021-03-25 13:42:59 -04:00
|
|
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
2020-12-18 04:19:33 -05:00
|
|
|
void
|
|
|
|
ossl_bn_ctx_free(void *ptr)
|
|
|
|
{
|
|
|
|
BN_CTX *ctx = (BN_CTX *)ptr;
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rb_ractor_local_storage_type ossl_bn_ctx_key_type = {
|
|
|
|
NULL, // mark
|
|
|
|
ossl_bn_ctx_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
rb_ractor_local_key_t ossl_bn_ctx_key;
|
|
|
|
|
|
|
|
BN_CTX *
|
|
|
|
ossl_bn_ctx_get(void)
|
|
|
|
{
|
|
|
|
// stored in ractor local storage
|
|
|
|
|
|
|
|
BN_CTX *ctx = rb_ractor_local_storage_ptr(ossl_bn_ctx_key);
|
|
|
|
if (!ctx) {
|
|
|
|
if (!(ctx = BN_CTX_new())) {
|
|
|
|
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
|
|
|
|
}
|
|
|
|
rb_ractor_local_storage_ptr_set(ossl_bn_ctx_key, ctx);
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
2020-12-18 12:04:34 -05:00
|
|
|
#else
|
|
|
|
// for ruby 2.x
|
|
|
|
static BN_CTX *gv_ossl_bn_ctx;
|
|
|
|
|
|
|
|
BN_CTX *
|
|
|
|
ossl_bn_ctx_get(void)
|
|
|
|
{
|
|
|
|
if (gv_ossl_bn_ctx == NULL) {
|
|
|
|
if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
|
|
|
|
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gv_ossl_bn_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ossl_bn_ctx_free(void)
|
|
|
|
{
|
|
|
|
BN_CTX_free(gv_ossl_bn_ctx);
|
|
|
|
gv_ossl_bn_ctx = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
2015-05-29 01:55:02 -04:00
|
|
|
VALUE obj = NewBN(klass);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(bn = BN_new())) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, bn);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
/*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 14:06:17 -05:00
|
|
|
* OpenSSL::BN.new(bn) => aBN
|
|
|
|
* OpenSSL::BN.new(integer) => aBN
|
|
|
|
* OpenSSL::BN.new(string) => aBN
|
|
|
|
* OpenSSL::BN.new(string, 0 | 2 | 10 | 16) => aBN
|
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* Construct a new OpenSSL BIGNUM object.
|
2007-04-03 03:02:44 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
VALUE str, bs;
|
|
|
|
int base = 10;
|
2019-04-10 08:57:39 -04:00
|
|
|
char *ptr;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
|
|
|
|
base = NUM2INT(bs);
|
|
|
|
}
|
2013-04-03 14:28:56 -04:00
|
|
|
|
2020-02-16 01:21:29 -05:00
|
|
|
if (NIL_P(str)) {
|
|
|
|
ossl_raise(rb_eArgError, "invalid argument");
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
if (RB_INTEGER_TYPE_P(str)) {
|
2013-04-25 03:02:31 -04:00
|
|
|
GetBN(self, bn);
|
2016-08-29 01:47:09 -04:00
|
|
|
integer_to_bnptr(str, bn);
|
2013-04-25 03:02:31 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2016-08-29 01:47:09 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (RTEST(rb_obj_is_kind_of(str, cBN))) {
|
|
|
|
BIGNUM *other;
|
|
|
|
|
2013-04-03 14:28:56 -04:00
|
|
|
GetBN(self, bn);
|
2003-07-23 12:12:24 -04:00
|
|
|
GetBN(str, other); /* Safe - we checked kind_of? above */
|
|
|
|
if (!BN_copy(bn, other)) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-04-03 14:28:56 -04:00
|
|
|
GetBN(self, bn);
|
2003-07-23 12:12:24 -04:00
|
|
|
switch (base) {
|
|
|
|
case 0:
|
2019-04-10 08:57:39 -04:00
|
|
|
ptr = StringValuePtr(str);
|
|
|
|
if (!BN_mpi2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2019-04-10 08:57:39 -04:00
|
|
|
ptr = StringValuePtr(str);
|
|
|
|
if (!BN_bin2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
2016-05-23 07:40:07 -04:00
|
|
|
if (!BN_dec2bn(&bn, StringValueCStr(str))) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2016-05-23 07:40:07 -04:00
|
|
|
if (!BN_hex2bn(&bn, StringValueCStr(str))) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
* regerror.c, string.c, io.c, lib/getoptlong.rb, lib/net/imap.rb,
compile.c, sprintf.c, parse.y, ext/win32ole/win32ole.c,
ext/tk/sample/demos-en/entry3.rb, ext/tk/lib/tcltk.rb,
ext/openssl/ossl_bn.c, numeric.c, vm.c,
benchmark/bm_so_meteor_contest.rb, bignum.c, ruby.c: don't "illegal"
for non law violation context.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-12-20 21:31:11 -05:00
|
|
|
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.to_s => string
|
|
|
|
* bn.to_s(base) => string
|
|
|
|
*
|
|
|
|
* === Parameters
|
2017-09-03 08:35:27 -04:00
|
|
|
* * _base_ - Integer
|
2016-08-29 01:47:09 -04:00
|
|
|
* Valid values:
|
|
|
|
* * 0 - MPI
|
|
|
|
* * 2 - binary
|
|
|
|
* * 10 - the default
|
|
|
|
* * 16 - hex
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
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);
|
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetBN(self, bn);
|
2003-07-23 12:12:24 -04:00
|
|
|
switch (base) {
|
|
|
|
case 0:
|
|
|
|
len = BN_bn2mpi(bn, NULL);
|
2003-09-17 05:05:02 -04:00
|
|
|
str = rb_str_new(0, len);
|
2008-07-22 11:34:23 -04:00
|
|
|
if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
len = BN_num_bytes(bn);
|
2003-09-17 05:05:02 -04:00
|
|
|
str = rb_str_new(0, len);
|
2008-07-22 11:34:23 -04:00
|
|
|
if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
break;
|
|
|
|
case 10:
|
2003-09-17 05:05:02 -04:00
|
|
|
if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
|
2011-03-24 03:29:21 -04:00
|
|
|
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
2003-07-23 12:12:24 -04:00
|
|
|
break;
|
|
|
|
case 16:
|
2003-09-17 05:05:02 -04:00
|
|
|
if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
|
2011-03-24 03:29:21 -04:00
|
|
|
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
2003-07-23 12:12:24 -04:00
|
|
|
break;
|
|
|
|
default:
|
* regerror.c, string.c, io.c, lib/getoptlong.rb, lib/net/imap.rb,
compile.c, sprintf.c, parse.y, ext/win32ole/win32ole.c,
ext/tk/sample/demos-en/entry3.rb, ext/tk/lib/tcltk.rb,
ext/openssl/ossl_bn.c, numeric.c, vm.c,
benchmark/bm_so_meteor_contest.rb, bignum.c, ruby.c: don't "illegal"
for non law violation context.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-12-20 21:31:11 -05:00
|
|
|
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-04-03 03:02:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.to_i => integer
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_to_i(VALUE self)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
char *txt;
|
|
|
|
VALUE num;
|
|
|
|
|
|
|
|
GetBN(self, bn);
|
|
|
|
|
2013-04-03 22:35:07 -04:00
|
|
|
if (!(txt = BN_bn2hex(bn))) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2013-04-03 22:35:07 -04:00
|
|
|
num = rb_cstr_to_inum(txt, 16, Qtrue);
|
2003-07-23 12:12:24 -04:00
|
|
|
OPENSSL_free(txt);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_to_bn(VALUE self)
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIGNUM_BOOL1(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
GetBN(self, bn); \
|
|
|
|
if (BN_##func(bn)) { \
|
|
|
|
return Qtrue; \
|
|
|
|
} \
|
|
|
|
return Qfalse; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#zero?
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.zero? => true | false
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BOOL1(is_zero)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#one?
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.one? => true | false
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BOOL1(is_one)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#odd?
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.odd? => true | false
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BOOL1(is_odd)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.negative? => true | false
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_is_negative(VALUE self)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
|
|
|
|
GetBN(self, bn);
|
|
|
|
if (BN_is_zero(bn))
|
|
|
|
return Qfalse;
|
|
|
|
return BN_is_negative(bn) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
#define BIGNUM_1c(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn, *result; \
|
|
|
|
VALUE obj; \
|
|
|
|
GetBN(self, bn); \
|
2016-11-30 09:41:46 -05:00
|
|
|
obj = NewBN(rb_obj_class(self)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#sqr
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.sqr => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_1c(sqr)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
#define BIGNUM_2(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE other) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
|
|
|
VALUE obj; \
|
|
|
|
GetBN(self, bn1); \
|
2016-11-30 09:41:46 -05:00
|
|
|
obj = NewBN(rb_obj_class(self)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, bn1, bn2) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#+
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn + bn2 => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2(add)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#-
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn - bn2 => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2(sub)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
#define BIGNUM_2c(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE other) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
|
|
|
VALUE obj; \
|
|
|
|
GetBN(self, bn1); \
|
2016-11-30 09:41:46 -05:00
|
|
|
obj = NewBN(rb_obj_class(self)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn * bn2 => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2c(mul)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#%
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn % bn2 => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2c(mod)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#**
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn ** bn2 => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2c(exp)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#gcd
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.gcd(bn2) => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2c(gcd)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mod_sqr
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mod_sqr(bn2) => aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_2c(mod_sqr)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2021-02-17 08:58:40 -05:00
|
|
|
* bn.mod_inverse(bn2) => aBN
|
2015-02-21 18:28:47 -05:00
|
|
|
*/
|
2021-02-17 08:58:40 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_mod_inverse(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result;
|
|
|
|
VALUE obj;
|
|
|
|
GetBN(self, bn1);
|
|
|
|
obj = NewBN(rb_obj_class(self));
|
|
|
|
if (!(result = BN_mod_inverse(NULL, bn1, bn2, ossl_bn_ctx)))
|
|
|
|
ossl_raise(eBNError, "BN_mod_inverse");
|
|
|
|
SetBN(obj, result);
|
|
|
|
return obj;
|
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2007-04-05 15:03:28 -04:00
|
|
|
* bn1 / bn2 => [result, remainder]
|
2015-02-21 14:06:17 -05:00
|
|
|
*
|
|
|
|
* Division of OpenSSL::BN instances
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_div(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other), *r1, *r2;
|
2016-11-30 09:41:46 -05:00
|
|
|
VALUE klass, obj1, obj2;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
GetBN(self, bn1);
|
|
|
|
|
2016-11-30 09:41:46 -05:00
|
|
|
klass = rb_obj_class(self);
|
|
|
|
obj1 = NewBN(klass);
|
|
|
|
obj2 = NewBN(klass);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(r1 = BN_new())) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
if (!(r2 = BN_new())) {
|
|
|
|
BN_free(r1);
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
if (!BN_div(r1, r2, bn1, bn2, ossl_bn_ctx)) {
|
|
|
|
BN_free(r1);
|
|
|
|
BN_free(r2);
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj1, r1);
|
|
|
|
SetBN(obj2, r2);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return rb_ary_new3(2, obj1, obj2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIGNUM_3c(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
|
|
|
|
BIGNUM *bn3 = GetBNPtr(other2), *result; \
|
|
|
|
VALUE obj; \
|
|
|
|
GetBN(self, bn1); \
|
2016-11-30 09:41:46 -05:00
|
|
|
obj = NewBN(rb_obj_class(self)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mod_add
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mod_add(bn1, bn2) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_3c(mod_add)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mod_sub
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mod_sub(bn1, bn2) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_3c(mod_sub)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mod_mul
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mod_mul(bn1, bn2) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_3c(mod_mul)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mod_exp
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mod_exp(bn1, bn2) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_3c(mod_exp)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
#define BIGNUM_BIT(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE bit) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
GetBN(self, bn); \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
|
|
|
return self; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#set_bit!
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.set_bit!(bit) -> self
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BIT(set_bit)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#clear_bit!
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.clear_bit!(bit) -> self
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BIT(clear_bit)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#mask_bit!
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.mask_bit!(bit) -> self
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_BIT(mask_bits)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
/*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
|
|
|
* bn.bit_set?(bit) => true | false
|
2015-02-21 14:06:17 -05:00
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* Tests bit _bit_ in _bn_ and returns +true+ if set, +false+ if not set.
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_is_bit_set(VALUE self, VALUE bit)
|
|
|
|
{
|
2004-12-15 01:35:55 -05:00
|
|
|
int b;
|
2003-07-23 12:12:24 -04:00
|
|
|
BIGNUM *bn;
|
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
b = NUM2INT(bit);
|
2003-07-23 12:12:24 -04:00
|
|
|
GetBN(self, bn);
|
2004-12-15 01:35:55 -05:00
|
|
|
if (BN_is_bit_set(bn, b)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIGNUM_SHIFT(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE bits) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn, *result; \
|
|
|
|
int b; \
|
|
|
|
VALUE obj; \
|
|
|
|
b = NUM2INT(bits); \
|
2004-12-15 01:35:55 -05:00
|
|
|
GetBN(self, bn); \
|
2016-11-30 09:41:46 -05:00
|
|
|
obj = NewBN(rb_obj_class(self)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, bn, b) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#<<
|
|
|
|
* call-seq:
|
|
|
|
* bn << bits -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_SHIFT(lshift)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#>>
|
|
|
|
* call-seq:
|
|
|
|
* bn >> bits -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_SHIFT(rshift)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
#define BIGNUM_SELF_SHIFT(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_self_##func(VALUE self, VALUE bits) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
int b; \
|
|
|
|
b = NUM2INT(bits); \
|
|
|
|
GetBN(self, bn); \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(bn, bn, b) <= 0) \
|
2007-03-29 13:29:03 -04:00
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
return self; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#lshift!
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.lshift!(bits) -> self
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_SELF_SHIFT(lshift)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#rshift!
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.rshift!(bits) -> self
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_SELF_SHIFT(rshift)
|
2007-03-29 13:29:03 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
#define BIGNUM_RAND(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *result; \
|
|
|
|
int bottom = 0, top = 0, b; \
|
|
|
|
VALUE bits, fill, odd, obj; \
|
|
|
|
\
|
|
|
|
switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
|
|
|
|
case 3: \
|
|
|
|
bottom = (odd == Qtrue) ? 1 : 0; \
|
|
|
|
/* FALLTHROUGH */ \
|
|
|
|
case 2: \
|
2008-05-29 13:41:56 -04:00
|
|
|
top = NUM2INT(fill); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} \
|
|
|
|
b = NUM2INT(bits); \
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewBN(klass); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func(result, b, top, bottom) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN.rand
|
|
|
|
* BN.rand(bits [, fill [, odd]]) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_RAND(rand)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN.pseudo_rand
|
|
|
|
* BN.pseudo_rand(bits [, fill [, odd]]) -> aBN
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_RAND(pseudo_rand)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
#define BIGNUM_RAND_RANGE(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn = GetBNPtr(range), *result; \
|
2015-05-29 01:55:02 -04:00
|
|
|
VALUE obj = NewBN(klass); \
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) { \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2021-02-17 08:58:40 -05:00
|
|
|
if (BN_##func##_range(result, bn) <= 0) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result); \
|
|
|
|
ossl_raise(eBNError, NULL); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result); \
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj; \
|
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN.rand_range
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* BN.rand_range(range) -> aBN
|
|
|
|
*
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_RAND_RANGE(rand)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN.pseudo_rand_range
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* BN.pseudo_rand_range(range) -> aBN
|
|
|
|
*
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_RAND_RANGE(pseudo_rand)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* BN.generate_prime(bits, [, safe [, add [, rem]]]) => bn
|
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* 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
|
2016-06-03 22:35:09 -04:00
|
|
|
* fulfills condition <tt>p % add = rem</tt>.
|
|
|
|
*
|
2007-04-05 15:03:28 -04:00
|
|
|
* === Parameters
|
2017-09-03 08:35:27 -04:00
|
|
|
* * _bits_ - integer
|
|
|
|
* * _safe_ - boolean
|
|
|
|
* * _add_ - BN
|
|
|
|
* * _rem_ - BN
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
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);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
num = NUM2INT(vnum);
|
|
|
|
|
|
|
|
if (vsafe == Qfalse) {
|
|
|
|
safe = 0;
|
|
|
|
}
|
|
|
|
if (!NIL_P(vadd)) {
|
|
|
|
add = GetBNPtr(vadd);
|
2007-04-05 15:03:28 -04:00
|
|
|
rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewBN(klass);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(result = BN_new())) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2016-06-03 22:35:09 -04:00
|
|
|
if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
BN_free(result);
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetBN(obj, result);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
return obj;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BIGNUM_NUM(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn; \
|
|
|
|
GetBN(self, bn); \
|
2016-08-29 01:47:09 -04:00
|
|
|
return INT2NUM(BN_##func(bn)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#num_bytes
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.num_bytes => integer
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_NUM(num_bytes)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#num_bits
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.num_bits => integer
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_NUM(num_bits)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
BIGNUM *bn1, *bn2;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_check_frozen(self);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (self == other) return self;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetBN(self, bn1);
|
|
|
|
bn2 = GetBNPtr(other);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!BN_copy(bn1, bn2)) {
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* +bn -> aBN
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_uplus(VALUE self)
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* -bn -> aBN
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
#define BIGNUM_CMP(func) \
|
|
|
|
static VALUE \
|
|
|
|
ossl_bn_##func(VALUE self, VALUE other) \
|
|
|
|
{ \
|
|
|
|
BIGNUM *bn1, *bn2 = GetBNPtr(other); \
|
|
|
|
GetBN(self, bn1); \
|
2016-08-29 01:47:09 -04:00
|
|
|
return INT2NUM(BN_##func(bn1, bn2)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#cmp
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.cmp(bn2) => integer
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#<=>
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn <=> bn2 => integer
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_CMP(cmp)
|
2015-02-21 18:28:47 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: OpenSSL::BN#ucmp
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
2015-02-21 18:28:47 -05:00
|
|
|
* bn.ucmp(bn2) => integer
|
|
|
|
*/
|
2009-03-12 09:44:37 -04:00
|
|
|
BIGNUM_CMP(ucmp)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2015-02-21 14:06:17 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-05-17 22:55:45 -04:00
|
|
|
* bn == obj => true or false
|
2015-02-21 14:06:17 -05:00
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* Returns +true+ only if _obj_ has the same value as _bn_. Contrast this
|
2016-05-17 22:55:45 -04:00
|
|
|
* with OpenSSL::BN#eql?, which requires obj to be OpenSSL::BN.
|
2015-02-21 14:06:17 -05:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2016-05-17 22:55:45 -04:00
|
|
|
ossl_bn_eq(VALUE self, VALUE other)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2016-05-17 22:55:45 -04:00
|
|
|
BIGNUM *bn1, *bn2;
|
|
|
|
|
|
|
|
GetBN(self, bn1);
|
2016-12-10 03:12:02 -05:00
|
|
|
other = try_convert_to_bn(other);
|
|
|
|
if (NIL_P(other))
|
|
|
|
return Qfalse;
|
|
|
|
GetBN(other, bn2);
|
2016-05-17 22:55:45 -04:00
|
|
|
|
2016-12-10 03:12:02 -05:00
|
|
|
if (!BN_cmp(bn1, bn2)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:55:45 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.eql?(obj) => true or false
|
|
|
|
*
|
|
|
|
* Returns <code>true</code> only if <i>obj</i> is a
|
2017-09-03 08:35:27 -04:00
|
|
|
* <code>OpenSSL::BN</code> with the same value as <i>bn</i>. Contrast this
|
2016-05-17 22:55:45 -04:00
|
|
|
* with OpenSSL::BN#==, which performs type conversions.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.hash => Integer
|
|
|
|
*
|
|
|
|
* Returns a hash code for this object.
|
|
|
|
*
|
|
|
|
* See also Object#hash.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_bn_hash(VALUE self)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
2017-11-25 09:12:08 -05:00
|
|
|
VALUE tmp, hash;
|
2016-05-17 22:55:45 -04:00
|
|
|
unsigned char *buf;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
GetBN(self, bn);
|
|
|
|
len = BN_num_bytes(bn);
|
2017-11-25 09:12:08 -05:00
|
|
|
buf = ALLOCV(tmp, len);
|
2016-05-17 22:55:45 -04:00
|
|
|
if (BN_bn2bin(bn, buf) != len) {
|
2017-11-25 09:12:08 -05:00
|
|
|
ALLOCV_END(tmp);
|
|
|
|
ossl_raise(eBNError, "BN_bn2bin");
|
2016-05-17 22:55:45 -04:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:24:08 -04:00
|
|
|
hash = ST2FIX(rb_memhash(buf, len));
|
2017-11-25 09:12:08 -05:00
|
|
|
ALLOCV_END(tmp);
|
2016-05-17 22:55:45 -04:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.prime? => true | false
|
|
|
|
* bn.prime?(checks) => true | false
|
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* Performs a Miller-Rabin probabilistic primality test with _checks_
|
|
|
|
* iterations. If _checks_ is not specified, a number of iterations is used
|
2016-08-29 01:47:09 -04:00
|
|
|
* that yields a false positive rate of at most 2^-80 for random input.
|
2016-06-03 22:35:09 -04:00
|
|
|
*
|
2007-04-05 15:03:28 -04:00
|
|
|
* === Parameters
|
2017-09-03 08:35:27 -04:00
|
|
|
* * _checks_ - integer
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
VALUE vchecks;
|
|
|
|
int checks = BN_prime_checks;
|
|
|
|
|
2010-09-12 20:40:10 -04:00
|
|
|
if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
|
2003-07-23 12:12:24 -04:00
|
|
|
checks = NUM2INT(vchecks);
|
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetBN(self, bn);
|
2016-06-03 22:35:09 -04:00
|
|
|
switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
case 1:
|
|
|
|
return Qtrue;
|
|
|
|
case 0:
|
|
|
|
return Qfalse;
|
|
|
|
default:
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
/* not reachable */
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bn.prime_fasttest? => true | false
|
|
|
|
* bn.prime_fasttest?(checks) => true | false
|
|
|
|
* bn.prime_fasttest?(checks, trial_div) => true | false
|
|
|
|
*
|
2016-08-29 01:47:09 -04:00
|
|
|
* Performs a Miller-Rabin primality test. This is same as #prime? except this
|
|
|
|
* first attempts trial divisions with some small primes.
|
2016-06-03 22:35:09 -04:00
|
|
|
*
|
2007-04-05 15:03:28 -04:00
|
|
|
* === Parameters
|
2017-09-03 08:35:27 -04:00
|
|
|
* * _checks_ - integer
|
|
|
|
* * _trial_div_ - boolean
|
2007-04-05 15:03:28 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
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);
|
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetBN(self, bn);
|
2003-07-23 12:12:24 -04:00
|
|
|
/* handle true/false */
|
|
|
|
if (vtrivdiv == Qfalse) {
|
|
|
|
do_trial_division = 0;
|
|
|
|
}
|
2016-06-03 22:35:09 -04:00
|
|
|
switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
case 1:
|
|
|
|
return Qtrue;
|
|
|
|
case 0:
|
|
|
|
return Qfalse;
|
|
|
|
default:
|
|
|
|
ossl_raise(eBNError, NULL);
|
|
|
|
}
|
|
|
|
/* not reachable */
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
* (NOTE: ordering of methods is the same as in 'man bn')
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_bn(void)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2010-12-05 19:54:44 -05:00
|
|
|
#if 0
|
2016-08-29 01:47:09 -04:00
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
2007-03-11 22:01:19 -04:00
|
|
|
#endif
|
|
|
|
|
2020-12-18 12:04:34 -05:00
|
|
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
2020-12-18 04:19:33 -05:00
|
|
|
ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
|
2020-12-18 12:04:34 -05:00
|
|
|
#else
|
|
|
|
ossl_bn_ctx_get();
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
eBNError = rb_define_class_under(mOSSL, "BNError", eOSSLError);
|
|
|
|
|
|
|
|
cBN = rb_define_class_under(mOSSL, "BN", rb_cObject);
|
|
|
|
|
|
|
|
rb_define_alloc_func(cBN, ossl_bn_alloc);
|
|
|
|
rb_define_method(cBN, "initialize", ossl_bn_initialize, -1);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
rb_define_method(cBN, "initialize_copy", ossl_bn_copy, 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cBN, "copy", ossl_bn_copy, 1);
|
|
|
|
|
|
|
|
/* swap (=coerce?) */
|
|
|
|
|
|
|
|
rb_define_method(cBN, "num_bytes", ossl_bn_num_bytes, 0);
|
|
|
|
rb_define_method(cBN, "num_bits", ossl_bn_num_bits, 0);
|
|
|
|
/* num_bits_word */
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
rb_define_method(cBN, "+@", ossl_bn_uplus, 0);
|
|
|
|
rb_define_method(cBN, "-@", ossl_bn_uminus, 0);
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cBN, "+", ossl_bn_add, 1);
|
|
|
|
rb_define_method(cBN, "-", ossl_bn_sub, 1);
|
|
|
|
rb_define_method(cBN, "*", ossl_bn_mul, 1);
|
|
|
|
rb_define_method(cBN, "sqr", ossl_bn_sqr, 0);
|
|
|
|
rb_define_method(cBN, "/", ossl_bn_div, 1);
|
|
|
|
rb_define_method(cBN, "%", ossl_bn_mod, 1);
|
|
|
|
/* nnmod */
|
|
|
|
|
|
|
|
rb_define_method(cBN, "mod_add", ossl_bn_mod_add, 2);
|
|
|
|
rb_define_method(cBN, "mod_sub", ossl_bn_mod_sub, 2);
|
|
|
|
rb_define_method(cBN, "mod_mul", ossl_bn_mod_mul, 2);
|
|
|
|
rb_define_method(cBN, "mod_sqr", ossl_bn_mod_sqr, 1);
|
|
|
|
rb_define_method(cBN, "**", ossl_bn_exp, 1);
|
|
|
|
rb_define_method(cBN, "mod_exp", ossl_bn_mod_exp, 2);
|
|
|
|
rb_define_method(cBN, "gcd", ossl_bn_gcd, 1);
|
|
|
|
|
|
|
|
/* add_word
|
|
|
|
* sub_word
|
|
|
|
* mul_word
|
|
|
|
* div_word
|
|
|
|
* mod_word */
|
|
|
|
|
|
|
|
rb_define_method(cBN, "cmp", ossl_bn_cmp, 1);
|
|
|
|
rb_define_alias(cBN, "<=>", "cmp");
|
|
|
|
rb_define_method(cBN, "ucmp", ossl_bn_ucmp, 1);
|
|
|
|
rb_define_method(cBN, "eql?", ossl_bn_eql, 1);
|
2016-05-17 22:55:45 -04:00
|
|
|
rb_define_method(cBN, "hash", ossl_bn_hash, 0);
|
|
|
|
rb_define_method(cBN, "==", ossl_bn_eq, 1);
|
|
|
|
rb_define_alias(cBN, "===", "==");
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cBN, "zero?", ossl_bn_is_zero, 0);
|
|
|
|
rb_define_method(cBN, "one?", ossl_bn_is_one, 0);
|
|
|
|
/* is_word */
|
|
|
|
rb_define_method(cBN, "odd?", ossl_bn_is_odd, 0);
|
2017-09-03 08:35:27 -04:00
|
|
|
rb_define_method(cBN, "negative?", ossl_bn_is_negative, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
/* zero
|
|
|
|
* one
|
|
|
|
* value_one - DON'T IMPL.
|
|
|
|
* set_word
|
|
|
|
* get_word */
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_singleton_method(cBN, "rand", ossl_bn_s_rand, -1);
|
|
|
|
rb_define_singleton_method(cBN, "pseudo_rand", ossl_bn_s_pseudo_rand, -1);
|
|
|
|
rb_define_singleton_method(cBN, "rand_range", ossl_bn_s_rand_range, 1);
|
|
|
|
rb_define_singleton_method(cBN, "pseudo_rand_range", ossl_bn_s_pseudo_rand_range, 1);
|
|
|
|
|
|
|
|
rb_define_singleton_method(cBN, "generate_prime", ossl_bn_s_generate_prime, -1);
|
|
|
|
rb_define_method(cBN, "prime?", ossl_bn_is_prime, -1);
|
2016-06-03 22:35:09 -04:00
|
|
|
rb_define_method(cBN, "prime_fasttest?", ossl_bn_is_prime_fasttest, -1);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
rb_define_method(cBN, "set_bit!", ossl_bn_set_bit, 1);
|
|
|
|
rb_define_method(cBN, "clear_bit!", ossl_bn_clear_bit, 1);
|
|
|
|
rb_define_method(cBN, "bit_set?", ossl_bn_is_bit_set, 1);
|
|
|
|
rb_define_method(cBN, "mask_bits!", ossl_bn_mask_bits, 1);
|
|
|
|
rb_define_method(cBN, "<<", ossl_bn_lshift, 1);
|
|
|
|
rb_define_method(cBN, ">>", ossl_bn_rshift, 1);
|
2007-03-29 13:29:03 -04:00
|
|
|
rb_define_method(cBN, "lshift!", ossl_bn_self_lshift, 1);
|
|
|
|
rb_define_method(cBN, "rshift!", ossl_bn_self_rshift, 1);
|
|
|
|
/* lshift1 - DON'T IMPL. */
|
2003-07-23 12:12:24 -04:00
|
|
|
/* rshift1 - DON'T IMPL. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bn2bin
|
|
|
|
* bin2bn
|
|
|
|
* bn2hex
|
|
|
|
* bn2dec
|
|
|
|
* hex2bn
|
|
|
|
* dec2bn - all these are implemented in ossl_bn_initialize, and ossl_bn_to_s
|
|
|
|
* print - NOT IMPL.
|
|
|
|
* print_fp - NOT IMPL.
|
|
|
|
* bn2mpi
|
|
|
|
* mpi2bn
|
|
|
|
*/
|
|
|
|
rb_define_method(cBN, "to_s", ossl_bn_to_s, -1);
|
|
|
|
rb_define_method(cBN, "to_i", ossl_bn_to_i, 0);
|
|
|
|
rb_define_alias(cBN, "to_int", "to_i");
|
|
|
|
rb_define_method(cBN, "to_bn", ossl_bn_to_bn, 0);
|
|
|
|
rb_define_method(cBN, "coerce", ossl_bn_coerce, 1);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* But how to: from_bin, from_mpi? PACK?
|
|
|
|
* to_bin
|
|
|
|
* to_mpi
|
|
|
|
*/
|
|
|
|
|
|
|
|
rb_define_method(cBN, "mod_inverse", ossl_bn_mod_inverse, 1);
|
|
|
|
|
|
|
|
/* RECiProcal
|
|
|
|
* MONTgomery */
|
|
|
|
}
|