Fix buffer over-read#4709
Merged
nikhil-gupta-tw merged 4 commits intofix/public-key-verify-messagefrom Mar 25, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens ECDSA signing and verification against short digests to prevent potential buffer over-reads, and adds regression tests to ensure short digests are rejected.
Changes:
- Add a minimum digest-length guard (32 bytes) for ECDSA verification in
PublicKey::verify/verifyAsDER. - Route DER signing through a digest-length-checked ECDSA signing helper.
- Add tests ensuring signing/verifying with short digests fails.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/PublicKey.cpp |
Adds a 32-byte minimum digest check before calling ecdsa_verify_digest for ECDSA key types. |
src/PrivateKey.cpp |
Switches DER signing to a checked ECDSA signing function that rejects short digests. |
tests/common/PublicKeyTests.cpp |
Adds tests asserting ECDSA verify paths reject short digests. |
tests/common/PrivateKeyTests.cpp |
Adds a test asserting DER signing rejects short digests. |
Comments suppressed due to low confidence (1)
src/PublicKey.cpp:224
- The PR description says
verifyAsDERrejects short digests for both SECP256k1 and NIST256p1, but the implementation ofPublicKey::verifyAsDERonly handlesTWPublicKeyTypeSECP256k1/SECP256k1Extendedand returnsfalsefor all other key types. Either update the PR description to match the actual supported behavior, or extendverifyAsDERto support NIST256p1 if that’s intended.
bool PublicKey::verifyAsDER(const Data& signature, const Data& message) const {
switch (type) {
case TWPublicKeyTypeSECP256k1:
case TWPublicKeyTypeSECP256k1Extended: {
if (message.size() < kEcdsaMinDigestSize) { return false; }
Data sig(64);
int ret = ecdsa_sig_from_der(signature.data(), signature.size(), sig.data());
if (ret) {
return false;
}
return ecdsa_verify_digest(&secp256k1, bytes.data(), sig.data(), message.data()) == 0;
}
default:
return false;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Binary size comparison➡️ aarch64-apple-ios: 14.34 MB ➡️ aarch64-apple-ios-sim: 14.34 MB ➡️ aarch64-linux-android: 18.77 MB ➡️ armv7-linux-androideabi: 16.20 MB ➡️ wasm32-unknown-emscripten: 13.68 MB |
…llet/wallet-core into fix/buffer-over-read
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request strengthens the validation of ECDSA signature operations by enforcing a minimum digest size and updating the relevant signing and verification logic. It also adds new tests to ensure that short digests are properly rejected. These changes help prevent potential misuse of cryptographic functions with invalid input lengths.
Validation improvements for ECDSA operations:
kEcdsaMinDigestSize(32 bytes) insrc/PublicKey.cppto define the minimum allowed digest size for ECDSA operations.PublicKey::verifyandPublicKey::verifyAsDERto explicitly reject digests shorter than the minimum size for SECP256k1 and NIST256p1 key types, returningfalseif the input is too short. [1] [2]PrivateKey::signAsDERto useecdsa_sign_digest_checked, which enforces digest size validation during signing.Test coverage enhancements:
SignAsDERRejectsShortDigesttest to verify that signing with a short digest returns an empty result.VerifyRejectsShortDigestandVerifyAsDERRejectsShortDigesttests to confirm that verification functions reject signatures with short digests.