From a3b8cf0d185b37a40348204886bc81de71e07e2c Mon Sep 17 00:00:00 2001 From: Andre-Philippe Paquet Date: Wed, 15 May 2019 11:20:49 -0400 Subject: [PATCH] Fix ED25519 signature validation --- core/src/identity/ed25519.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/core/src/identity/ed25519.rs b/core/src/identity/ed25519.rs index 89ed0b26706..1c6662c2aec 100644 --- a/core/src/identity/ed25519.rs +++ b/core/src/identity/ed25519.rs @@ -100,7 +100,7 @@ pub struct PublicKey(ed25519::PublicKey); impl PublicKey { /// Verify the Ed25519 signature on a message using the public key. pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { - ed25519::Signature::from_bytes(sig).map(|s| self.0.verify(msg, &s)).is_ok() + ed25519::Signature::from_bytes(sig).and_then(|s| self.0.verify(msg, &s)).is_ok() } /// Encode the public key into a byte array in compressed form, i.e. @@ -189,5 +189,21 @@ mod tests { } QuickCheck::new().tests(10).quickcheck(prop as fn() -> _); } -} + #[test] + fn ed25519_signature() { + let kp = Keypair::generate(); + let pk = kp.public(); + + let msg = "hello world".as_bytes(); + let sig = kp.sign(msg); + assert!(pk.verify(msg, &sig)); + + let mut invalid_sig = sig.clone(); + invalid_sig[3..6].copy_from_slice(&[10, 23, 42]); + assert!(!pk.verify(msg, &invalid_sig)); + + let invalid_msg = "h3ll0 w0rld".as_bytes(); + assert!(!pk.verify(invalid_msg, &sig)); + } +}