@@ -1515,6 +1515,40 @@ static int s_ecc_import_export(void) {
1515
1515
return CRYPT_OK ;
1516
1516
}
1517
1517
1518
+ static int s_ecc_test_ethereum (void )
1519
+ {
1520
+ #ifdef LTC_ECC_SECP256K1
1521
+ int stat ;
1522
+ const ltc_ecc_curve * dp ;
1523
+ ecc_key key , reckey ;
1524
+ unsigned char buf [128 ];
1525
+ unsigned long len ;
1526
+ unsigned char data16 [16 ] = { 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 };
1527
+
1528
+ DO (ecc_find_curve ("SECP256K1" , & dp ));
1529
+
1530
+ DO (ecc_make_key_ex (& yarrow_prng , find_prng ("yarrow" ), & key , dp ));
1531
+
1532
+ /* test Ethereum signature */
1533
+ len = sizeof (buf );
1534
+ DO (ecc_sign_hash_eth27 (data16 , 16 , buf , & len , & yarrow_prng , find_prng ("yarrow" ), & key ));
1535
+ stat = 0 ;
1536
+ DO (ecc_verify_hash_eth27 (buf , len , data16 , 16 , & stat , & key ));
1537
+ if (stat != 1 ) return CRYPT_FAIL_TESTVECTOR ;
1538
+
1539
+ DO (ecc_set_curve (dp , & reckey ));
1540
+ DO (ecc_recover_key (buf , len , data16 , 16 , -1 , LTC_ECCSIG_ETH27 , & reckey ));
1541
+ DO (ecc_key_cmp (PK_PUBLIC , & key , & reckey ));
1542
+
1543
+ /* cleanup */
1544
+ ecc_free (& reckey );
1545
+ ecc_free (& key );
1546
+ return CRYPT_OK ;
1547
+ #else
1548
+ return CRYPT_NOP ;
1549
+ #endif
1550
+ }
1551
+
1518
1552
#ifdef LTC_ECC_SHAMIR
1519
1553
static int s_ecc_test_recovery (void )
1520
1554
{
@@ -1553,14 +1587,12 @@ static int s_ecc_test_recovery(void)
1553
1587
DO (ecc_set_key (eth_pubkey , sizeof (eth_pubkey ), PK_PUBLIC , & pubkey ));
1554
1588
1555
1589
DO (ecc_set_curve (dp , & reckey ));
1556
- stat = ecc_recover_key (eth_sig , sizeof (eth_sig )- 1 , eth_hash , sizeof (eth_hash ), 0 , LTC_ECCSIG_RFC7518 , & reckey );
1557
- if (stat != CRYPT_OK ) return CRYPT_FAIL_TESTVECTOR ;
1590
+ DO (ecc_recover_key (eth_sig , sizeof (eth_sig )- 1 , eth_hash , sizeof (eth_hash ), 0 , LTC_ECCSIG_RFC7518 , & reckey ));
1558
1591
DO (ecc_key_cmp (PK_PUBLIC , & pubkey , & reckey ));
1559
1592
ecc_free (& reckey );
1560
1593
1561
1594
DO (ecc_set_curve (dp , & reckey ));
1562
- stat = ecc_recover_key (eth_sig , sizeof (eth_sig ), eth_hash , sizeof (eth_hash ), -1 , LTC_ECCSIG_ETH27 , & reckey );
1563
- if (stat != CRYPT_OK ) return CRYPT_FAIL_TESTVECTOR ;
1595
+ DO (ecc_recover_key (eth_sig , sizeof (eth_sig ), eth_hash , sizeof (eth_hash ), -1 , LTC_ECCSIG_ETH27 , & reckey ));
1564
1596
DO (ecc_key_cmp (PK_PUBLIC , & pubkey , & reckey ));
1565
1597
ecc_free (& reckey );
1566
1598
@@ -1633,6 +1665,7 @@ int ecc_test(void)
1633
1665
DO (s_ecc_test_shamir ());
1634
1666
DO (s_ecc_test_recovery ());
1635
1667
#endif
1668
+ DO (s_ecc_test_ethereum ());
1636
1669
return CRYPT_OK ;
1637
1670
}
1638
1671
0 commit comments