Skip to content

Commit b00be65

Browse files
committed
[ECDH API change] Support custom hash function
1 parent 452d8e4 commit b00be65

File tree

5 files changed

+93
-35
lines changed

5 files changed

+93
-35
lines changed

include/secp256k1_ecdh.h

+24-4
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,41 @@
77
extern "C" {
88
#endif
99

10+
/** A pointer to a function that applies hash function to a point
11+
*
12+
* Returns: 1 if a point was successfully hashed. 0 will cause ecdh to fail
13+
* Out: output: pointer to an array to be filled by the function
14+
* In: x: pointer to a 32-byte x coordinate
15+
* y: pointer to a 32-byte y coordinate
16+
*/
17+
typedef int (*secp256k1_ecdh_hash_function)(
18+
unsigned char *output,
19+
const unsigned char *x,
20+
const unsigned char *y
21+
);
22+
23+
/** An implementation of SHA256 hash function that applies to compressed public key. */
24+
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256;
25+
26+
/** A default ecdh hash function (currently equal to secp256k1_ecdh_hash_function_sha256). */
27+
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default;
28+
1029
/** Compute an EC Diffie-Hellman secret in constant time
1130
* Returns: 1: exponentiation was successful
1231
* 0: scalar was invalid (zero or overflow)
1332
* Args: ctx: pointer to a context object (cannot be NULL)
14-
* Out: result: a 32-byte array which will be populated by an ECDH
15-
* secret computed from the point and scalar
33+
* Out: output: pointer to an array to be filled by the function
1634
* In: pubkey: a pointer to a secp256k1_pubkey containing an
1735
* initialized public key
1836
* privkey: a 32-byte scalar with which to multiply the point
37+
* hashfp: pointer to a hash function. If NULL, secp256k1_ecdh_hash_function_sha256 is used
1938
*/
2039
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(
2140
const secp256k1_context* ctx,
22-
unsigned char *result,
41+
unsigned char *output,
2342
const secp256k1_pubkey *pubkey,
24-
const unsigned char *privkey
43+
const unsigned char *privkey,
44+
secp256k1_ecdh_hash_function hashfp
2545
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
2646

2747
#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) == 1);
45+
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL) == 1);
4646
}
4747
}
4848

src/java/org_bitcoin_NativeSecp256k1.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e
8383

8484
secp256k1_ecdsa_signature sig[72];
8585

86-
int ret = secp256k1_ecdsa_sign(ctx, sig, data, secKey, NULL, NULL );
86+
int ret = secp256k1_ecdsa_sign(ctx, sig, data, secKey, NULL, NULL);
8787

8888
unsigned char outputSer[72];
8989
size_t outputLen = 72;
@@ -353,7 +353,8 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e
353353
ctx,
354354
nonce_res,
355355
&pubkey,
356-
secdata
356+
secdata,
357+
NULL
357358
);
358359
}
359360

src/modules/ecdh/main_impl.h

+25-13
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,53 @@
1010
#include "include/secp256k1_ecdh.h"
1111
#include "ecmult_const_impl.h"
1212

13-
int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *result, const secp256k1_pubkey *point, const unsigned char *scalar) {
13+
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x, const unsigned char *y) {
14+
unsigned char version = (y[31] & 0x01) | 0x02;
15+
secp256k1_sha256 sha;
16+
17+
secp256k1_sha256_initialize(&sha);
18+
secp256k1_sha256_write(&sha, &version, 1);
19+
secp256k1_sha256_write(&sha, x, 32);
20+
secp256k1_sha256_finalize(&sha, output);
21+
22+
return 1;
23+
}
24+
25+
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256 = ecdh_hash_function_sha256;
26+
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default = ecdh_hash_function_sha256;
27+
28+
int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pubkey *point, const unsigned char *scalar, secp256k1_ecdh_hash_function hashfp) {
1429
int ret = 0;
1530
int overflow = 0;
1631
secp256k1_gej res;
1732
secp256k1_ge pt;
1833
secp256k1_scalar s;
1934
VERIFY_CHECK(ctx != NULL);
20-
ARG_CHECK(result != NULL);
35+
ARG_CHECK(output != NULL);
2136
ARG_CHECK(point != NULL);
2237
ARG_CHECK(scalar != NULL);
38+
if (hashfp == NULL) {
39+
hashfp = secp256k1_ecdh_hash_function_default;
40+
}
2341

2442
secp256k1_pubkey_load(ctx, &pt, point);
2543
secp256k1_scalar_set_b32(&s, scalar, &overflow);
2644
if (overflow || secp256k1_scalar_is_zero(&s)) {
2745
ret = 0;
2846
} else {
2947
unsigned char x[32];
30-
unsigned char y[1];
31-
secp256k1_sha256 sha;
48+
unsigned char y[32];
3249

3350
secp256k1_ecmult_const(&res, &pt, &s, 256);
3451
secp256k1_ge_set_gej(&pt, &res);
35-
/* Compute a hash of the point in compressed form
36-
* Note we cannot use secp256k1_eckey_pubkey_serialize here since it does not
37-
* expect its output to be secret and has a timing sidechannel. */
52+
53+
/* Compute a hash of the point */
3854
secp256k1_fe_normalize(&pt.x);
3955
secp256k1_fe_normalize(&pt.y);
4056
secp256k1_fe_get_b32(x, &pt.x);
41-
y[0] = 0x02 | secp256k1_fe_is_odd(&pt.y);
57+
secp256k1_fe_get_b32(y, &pt.y);
4258

43-
secp256k1_sha256_initialize(&sha);
44-
secp256k1_sha256_write(&sha, y, sizeof(y));
45-
secp256k1_sha256_write(&sha, x, sizeof(x));
46-
secp256k1_sha256_finalize(&sha, result);
47-
ret = 1;
59+
ret = hashfp(output, x, y);
4860
}
4961

5062
secp256k1_scalar_clear(&s);

src/modules/ecdh/tests_impl.h

+40-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
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+
1025
void 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

99124
void run_ecdh_tests(void) {

0 commit comments

Comments
 (0)