ruby-changes:23955
From: emboss <ko1@a...>
Date: Sun, 10 Jun 2012 11:38:50 +0900 (JST)
Subject: [ruby-changes:23955] emboss:r36006 (trunk): * ext/openssl/ossl_pkey_ec.c
emboss 2012-06-10 11:38:40 +0900 (Sun, 10 Jun 2012) New Revision: 36006 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36006 Log: * ext/openssl/ossl_pkey_ec.c test/openssl/test_pkey_ec.rb: Add support for EC_POINT_mul. Patch provided by Sambasiva Suda. Thanks! [ruby-core:44408][ruby-trunk - Feature #6310] Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_pkey_ec.c trunk/test/openssl/test_pkey_ec.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36005) +++ ChangeLog (revision 36006) @@ -1,3 +1,10 @@ +Sun Jun 10 11:33:01 2012 Martin Bosslet <Martin.Bosslet@g...> + + * ext/openssl/ossl_pkey_ec.c + test/openssl/test_pkey_ec.rb: Add support for EC_POINT_mul. + Patch provided by Sambasiva Suda. Thanks! + [ruby-core:44408][ruby-trunk - Feature #6310] + Sun Jun 10 10:48:15 2012 Martin Bosslet <Martin.Bosslet@g...> * lib/openssl/ssl.rb: Use a simple random number to generate the Index: ext/openssl/ossl_pkey_ec.c =================================================================== --- ext/openssl/ossl_pkey_ec.c (revision 36005) +++ ext/openssl/ossl_pkey_ec.c (revision 36006) @@ -1470,6 +1470,79 @@ return bn_obj; } +/* + * call-seq: + * point.mul(bn) => point + * point.mul(bn, bn) => point + * point.mul([bn], [point]) => point + * point.mul([bn], [point], bn) => point + */ +static VALUE ossl_ec_point_mul(int argc, VALUE *argv, VALUE self) +{ + EC_POINT *point1, *point2; + const EC_GROUP *group; + VALUE group_v = rb_iv_get(self, "@group"); + VALUE args[1] = {group_v}; + VALUE bn_v1, bn_v2, r, points_v; + BIGNUM *bn1 = NULL, *bn2 = NULL; + + Require_EC_POINT(self, point1); + SafeRequire_EC_GROUP(group_v, group); + + r = rb_obj_alloc(cEC_POINT); + ossl_ec_point_initialize(1, args, r); + Require_EC_POINT(r, point2); + + argc = rb_scan_args(argc, argv, "12", &bn_v1, &points_v, &bn_v2); + + if (rb_obj_is_kind_of(bn_v1, cBN)) { + bn1 = GetBNPtr(bn_v1); + if (argc >= 2) { + bn2 = GetBNPtr(points_v); + } + if (EC_POINT_mul(group, point2, bn2, point1, bn1, ossl_bn_ctx) != 1) + ossl_raise(eEC_POINT, "Multiplication failed"); + } else { + size_t i, points_len, bignums_len; + const EC_POINT **points; + const BIGNUM **bignums; + + Check_Type(bn_v1, T_ARRAY); + bignums_len = RARRAY_LEN(bn_v1); + bignums = (const BIGNUM **)OPENSSL_malloc(bignums_len * sizeof(BIGNUM *)); + + for (i = 0; i < bignums_len; ++i) { + bignums[i] = GetBNPtr(rb_ary_entry(bn_v1, i)); + } + + if (!rb_obj_is_kind_of(points_v, rb_cArray)) { + OPENSSL_free(bignums); + rb_raise(rb_eTypeError, "Argument2 must be an array"); + } + + rb_ary_unshift(points_v, self); + points_len = RARRAY_LEN(points_v); + points = (const EC_POINT **)OPENSSL_malloc(points_len * sizeof(EC_POINT *)); + + for (i = 0; i < points_len; ++i) { + Get_EC_POINT(rb_ary_entry(points_v, i), points[i]); + } + + if (argc >= 3) { + bn2 = GetBNPtr(bn_v2); + } + if (EC_POINTs_mul(group, point2, bn2, points_len, points, bignums, ossl_bn_ctx) != 1) { + OPENSSL_free(bignums); + OPENSSL_free(points); + ossl_raise(eEC_POINT, "Multiplication failed"); + } + OPENSSL_free(bignums); + OPENSSL_free(points); + } + + return r; +} + static void no_copy(VALUE klass) { rb_undef_method(klass, "copy"); @@ -1591,6 +1664,7 @@ /* all the other methods */ rb_define_method(cEC_POINT, "to_bn", ossl_ec_point_to_bn, 0); + rb_define_method(cEC_POINT, "mul", ossl_ec_point_mul, -1); no_copy(cEC); no_copy(cEC_GROUP); Index: test/openssl/test_pkey_ec.rb =================================================================== --- test/openssl/test_pkey_ec.rb (revision 36005) +++ test/openssl/test_pkey_ec.rb (revision 36006) @@ -184,6 +184,22 @@ assert(pem) end + def test_ec_point_mul + ec = OpenSSL::TestUtils::TEST_KEY_EC_P256V1 + p1 = ec.public_key + bn1 = OpenSSL::BN.new('10') + bn2 = OpenSSL::BN.new('20') + + p2 = p1.mul(bn1) + assert(p1.group == p2.group) + p2 = p1.mul(bn1, bn2) + assert(p1.group == p2.group) + p2 = p1.mul([bn1, bn2], [p1]) + assert(p1.group == p2.group) + p2 = p1.mul([bn1, bn2], [p1], bn2) + assert(p1.group == p2.group) + end + # test Group: asn1_flag, point_conversion end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/