7
7
#ifndef SECP256K1_MODULE_ECDH_TESTS_H
8
8
#define SECP256K1_MODULE_ECDH_TESTS_H
9
9
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
+
10
25
void test_ecdh_api (void ) {
11
26
/* Setup context that just counts errors */
12
27
secp256k1_context * tctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
@@ -21,15 +36,15 @@ void test_ecdh_api(void) {
21
36
CHECK (secp256k1_ec_pubkey_create (tctx , & point , s_one ) == 1 );
22
37
23
38
/* 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 );
25
40
CHECK (ecount == 0 );
26
- CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one ) == 0 );
41
+ CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one , NULL ) == 0 );
27
42
CHECK (ecount == 1 );
28
- CHECK (secp256k1_ecdh (tctx , res , NULL , s_one ) == 0 );
43
+ CHECK (secp256k1_ecdh (tctx , res , NULL , s_one , NULL ) == 0 );
29
44
CHECK (ecount == 2 );
30
- CHECK (secp256k1_ecdh (tctx , res , & point , NULL ) == 0 );
45
+ CHECK (secp256k1_ecdh (tctx , res , & point , NULL , NULL ) == 0 );
31
46
CHECK (ecount == 3 );
32
- CHECK (secp256k1_ecdh (tctx , res , & point , s_one ) == 1 );
47
+ CHECK (secp256k1_ecdh (tctx , res , & point , s_one , NULL ) == 1 );
33
48
CHECK (ecount == 3 );
34
49
35
50
/* Cleanup */
@@ -46,27 +61,34 @@ void test_ecdh_generator_basepoint(void) {
46
61
for (i = 0 ; i < 100 ; ++ i ) {
47
62
secp256k1_sha256 sha ;
48
63
unsigned char s_b32 [32 ];
49
- unsigned char output_ecdh [32 ];
64
+ unsigned char output_ecdh [65 ];
50
65
unsigned char output_ser [32 ];
51
- unsigned char point_ser [33 ];
66
+ unsigned char point_ser [65 ];
52
67
size_t point_ser_len = sizeof (point_ser );
53
68
secp256k1_scalar s ;
54
69
55
70
random_scalar_order (& s );
56
71
secp256k1_scalar_get_b32 (s_b32 , & s );
57
72
58
- /* compute using ECDH function */
59
73
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" */
62
74
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" */
63
86
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 ));
65
87
secp256k1_sha256_initialize (& sha );
66
88
secp256k1_sha256_write (& sha , point_ser , point_ser_len );
67
89
secp256k1_sha256_finalize (& sha , output_ser );
68
90
/* compare */
69
- CHECK (memcmp (output_ecdh , output_ser , sizeof ( output_ser ) ) == 0 );
91
+ CHECK (memcmp (output_ecdh , output_ser , 32 ) == 0 );
70
92
}
71
93
}
72
94
@@ -89,11 +111,14 @@ void test_bad_scalar(void) {
89
111
CHECK (secp256k1_ec_pubkey_create (ctx , & point , s_rand ) == 1 );
90
112
91
113
/* 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 );
94
116
/* ...and a good one */
95
117
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 );
97
122
}
98
123
99
124
void run_ecdh_tests (void ) {
0 commit comments