Skip to content

Commit 1086fda

Browse files
committed
Merge bitcoin#354: [ECDH API change] Support custom hash function
c8fbc3c [ECDH API change] Allow pass arbitrary data to hash function (Kirill Fomichev) b00be65 [ECDH API change] Support custom hash function (Kirill Fomichev) Pull request description: Solve bitcoin#352 Tree-SHA512: f5985874d03e976cdb3d59036af7720636ad1488da40fd3bd7881b1fb71b05036a952013d519baa84c4ce4b558bdef25c4ce76b384b297e4d0aece9e37e78a01
2 parents 1e6f1f5 + c8fbc3c commit 1086fda

File tree

5 files changed

+101
-35
lines changed

5 files changed

+101
-35
lines changed

include/secp256k1_ecdh.h

+28-4
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,45 @@
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+
* data: Arbitrary data pointer that is passed through
17+
*/
18+
typedef int (*secp256k1_ecdh_hash_function)(
19+
unsigned char *output,
20+
const unsigned char *x,
21+
const unsigned char *y,
22+
void *data
23+
);
24+
25+
/** An implementation of SHA256 hash function that applies to compressed public key. */
26+
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256;
27+
28+
/** A default ecdh hash function (currently equal to secp256k1_ecdh_hash_function_sha256). */
29+
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default;
30+
1031
/** Compute an EC Diffie-Hellman secret in constant time
1132
* Returns: 1: exponentiation was successful
1233
* 0: scalar was invalid (zero or overflow)
1334
* 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
35+
* Out: output: pointer to an array to be filled by the function
1636
* In: pubkey: a pointer to a secp256k1_pubkey containing an
1737
* initialized public key
1838
* privkey: a 32-byte scalar with which to multiply the point
39+
* hashfp: pointer to a hash function. If NULL, secp256k1_ecdh_hash_function_sha256 is used
40+
* data: Arbitrary data pointer that is passed through
1941
*/
2042
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(
2143
const secp256k1_context* ctx,
22-
unsigned char *result,
44+
unsigned char *output,
2345
const secp256k1_pubkey *pubkey,
24-
const unsigned char *privkey
46+
const unsigned char *privkey,
47+
secp256k1_ecdh_hash_function hashfp,
48+
void *data
2549
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
2650

2751
#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, NULL) == 1);
4646
}
4747
}
4848

src/java/org_bitcoin_NativeSecp256k1.c

+4-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,9 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e
353353
ctx,
354354
nonce_res,
355355
&pubkey,
356-
secdata
356+
secdata,
357+
NULL,
358+
NULL
357359
);
358360
}
359361

src/modules/ecdh/main_impl.h

