@@ -355,6 +355,15 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
355
355
* In: sig: the signature being verified (cannot be NULL)
356
356
* msg32: the 32-byte message hash being verified (cannot be NULL)
357
357
* pubkey: pointer to an initialized public key to verify with (cannot be NULL)
358
+ *
359
+ * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
360
+ * form are accepted.
361
+ *
362
+ * If you need to accept ECDSA signatures from sources that do not obey this
363
+ * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
364
+ * validation, but be aware that doing so results in malleable signatures.
365
+ *
366
+ * For details, see the comments for that function.
358
367
*/
359
368
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify (
360
369
const secp256k1_context * ctx ,
@@ -363,6 +372,54 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
363
372
const secp256k1_pubkey * pubkey
364
373
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 );
365
374
375
+ /** Convert a signature to a normalized lower-S form.
376
+ *
377
+ * Returns: 1 if sigin was not normalized, 0 if it already was.
378
+ * Args: ctx: a secp256k1 context object
379
+ * Out: sigout: a pointer to a signature to fill with the normalized form,
380
+ * or copy if the input was already normalized. (can be NULL if
381
+ * you're only interested in whether the input was already
382
+ * normalized).
383
+ * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
384
+ * can be identical to sigout)
385
+ *
386
+ * With ECDSA a third-party can forge a second distinct signature of the same
387
+ * message, given a single initial signature, but without knowing the key. This
388
+ * is done by negating the S value modulo the order of the curve, 'flipping'
389
+ * the sign of the random point R which is not included in the signature.
390
+ *
391
+ * Forgery of the same message isn't universally problematic, but in systems
392
+ * where message malleability or uniqueness of signatures is important this can
393
+ * cause issues. This forgery can be blocked by all verifiers forcing signers
394
+ * to use a normalized form.
395
+ *
396
+ * The lower-S form reduces the size of signatures slightly on average when
397
+ * variable length encodings (such as DER) are used and is cheap to verify,
398
+ * making it a good choice. Security of always using lower-S is assured because
399
+ * anyone can trivially modify a signature after the fact to enforce this
400
+ * property anyway.
401
+ *
402
+ * The lower S value is always between 0x1 and
403
+ * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
404
+ * inclusive.
405
+ *
406
+ * No other forms of ECDSA malleability are known and none seem likely, but
407
+ * there is no formal proof that ECDSA, even with this additional restriction,
408
+ * is free of other malleability. Commonly used serialization schemes will also
409
+ * accept various non-unique encodings, so care should be taken when this
410
+ * property is required for an application.
411
+ *
412
+ * The secp256k1_ecdsa_sign function will by default create signatures in the
413
+ * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
414
+ * signatures come from a system that cannot enforce this property,
415
+ * secp256k1_ecdsa_signature_normalize must be called before verification.
416
+ */
417
+ SECP256K1_API int secp256k1_ecdsa_signature_normalize (
418
+ const secp256k1_context * ctx ,
419
+ secp256k1_ecdsa_signature * sigout ,
420
+ const secp256k1_ecdsa_signature * sigin
421
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (3 );
422
+
366
423
/** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
367
424
* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
368
425
* extra entropy.
@@ -383,32 +440,8 @@ SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_def
383
440
* noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
384
441
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
385
442
*
386
- * The sig always has an s value in the lower half of the range (From 0x1
387
- * to 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
388
- * inclusive), unlike many other implementations.
389
- *
390
- * With ECDSA a third-party can can forge a second distinct signature
391
- * of the same message given a single initial signature without knowing
392
- * the key by setting s to its additive inverse mod-order, 'flipping' the
393
- * sign of the random point R which is not included in the signature.
394
- * Since the forgery is of the same message this isn't universally
395
- * problematic, but in systems where message malleability or uniqueness
396
- * of signatures is important this can cause issues. This forgery can be
397
- * blocked by all verifiers forcing signers to use a canonical form. The
398
- * lower-S form reduces the size of signatures slightly on average when
399
- * variable length encodings (such as DER) are used and is cheap to
400
- * verify, making it a good choice. Security of always using lower-S is
401
- * assured because anyone can trivially modify a signature after the
402
- * fact to enforce this property. Adjusting it inside the signing
403
- * function avoids the need to re-serialize or have curve specific
404
- * constants outside of the library. By always using a canonical form
405
- * even in applications where it isn't needed it becomes possible to
406
- * impose a requirement later if a need is discovered.
407
- * No other forms of ECDSA malleability are known and none seem likely,
408
- * but there is no formal proof that ECDSA, even with this additional
409
- * restriction, is free of other malleability. Commonly used serialization
410
- * schemes will also accept various non-unique encodings, so care should
411
- * be taken when this property is required for an application.
443
+ * The created signature is always in lower-S form. See
444
+ * secp256k1_ecdsa_signature_normalize for more details.
412
445
*/
413
446
SECP256K1_API int secp256k1_ecdsa_sign (
414
447
const secp256k1_context * ctx ,
0 commit comments