Verify the pubnonce in the PSBT during round 2 of MuSig2
What changed, and why it matters
This commit fixes a missing safety check in the Ledger Bitcoin app when using the MuSig2 multi-signature protocol. During round 2 of signing, the app now verifies that the public nonce in the PSBT (the data file used to coordinate signing) matches the nonce the device itself generated earlier. Without this check, the device could produce a partial signature that doesn't match what other signers expect, making the final combined signature invalid and causing other participants to blame this device as the 'disruptive signer.' The fix prevents that by failing cleanly instead of producing a bad signature.
Treat this as a security-hardening fix for a functional bug with denial-of-reputation / multi-party disruption implications. Review whether prior firmware versions could be induced to emit invalid partial signatures through stale or swapped MuSig2 sessions, and consider whether a security advisory is warranted for users relying on MuSig2.
Security signals we found
Missing input validation in cryptographic signing path
MuSig2 nonce mismatch could produce invalid aggregate signature
Device could be falsely identified as disruptive signer per BIP-327
Fix adds explicit memcmp verification before partial signature generation
Regression test added for wrong-pubnonce rejection path
Secret nonce zeroized on failure path
Evidence from the diff
In src/handler/sign_psbt/musig_signing.c, the function sign_sighash_musig_and_yield now compares the recomputed pubnonce against the pubnonce present in the PSBT using memcmp. If they differ, it zeroizes the secret nonce and returns SW_INCORRECT_DATA. This implements the round-2 verification step described in BIP-327 and doc/musig.md. The commit also adds a regression test (test_sign_psbt_musig2_wrong_pubnonce) that injects a mismatched but syntactically valid pubnonce and confirms the device rejects with IncorrectDataError. The change removes reliance on the transaction hash inside psbt_session_id as a proxy for session consistency and instead directly verifies nonce identity.
Changed components
src/handler/sign_psbt/musig_signing.ctests/test_sign_psbt_musig.pyLedger Bitcoin app MuSig2 signing flowInspect captured patch +82 / −12
### src/handler/sign_psbt/musig_signing.c
@@ -472,6 +472,20 @@ bool __attribute__((noinline)) sign_sighash_musig_and_yield(dispatcher_context_t
return false;
}
+ // Check that the pubnonce we just recomputed is indeed the one that the client put in the psbt.
+ // A mismatch means that the psbt was not built with the pubnonces of this session; for example,
+ // because a more recent round 1 for the same wallet policy replaced the session in storage.
+ // Signing anyway would produce a partial signature that does not match the aggregate nonce, and
+ // therefore an invalid aggregate signature; per the "Identifying Disruptive Signers" section of
+ // BIP-327, the other cosigners would then legitimately blame this signer as disruptive.
+ // Fail cleanly instead.
+ if (memcmp(&pubnonce, &my_pubnonce, sizeof(pubnonce)) != 0) {
+ PRINTF("The pubnonce in the PSBT does not match the MuSig2 session\n");
+ explicit_bzero(&secnonce, sizeof(secnonce));
+ SEND_SW(dc, SW_INCORRECT_DATA);
+ return false;
+ }
+
// generate and yield partial signature
cx_ecfp_private_key_t private_key = {0};
### tests/test_sign_psbt_musig.py
@@ -3,9 +3,14 @@
from hashlib import sha256
import hmac
-from typing import Optional
+from typing import Optional, Tuple
+import pytest
+from ragger.error import ExceptionRAPDU
+
+from ledger_bitcoin.exception.errors import IncorrectDataError
+from ledger_bitcoin.exception.device_exception import DeviceException
from ledger_bitcoin.client_base import Client, MusigPartialSignature, MusigPubNonce
from ledger_bitcoin.key import ExtendedKey
from ledger_bitcoin.psbt import PSBT
@@ -107,28 +112,38 @@ def generate_partial_signatures(self, psbt: PSBT) -> None:
raise ValueError("Expected partial signatures, got a pubnonce")
-def test_sign_psbt_musig2_keypath(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
- cosigner_1_xpub = "[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT"
+KEYPATH_COSIGNER_1_XPUB = "[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT"
+KEYPATH_COSIGNER_2_XPRIV = "tprv8gFWbQBTLFhbX3EK3cS7LmenwE3JjXbD9kN9yXfq7LcBm81RSf8vPGPqGPjZSeX41LX9ZN14St3z8YxW48aq5Yhr9pQZVAyuBthfi6quTCf"
+KEYPATH_COSIGNER_2_XPUB = "tpubDCwYjpDhUdPGQWG6wG6hkBJuWFZEtrn7j3xwG3i8XcQabcGC53xWZm1hSXrUPFS5UvZ3QhdPSjXWNfWmFGTioARHuG5J7XguEjgg7p8PxAm"
+
+KEYPATH_PSBT_B64 = "cHNidP8BAIACAAAAAdF2HhQ2XCgTpd3Sel7VkS5FvESbwo1rgeuG4tBt9GICAAAAAAD9////AQAAAAAAAAAARGpCVGhpcyBpbnB1dHMgaGFzIHR3byBwdWJrZXlzIGJ1dCB5b3Ugb25seSBzZWUgb25lLiAjbXBjZ2FuZyByZXZlbmdlAAAAAAABASuf/gQAAAAAACJRIMH9/r7QY6oUg0DEUTLmcY2N6BRmriuQkp49kyg2TNbtIRaQZkYWUCCfi7xZsFr10WFcUPX3nBiNe+dC/ZMiUvaPDA0AW4+8kwAAAAADAAAAAAA="
+
+KEYPATH_SIGHASHES = [
+ bytes.fromhex(
+ "a3aeecb6c236b4a7e72c95fa138250d449b97a75c573f8ab612356279ff64046")
+]
- cosigner_2_xpriv = "tprv8gFWbQBTLFhbX3EK3cS7LmenwE3JjXbD9kN9yXfq7LcBm81RSf8vPGPqGPjZSeX41LX9ZN14St3z8YxW48aq5Yhr9pQZVAyuBthfi6quTCf"
- cosigner_2_xpub = "tpubDCwYjpDhUdPGQWG6wG6hkBJuWFZEtrn7j3xwG3i8XcQabcGC53xWZm1hSXrUPFS5UvZ3QhdPSjXWNfWmFGTioARHuG5J7XguEjgg7p8PxAm"
+def keypath_wallet_policy(speculos_globals: SpeculosGlobals) -> Tuple[WalletPolicy, bytes]:
wallet_policy = WalletPolicy(
name="Musig for my ears",
descriptor_template="tr(musig(@0,@1)/**)",
- keys_info=[cosigner_1_xpub, cosigner_2_xpub]
+ keys_info=[KEYPATH_COSIGNER_1_XPUB, KEYPATH_COSIGNER_2_XPUB]
)
wallet_hmac = hmac.new(
speculos_globals.wallet_registration_key, wallet_policy.id, sha256).digest()
+ return wallet_policy, wallet_hmac
+
+
+def test_sign_psbt_musig2_keypath(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
+ cosigner_2_xpriv = KEYPATH_COSIGNER_2_XPRIV
+
+ wallet_policy, wallet_hmac = keypath_wallet_policy(speculos_globals)
- psbt_b64 = "cHNidP8BAIACAAAAAdF2HhQ2XCgTpd3Sel7VkS5FvESbwo1rgeuG4tBt9GICAAAAAAD9////AQAAAAAAAAAARGpCVGhpcyBpbnB1dHMgaGFzIHR3byBwdWJrZXlzIGJ1dCB5b3Ugb25seSBzZWUgb25lLiAjbXBjZ2FuZyByZXZlbmdlAAAAAAABASuf/gQAAAAAACJRIMH9/r7QY6oUg0DEUTLmcY2N6BRmriuQkp49kyg2TNbtIRaQZkYWUCCfi7xZsFr10WFcUPX3nBiNe+dC/ZMiUvaPDA0AW4+8kwAAAAADAAAAAAA="
psbt = PSBT()
- psbt.deserialize(psbt_b64)
+ psbt.deserialize(KEYPATH_PSBT_B64)
- sighashes = [
- bytes.fromhex(
- "a3aeecb6c236b4a7e72c95fa138250d449b97a75c573f8ab612356279ff64046")
- ]
+ sighashes = KEYPATH_SIGHASHES
signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
navigator=navigator, instructions=sign_psbt_instruction_approve(firmware, save_screenshot=False, has_spend_from_wallet=True, has_feewarning=True), testname=test_name)
@@ -170,3 +185,44 @@ def test_sign_psbt_musig2_scriptpath(navigator: Navigator, firmware: Firmware, c
signer_2 = HotMusig2Cosigner(wallet_policy, cosigner_2_xpriv)
run_musig2_test(wallet_policy, psbt, [signer_1, signer_2], sighashes)
+
+
+def test_sign_psbt_musig2_wrong_pubnonce(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
+ # If the pubnonce in the psbt is not the one that the device would derive for the current
+ # session, the device must refuse to produce a partial signature: signing anyway would produce a
+ # partial signature that does not match the aggregate nonce.
+ wallet_policy, wallet_hmac = keypath_wallet_policy(speculos_globals)
+
+ psbt = PSBT()
+ psbt.deserialize(KEYPATH_PSBT_B64)
+
+ signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
+ navigator=navigator, instructions=sign_psbt_instruction_approve(firmware, save_screenshot=False, has_spend_from_wallet=True, has_feewarning=True), testname=test_name)
+ signer_2 = HotMusig2Cosigner(
+ wallet_policy, KEYPATH_COSIGNER_2_XPRIV)
+
+ # Round 1: both cosigners add their pubnonce
+ signer_1.generate_public_nonces(psbt)
+ signer_2.generate_public_nonces(psbt)
+
+ # Replace the device's pubnonce with a different, but still valid, one. Corrupting the bytes
+ # arbitrarily would not do: the device would fail earlier while aggregating the nonces, which is
+ # a different error path.
+ ledger_pubkey = signer_1.pubkey.pubkey
+ n_replaced = 0
+ for input in psbt.inputs:
+ for psbt_key in input.musig2_pub_nonces.keys():
+ if psbt_key[0] != ledger_pubkey:
+ continue
+ _, other_pubnonce = bip0327.nonce_gen_internal(
+ rand_=b'\x42' * 32, sk=None, pk=ledger_pubkey, 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
+
+ # Round 2 must fail, rather than yielding a partial signature for a nonce it did not commit to
+ with pytest.raises(ExceptionRAPDU) as e:
+ signer_1.generate_partial_signatures(psbt)
+
+ assert DeviceException.exc.get(e.value.status) == IncorrectDataErrorWhy this scored 60/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.