Skip to content

Commit 1aba679

Browse files
authored
fix: ensure matching ECDH shared secret
1 parent 88be4e8 commit 1aba679

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

examples/ecdh.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
#include "examples_util.h"
1919

20+
/* Identity hash function for ECDH, returns x coordinate directly */
21+
int ecdh_hash(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) {
22+
(void)y32;
23+
(void)data;
24+
memcpy(output, x32, 32);
25+
return 1;
26+
}
27+
2028
int main(void) {
2129
unsigned char seckey1[32];
2230
unsigned char seckey2[32];
@@ -79,12 +87,12 @@ int main(void) {
7987

8088
/* Perform ECDH with seckey1 and pubkey2. Should never fail with a verified
8189
* seckey and valid pubkey */
82-
return_val = secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL);
90+
return_val = secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, ecdh_hash, NULL);
8391
assert(return_val);
8492

8593
/* Perform ECDH with seckey2 and pubkey1. Should never fail with a verified
8694
* seckey and valid pubkey */
87-
return_val = secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL);
95+
return_val = secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, ecdh_hash, NULL);
8896
assert(return_val);
8997

9098
/* Both parties should end up with the same shared secret */

0 commit comments

Comments
 (0)