Skip to content

Commit 1a36898

Browse files
rustyrussellsipa
authored andcommitted
Make flags more explicit, add runtime checks.
Signed-off-by: Rusty Russell <[email protected]>
1 parent 1a3e03a commit 1a36898

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

include/secp256k1.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,21 @@ typedef int (*secp256k1_nonce_function)(
148148
# endif
149149

150150
/** Flags to pass to secp256k1_context_create. */
151-
# define SECP256K1_CONTEXT_VERIFY (1 << 0)
152-
# define SECP256K1_CONTEXT_SIGN (1 << 1)
153-
151+
# define SECP256K1_CONTEXT_VERIFY (SECP256K1_CONTEXT_VERIFY_BIT|SECP256K1_CONTEXT_CHECK_BIT)
152+
# define SECP256K1_CONTEXT_SIGN (SECP256K1_CONTEXT_SIGN_BIT|SECP256K1_CONTEXT_CHECK_BIT)
153+
# define SECP256K1_CONTEXT_NONE (SECP256K1_CONTEXT_CHECK_BIT)
154+
155+
# define SECP256K1_CONTEXT_VERIFY_BIT (1 << 2)
156+
# define SECP256K1_CONTEXT_SIGN_BIT (2 << 2)
157+
# define SECP256K1_CONTEXT_CHECK_BIT (1)
158+
154159
/** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
155-
# define SECP256K1_EC_COMPRESSED (1 << 0)
160+
# define SECP256K1_EC_COMPRESSED (SECP256K1_EC_COMPRESSED_BIT|SECP256K1_EC_CHECK_BIT)
161+
# define SECP256K1_EC_UNCOMPRESSED (SECP256K1_EC_CHECK_BIT)
156162

163+
# define SECP256K1_EC_COMPRESSED_BIT (1 << 2)
164+
# define SECP256K1_EC_CHECK_BIT (2)
165+
157166
/** Create a secp256k1 context object.
158167
*
159168
* Returns: a newly created context object.
@@ -261,7 +270,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
261270
* In: pubkey: a pointer to a secp256k1_pubkey containing an initialized
262271
* public key.
263272
* flags: SECP256K1_EC_COMPRESSED if serialization should be in
264-
* compressed format.
273+
* compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
265274
*/
266275
SECP256K1_API int secp256k1_ec_pubkey_serialize(
267276
const secp256k1_context* ctx,

src/eckey_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *p
4040
secp256k1_fe_normalize_var(&elem->x);
4141
secp256k1_fe_normalize_var(&elem->y);
4242
secp256k1_fe_get_b32(&pub[1], &elem->x);
43-
if (flags & SECP256K1_EC_COMPRESSED) {
43+
if (flags & SECP256K1_EC_COMPRESSED_BIT) {
4444
*size = 33;
4545
pub[0] = 0x02 | (secp256k1_fe_is_odd(&elem->y) ? 0x01 : 0x00);
4646
} else {

src/secp256k1.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,20 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
6262
ret->illegal_callback = default_illegal_callback;
6363
ret->error_callback = default_error_callback;
6464

65+
if (EXPECT(!(flags & SECP256K1_CONTEXT_CHECK_BIT), 0)) {
66+
secp256k1_callback_call(&ret->illegal_callback,
67+
"Invalid flags");
68+
free(ret);
69+
return NULL;
70+
}
71+
6572
secp256k1_ecmult_context_init(&ret->ecmult_ctx);
6673
secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
6774

68-
if (flags & SECP256K1_CONTEXT_SIGN) {
75+
if (flags & SECP256K1_CONTEXT_SIGN_BIT) {
6976
secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback);
7077
}
71-
if (flags & SECP256K1_CONTEXT_VERIFY) {
78+
if (flags & SECP256K1_CONTEXT_VERIFY_BIT) {
7279
secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback);
7380
}
7481

@@ -166,6 +173,7 @@ int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *o
166173
ARG_CHECK(output != NULL);
167174
ARG_CHECK(outputlen != NULL);
168175
ARG_CHECK(pubkey != NULL);
176+
ARG_CHECK(flags & SECP256K1_EC_CHECK_BIT);
169177
return (secp256k1_pubkey_load(ctx, &Q, pubkey) &&
170178
secp256k1_eckey_pubkey_serialize(&Q, output, outputlen, flags));
171179
}

src/tests.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void run_context_tests(void) {
139139
unsigned char ctmp[32];
140140
int32_t ecount;
141141
int32_t ecount2;
142-
secp256k1_context *none = secp256k1_context_create(0);
142+
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
143143
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
144144
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
145145
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
@@ -1544,9 +1544,9 @@ void test_point_times_order(const secp256k1_gej *point) {
15441544
secp256k1_ge_set_gej(&res3, &res1);
15451545
CHECK(secp256k1_ge_is_infinity(&res3));
15461546
CHECK(secp256k1_ge_is_valid_var(&res3) == 0);
1547-
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 0) == 0);
1547+
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, SECP256K1_EC_UNCOMPRESSED) == 0);
15481548
psize = 65;
1549-
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 1) == 0);
1549+
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, SECP256K1_EC_COMPRESSED) == 0);
15501550
/* check zero/one edge cases */
15511551
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &zero);
15521552
secp256k1_ge_set_gej(&res3, &res1);
@@ -1964,7 +1964,7 @@ void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, int yvali
19641964
VG_CHECK(&pubkey, sizeof(pubkey));
19651965
outl = 65;
19661966
VG_UNDEF(pubkeyo, 65);
1967-
CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyo, &outl, &pubkey, 0) == 1);
1967+
CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyo, &outl, &pubkey, SECP256K1_EC_UNCOMPRESSED) == 1);
19681968
VG_CHECK(pubkeyo, outl);
19691969
CHECK(outl == 65);
19701970
CHECK(pubkeyo[0] == 4);
@@ -2575,12 +2575,12 @@ void test_ecdsa_end_to_end(void) {
25752575
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, privkey) == 1);
25762576

