Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

ed25519: Don't panic for invalid signature #12965

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions primitives/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ parking_lot = { version = "0.12.1", optional = true }
secp256k1 = { version = "0.24.0", features = ["recovery", "global-context"], optional = true }
tracing = { version = "0.1.29", default-features = false }
tracing-core = { version = "0.1.28", default-features = false}

# Required for backwards compatibility reason, but only used for verifying when `UseDalekExt` is set.
ed25519-dalek = { version = "1.0.1", default-features = false, optional = true }
# Force the usage of ed25519, this is being used in `ed25519-dalek`.
ed25519 = { version = "1.5.2", optional = true }
Comment on lines +40 to +41
Copy link
Member

@davxy davxy Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Force the usage of ed25519, this is being used in `ed25519-dalek`.
ed25519 = { version = "1.5.2", optional = true }

Why you need this? We are using ed25519 from sp-core crate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have found it there :P


[features]
default = ["std"]
Expand All @@ -59,6 +63,7 @@ std = [
"futures",
"parking_lot",
"ed25519-dalek",
"ed25519",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ed25519",

]

with-tracing = [
Expand Down
26 changes: 22 additions & 4 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,13 +783,13 @@ pub trait Crypto {
{
use ed25519_dalek::Verifier;

let public_key = if let Ok(vk) = ed25519_dalek::PublicKey::from_bytes(&pub_key.0) {
vk
} else {
let Ok(public_key) = ed25519_dalek::PublicKey::from_bytes(&pub_key.0) else {
return false
};

let sig = ed25519_dalek::Signature::from(sig.0);
let Ok(sig) = ed25519_dalek::Signature::from_bytes(&sig.0) else {
return false
};

public_key.verify(msg, &sig).is_ok()
} else {
Expand Down Expand Up @@ -1946,4 +1946,22 @@ mod tests {
assert!(crypto::ed25519_verify(&zero_ed_sig(), &Vec::new(), &zero_ed_pub()));
})
}

#[test]
fn dalek_should_not_panic_on_invalid_signature() {
let mut ext = BasicExternalities::default();
ext.register_extension(UseDalekExt::default());

ext.execute_with(|| {
let mut bytes = [0u8; 64];
// Make it invalid
bytes[63] = 0b1110_0000;

assert!(!crypto::ed25519_verify(
&ed25519::Signature::from_raw(bytes),
&Vec::new(),
&zero_ed_pub()
));
});
}
}