Skip to content

Commit 6198375

Browse files
Make randomization of a non-signing context a noop
Before this commit secp256k1_context_randomize called illegal_callback when called on a context not initialized for signing. This is not documented. Moreover, it is not desirable because non-signing contexts may use randomization in the future. This commit makes secp256k1_context_randomize a noop in this case. This is safe because the context cannot be used for signing anyway. This fixes bitcoin#573 and it fixes rust-bitcoin/rust-secp256k1#82.
1 parent e34ceb3 commit 6198375

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

include/secp256k1.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
615615
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
616616

617617
/** Updates the context randomization to protect against side-channel leakage.
618-
* Returns: 1: randomization successfully updated
618+
* Returns: 1: randomization successfully updated or nothing to randomize
619619
* 0: error
620620
* Args: ctx: pointer to a context object (cannot be NULL)
621621
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
@@ -630,6 +630,11 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
630630
* that it does not affect function results, but shields against attacks which
631631
* rely on any input-dependent behaviour.
632632
*
633+
* This function has currently an effect only on contexts initialized for signing
634+
* because randomization is currently used only for signing. However, this is not
635+
* guaranteed and may change in the future. It is safe to call this function on
636+
* contexts not initialized for signing; then it will have no effect and return 1.
637+
*
633638
* You should call this after secp256k1_context_create or
634639
* secp256k1_context_clone, and may call this repeatedly afterwards.
635640
*/

src/secp256k1.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey
570570

571571
int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
572572
VERIFY_CHECK(ctx != NULL);
573-
CHECK(ctx != secp256k1_context_no_precomp);
574-
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
575-
secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
573+
if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
574+
secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
575+
}
576576
return 1;
577577
}
578578

src/tests.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,12 @@ void run_context_tests(void) {
218218
CHECK(ecount == 3);
219219
CHECK(secp256k1_ec_pubkey_tweak_mul(vrfy, &pubkey, ctmp) == 1);
220220
CHECK(ecount == 3);
221-
CHECK(secp256k1_context_randomize(vrfy, ctmp) == 0);
222-
CHECK(ecount == 4);
221+
CHECK(secp256k1_context_randomize(vrfy, ctmp) == 1);
222+
CHECK(ecount == 3);
223+
CHECK(secp256k1_context_randomize(vrfy, NULL) == 1);
224+
CHECK(ecount == 3);
225+
CHECK(secp256k1_context_randomize(sign, ctmp) == 1);
226+
CHECK(ecount2 == 14);
223227
CHECK(secp256k1_context_randomize(sign, NULL) == 1);
224228
CHECK(ecount2 == 14);
225229
secp256k1_context_set_illegal_callback(vrfy, NULL, NULL);

0 commit comments

Comments
 (0)