Skip to content

Commit 3487278

Browse files
contexts: Forbid randomizing secp256k1_context_static
1 parent 1892b7c commit 3487278

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
#### Changed
1111
- Forbade cloning or destroying `secp256k1_context_static`. Create a new context instead of cloning the static context. (If this change breaks your code, your code is probably wrong.)
12+
- Forbade randomizing (copies of) `secp256k1_context_static`. Randomizing a copy of `secp256k1_context_static` did not have any effect and did not provide defense-in-depth protection against side-channel attacks. Create a new context if you want to benefit from randomization.
1213

1314
## [0.2.0] - 2022-12-12
1415

include/secp256k1.h

+6-10
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,10 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
824824

825825
/** Randomizes the context to provide enhanced protection against side-channel leakage.
826826
*
827-
* Returns: 1: randomization successful (or called on copy of secp256k1_context_static)
827+
* Returns: 1: randomization successful
828828
* 0: error
829-
* Args: ctx: pointer to a context object.
830-
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
829+
* Args: ctx: pointer to a context object (not secp256k1_context_static).
830+
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state).
831831
*
832832
* While secp256k1 code is written and tested to be constant-time no matter what
833833
* secret values are, it is possible that a compiler may output code which is not,
@@ -842,21 +842,17 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
842842
* functions that perform computations involving secret keys, e.g., signing and
843843
* public key generation. It is possible to call this function more than once on
844844
* the same context, and doing so before every few computations involving secret
845-
* keys is recommended as a defense-in-depth measure.
845+
* keys is recommended as a defense-in-depth measure. Randomization of the static
846+
* context secp256k1_context_static is not supported.
846847
*
847848
* Currently, the random seed is mainly used for blinding multiplications of a
848849
* secret scalar with the elliptic curve base point. Multiplications of this
849850
* kind are performed by exactly those API functions which are documented to
850-
* require a context that is not the secp256k1_context_static. As a rule of thumb,
851+
* require a context that is not secp256k1_context_static. As a rule of thumb,
851852
* these are all functions which take a secret key (or a keypair) as an input.
852853
* A notable exception to that rule is the ECDH module, which relies on a different
853854
* kind of elliptic curve point multiplication and thus does not benefit from
854855
* enhanced protection against side-channel leakage currently.
855-
*
856-
* It is safe to call this function on a copy of secp256k1_context_static in writable
857-
* memory (e.g., obtained via secp256k1_context_clone). In that case, this
858-
* function is guaranteed to return 1, but the call will have no effect because
859-
* the static context (or a copy thereof) is not meant to be randomized.
860856
*/
861857
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
862858
secp256k1_context* ctx,

src/secp256k1.c

+2
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@ int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey
753753

754754
int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
755755
VERIFY_CHECK(ctx != NULL);
756+
ARG_CHECK(secp256k1_context_is_proper(ctx));
757+
756758
if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
757759
secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
758760
}

src/tests.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ void run_context_tests(int use_prealloc) {
306306
CHECK(ecount2 == 11);
307307
CHECK(secp256k1_ec_pubkey_tweak_mul(my_sttc, &pubkey, ctmp) == 1);
308308
CHECK(ecount == 3);
309-
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 1);
310-
CHECK(ecount == 3);
311-
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 1);
312-
CHECK(ecount == 3);
309+
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 0);
310+
CHECK(ecount == 4);
311+
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 0);
312+
CHECK(ecount == 5);
313313
CHECK(secp256k1_context_randomize(ctx, ctmp) == 1);
314314
CHECK(ecount2 == 11);
315315
CHECK(secp256k1_context_randomize(ctx, NULL) == 1);

0 commit comments

Comments
 (0)