-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tendermint: crypto::signature::Verifier trait
Define Verifier trait to abstract signature verification given a PublicKey. The ed25519-consensus dependency is made optional and gated by the "rust-crypto" feature. The Verifier implementation is provided for a dummy type crate::crypto::default::signature::Verifier, using ed25519-consensus. The Ed25519 key types in public_key and private_key module are redefined to in-crate newtypes.
- Loading branch information
Showing
18 changed files
with
219 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! The pure Rust implementation of signature verification functions. | ||
use crate::crypto::signature::Error; | ||
use crate::{PublicKey, Signature}; | ||
|
||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] | ||
pub struct Verifier; | ||
|
||
impl crate::crypto::signature::Verifier for Verifier { | ||
fn verify(pubkey: PublicKey, msg: &[u8], signature: &Signature) -> Result<(), Error> { | ||
#[allow(unreachable_patterns)] | ||
match pubkey { | ||
PublicKey::Ed25519(pk) => { | ||
let pubkey = ed25519_consensus::VerificationKey::try_from(pk) | ||
.map_err(|_| Error::MalformedPublicKey)?; | ||
let sig = ed25519_consensus::Signature::try_from(signature.as_bytes()) | ||
.map_err(|_| Error::MalformedSignature)?; | ||
pubkey | ||
.verify(&sig, msg) | ||
.map_err(|_| Error::VerificationFailed) | ||
}, | ||
#[cfg(feature = "secp256k1")] | ||
PublicKey::Secp256k1(pk) => { | ||
let signature = k256::ecdsa::Signature::try_from(signature.as_bytes()) | ||
.map_err(|_| Error::MalformedSignature)?; | ||
pk.verify(msg, &sig).map_err(|_| Error::VerificationFailed) | ||
}, | ||
_ => Err(Error::UnsupportedKeyType), | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod signing_key; | ||
mod verification_key; | ||
|
||
pub use signing_key::SigningKey; | ||
pub use verification_key::VerificationKey; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::VerificationKey; | ||
use crate::Error; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct SigningKey([u8; 32]); | ||
|
||
impl SigningKey { | ||
pub fn as_bytes(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
|
||
#[cfg(feature = "rust-crypto")] | ||
pub fn verification_key(&self) -> VerificationKey { | ||
let privkey = ed25519_consensus::SigningKey::from(self.0); | ||
let pubkey = privkey.verification_key(); | ||
let pubkey_bytes = pubkey.to_bytes(); | ||
VerificationKey::new(pubkey_bytes) | ||
} | ||
} | ||
|
||
impl TryFrom<&'_ [u8]> for SigningKey { | ||
type Error = Error; | ||
|
||
fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> { | ||
if slice.len() != 32 { | ||
return Err(Error::invalid_key("invalid ed25519 key length".into())); | ||
} | ||
let mut bytes = [0u8; 32]; | ||
bytes[..].copy_from_slice(slice); | ||
Ok(Self(bytes)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "rust-crypto")] | ||
impl TryFrom<SigningKey> for ed25519_consensus::SigningKey { | ||
type Error = Error; | ||
|
||
fn try_from(src: SigningKey) -> Result<Self, Error> { | ||
ed25519_consensus::SigningKey::try_from(src.0) | ||
.map_err(|_| Error::invalid_key("malformed Ed25519 private key".into())) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::Error; | ||
|
||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct VerificationKey([u8; 32]); | ||
|
||
impl VerificationKey { | ||
pub(super) fn new(bytes: [u8; 32]) -> Self { | ||
Self(bytes) | ||
} | ||
|
||
pub fn as_bytes(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl TryFrom<&'_ [u8]> for VerificationKey { | ||
type Error = Error; | ||
|
||
fn try_from(slice: &'_ [u8]) -> Result<Self, Self::Error> { | ||
if slice.len() != 32 { | ||
return Err(Error::invalid_key("invalid ed25519 key length".into())); | ||
} | ||
let mut bytes = [0u8; 32]; | ||
bytes[..].copy_from_slice(slice); | ||
Ok(Self(bytes)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "rust-crypto")] | ||
impl TryFrom<VerificationKey> for ed25519_consensus::VerificationKey { | ||
type Error = Error; | ||
|
||
fn try_from(src: VerificationKey) -> Result<Self, Error> { | ||
ed25519_consensus::VerificationKey::try_from(src.0) | ||
.map_err(|_| Error::invalid_key("malformed Ed25519 public key".into())) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use core::fmt::{self, Display}; | ||
|
||
use crate::{PublicKey, Signature}; | ||
|
||
/// Signature error. | ||
/// | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// This variant is deliberately opaque as to avoid side-channel leakage. | ||
VerificationFailed, | ||
/// The key used to verify a signature is not of a type supported by the implementation. | ||
UnsupportedKeyType, | ||
/// The encoding of the public key was malformed. | ||
MalformedPublicKey, | ||
/// The signature data was malformed. | ||
MalformedSignature, | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Error::VerificationFailed => f.write_str("invalid signature"), | ||
Error::UnsupportedKeyType => f.write_str("key type not supported"), | ||
Error::MalformedPublicKey => f.write_str("malformed public key encoding"), | ||
Error::MalformedSignature => f.write_str("malformed signature"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for Error {} | ||
|
||
pub trait Verifier { | ||
fn verify(pubkey: PublicKey, msg: &[u8], signature: &Signature) -> Result<(), Error>; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
extern crate alloc; | ||
|
||
#[cfg(test)] | ||
#[cfg(any(feature = "std", test))] | ||
extern crate std; | ||
|
||
#[macro_use] | ||
|
This file contains 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
Oops, something went wrong.