@@ -26,11 +26,17 @@ extern "C" {
26
26
27
27
/** Opaque data structure that holds context information
28
28
*
29
- * The purpose of context structures is to store the randomization data for
30
- * blinding, see secp256k1_context_randomize.
31
- *
32
- * Do not create a new context object for each operation, as construction is
33
- * far slower than all other API calls.
29
+ * The primary purpose of context objects is to store randomization data for
30
+ * enhanced protection against side-channel leakage. This protection is only
31
+ * effective if the context is randomized after its creation. See
32
+ * secp256k1_context_create for creation of contexts and
33
+ * secp256k1_context_randomize for randomization.
34
+ *
35
+ * A secondary purpose of context objects is to store pointers to callback
36
+ * functions that the library will call when certain error states arise. See
37
+ * secp256k1_context_set_error_callback as well as
38
+ * secp256k1_context_set_illegal_callback for details. Future library versions
39
+ * may use context objects for additional purposes.
34
40
*
35
41
* A constructed context can safely be used from multiple threads
36
42
* simultaneously, but API calls that take a non-const pointer to a context
@@ -43,7 +49,7 @@ extern "C" {
43
49
*/
44
50
typedef struct secp256k1_context_struct secp256k1_context ;
45
51
46
- /** Opaque data structure that holds rewriteable "scratch space"
52
+ /** Opaque data structure that holds rewritable "scratch space"
47
53
*
48
54
* The purpose of this structure is to replace dynamic memory allocations,
49
55
* because we target architectures where this may not be available. It is
@@ -265,6 +271,15 @@ SECP256K1_API void secp256k1_selftest(void);
265
271
* offered by the library. All other (deprecated) flags will be treated as equivalent
266
272
* to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
267
273
* historical reasons, future versions of the library may introduce new flags.
274
+ *
275
+ * If the context is intended to be used for API functions that perform computations
276
+ * involving secret keys, e.g., signing and public key generation, then it is highly
277
+ * recommended to call secp256k1_context_randomize on the context before calling
278
+ * those API functions. This will provide enhanced protection against side-channel
279
+ * leakage, see secp256k1_context_randomize for details.
280
+ *
281
+ * Do not create a new context object for each operation, as construction and
282
+ * randomization can take non-negligible time.
268
283
*/
269
284
SECP256K1_API secp256k1_context * secp256k1_context_create (
270
285
unsigned int flags
@@ -344,7 +359,10 @@ SECP256K1_API void secp256k1_context_set_illegal_callback(
344
359
) SECP256K1_ARG_NONNULL (1 );
345
360
346
361
/** Set a callback function to be called when an internal consistency check
347
- * fails. The default is crashing.
362
+ * fails.
363
+ *
364
+ * The default callback writes an error message to stderr and calls abort
365
+ * to abort the program.
348
366
*
349
367
* This can only trigger in case of a hardware failure, miscompilation,
350
368
* memory corruption, serious bug in the library, or other error would can
@@ -800,30 +818,41 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
800
818
const unsigned char * tweak32
801
819
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 );
802
820
803
- /** Updates the context randomization to protect against side-channel leakage.
804
- * Returns: 1: randomization successfully updated or nothing to randomize
821
+ /** Randomizes the context to provide enhanced protection against side-channel leakage.
822
+ *
823
+ * Returns: 1: randomization successful (or called on copy of secp256k1_context_static)
805
824
* 0: error
806
825
* Args: ctx: pointer to a context object.
807
826
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
808
827
*
809
- * While secp256k1 code is written to be constant-time no matter what secret
810
- * values are, it's possible that a future compiler may output code which isn't ,
828
+ * While secp256k1 code is written and tested to be constant-time no matter what
829
+ * secret values are, it is possible that a compiler may output code which is not ,
811
830
* and also that the CPU may not emit the same radio frequencies or draw the same
812
- * amount power for all values.
813
- *
814
- * This function provides a seed which is combined into the blinding value: that
815
- * blinding value is added before each multiplication (and removed afterwards) so
816
- * that it does not affect function results, but shields against attacks which
817
- * rely on any input-dependent behaviour.
818
- *
819
- * This function has currently an effect only on contexts initialized for signing
820
- * because randomization is currently used only for signing. However, this is not
821
- * guaranteed and may change in the future. It is safe to call this function on
822
- * contexts not initialized for signing; then it will have no effect and return 1.
823
- *
824
- * You should call this after secp256k1_context_create or
825
- * secp256k1_context_clone (and secp256k1_context_preallocated_create or
826
- * secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
831
+ * amount of power for all values. Randomization of the context shields against
832
+ * side-channel observations which aim to exploit secret-dependent behaviour in
833
+ * certain computations which involve secret keys.
834
+ *
835
+ * It is highly recommended to call this function on contexts returned from
836
+ * secp256k1_context_create or secp256k1_context_clone (or from the corresponding
837
+ * functions in secp256k1_preallocated.h) before using these contexts to call API
838
+ * functions that perform computations involving secret keys, e.g., signing and
839
+ * public key generation. It is possible to call this function more than once on
840
+ * the same context, and doing so before every few computations involving secret
841
+ * keys is recommended as a defense-in-depth measure.
842
+ *
843
+ * Currently, the random seed is mainly used for blinding multiplications of a
844
+ * secret scalar with the elliptic curve base point. Multiplications of this
845
+ * kind are performed by exactly those API functions which are documented to
846
+ * require a context that is not the secp256k1_context_static. As a rule of thumb,
847
+ * these are all functions which take a secret key (or a keypair) as an input.
848
+ * A notable exception to that rule is the ECDH module, which relies on a different
849
+ * kind of elliptic curve point multiplication and thus does not benefit from
850
+ * enhanced protection against side-channel leakage currently.
851
+ *
852
+ * It is safe call this function on a copy of secp256k1_context_static in writable
853
+ * memory (e.g., obtained via secp256k1_context_clone). In that case, this
854
+ * function is guaranteed to return 1, but the call will have no effect because
855
+ * the static context (or a copy thereof) is not meant to be randomized.
827
856
*/
828
857
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize (
829
858
secp256k1_context * ctx ,
0 commit comments