7
7
#include "impl/ecdsa.h"
8
8
#include "impl/util.h"
9
9
10
+ #ifdef ENABLE_OPENSSL_TESTS
11
+ #include "openssl/bn.h"
12
+ #include "openssl/ec.h"
13
+ #include "openssl/ecdsa.h"
14
+ #include "openssl/obj_mac.h"
15
+ #endif
16
+
10
17
static int count = 100 ;
11
18
12
19
/***** NUM TESTS *****/
@@ -330,28 +337,33 @@ void run_wnaf() {
330
337
secp256k1_num_free (& n );
331
338
}
332
339
340
+ void random_sign (secp256k1_ecdsa_sig_t * sig , const secp256k1_num_t * key , const secp256k1_num_t * msg , int * recid ) {
341
+ secp256k1_num_t nonce ;
342
+ secp256k1_num_init (& nonce );
343
+ do {
344
+ random_num_order_test (& nonce );
345
+ } while (!secp256k1_ecdsa_sig_sign (sig , key , msg , & nonce , recid ));
346
+ secp256k1_num_free (& nonce );
347
+ }
348
+
333
349
void test_ecdsa_sign_verify () {
334
350
const secp256k1_ge_consts_t * c = secp256k1_ge_consts ;
335
- secp256k1_num_t msg , key , nonce ;
351
+ secp256k1_num_t msg , key ;
336
352
secp256k1_num_init (& msg );
337
353
random_num_order_test (& msg );
338
354
secp256k1_num_init (& key );
339
355
random_num_order_test (& key );
340
- secp256k1_num_init (& nonce );
341
356
secp256k1_gej_t pubj ; secp256k1_ecmult_gen (& pubj , & key );
342
357
secp256k1_ge_t pub ; secp256k1_ge_set_gej (& pub , & pubj );
343
358
secp256k1_ecdsa_sig_t sig ;
344
359
secp256k1_ecdsa_sig_init (& sig );
345
- do {
346
- random_num_order_test (& nonce );
347
- } while (!secp256k1_ecdsa_sig_sign (& sig , & key , & msg , & nonce , NULL ));
360
+ random_sign (& sig , & key , & msg , NULL );
348
361
assert (secp256k1_ecdsa_sig_verify (& sig , & pub , & msg ));
349
362
secp256k1_num_inc (& msg );
350
363
assert (!secp256k1_ecdsa_sig_verify (& sig , & pub , & msg ));
351
364
secp256k1_ecdsa_sig_free (& sig );
352
365
secp256k1_num_free (& msg );
353
366
secp256k1_num_free (& key );
354
- secp256k1_num_free (& nonce );
355
367
}
356
368
357
369
void run_ecdsa_sign_verify () {
@@ -360,10 +372,75 @@ void run_ecdsa_sign_verify() {
360
372
}
361
373
}
362
374
375
+ #ifdef ENABLE_OPENSSL_TESTS
376
+ EC_KEY * get_openssl_key (const secp256k1_num_t * key ) {
377
+ EC_KEY * ec_key = EC_KEY_new_by_curve_name (NID_secp256k1 );
378
+ BN_CTX * ctx = BN_CTX_new ();
379
+ BN_CTX_start (ctx );
380
+ BIGNUM * priv_key = BN_CTX_get (ctx );
381
+ unsigned char keyb [32 ];
382
+ secp256k1_num_get_bin (keyb , 32 , key );
383
+ BN_bin2bn (keyb , 32 , priv_key );
384
+ const EC_GROUP * group = EC_KEY_get0_group (ec_key );
385
+ EC_POINT * pub_key = EC_POINT_new (group );
386
+ EC_POINT_mul (group , pub_key , priv_key , NULL , NULL , ctx );
387
+ EC_KEY_set_private_key (ec_key , priv_key );
388
+ EC_KEY_set_public_key (ec_key , pub_key );
389
+ EC_POINT_free (pub_key );
390
+ BN_CTX_end (ctx );
391
+ BN_CTX_free (ctx );
392
+ return ec_key ;
393
+ }
394
+
395
+ void test_ecdsa_openssl () {
396
+ const secp256k1_ge_consts_t * c = secp256k1_ge_consts ;
397
+ secp256k1_num_t key , msg ;
398
+ secp256k1_num_init (& msg );
399
+ unsigned char message [32 ];
400
+ secp256k1_rand256_test (message );
401
+ secp256k1_num_set_bin (& msg , message , 32 );
402
+ secp256k1_num_init (& key );
403
+ random_num_order_test (& key );
404
+ secp256k1_gej_t qj ;
405
+ secp256k1_ecmult_gen (& qj , & key );
406
+ secp256k1_ge_t q ;
407
+ secp256k1_ge_set_gej (& q , & qj );
408
+ EC_KEY * ec_key = get_openssl_key (& key );
409
+ assert (ec_key );
410
+ unsigned char signature [80 ];
411
+ int sigsize = 80 ;
412
+ assert (ECDSA_sign (0 , message , sizeof (message ), signature , & sigsize , ec_key ));
413
+ secp256k1_ecdsa_sig_t sig ;
414
+ secp256k1_ecdsa_sig_init (& sig );
415
+ assert (secp256k1_ecdsa_sig_parse (& sig , signature , sigsize ));
416
+ assert (secp256k1_ecdsa_sig_verify (& sig , & q , & msg ));
417
+ secp256k1_num_inc (& sig .r );
418
+ assert (!secp256k1_ecdsa_sig_verify (& sig , & q , & msg ));
419
+
420
+ random_sign (& sig , & key , & msg , NULL );
421
+ sigsize = 80 ;
422
+ assert (secp256k1_ecdsa_sig_serialize (signature , & sigsize , & sig ));
423
+ assert (ECDSA_verify (0 , message , sizeof (message ), signature , sigsize , ec_key ) == 1 );
424
+
425
+ secp256k1_ecdsa_sig_free (& sig );
426
+ EC_KEY_free (ec_key );
427
+ secp256k1_num_free (& key );
428
+ secp256k1_num_free (& msg );
429
+ }
430
+
431
+ void run_ecdsa_openssl () {
432
+ for (int i = 0 ; i < 10 * count ; i ++ ) {
433
+ test_ecdsa_openssl ();
434
+ }
435
+ }
436
+ #endif
437
+
363
438
int main (int argc , char * * argv ) {
364
439
if (argc > 1 )
365
440
count = strtol (argv [1 ], NULL , 0 )* 47 ;
366
441
442
+ printf ("test count = %i\n" , count );
443
+
367
444
// initialize
368
445
secp256k1_fe_start ();
369
446
secp256k1_ge_start ();
@@ -379,6 +456,9 @@ int main(int argc, char **argv) {
379
456
380
457
// ecdsa tests
381
458
run_ecdsa_sign_verify ();
459
+ #ifdef ENABLE_OPENSSL_TESTS
460
+ run_ecdsa_openssl ();
461
+ #endif
382
462
383
463
// shutdown
384
464
secp256k1_ecmult_stop ();
0 commit comments