25772577
/* Verify exporting and importing public key. */
2578-
CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyc, &pubkeyclen, &pubkey, secp256k1_rand_bits(1)) == 1);
2578+
CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyc, &pubkeyclen, &pubkey, secp256k1_rand_bits(1) == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
25792579
memset(&pubkey, 0, sizeof(pubkey));
25802580
CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, pubkeyclen) == 1);
25812581

25822582
/* Verify private key import and export. */
2583-
CHECK(secp256k1_ec_privkey_export_der(ctx, seckey, &seckeylen, privkey, secp256k1_rand_bits(1) == 1) ? SECP256K1_EC_COMPRESSED : 0);
2583+
CHECK(secp256k1_ec_privkey_export_der(ctx, seckey, &seckeylen, privkey, secp256k1_rand_bits(1) == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
25842584
CHECK(secp256k1_ec_privkey_import_der(ctx, privkey2, seckey, seckeylen) == 1);
25852585
CHECK(memcmp(privkey, privkey2, 32) == 0);
25862586

@@ -2698,15 +2698,15 @@ void test_random_pubkeys(void) {
26982698
size_t size = len;
26992699
firstb = in[0];
27002700
/* If the pubkey can be parsed, it should round-trip... */
2701-
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, (len == 33) ? SECP256K1_EC_COMPRESSED : 0));
2701+
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, (len == 33) ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
27022702
CHECK(size == len);
27032703
CHECK(memcmp(&in[1], &out[1], len-1) == 0);
27042704
/* ... except for the type of hybrid inputs. */
27052705
if ((in[0] != 6) && (in[0] != 7)) {
27062706
CHECK(in[0] == out[0]);
27072707
}
27082708
size = 65;
2709-
CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, 0));
2709+
CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, SECP256K1_EC_UNCOMPRESSED));
27102710
CHECK(size == 65);
27112711
CHECK(secp256k1_eckey_pubkey_parse(&elem2, in, size));
27122712
ge_equals_ge(&elem,&elem2);
@@ -2722,7 +2722,7 @@ void test_random_pubkeys(void) {
27222722
}
27232723
if (res) {
27242724
ge_equals_ge(&elem,&elem2);
2725-
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, 0));
2725+
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, SECP256K1_EC_UNCOMPRESSED));
27262726
CHECK(memcmp(&in[1], &out[1], 64) == 0);
27272727
}
27282728
}
@@ -3399,7 +3399,7 @@ void test_ecdsa_edge_cases(void) {
33993399
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41,
34003400
};
34013401
size_t outlen = 300;
3402-
CHECK(!secp256k1_ec_privkey_export_der(ctx, privkey, &outlen, seckey, 0));
3402+
CHECK(!secp256k1_ec_privkey_export_der(ctx, privkey, &outlen, seckey, SECP256K1_EC_UNCOMPRESSED));
34033403
outlen = 300;
34043404
CHECK(!secp256k1_ec_privkey_export_der(ctx, privkey, &outlen, seckey, SECP256K1_EC_COMPRESSED));
34053405
}
@@ -3416,7 +3416,7 @@ EC_KEY *get_openssl_key(const secp256k1_scalar *key) {
34163416
const unsigned char* pbegin = privkey;
34173417
int compr = secp256k1_rand_bits(1);
34183418
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
3419-
CHECK(secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, &privkeylen, key, compr ? SECP256K1_EC_COMPRESSED : 0));
3419+
CHECK(secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, &privkeylen, key, compr ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
34203420
CHECK(d2i_ECPrivateKey(&ec_key, &pbegin, privkeylen));
34213421
CHECK(EC_KEY_check_key(ec_key));
34223422
return ec_key;

0 commit comments

Comments
 (0)