Test that the session is actually deleted once round 2 starts
What changed, and why it matters
This commit adds a security test and a small code change for Ledger's Bitcoin app to make sure a secret signing session is wiped from memory once the second round of a MuSig2 multi-signature operation starts. The concern is that if the session were not deleted, an attacker could trick the device into reusing secret randomness (a 'nonce') for a different transaction, which can leak the private key. The patch zeroes out the signing state at the end of round 1 and adds tests proving the session is consumed whether round 2 succeeds or fails.
Review whether explicit_bzero is placed in the correct function and whether any other round-1 exit paths (error/abort) also clear the signing state, because the header comment still says musigsession_commit must not be called on error. Ensure the production firmware passes the new tests and consider whether additional nonce-reuse defenses (e.g., deterministic nonce derivation or persistent anti-replay counters) are warranted.
Security signals we found
explicit_bzero clears signing state after round 1 to prevent secret leakage
New test asserts session is single-use and cannot be replayed for a different transaction
New test asserts a failed round 2 also consumes/deletes the session
Comment explicitly states 'leave no secret behind' and 'signing flow is over'
Relates to MuSig2 nonce-reuse private-key-exposure risk
Evidence from the diff
The change modifies musigsession_commit() in src/musig/musig_sessions.c to call explicit_bzero() on the musig_signing_state after storing the round 1 session, ensuring no secret material (secnonce) remains in volatile memory once the round-1 signing flow ends. The header comment is updated accordingly. A new Python test verifies that a second round-2 call with a different PSBT/aggregate nonce fails with BadStateError after a successful round 2, and also that a failed round 2 (due to a wrong pubnonce) consumes the session so a retry with the correct PSBT also fails. This addresses the risk of nonce reuse in BIP327 MuSig2, where reusing a secnonce with a different message or aggregate nonce leaks the signer’s secret key.
Changed components
src/musig/musig_sessions.csrc/musig/musig_sessions.htests/test_sign_psbt_musig.pyMuSig2 signing session state managementInspect captured patch +79 / −2
### src/musig/musig_sessions.c
@@ -145,4 +145,7 @@ void musigsession_commit(musig_signing_state_t *musig_signing_state) {
sizeof(musig_signing_state->_round1._id))) {
musigsession_store(musig_signing_state->_round1._id, &musig_signing_state->_round1);
}
+
+ // The signing flow is over: leave no secret behind
+ explicit_bzero(musig_signing_state, sizeof(musig_signing_state_t));
}
### src/musig/musig_sessions.h
@@ -100,7 +100,7 @@ __attribute__((warn_unused_result)) const musig_psbt_session_t *musigsession_rou
/**
* If a session produced in round 1 is active in volatile memory, it is stored in the persistent
- * memory.
+ * memory. The signing state is then zeroed out, as the signing flow is over.
* This must be called at the end of a successful signing flow, after all the public nonces have
* been returned to the client. It must _not_ be called if any error occurs, or if the signing
* process is aborted for any reason.
### tests/test_sign_psbt_musig.py
@@ -9,7 +9,7 @@
from ragger.error import ExceptionRAPDU
-from ledger_bitcoin.exception.errors import IncorrectDataError
+from ledger_bitcoin.exception.errors import BadStateError, IncorrectDataError
from ledger_bitcoin.exception.device_exception import DeviceException
from ledger_bitcoin.client_base import Client, MusigPartialSignature, MusigPubNonce
from ledger_bitcoin.key import ExtendedKey, KeyOriginInfo
@@ -438,3 +438,77 @@ def test_sign_psbt_musig2_wrong_pubnonce(navigator: Navigator, firmware: Firmwar
signer_1.generate_partial_signatures(psbt)
assert DeviceException.exc.get(e.value.status) == IncorrectDataError
+
+
+def test_sign_psbt_musig2_session_is_single_use(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
+ # Since the psbt_session_id and the nonces do not depend on the transaction, the only thing
+ # preventing the device from reusing a nonce is that round 2 deletes the session from storage
+ # before producing any partial signature, whether it then succeeds or not. Reusing a secnonce for
+ # a different message, or with a different aggregate nonce, would leak the private key.
+ wallet_policy, wallet_hmac = keypath_wallet_policy(speculos_globals)
+ ledger_instructions = sign_psbt_instruction_approve(
+ firmware, save_screenshot=False, has_spend_from_wallet=True, has_feewarning=True)
+
+ signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
+ navigator=navigator, instructions=ledger_instructions, testname=test_name)
+ signer_2 = HotMusig2Cosigner(wallet_policy, KEYPATH_COSIGNER_2_XPRIV)
+ ledger_pubkey = signer_1.pubkey.pubkey
+
+ def replace_pubnonces(psbt: PSBT, own: bool, rand_: bytes) -> None:
+ # replaces the pubnonce of the device (if own is True) or of the other cosigner with a
+ # different, but still valid, one
+ n_replaced = 0
+ for input in psbt.inputs:
+ for psbt_key in input.musig2_pub_nonces.keys():
+ if (psbt_key[0] == ledger_pubkey) != own:
+ continue
+ _, other_pubnonce = bip0327.nonce_gen_internal(
+ rand_=rand_, sk=None, pk=psbt_key[0], aggpk=None, msg=None, extra_in=None)
+ assert other_pubnonce != input.musig2_pub_nonces[psbt_key]
+ input.musig2_pub_nonces[psbt_key] = other_pubnonce
+ n_replaced += 1
+ assert n_replaced == 1
+
+ def copy_psbt(psbt: PSBT) -> PSBT:
+ result = PSBT()
+ result.deserialize(psbt.serialize())
+ return result
+
+ # 1) A successful round 2 consumes the session: a second round 2 for a different transaction, and
+ # with a different aggregate nonce, must not produce another partial signature.
+ psbt = PSBT()
+ psbt.deserialize(KEYPATH_PSBT_B64)
+ signer_1.generate_public_nonces(psbt)
+ signer_2.generate_public_nonces(psbt)
+
+ replayed_psbt = copy_psbt(psbt)
+ replayed_psbt.tx.vout[0].nValue += 1000
+ replayed_psbt.tx.rehash()
+ replace_pubnonces(replayed_psbt, own=False, rand_=b'\x42' * 32)
+
+ signer_1.generate_partial_signatures(psbt)
+ assert len(psbt.inputs[0].musig2_partial_sigs) == 1
+
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(replayed_psbt)
+ assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert len(replayed_psbt.inputs[0].musig2_partial_sigs) == 0
+
+ # 2) A failed round 2 consumes the session as well: after a round 2 that is rejected because of
+ # a wrong pubnonce, retrying with the correct psbt must fail too.
+ psbt = PSBT()
+ psbt.deserialize(KEYPATH_PSBT_B64)
+ signer_1.generate_public_nonces(psbt)
+ signer_2.generate_public_nonces(psbt)
+
+ wrong_psbt = copy_psbt(psbt)
+ replace_pubnonces(wrong_psbt, own=True, rand_=b'\x43' * 32)
+
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(wrong_psbt)
+ assert DeviceException.exc.get(e.value.status) == IncorrectDataError
+
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(psbt)
+ assert DeviceException.exc.get(e.value.status) == BadStateError
+ assert len(psbt.inputs[0].musig2_partial_sigs) == 0Why this scored 63/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.