Skip to content

Commit c8fbc3c

Browse files
committed
[ECDH API change] Allow pass arbitrary data to hash function
1 parent b00be65 commit c8fbc3c

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

include/secp256k1_ecdh.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ extern "C" {
1313
* Out: output: pointer to an array to be filled by the function
1414
* In: x: pointer to a 32-byte x coordinate
1515
* y: pointer to a 32-byte y coordinate
16+
* data: Arbitrary data pointer that is passed through
1617
*/
1718
typedef int (*secp256k1_ecdh_hash_function)(
1819
unsigned char *output,
1920
const unsigned char *x,
20-
const unsigned char *y
21+
const unsigned char *y,
22+
void *data
2123
);
2224

2325
/** An implementation of SHA256 hash function that applies to compressed public key. */
@@ -35,13 +37,15 @@ SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_func
3537
* initialized public key
3638
* privkey: a 32-byte scalar with which to multiply the point
3739
* hashfp: pointer to a hash function. If NULL, secp256k1_ecdh_hash_function_sha256 is used
40+
* data: Arbitrary data pointer that is passed through
3841
*/
3942
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(
4043
const secp256k1_context* ctx,
4144
unsigned char *output,
4245
const secp256k1_pubkey *pubkey,
4346
const unsigned char *privkey,
44-
secp256k1_ecdh_hash_function hashfp
47+
secp256k1_ecdh_hash_function hashfp,
48+
void *data
4549
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
4650

4751
#ifdef __cplusplus

src/bench_ecdh.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void bench_ecdh(void* arg) {
4242
bench_ecdh_data *data = (bench_ecdh_data*)arg;
4343

4444
for (i = 0; i < 20000; i++) {
45-
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL) == 1);
45+
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
4646
}
4747
}
4848

src/java/org_bitcoin_NativeSecp256k1.c

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e
354354
nonce_res,
355355
&pubkey,
356356
secdata,
357+
NULL,
357358
NULL
358359
);
359360
}

src/modules/ecdh/main_impl.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#include "include/secp256k1_ecdh.h"
1111
#include "ecmult_const_impl.h"
1212

13-
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x, const unsigned char *y) {
13+
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
1414
unsigned char version = (y[31] & 0x01) | 0x02;
1515
secp256k1_sha256 sha;
16+
(void)data;
1617

1718
secp256k1_sha256_initialize(&sha);
1819
secp256k1_sha256_write(&sha, &version, 1);
@@ -25,7 +26,7 @@ static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char
2526
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256 = ecdh_hash_function_sha256;
2627
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default = ecdh_hash_function_sha256;
2728

28-
int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pubkey *point, const unsigned char *scalar, secp256k1_ecdh_hash_function hashfp) {
29+
int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pubkey *point, const unsigned char *scalar, secp256k1_ecdh_hash_function hashfp, void *data) {
2930
int ret = 0;
3031
int overflow = 0;
3132
secp256k1_gej res;
@@ -56,7 +57,7 @@ int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const se
5657
secp256k1_fe_get_b32(x, &pt.x);
5758
secp256k1_fe_get_b32(y, &pt.y);
5859

59-
ret = hashfp(output, x, y);
60+
ret = hashfp(output, x, y, data);
6061
}
6162

6263
secp256k1_scalar_clear(&s);

src/modules/ecdh/tests_impl.h

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
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) {
10+
int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
1111
(void)output;
1212
(void)x;
1313
(void)y;
14+
(void)data;
1415
return 0;
1516
}
1617

17-
int ecdh_hash_function_custom(unsigned char *output, const unsigned char *x, const unsigned char *y) {
18+
int ecdh_hash_function_custom(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
19+
(void)data;
1820
/* Save x and y as uncompressed public key */
1921
output[0] = 0x04;
2022
memcpy(output + 1, x, 32);
@@ -36,15 +38,15 @@ void test_ecdh_api(void) {
3638
CHECK(secp256k1_ec_pubkey_create(tctx, &point, s_one) == 1);
3739

3840
/* Check all NULLs are detected */
39-
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL) == 1);
41+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL, NULL) == 1);
4042
CHECK(ecount == 0);
41-
CHECK(secp256k1_ecdh(tctx, NULL, &point, s_one, NULL) == 0);
43+
CHECK(secp256k1_ecdh(tctx, NULL, &point, s_one, NULL, NULL) == 0);
4244
CHECK(ecount == 1);
43-
CHECK(secp256k1_ecdh(tctx, res, NULL, s_one, NULL) == 0);
45+
CHECK(secp256k1_ecdh(tctx, res, NULL, s_one, NULL, NULL) == 0);
4446
CHECK(ecount == 2);
45-
CHECK(secp256k1_ecdh(tctx, res, &point, NULL, NULL) == 0);
47+
CHECK(secp256k1_ecdh(tctx, res, &point, NULL, NULL, NULL) == 0);
4648
CHECK(ecount == 3);
47-
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL) == 1);
49+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL, NULL) == 1);
4850
CHECK(ecount == 3);
4951

5052
/* Cleanup */
@@ -74,14 +76,14 @@ void test_ecdh_generator_basepoint(void) {
7476
CHECK(secp256k1_ec_pubkey_create(ctx, &point[1], s_b32) == 1);
7577

7678
/* compute using ECDH function with custom hash function */
77-
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, ecdh_hash_function_custom) == 1);
79+
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, ecdh_hash_function_custom, NULL) == 1);
7880
/* compute "explicitly" */
7981
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], SECP256K1_EC_UNCOMPRESSED) == 1);
8082
/* compare */
8183
CHECK(memcmp(output_ecdh, point_ser, 65) == 0);
8284

8385
/* compute using ECDH function with default hash function */
84-
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, NULL) == 1);
86+
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, NULL, NULL) == 1);
8587
/* compute "explicitly" */
8688
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], SECP256K1_EC_COMPRESSED) == 1);
8789
secp256k1_sha256_initialize(&sha);
@@ -111,14 +113,14 @@ void test_bad_scalar(void) {
111113
CHECK(secp256k1_ec_pubkey_create(ctx, &point, s_rand) == 1);
112114

113115
/* Try to multiply it by bad values */
114-
CHECK(secp256k1_ecdh(ctx, output, &point, s_zero, NULL) == 0);
115-
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL) == 0);
116+
CHECK(secp256k1_ecdh(ctx, output, &point, s_zero, NULL, NULL) == 0);
117+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL, NULL) == 0);
116118
/* ...and a good one */
117119
s_overflow[31] -= 1;
118-
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL) == 1);
120+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL, NULL) == 1);
119121

120122
/* Hash function failure results in ecdh failure */
121-
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, ecdh_hash_function_test_fail) == 0);
123+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, ecdh_hash_function_test_fail, NULL) == 0);
122124
}
123125

124126
void run_ecdh_tests(void) {

0 commit comments

Comments
 (0)