77#ifndef SECP256K1_MODULE_ECDH_TESTS_H
88#define SECP256K1_MODULE_ECDH_TESTS_H
99
10+ int ecdh_hash_function_test_fail (unsigned char * output , const unsigned char * x , const unsigned char * y ) {
11+ (void )output ;
12+ (void )x ;
13+ (void )y ;
14+ return 0 ;
15+ }
16+
17+ int ecdh_hash_function_custom (unsigned char * output , const unsigned char * x , const unsigned char * y ) {
18+ /* Save x and y as uncompressed public key */
19+ output [0 ] = 0x04 ;
20+ memcpy (output + 1 , x , 32 );
21+ memcpy (output + 33 , y , 32 );
22+ return 1 ;
23+ }
24+
1025void test_ecdh_api (void ) {
1126 /* Setup context that just counts errors */
1227 secp256k1_context * tctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
@@ -21,15 +36,15 @@ void test_ecdh_api(void) {
2136 CHECK (secp256k1_ec_pubkey_create (tctx , & point , s_one ) == 1 );
2237
2338 /* Check all NULLs are detected */
24- CHECK (secp256k1_ecdh (tctx , res , & point , s_one ) == 1 );
39+ CHECK (secp256k1_ecdh (tctx , res , & point , s_one , NULL ) == 1 );
2540 CHECK (ecount == 0 );
26- CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one ) == 0 );
41+ CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one , NULL ) == 0 );
2742 CHECK (ecount == 1 );
28- CHECK (secp256k1_ecdh (tctx , res , NULL , s_one ) == 0 );
43+ CHECK (secp256k1_ecdh (tctx , res , NULL , s_one , NULL ) == 0 );
2944 CHECK (ecount == 2 );
30- CHECK (secp256k1_ecdh (tctx , res , & point , NULL ) == 0 );
45+ CHECK (secp256k1_ecdh (tctx , res , & point , NULL , NULL ) == 0 );
3146 CHECK (ecount == 3 );
32- CHECK (secp256k1_ecdh (tctx , res , & point , s_one ) == 1 );
47+ CHECK (secp256k1_ecdh (tctx , res , & point , s_one , NULL ) == 1 );
3348 CHECK (ecount == 3 );
3449
3550 /* Cleanup */
@@ -46,27 +61,34 @@ void test_ecdh_generator_basepoint(void) {
4661 for (i = 0 ; i < 100 ; ++ i ) {
4762 secp256k1_sha256 sha ;
4863 unsigned char s_b32 [32 ];
49- unsigned char output_ecdh [32 ];
64+ unsigned char output_ecdh [65 ];
5065 unsigned char output_ser [32 ];
51- unsigned char point_ser [33 ];
66+ unsigned char point_ser [65 ];
5267 size_t point_ser_len = sizeof (point_ser );
5368 secp256k1_scalar s ;
5469
5570 random_scalar_order (& s );
5671 secp256k1_scalar_get_b32 (s_b32 , & s );
5772
58- /* compute using ECDH function */
5973 CHECK (secp256k1_ec_pubkey_create (ctx , & point [0 ], s_one ) == 1 );
60- CHECK (secp256k1_ecdh (ctx , output_ecdh , & point [0 ], s_b32 ) == 1 );
61- /* compute "explicitly" */
6274 CHECK (secp256k1_ec_pubkey_create (ctx , & point [1 ], s_b32 ) == 1 );
75+
76+ /* compute using ECDH function with custom hash function */
77+ CHECK (secp256k1_ecdh (ctx , output_ecdh , & point [0 ], s_b32 , ecdh_hash_function_custom ) == 1 );
78+ /* compute "explicitly" */
79+ CHECK (secp256k1_ec_pubkey_serialize (ctx , point_ser , & point_ser_len , & point [1 ], SECP256K1_EC_UNCOMPRESSED ) == 1 );
80+ /* compare */
81+ CHECK (memcmp (output_ecdh , point_ser , 65 ) == 0 );
82+
83+ /* compute using ECDH function with default hash function */
84+ CHECK (secp256k1_ecdh (ctx , output_ecdh , & point [0 ], s_b32 , NULL ) == 1 );
85+ /* compute "explicitly" */
6386 CHECK (secp256k1_ec_pubkey_serialize (ctx , point_ser , & point_ser_len , & point [1 ], SECP256K1_EC_COMPRESSED ) == 1 );
64- CHECK (point_ser_len == sizeof (point_ser ));
6587 secp256k1_sha256_initialize (& sha );
6688 secp256k1_sha256_write (& sha , point_ser , point_ser_len );
6789 secp256k1_sha256_finalize (& sha , output_ser );
6890 /* compare */
69- CHECK (memcmp (output_ecdh , output_ser , sizeof ( output_ser ) ) == 0 );
91+ CHECK (memcmp (output_ecdh , output_ser , 32 ) == 0 );
7092 }
7193}
7294
@@ -89,11 +111,14 @@ void test_bad_scalar(void) {
89111 CHECK (secp256k1_ec_pubkey_create (ctx , & point , s_rand ) == 1 );
90112
91113 /* Try to multiply it by bad values */
92- CHECK (secp256k1_ecdh (ctx , output , & point , s_zero ) == 0 );
93- CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow ) == 0 );
114+ CHECK (secp256k1_ecdh (ctx , output , & point , s_zero , NULL ) == 0 );
115+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , NULL ) == 0 );
94116 /* ...and a good one */
95117 s_overflow [31 ] -= 1 ;
96- CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow ) == 1 );
118+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , NULL ) == 1 );
119+
120+ /* Hash function failure results in ecdh failure */
121+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , ecdh_hash_function_test_fail ) == 0 );
97122}
98123
99124void run_ecdh_tests (void ) {
0 commit comments