Skip to content

Commit 0a3734e

Browse files
committed
Merge #718: Verify signatures after signing
7b1ad1b Verify signatures after signing (Scott Robinson) Pull request description: ### Description Verify signatures after signing As per [BIP-340, footnote 14][fn]: > Verifying the signature before leaving the signer prevents random or > attacker provoked computation errors. This prevents publishing invalid > signatures which may leak information about the secret key. It is > recommended, but can be omitted if the computation cost is prohibitive. [fn]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#cite_note-14 ### Notes to the reviewers How do we test this? ### Checklists #### All Submissions: * [ ] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: afilini: re-ACK 7b1ad1b Tree-SHA512: 7319db1f8cec2fcfe4ac443ab5728893f9fb6133b33331b35ec6910662c45de8a7cdcf80ac1f3bb435815e914ccf639682a5c07ff0baef42605bf044a34a8232
2 parents a5d1a3d + 7b1ad1b commit 0a3734e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/wallet/signer.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ fn sign_psbt_ecdsa(
475475
hash_ty: EcdsaSighashType,
476476
secp: &SecpCtx,
477477
) {
478-
let sig = secp.sign_ecdsa(
479-
&Message::from_slice(&hash.into_inner()[..]).unwrap(),
480-
secret_key,
481-
);
478+
let msg = &Message::from_slice(&hash.into_inner()[..]).unwrap();
479+
let sig = secp.sign_ecdsa(msg, secret_key);
480+
secp.verify_ecdsa(msg, &sig, &pubkey.inner)
481+
.expect("invalid or corrupted ecdsa signature");
482482

483483
let final_signature = ecdsa::EcdsaSig { sig, hash_ty };
484484
psbt_input.partial_sigs.insert(pubkey, final_signature);
@@ -504,10 +504,10 @@ fn sign_psbt_schnorr(
504504
Some(_) => keypair, // no tweak for script spend
505505
};
506506

507-
let sig = secp.sign_schnorr(
508-
&Message::from_slice(&hash.into_inner()[..]).unwrap(),
509-
&keypair,
510-
);
507+
let msg = &Message::from_slice(&hash.into_inner()[..]).unwrap();
508+
let sig = secp.sign_schnorr(msg, &keypair);
509+
secp.verify_schnorr(&sig, msg, &XOnlyPublicKey::from_keypair(&keypair))
510+
.expect("invalid or corrupted schnorr signature");
511511

512512
let final_signature = schnorr::SchnorrSig { sig, hash_ty };
513513

0 commit comments

Comments
 (0)