@@ -616,34 +616,37 @@ void test_ecdsa_adaptor_spec_vectors(void) {
616
616
}
617
617
618
618
/* Nonce function that returns constant 0 */
619
- static int ecdsa_adaptor_nonce_function_failing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo16 , void * data ) {
619
+ static int ecdsa_adaptor_nonce_function_failing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo , size_t algolen , void * data ) {
620
620
(void ) msg32 ;
621
621
(void ) key32 ;
622
622
(void ) encryption_key33 ;
623
- (void ) algo16 ;
623
+ (void ) algo ;
624
+ (void ) algolen ;
624
625
(void ) data ;
625
626
(void ) nonce32 ;
626
627
return 0 ;
627
628
}
628
629
629
630
/* Nonce function that sets nonce to 0 */
630
- static int ecdsa_adaptor_nonce_function_0 (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo16 , void * data ) {
631
+ static int ecdsa_adaptor_nonce_function_0 (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo , size_t algolen , void * data ) {
631
632
(void ) msg32 ;
632
633
(void ) key32 ;
633
634
(void ) encryption_key33 ;
634
- (void ) algo16 ;
635
+ (void ) algo ;
636
+ (void ) algolen ;
635
637
(void ) data ;
636
638
637
639
memset (nonce32 , 0 , 32 );
638
640
return 1 ;
639
641
}
640
642
641
643
/* Nonce function that sets nonce to 0xFF...0xFF */
642
- static int ecdsa_adaptor_nonce_function_overflowing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo16 , void * data ) {
644
+ static int ecdsa_adaptor_nonce_function_overflowing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * encryption_key33 , const unsigned char * algo , size_t algolen , void * data ) {
643
645
(void ) msg32 ;
644
646
(void ) key32 ;
645
647
(void ) encryption_key33 ;
646
- (void ) algo16 ;
648
+ (void ) algo ;
649
+ (void ) algolen ;
647
650
(void ) data ;
648
651
649
652
memset (nonce32 , 0xFF , 32 );
@@ -653,11 +656,11 @@ static int ecdsa_adaptor_nonce_function_overflowing(unsigned char *nonce32, cons
653
656
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
654
657
* bytes) changes the hash function
655
658
*/
656
- void nonce_function_ecdsa_adaptor_bitflip (unsigned char * * args , size_t n_flip , size_t n_bytes ) {
659
+ void nonce_function_ecdsa_adaptor_bitflip (unsigned char * * args , size_t n_flip , size_t n_bytes , size_t algolen ) {
657
660
unsigned char nonces [2 ][32 ];
658
- CHECK (nonce_function_ecdsa_adaptor (nonces [0 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ]) == 1 );
661
+ CHECK (nonce_function_ecdsa_adaptor (nonces [0 ], args [0 ], args [1 ], args [2 ], args [3 ], algolen , args [4 ]) == 1 );
659
662
secp256k1_testrand_flip (args [n_flip ], n_bytes );
660
- CHECK (nonce_function_ecdsa_adaptor (nonces [1 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ]) == 1 );
663
+ CHECK (nonce_function_ecdsa_adaptor (nonces [1 ], args [0 ], args [1 ], args [2 ], args [3 ], algolen , args [4 ]) == 1 );
661
664
CHECK (secp256k1_memcmp_var (nonces [0 ], nonces [1 ], 32 ) != 0 );
662
665
}
663
666
@@ -675,7 +678,8 @@ void ecdsa_adaptor_test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_
675
678
void run_nonce_function_ecdsa_adaptor_tests (void ) {
676
679
unsigned char tag [16 ] = "ECDSAadaptor/non" ;
677
680
unsigned char aux_tag [16 ] = "ECDSAadaptor/aux" ;
678
- unsigned char algo16 [16 ] = "ECDSAadaptor/non" ;
681
+ unsigned char algo [16 ] = "ECDSAadaptor/non" ;
682
+ size_t algolen = sizeof (algo );
679
683
unsigned char dleq_tag [4 ] = "DLEQ" ;
680
684
secp256k1_sha256 sha ;
681
685
secp256k1_sha256 sha_optimized ;
@@ -717,33 +721,40 @@ void run_nonce_function_ecdsa_adaptor_tests(void) {
717
721
args [0 ] = msg ;
718
722
args [1 ] = key ;
719
723
args [2 ] = pk ;
720
- args [3 ] = algo16 ;
724
+ args [3 ] = algo ;
721
725
args [4 ] = aux_rand ;
722
726
for (i = 0 ; i < count ; i ++ ) {
723
- nonce_function_ecdsa_adaptor_bitflip (args , 0 , 32 );
724
- nonce_function_ecdsa_adaptor_bitflip (args , 1 , 32 );
725
- nonce_function_ecdsa_adaptor_bitflip (args , 2 , 32 );
726
- /* Flip algo16 special case "ECDSAadaptor/non" */
727
- nonce_function_ecdsa_adaptor_bitflip (args , 3 , 16 );
728
- /* Flip algo16 again */
729
- nonce_function_ecdsa_adaptor_bitflip (args , 3 , 16 );
730
- nonce_function_ecdsa_adaptor_bitflip (args , 4 , 32 );
727
+ nonce_function_ecdsa_adaptor_bitflip (args , 0 , 32 , algolen );
728
+ nonce_function_ecdsa_adaptor_bitflip (args , 1 , 32 , algolen );
729
+ nonce_function_ecdsa_adaptor_bitflip (args , 2 , 32 , algolen );
730
+ /* Flip algo special case "ECDSAadaptor/non" */
731
+ nonce_function_ecdsa_adaptor_bitflip (args , 3 , 16 , algolen );
732
+ /* Flip algo again */
733
+ nonce_function_ecdsa_adaptor_bitflip (args , 3 , 16 , algolen );
734
+ nonce_function_ecdsa_adaptor_bitflip (args , 4 , 32 , algolen );
731
735
}
732
736
733
- /* NULL algo16 is disallowed */
734
- CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , NULL , NULL ) == 0 );
735
- /* Empty algo16 is fine */
736
- memset (algo16 , 0x00 , 16 );
737
- CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo16 , NULL ) == 1 );
738
- /* algo16 with terminating null bytes is fine */
739
- algo16 [1 ] = 65 ;
740
- CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo16 , NULL ) == 1 );
741
- /* Other algo16 is fine */
742
- memset (algo16 , 0xFF , 16 );
743
- CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo16 , NULL ) == 1 );
737
+ /* NULL algo is disallowed */
738
+ CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , NULL , 0 , NULL ) == 0 );
739
+ /* Empty algo is fine */
740
+ memset (algo , 0x00 , algolen );
741
+ CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
742
+ /* Other algo is fine */
743
+ memset (algo , 0xFF , algolen );
744
+ CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
745
+
746
+ /* Different algolen gives different nonce */
747
+ for (i = 0 ; i < count ; i ++ ) {
748
+ unsigned char nonce2 [32 ];
749
+ uint32_t offset = secp256k1_testrand_int (algolen - 1 );
750
+ size_t algolen_tmp = (algolen + offset ) % algolen ;
751
+
752
+ CHECK (nonce_function_ecdsa_adaptor (nonce2 , msg , key , pk , algo , algolen_tmp , NULL ) == 1 );
753
+ CHECK (secp256k1_memcmp_var (nonce , nonce2 , 32 ) != 0 );
754
+ }
744
755
745
756
/* NULL aux_rand argument is allowed. */
746
- CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo16 , NULL ) == 1 );
757
+ CHECK (nonce_function_ecdsa_adaptor (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
747
758
}
748
759
749
760
void test_ecdsa_adaptor_api (void ) {
0 commit comments