Skip to content

Commit 5e64c27

Browse files
committed
Return false instead of panic in PublicKey::verify_signature
1 parent d0ac20b commit 5e64c27

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Changed
10+
11+
- Remove panic from `PublicKey::verify_signature`. Instead return `false` if key does not
12+
parse.
13+
914

1015
## [0.2.0] - 2026-02-09
1116

src/crypto.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ pub struct PublicKey {
1515

1616
impl PublicKey {
1717
/// Verify that `sig` is a valid signature on `data` from this key.
18-
///
19-
/// # Panics
20-
///
21-
/// Panics if the public key bytes are not a valid Ed25519 public key.
2218
pub fn verify_signature(&self, data: impl AsRef<[u8]>, sig: &Signature) -> bool {
23-
let verifier = ed25519_dalek::VerifyingKey::from_bytes(&self.bytes).unwrap();
19+
let Ok(verifier) = ed25519_dalek::VerifyingKey::from_bytes(&self.bytes) else {
20+
return false;
21+
};
2422
let signature = ed25519_dalek::Signature::from(&sig.bytes);
2523
verifier.verify(data.as_ref(), &signature).is_ok()
2624
}
@@ -170,4 +168,17 @@ mod tests {
170168
hex!("ed9cdbc8d80d93ec12581be61413b5fdba1cda57f1cde986ef9e83f0558e7e67").into();
171169
assert!(!pubkey.verify_signature(data, &signature));
172170
}
171+
172+
/// This test checks that with a key that does not pass the
173+
/// ed25519_dalek::VerifyingKey::from_bytes verification,
174+
/// returns false and does not panic or return true.
175+
#[test]
176+
fn publickey_verify_signature_invalid_key() {
177+
let data = b"Time is an illusion. Lunchtime doubly so.";
178+
let signature :Signature= hex!("b115534da664b0d98e307f6562cf2304921e74d82a25b0a8c034fc46560257716f62d09eab1e8dcac09ffb675285d10bff5f0d650899d5236b51291d6f674607").into();
179+
// This public key does not pass the dalek key verification
180+
let pubkey: PublicKey =
181+
hex!("95fbfdc65f4a1c92469440b3fb23cefefe9f26d86057b805243c607ec7eb4f7b").into();
182+
assert!(!pubkey.verify_signature(data, &signature));
183+
}
173184
}

0 commit comments

Comments
 (0)