Skip to content

Commit 19be9a6

Browse files
committed
fix and verify compressed argument in _eckey_pubkey_serialize calls
In several calls of the internal function `secp256k1_eckey_pubkey_serialize`, the public API flag `SECP256K1_EC_COMPRESSED` is passed, which is meant to be only used for the public function `secp256k1_ec_pubkey_serialize`. It works as intended in all of those cases (it wouldn't for `..._UNCOMPRESSED` though), but it's still kind of a type mismatch that can't be detected by the compiler. To avoid cases like this in the future, a VERIFY_CHECK is added that the `compressed` parameter needs to be either 0 or 1.
1 parent 6152622 commit 19be9a6

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

src/eckey_impl.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "eckey.h"
1111

12+
#include "util.h"
1213
#include "scalar.h"
1314
#include "field.h"
1415
#include "group.h"
@@ -35,6 +36,8 @@ static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char
3536
}
3637

3738
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed) {
39+
VERIFY_CHECK(compressed == 0 || compressed == 1);
40+
3841
if (secp256k1_ge_is_infinity(elem)) {
3942
return 0;
4043
}

src/modules/ecdsa_adaptor/main_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ int secp256k1_ecdsa_adaptor_recover(const secp256k1_context* ctx, unsigned char
339339
/* We declassify non-secret enckey_expected_ge to allow using it as a
340340
* branch point. */
341341
secp256k1_declassify(ctx, &enckey_expected_ge, sizeof(enckey_expected_ge));
342-
if (!secp256k1_eckey_pubkey_serialize(&enckey_expected_ge, enckey_expected33, &size, SECP256K1_EC_COMPRESSED)) {
342+
if (!secp256k1_eckey_pubkey_serialize(&enckey_expected_ge, enckey_expected33, &size, 1)) {
343343
/* Unreachable from tests (and other VERIFY builds) and therefore this
344344
* branch should be ignored in test coverage analysis.
345345
*

src/modules/musig/session_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
368368
if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
369369
return 0;
370370
}
371-
pk_serialize_success = secp256k1_eckey_pubkey_serialize(&pk, pk_ser, &pk_ser_len, SECP256K1_EC_COMPRESSED);
371+
pk_serialize_success = secp256k1_eckey_pubkey_serialize(&pk, pk_ser, &pk_ser_len, 1);
372372

373373
#ifdef VERIFY
374374
/* A pubkey cannot be the point at infinity */

src/modules/whitelist/whitelist_impl.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int secp256k1_whitelist_hash_pubkey(secp256k1_scalar* output, secp256k1_g
1818
secp256k1_ge_set_gej(&ge, pubkey);
1919

2020
secp256k1_sha256_initialize(&sha);
21-
if (!secp256k1_eckey_pubkey_serialize(&ge, c, &size, SECP256K1_EC_COMPRESSED)) {
21+
if (!secp256k1_eckey_pubkey_serialize(&ge, c, &size, 1)) {
2222
return 0;
2323
}
2424
secp256k1_sha256_write(&sha, c, size);
@@ -94,7 +94,7 @@ static int secp256k1_whitelist_compute_keys_and_message(const secp256k1_context*
9494
secp256k1_pubkey_load(ctx, &subkey_ge, sub_pubkey);
9595

9696
/* commit to sub-key */
97-
if (!secp256k1_eckey_pubkey_serialize(&subkey_ge, c, &size, SECP256K1_EC_COMPRESSED)) {
97+
if (!secp256k1_eckey_pubkey_serialize(&subkey_ge, c, &size, 1)) {
9898
return 0;
9999
}
100100
secp256k1_sha256_write(&sha, c, size);
@@ -105,12 +105,12 @@ static int secp256k1_whitelist_compute_keys_and_message(const secp256k1_context*
105105

106106
/* commit to fixed keys */
107107
secp256k1_pubkey_load(ctx, &offline_ge, &offline_pubkeys[i]);
108-
if (!secp256k1_eckey_pubkey_serialize(&offline_ge, c, &size, SECP256K1_EC_COMPRESSED)) {
108+
if (!secp256k1_eckey_pubkey_serialize(&offline_ge, c, &size, 1)) {
109109
return 0;
110110
}
111111
secp256k1_sha256_write(&sha, c, size);
112112
secp256k1_pubkey_load(ctx, &online_ge, &online_pubkeys[i]);
113-
if (!secp256k1_eckey_pubkey_serialize(&online_ge, c, &size, SECP256K1_EC_COMPRESSED)) {
113+
if (!secp256k1_eckey_pubkey_serialize(&online_ge, c, &size, 1)) {
114114
return 0;
115115
}
116116
secp256k1_sha256_write(&sha, c, size);

src/secp256k1.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *o
300300
ARG_CHECK(pubkey != NULL);
301301
ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
302302
if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
303-
ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
303+
ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, !!(flags & SECP256K1_FLAGS_BIT_COMPRESSION));
304304
if (ret) {
305305
*outputlen = len;
306306
}

0 commit comments

Comments
 (0)