-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft of a trait to abstract signature verification.
- Loading branch information
Showing
12 changed files
with
140 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::crypto::ed25519::VerificationKey; | ||
use crate::{Error, Signature}; | ||
|
||
pub struct Verifier; | ||
|
||
impl crate::crypto::ed25519::Verifier for Verifier { | ||
fn verify(pubkey: VerificationKey, msg: &[u8], signature: &Signature) -> Result<(), Error> { | ||
let pubkey = ed25519_consensus::VerificationKey::try_from(pubkey)?; | ||
// Fixme: need more clearly distinguished error variants for the two failure cases. | ||
let signature = ed25519_consensus::Signature::try_from(signature.as_bytes()) | ||
.map_err(|_| Error::invalid_signature("invalid Ed25519 signature".into()))?; | ||
pubkey | ||
.verify(&signature, msg) | ||
.map_err(|_| Error::signature_invalid("Ed25519 signature verification failed".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,11 @@ | ||
mod signing_key; | ||
mod verification_key; | ||
|
||
pub use signing_key::SigningKey; | ||
pub use verification_key::VerificationKey; | ||
|
||
use crate::{Error, Signature}; | ||
|
||
pub trait Verifier { | ||
fn verify(pubkey: VerificationKey, 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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)) | ||
} | ||
} |
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
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