674
660
ossl_raise(eECError, "Private EC key needed!");
676
662
str = rb_str_new(0, ECDSA_size(ec) + 16);
677
if (ECDSA_sign(0, RSTRING_PTR(data), RSTRING_LEN(data), RSTRING_PTR(str), &buf_len, ec) != 1)
663
if (ECDSA_sign(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LEN(data), (unsigned char *) RSTRING_PTR(str), &buf_len, ec) != 1)
678
664
ossl_raise(eECError, "ECDSA_sign");
680
666
rb_str_resize(str, buf_len);
791
const char *name = STR2CSTR(arg1);
792
int nid = OBJ_sn2nid(name);
777
const char *name = STR2CSTR(arg1);
778
int nid = OBJ_sn2nid(name);
794
if (nid == NID_undef)
795
ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
780
if (nid == NID_undef)
781
ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
797
783
group = EC_GROUP_new_by_curve_name(nid);
798
784
if (group == NULL)
799
785
ossl_raise(eEC_GROUP, "unable to create curve (%s)", name);
787
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
788
EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
829
* group1 == group2 => true | false
832
static VALUE ossl_ec_group_eql(VALUE a, VALUE b)
834
EC_GROUP *group1 = NULL, *group2 = NULL;
836
Require_EC_GROUP(a, group1);
837
SafeRequire_EC_GROUP(b, group2);
839
if (EC_GROUP_cmp(group1, group2, ossl_bn_ctx) == 1)
840
846
* group.generator => ec_point
842
848
* See the OpenSSL documentation for EC_GROUP_get0_generator()
1298
* point1 == point2 => true | false
1301
static VALUE ossl_ec_point_eql(VALUE a, VALUE b)
1303
EC_POINT *point1, *point2;
1304
VALUE group_v1 = rb_iv_get(a, "@group");
1305
VALUE group_v2 = rb_iv_get(b, "@group");
1306
const EC_GROUP *group;
1308
if (ossl_ec_group_eql(group_v1, group_v2) == Qfalse)
1311
Require_EC_POINT(a, point1);
1312
SafeRequire_EC_POINT(b, point2);
1313
SafeRequire_EC_GROUP(group_v1, group);
1315
if (EC_POINT_cmp(group, point1, point2, ossl_bn_ctx) == 1)
1323
* point.infinity? => true | false
1326
static VALUE ossl_ec_point_is_at_infinity(VALUE self)
1329
VALUE group_v = rb_iv_get(self, "@group");
1330
const EC_GROUP *group;
1332
Require_EC_POINT(self, point);
1333
SafeRequire_EC_GROUP(group_v, group);
1335
switch (EC_POINT_is_at_infinity(group, point)) {
1336
case 1: return Qtrue;
1337
case 0: return Qfalse;
1338
default: ossl_raise(cEC_POINT, "EC_POINT_is_at_infinity");
1344
* point.on_curve? => true | false
1347
static VALUE ossl_ec_point_is_on_curve(VALUE self)
1350
VALUE group_v = rb_iv_get(self, "@group");
1351
const EC_GROUP *group;
1353
Require_EC_POINT(self, point);
1354
SafeRequire_EC_GROUP(group_v, group);
1356
switch (EC_POINT_is_on_curve(group, point, ossl_bn_ctx)) {
1357
case 1: return Qtrue;
1358
case 0: return Qfalse;
1359
default: ossl_raise(cEC_POINT, "EC_POINT_is_on_curve");
1365
* point.make_affine! => self
1368
static VALUE ossl_ec_point_make_affine(VALUE self)
1371
VALUE group_v = rb_iv_get(self, "@group");
1372
const EC_GROUP *group;
1374
Require_EC_POINT(self, point);
1375
SafeRequire_EC_GROUP(group_v, group);
1377
if (EC_POINT_make_affine(group, point, ossl_bn_ctx) != 1)
1378
ossl_raise(cEC_POINT, "EC_POINT_make_affine");
1385
* point.invert! => self
1388
static VALUE ossl_ec_point_invert(VALUE self)
1391
VALUE group_v = rb_iv_get(self, "@group");
1392
const EC_GROUP *group;
1394
Require_EC_POINT(self, point);
1395
SafeRequire_EC_GROUP(group_v, group);
1397
if (EC_POINT_invert(group, point, ossl_bn_ctx) != 1)
1398
ossl_raise(cEC_POINT, "EC_POINT_invert");
1405
* point.set_to_infinity! => self
1408
static VALUE ossl_ec_point_set_to_infinity(VALUE self)
1411
VALUE group_v = rb_iv_get(self, "@group");
1412
const EC_GROUP *group;
1414
Require_EC_POINT(self, point);
1415
SafeRequire_EC_GROUP(group_v, group);
1417
if (EC_POINT_set_to_infinity(group, point) != 1)
1418
ossl_raise(cEC_POINT, "EC_POINT_set_to_infinity");
1292
1425
* point.to_bn => OpenSSL::BN
1294
1427
* See the OpenSSL documentation for EC_POINT_point2bn()
1350
1483
ID_compressed = rb_intern("compressed");
1351
1484
ID_hybrid = rb_intern("hybrid");
1486
#ifdef OPENSSL_EC_NAMED_CURVE
1487
rb_define_const(cEC, "NAMED_CURVE", ULONG2NUM(OPENSSL_EC_NAMED_CURVE));
1353
1490
rb_define_singleton_method(cEC, "builtin_curves", ossl_s_builtin_curves, 0);
1355
1492
rb_define_method(cEC, "initialize", ossl_ec_key_initialize, -1);
1373
1510
rb_define_method(cEC, "generate_key", ossl_ec_key_generate_key, 0);
1374
1511
rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
1376
rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 2);
1513
rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 1);
1377
1514
rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1378
1515
rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1379
1516
/* do_sign/do_verify */
1386
1523
rb_define_alloc_func(cEC_GROUP, ossl_ec_group_alloc);
1387
1524
rb_define_method(cEC_GROUP, "initialize", ossl_ec_group_initialize, -1);
1525
rb_define_method(cEC_GROUP, "eql?", ossl_ec_group_eql, 1);
1526
rb_define_alias(cEC_GROUP, "==", "eql?");
1388
1527
/* copy/dup/cmp */
1390
1529
rb_define_method(cEC_GROUP, "generator", ossl_ec_group_get_generator, 0);
1419
1558
rb_define_alloc_func(cEC_POINT, ossl_ec_point_alloc);
1420
1559
rb_define_method(cEC_POINT, "initialize", ossl_ec_point_initialize, -1);
1421
1560
rb_attr(cEC_POINT, rb_intern("group"), 1, 0, 0);
1561
rb_define_method(cEC_POINT, "eql?", ossl_ec_point_eql, 1);
1562
rb_define_alias(cEC_POINT, "==", "eql?");
1564
rb_define_method(cEC_POINT, "infinity?", ossl_ec_point_is_at_infinity, 0);
1565
rb_define_method(cEC_POINT, "on_curve?", ossl_ec_point_is_on_curve, 0);
1566
rb_define_method(cEC_POINT, "make_affine!", ossl_ec_point_make_affine, 0);
1567
rb_define_method(cEC_POINT, "invert!", ossl_ec_point_invert, 0);
1568
rb_define_method(cEC_POINT, "set_to_infinity!", ossl_ec_point_set_to_infinity, 0);
1422
1569
/* all the other methods */
1424
1571
rb_define_method(cEC_POINT, "to_bn", ossl_ec_point_to_bn, 0);