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
+ return 0 ;
12
+ }
13
+
14
+ int ecdh_hash_function_custom (unsigned char * output , const unsigned char * x , const unsigned char * y ) {
15
+ /* Save x and y as uncompressed public key */
16
+ output [0 ] = 0x04 ;
17
+ memcpy (output + 1 , x , 32 );
18
+ memcpy (output + 33 , y , 32 );
19
+ return 1 ;
20
+ }
21
+
10
22
void test_ecdh_api (void ) {
11
23
/* Setup context that just counts errors */
12
24
secp256k1_context * tctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
@@ -21,15 +33,15 @@ void test_ecdh_api(void) {
21
33
CHECK (secp256k1_ec_pubkey_create (tctx , & point , s_one ) == 1 );
22
34
23
35
/* Check all NULLs are detected */
24
- CHECK (secp256k1_ecdh (tctx , res , & point , s_one ) == 1 );
36
+ CHECK (secp256k1_ecdh (tctx , res , & point , s_one , NULL ) == 1 );
25
37
CHECK (ecount == 0 );
26
- CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one ) == 0 );
38
+ CHECK (secp256k1_ecdh (tctx , NULL , & point , s_one , NULL ) == 0 );
27
39
CHECK (ecount == 1 );
28
- CHECK (secp256k1_ecdh (tctx , res , NULL , s_one ) == 0 );
40
+ CHECK (secp256k1_ecdh (tctx , res , NULL , s_one , NULL ) == 0 );
29
41
CHECK (ecount == 2 );
30
- CHECK (secp256k1_ecdh (tctx , res , & point , NULL ) == 0 );
42
+ CHECK (secp256k1_ecdh (tctx , res , & point , NULL , NULL ) == 0 );
31
43
CHECK (ecount == 3 );
32
- CHECK (secp256k1_ecdh (tctx , res , & point , s_one ) == 1 );
44
+ CHECK (secp256k1_ecdh (tctx , res , & point , s_one , NULL ) == 1 );
33
45
CHECK (ecount == 3 );
34
46
35
47
/* Cleanup */
@@ -46,27 +58,34 @@ void test_ecdh_generator_basepoint(void) {
46
58
for (i = 0 ; i < 100 ; ++ i ) {
47
59
secp256k1_sha256 sha ;
48
60
unsigned char s_b32 [32 ];
49
- unsigned char output_ecdh [32 ];
61
+ unsigned char output_ecdh [65 ];
50
62
unsigned char output_ser [32 ];
51
- unsigned char point_ser [33 ];
63
+ unsigned char point_ser [65 ];
52
64
size_t point_ser_len = sizeof (point_ser );
53
65
secp256k1_scalar s ;
54
66
55
67
random_scalar_order (& s );
56
68
secp256k1_scalar_get_b32 (s_b32 , & s );
57
69
58
- /* compute using ECDH function */
59
70
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
71
CHECK (secp256k1_ec_pubkey_create (ctx , & point [1 ], s_b32 ) == 1 );
72
+
73
+ /* compute using ECDH function with custom hash function */
74
+ CHECK (secp256k1_ecdh (ctx , output_ecdh , & point [0 ], s_b32 , ecdh_hash_function_custom ) == 1 );
75
+ /* compute "explicitly" */
76
+ CHECK (secp256k1_ec_pubkey_serialize (ctx , point_ser , & point_ser_len , & point [1 ], SECP256K1_EC_UNCOMPRESSED ) == 1 );
77
+ /* compare */
78
+ CHECK (memcmp (output_ecdh , point_ser , 65 ) == 0 );
79
+
80
+ /* compute using ECDH function with default hash function */
81
+ CHECK (secp256k1_ecdh (ctx , output_ecdh , & point [0 ], s_b32 , NULL ) == 1 );
82
+ /* compute "explicitly" */
63
83
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
84
secp256k1_sha256_initialize (& sha );
66
85
secp256k1_sha256_write (& sha , point_ser , point_ser_len );
67
86
secp256k1_sha256_finalize (& sha , output_ser );
68
87
/* compare */
69
- CHECK (memcmp (output_ecdh , output_ser , sizeof ( output_ser ) ) == 0 );
88
+ CHECK (memcmp (output_ecdh , output_ser , 32 ) == 0 );
70
89
}
71
90
}
72
91
@@ -89,11 +108,14 @@ void test_bad_scalar(void) {
89
108
CHECK (secp256k1_ec_pubkey_create (ctx , & point , s_rand ) == 1 );
90
109
91
110
/* 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 );
111
+ CHECK (secp256k1_ecdh (ctx , output , & point , s_zero , NULL ) == 0 );
112
+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , NULL ) == 0 );
94
113
/* ...and a good one */
95
114
s_overflow [31 ] -= 1 ;
96
- CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow ) == 1 );
115
+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , NULL ) == 1 );
116
+
117
+ /* Hash function failure results in ecdh failure */
118
+ CHECK (secp256k1_ecdh (ctx , output , & point , s_overflow , ecdh_hash_function_test_fail ) == 0 );
97
119
}
98
120
99
121
void run_ecdh_tests (void ) {
0 commit comments