@@ -1516,6 +1516,44 @@ static int s_ecc_import_export(void) {
1516
1516
}
1517
1517
1518
1518
#ifdef LTC_ECC_SHAMIR
1519
+ static int s_ecc_test_ethereum (void )
1520
+ {
1521
+ #ifdef LTC_ECC_SECP256K1
1522
+ int stat ;
1523
+ const ltc_ecc_curve * dp ;
1524
+ ecc_key key , reckey ;
1525
+ unsigned char buf [128 ];
1526
+ unsigned long len ;
1527
+ unsigned char data16 [16 ] = { 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 , 0xd1 };
1528
+
1529
+ DO (ecc_find_curve ("SECP256K1" , & dp ));
1530
+
1531
+ DO (ecc_make_key_ex (& yarrow_prng , find_prng ("yarrow" ), & key , dp ));
1532
+
1533
+ /* test Ethereum signature */
1534
+ len = sizeof (buf );
1535
+ DO (ecc_sign_hash_eth27 (data16 , 16 , buf , & len , & yarrow_prng , find_prng ("yarrow" ), & key ));
1536
+ stat = 0 ;
1537
+ DO (ecc_verify_hash_eth27 (buf , len , data16 , 16 , & stat , & key ));
1538
+ if (stat != 1 ) return CRYPT_FAIL_TESTVECTOR ;
1539
+
1540
+ /* XXX-FIXME: TFM does not support sqrtmod_prime */
1541
+ if (strcmp (ltc_mp .name , "TomsFastMath" ) != 0 ) {
1542
+ DO (ecc_set_curve (dp , & reckey ));
1543
+ DO (ecc_recover_key (buf , len , data16 , 16 , -1 , LTC_ECCSIG_ETH27 , & reckey ));
1544
+ DO (ecc_key_cmp (PK_PUBLIC , & key , & reckey ));
1545
+
1546
+ /* cleanup */
1547
+ ecc_free (& reckey );
1548
+ }
1549
+ /* cleanup */
1550
+ ecc_free (& key );
1551
+ return CRYPT_OK ;
1552
+ #else
1553
+ return CRYPT_NOP ;
1554
+ #endif
1555
+ }
1556
+
1519
1557
static int s_ecc_test_recovery (void )
1520
1558
{
1521
1559
int i , recid , stat ;
@@ -1553,14 +1591,12 @@ static int s_ecc_test_recovery(void)
1553
1591
DO (ecc_set_key (eth_pubkey , sizeof (eth_pubkey ), PK_PUBLIC , & pubkey ));
1554
1592
1555
1593
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 ;
1594
+ DO (ecc_recover_key (eth_sig , sizeof (eth_sig )- 1 , eth_hash , sizeof (eth_hash ), 0 , LTC_ECCSIG_RFC7518 , & reckey ));
1558
1595
DO (ecc_key_cmp (PK_PUBLIC , & pubkey , & reckey ));
1559
1596
ecc_free (& reckey );
1560
1597
1561
1598
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 ;
1599
+ DO (ecc_recover_key (eth_sig , sizeof (eth_sig ), eth_hash , sizeof (eth_hash ), -1 , LTC_ECCSIG_ETH27 , & reckey ));
1564
1600
DO (ecc_key_cmp (PK_PUBLIC , & pubkey , & reckey ));
1565
1601
ecc_free (& reckey );
1566
1602
@@ -1632,6 +1668,7 @@ int ecc_test(void)
1632
1668
#ifdef LTC_ECC_SHAMIR
1633
1669
DO (s_ecc_test_shamir ());
1634
1670
DO (s_ecc_test_recovery ());
1671
+ DO (s_ecc_test_ethereum ());
1635
1672
#endif
1636
1673
return CRYPT_OK ;
1637
1674
}
0 commit comments