Skip to content

Commit cbf137e

Browse files
authored
Fix ecdhUnsafe for bindings (#139)
1 parent e9d38b2 commit cbf137e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/ecdh.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ NAN_METHOD(ecdh) {
3636
}
3737

3838
int ecdh_hash_function_unsafe(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
39-
memcpy(output, x, 32);
40-
memcpy(output + 32, y, 32);
39+
output[0] = 0x04;
40+
memcpy(output + 1, x, 32);
41+
memcpy(output + 33, y, 32);
4142
return 1;
4243
}
4344

@@ -63,12 +64,20 @@ NAN_METHOD(ecdhUnsafe) {
6364
unsigned int flags = SECP256K1_EC_COMPRESSED;
6465
UPDATE_COMPRESSED_VALUE(flags, info[2], SECP256K1_EC_COMPRESSED, SECP256K1_EC_UNCOMPRESSED);
6566

66-
if (secp256k1_ecdh(secp256k1ctx, &public_key.data[0], &public_key, private_key, ecdh_hash_function_unsafe, NULL) == 0) {
67+
unsigned char output[65];
68+
size_t output_length = flags == SECP256K1_EC_COMPRESSED ? 33 : 65;
69+
70+
if (secp256k1_ecdh(secp256k1ctx, &output[0], &public_key, private_key, ecdh_hash_function_unsafe, NULL) == 0) {
6771
return Nan::ThrowError(ECDH_FAIL);
6872
}
6973

70-
unsigned char output[65];
71-
size_t output_length = flags == SECP256K1_EC_COMPRESSED ? 33 : 65;
72-
secp256k1_ec_pubkey_serialize(secp256k1ctx, &output[0], &output_length, &public_key, flags);
74+
if (output_length == 33) {
75+
if (secp256k1_ec_pubkey_parse(secp256k1ctx, &public_key, output, 65) == 0) {
76+
return Nan::ThrowError(EC_PUBLIC_KEY_PARSE_FAIL);
77+
}
78+
79+
secp256k1_ec_pubkey_serialize(secp256k1ctx, &output[0], &output_length, &public_key, flags);
80+
}
81+
7382
info.GetReturnValue().Set(COPY_BUFFER(&output[0], output_length));
7483
}

0 commit comments

Comments
 (0)