@@ -1229,6 +1229,11 @@ static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char
12291229 return nonce_function_rfc6979 (nonce32 , msg32 , key32 , counter - 5 , data );
12301230}
12311231
1232+ int is_empty_compact_signature (const unsigned char * sig64 ) {
1233+ static const unsigned char res [64 ] = {0 };
1234+ return memcmp (sig64 , res , 64 ) == 0 ;
1235+ }
1236+
12321237void test_ecdsa_end_to_end (void ) {
12331238 unsigned char privkey [32 ];
12341239 unsigned char message [32 ];
@@ -1300,6 +1305,7 @@ void test_ecdsa_end_to_end(void) {
13001305
13011306 /* Sign. */
13021307 CHECK (secp256k1_ecdsa_sign (message , signature , & signaturelen , privkey , NULL , NULL ) == 1 );
1308+ CHECK (signaturelen > 0 );
13031309 /* Verify. */
13041310 CHECK (secp256k1_ecdsa_verify (message , signature , signaturelen , pubkey , pubkeylen ) == 1 );
13051311 /* Destroy signature and verify again. */
@@ -1308,6 +1314,7 @@ void test_ecdsa_end_to_end(void) {
13081314
13091315 /* Compact sign. */
13101316 CHECK (secp256k1_ecdsa_sign_compact (message , csignature , privkey , NULL , NULL , & recid ) == 1 );
1317+ CHECK (!is_empty_compact_signature (csignature ));
13111318 /* Recover. */
13121319 CHECK (secp256k1_ecdsa_recover_compact (message , csignature , recpubkey , & recpubkeylen , pubkeylen == 33 , recid ) == 1 );
13131320 CHECK (recpubkeylen == pubkeylen );
@@ -1588,13 +1595,18 @@ void test_ecdsa_edge_cases(void) {
15881595 unsigned char sig [72 ];
15891596 int siglen = 72 ;
15901597 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , precomputed_nonce_function , nonce ) == 0 );
1598+ CHECK (siglen == 0 );
15911599 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , precomputed_nonce_function , nonce2 ) == 0 );
1600+ CHECK (siglen == 0 );
15921601 msg [31 ] = 0xaa ;
15931602 siglen = 72 ;
15941603 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , precomputed_nonce_function , nonce ) == 1 );
1604+ CHECK (siglen > 0 );
15951605 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , precomputed_nonce_function , nonce2 ) == 1 );
1606+ CHECK (siglen > 0 );
15961607 siglen = 10 ;
15971608 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , precomputed_nonce_function , nonce ) != 1 );
1609+ CHECK (siglen == 0 );
15981610 }
15991611
16001612 /* Nonce function corner cases. */
@@ -1608,36 +1620,54 @@ void test_ecdsa_edge_cases(void) {
16081620 int siglen = 72 ;
16091621 int siglen2 = 72 ;
16101622 int recid2 ;
1611- memset (key , 0 , 32 );
16121623 memset (msg , 0 , 32 );
1613- key [31 ] = 1 ;
16141624 msg [31 ] = 1 ;
1625+ /* High key results in signature failure. */
1626+ memset (key , 0xFF , 32 );
1627+ CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , NULL , NULL ) == 0 );
1628+ CHECK (siglen == 0 );
1629+ /* Zero key results in signature failure. */
1630+ memset (key , 0 , 32 );
1631+ CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , NULL , NULL ) == 0 );
1632+ CHECK (siglen == 0 );
16151633 /* Nonce function failure results in signature failure. */
1634+ key [31 ] = 1 ;
16161635 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , nonce_function_test_fail , NULL ) == 0 );
1636+ CHECK (siglen == 0 );
16171637 CHECK (secp256k1_ecdsa_sign_compact (msg , sig , key , nonce_function_test_fail , NULL , & recid ) == 0 );
1638+ CHECK (is_empty_compact_signature (sig ));
16181639 /* The retry loop successfully makes its way to the first good value. */
16191640 siglen = 72 ;
16201641 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , nonce_function_test_retry , NULL ) == 1 );
1642+ CHECK (siglen > 0 );
16211643 CHECK (secp256k1_ecdsa_sign (msg , sig2 , & siglen2 , key , nonce_function_rfc6979 , NULL ) == 1 );
1644+ CHECK (siglen > 0 );
16221645 CHECK ((siglen == siglen2 ) && (memcmp (sig , sig2 , siglen ) == 0 ));
16231646 CHECK (secp256k1_ecdsa_sign_compact (msg , sig , key , nonce_function_test_retry , NULL , & recid ) == 1 );
1647+ CHECK (!is_empty_compact_signature (sig ));
16241648 CHECK (secp256k1_ecdsa_sign_compact (msg , sig2 , key , nonce_function_rfc6979 , NULL , & recid2 ) == 1 );
1649+ CHECK (!is_empty_compact_signature (sig2 ));
16251650 CHECK ((recid == recid2 ) && (memcmp (sig , sig2 , 64 ) == 0 ));
16261651 /* The default nonce function is determinstic. */
16271652 siglen = 72 ;
16281653 siglen2 = 72 ;
16291654 CHECK (secp256k1_ecdsa_sign (msg , sig , & siglen , key , NULL , NULL ) == 1 );
1655+ CHECK (siglen > 0 );
16301656 CHECK (secp256k1_ecdsa_sign (msg , sig2 , & siglen2 , key , NULL , NULL ) == 1 );
1657+ CHECK (siglen2 > 0 );
16311658 CHECK ((siglen == siglen2 ) && (memcmp (sig , sig2 , siglen ) == 0 ));
16321659 CHECK (secp256k1_ecdsa_sign_compact (msg , sig , key , NULL , NULL , & recid ) == 1 );
1660+ CHECK (!is_empty_compact_signature (sig ));
16331661 CHECK (secp256k1_ecdsa_sign_compact (msg , sig2 , key , NULL , NULL , & recid2 ) == 1 );
1662+ CHECK (!is_empty_compact_signature (sig ));
16341663 CHECK ((recid == recid2 ) && (memcmp (sig , sig2 , 64 ) == 0 ));
16351664 /* The default nonce function changes output with different messages. */
16361665 for (i = 0 ; i < 256 ; i ++ ) {
16371666 int j ;
16381667 siglen2 = 72 ;
16391668 msg [0 ] = i ;
16401669 CHECK (secp256k1_ecdsa_sign (msg , sig2 , & siglen2 , key , NULL , NULL ) == 1 );
1670+ CHECK (!is_empty_compact_signature (sig ));
16411671 CHECK (secp256k1_ecdsa_sig_parse (& s [i ], sig2 , siglen2 ));
16421672 for (j = 0 ; j < i ; j ++ ) {
16431673 CHECK (!secp256k1_scalar_eq (& s [i ].r , & s [j ].r ));
0 commit comments