+26-13
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,54 @@
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, void *data) {
14+
unsigned char version = (y[31] & 0x01) | 0x02;
15+
secp256k1_sha256 sha;
16+
(void)data;
17+
18+
secp256k1_sha256_initialize(&sha);
19+
secp256k1_sha256_write(&sha, &version, 1);
20+
secp256k1_sha256_write(&sha, x, 32);
21+
secp256k1_sha256_finalize(&sha, output);
22+
23+
return 1;
24+
}
25+
26+
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256 = ecdh_hash_function_sha256;
27+
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default = ecdh_hash_function_sha256;
28+
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) {
1430
int ret = 0;
1531
int overflow = 0;
1632
secp256k1_gej res;
1733
secp256k1_ge pt;
1834
secp256k1_scalar s;
1935
VERIFY_CHECK(ctx != NULL);
20-
ARG_CHECK(result != NULL);
36+
ARG_CHECK(output != NULL);
2137
ARG_CHECK(point != NULL);
2238
ARG_CHECK(scalar != NULL);
39+
if (hashfp == NULL) {
40+
hashfp = secp256k1_ecdh_hash_function_default;
41+
}
2342

2443
secp256k1_pubkey_load(ctx, &pt, point);
2544
secp256k1_scalar_set_b32(&s, scalar, &overflow);
2645
if (overflow || secp256k1_scalar_is_zero(&s)) {
2746
ret = 0;
2847
} else {
2948
unsigned char x[32];
30-
unsigned char y[1];
31-
secp256k1_sha256 sha;
49+
unsigned char y[32];
3250

3351
secp256k1_ecmult_const(&res, &pt, &s, 256);
3452
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. */
53+
54+
/* Compute a hash of the point */
3855
secp256k1_fe_normalize(&pt.x);
3956
secp256k1_fe_normalize(&pt.y);
4057
secp256k1_fe_get_b32(x, &pt.x);
41-
y[0] = 0x02 | secp256k1_fe_is_odd(&pt.y);
58+
secp256k1_fe_get_b32(y, &pt.y);
4259

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;
60+
ret = hashfp(output, x, y, data);
4861
}
4962

5063
secp256k1_scalar_clear(&s);

src/modules/ecdh/tests_impl.h

+42-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
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, void *data) {
11+
(void)output;
12+
(void)x;
13+
(void)y;
14+
(void)data;
15+
return 0;
16+
}
17+
18+
int ecdh_hash_function_custom(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
19+
(void)data;
20+
/* Save x and y as uncompressed public key */
21+
output[0] = 0x04;
22+
memcpy(output + 1, x, 32);
23+
memcpy(output + 33, y, 32);
24+
return 1;
25+
}
26+
1027
void test_ecdh_api(void) {
1128
/* Setup context that just counts errors */
1229
secp256k1_context *tctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
@@ -21,15 +38,15 @@ void test_ecdh_api(void) {
2138
CHECK(secp256k1_ec_pubkey_create(tctx, &point, s_one) == 1);
2239

2340
/* Check all NULLs are detected */
24-
CHECK(secp256k1_ecdh(tctx, res, &point, s_one) == 1);
41+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL, NULL) == 1);
2542
CHECK(ecount == 0);
26-
CHECK(secp256k1_ecdh(tctx, NULL, &point, s_one) == 0);
43+
CHECK(secp256k1_ecdh(tctx, NULL, &point, s_one, NULL, NULL) == 0);
2744
CHECK(ecount == 1);
28-
CHECK(secp256k1_ecdh(tctx, res, NULL, s_one) == 0);
45+
CHECK(secp256k1_ecdh(tctx, res, NULL, s_one, NULL, NULL) == 0);
2946
CHECK(ecount == 2);
30-
CHECK(secp256k1_ecdh(tctx, res, &point, NULL) == 0);
47+
CHECK(secp256k1_ecdh(tctx, res, &point, NULL, NULL, NULL) == 0);
3148
CHECK(ecount == 3);
32-
CHECK(secp256k1_ecdh(tctx, res, &point, s_one) == 1);
49+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one, NULL, NULL) == 1);
3350
CHECK(ecount == 3);
3451

3552
/* Cleanup */
@@ -46,27 +63,34 @@ void test_ecdh_generator_basepoint(void) {
4663
for (i = 0; i < 100; ++i) {
4764
secp256k1_sha256 sha;
4865
unsigned char s_b32[32];
49-
unsigned char output_ecdh[32];
66+
unsigned char output_ecdh[65];
5067
unsigned char output_ser[32];
51-
unsigned char point_ser[33];
68+
unsigned char point_ser[65];
5269
size_t point_ser_len = sizeof(point_ser);
5370
secp256k1_scalar s;
5471

5572
random_scalar_order(&s);
5673
secp256k1_scalar_get_b32(s_b32, &s);
5774

58-
/* compute using ECDH function */
5975
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" */
6276
CHECK(secp256k1_ec_pubkey_create(ctx, &point[1], s_b32) == 1);
77+
78+
/* compute using ECDH function with custom hash function */
79+
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, ecdh_hash_function_custom, NULL) == 1);
80+
/* compute "explicitly" */
81+
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], SECP256K1_EC_UNCOMPRESSED) == 1);
82+
/* compare */
83+
CHECK(memcmp(output_ecdh, point_ser, 65) == 0);
84+
85+
/* compute using ECDH function with default hash function */
86+
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32, NULL, NULL) == 1);
87+
/* compute "explicitly" */
6388
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));
6589
secp256k1_sha256_initialize(&sha);
6690
secp256k1_sha256_write(&sha, point_ser, point_ser_len);
6791
secp256k1_sha256_finalize(&sha, output_ser);
6892
/* compare */
69-
CHECK(memcmp(output_ecdh, output_ser, sizeof(output_ser)) == 0);
93+
CHECK(memcmp(output_ecdh, output_ser, 32) == 0);
7094
}
7195
}
7296

@@ -89,11 +113,14 @@ void test_bad_scalar(void) {
89113
CHECK(secp256k1_ec_pubkey_create(ctx, &point, s_rand) == 1);
90114

91115
/* 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);
116+
CHECK(secp256k1_ecdh(ctx, output, &point, s_zero, NULL, NULL) == 0);
117+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL, NULL) == 0);
94118
/* ...and a good one */
95119
s_overflow[31] -= 1;
96-
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow) == 1);
120+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, NULL, NULL) == 1);
121+
122+
/* Hash function failure results in ecdh failure */
123+
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, ecdh_hash_function_test_fail, NULL) == 0);
97124
}
98125

99126
void run_ecdh_tests(void) {

0 commit comments

Comments
 (0)