Merge pull request #574 from LedgerHQ/harden-bip340
What changed, and why it matters
This commit hardens how the Ledger Bitcoin app creates Schnorr (BIP-340) signatures when signing Bitcoin transactions. It now injects its own auxiliary randomness and asks the device's crypto library to use it, then verifies every signature before releasing it. These changes reduce the risk that a broken random number generator or a computation fault could produce a weak or invalid signature that might leak the private key or be rejected by the network.
Review the SDK behavior of CX_RND_PROVIDED with CX_ECSCHNORR_BIP0340 to confirm it correctly XORs aux_rand with the private key and derives the synthetic nonce as documented. Ensure cx_ecschnorr_verify failure paths are tested, including fault-injection and low-entropy scenarios. Consider whether the 64-byte sig buffer is large enough for the 32-byte aux_rand prefix plus the resulting 64-byte signature plus optional sighash byte.
Security signals we found
Schnorr nonce generation changed from device RNG to caller-provided auxiliary randomness with synthetic nonce derivation
Signature self-verification added before returning the signature
Failure to obtain random bytes now aborts signing
Invalid/faulty signatures are now rejected with SW_BAD_STATE instead of being returned
BIP-340 conformance explicitly cited in comments and commit title
Evidence from the diff
In sign_input.c, the BIP-340 signing path is changed from CX_RND_TRNG (device-provided randomness) to CX_RND_PROVIDED (caller-provided auxiliary randomness). The code first fills the first 32 bytes of the signature buffer with cx_get_random_bytes() and passes that as aux_rand to cx_ecschnorr_sign_no_throw(). After signing, it calls cx_ecschnorr_verify() with the tweaked public key and sighash, and aborts with SW_BAD_STATE if verification fails. This aligns with BIP-340’s recommended nonce derivation and the BIP-340 guidance to verify signatures before release.
Changed components
src/handler/sign_psbt/sign_input.csign_sighash_schnorr_and_yield()BIP-340/Schnorr signature generation path in PSBT signingInspect captured patch +23 / −1
### src/handler/sign_psbt/sign_input.c
@@ -173,8 +173,17 @@ bool __attribute__((noinline)) sign_sighash_schnorr_and_yield(dispatcher_context
break;
}
+ // BIP-340 default signing: the first 32 bytes of sig are the auxiliary randomness, and
+ // with CX_RND_PROVIDED the SDK derives the synthetic nonce
+ // k = hash_BIP0340/nonce((d XOR hash_BIP0340/aux(aux_rand)) || P || m) mod n.
+ // This makes sure nonces are unique per message even in the event of RNG failure.
+ if (cx_get_random_bytes(sig, 32) != CX_OK) {
+ error = true;
+ break;
+ }
+
err = cx_ecschnorr_sign_no_throw(&private_key,
- CX_ECSCHNORR_BIP0340 | CX_RND_TRNG,
+ CX_ECSCHNORR_BIP0340 | CX_RND_PROVIDED,
CX_SHA256,
sighash,
32,
@@ -199,6 +208,19 @@ bool __attribute__((noinline)) sign_sighash_schnorr_and_yield(dispatcher_context
return false;
}
+ // As recommended in BIP-340, verify the signature before releasing it, refusing to produce
+ // an invalid signature that is the result of an incorrect/faulty computation
+ if (!cx_ecschnorr_verify(&pubkey_tweaked,
+ CX_ECSCHNORR_BIP0340,
+ CX_SHA256,
+ sighash,
+ 32,
+ sig,
+ sig_len)) {
+ SEND_SW(dc, SW_BAD_STATE);
+ return false;
+ }
+
// only append the sighash type byte if it is non-zero
if (sighash_byte != 0x00) {
// only add the sighash byte if not 0Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.