Merge pull request #13 from RustCrypto/digest-refactor

Factor Digest-related traits out of 'digest' module
This commit is contained in:
Tony Arcieri
2019-04-01 07:27:02 -07:00
committed by GitHub
9 changed files with 85 additions and 101 deletions
-40
View File
@@ -1,40 +0,0 @@
use crate::{
digest::{self, Digest},
error::Error,
signature::Signature,
signer::Signer,
verifier::Verifier,
};
/// Marker trait for `Signature` types computable as `S(H(m))` where:
///
/// - `S`: signature algorithm
/// - `H`: hash (a.k.a. digest) function
/// - `m`: message
///
/// For signature types that implement this trait, a blanket impl of
/// `Signer` will be provided for all types that `impl digest::Signer`.
pub trait Digestable: Signature {
/// Preferred `Digest` algorithm to use when computing this signature type.
type Digest: Digest;
}
impl<S, T> Signer<S> for T
where
S: Digestable + Signature,
T: digest::Signer<S::Digest, S>,
{
fn sign(&self, msg: &[u8]) -> Result<S, Error> {
self.sign_digest(S::Digest::digest(msg))
}
}
impl<S, T> Verifier<S> for T
where
S: Digestable + Signature,
T: digest::Verifier<S::Digest, S>,
{
fn verify(&self, msg: &[u8], signature: &S) -> Result<(), Error> {
self.verify_digest(S::Digest::digest(msg), signature)
}
}
-12
View File
@@ -1,12 +0,0 @@
//! Support for using hash functions that impl the `Digest` trait in order
//! to hash the input message in order to compute a signature.
mod digestable;
mod signer;
mod verifier;
/// Re-export the `Digest` trait from the `digest` crate, as it's the main
/// trait this module depends on.
pub use ::digest::Digest;
pub use self::{digestable::Digestable, signer::Signer, verifier::Verifier};
-19
View File
@@ -1,19 +0,0 @@
//! Support for signing messages which have been prehashed messages using
//! the `Digest` trait.
//!
//! For use signature algorithms that support an Initialize-Update-Finalize
//! (IUF) API, such as ECDSA or Ed25519ph.
use super::Digest;
use crate::{error::Error, signature::Signature};
use digest::generic_array::GenericArray;
/// Sign the given prehashed message `Digest` using `Self`.
pub trait Signer<D, S>
where
D: Digest,
S: Signature,
{
/// Sign the given prehashed message `Digest`, returning a signature.
fn sign_digest(&self, digest: GenericArray<u8, D::OutputSize>) -> Result<S, Error>;
}
-24
View File
@@ -1,24 +0,0 @@
//! Support for verifying messages which have been prehashed messages using
//! the `Digest` trait.
//!
//! For use signature algorithms that support an Initialize-Update-Finalize
//! (IUF) API, such as ECDSA or Ed25519ph.
use super::Digest;
use crate::{error::Error, signature::Signature};
use digest::generic_array::GenericArray;
/// Verify the provided signature for the given prehashed message `Digest`
/// is authentic.
pub trait Verifier<D, S>
where
D: Digest,
S: Signature,
{
/// Verify the signature against the given `Digest`
fn verify_digest(
&self,
digest: GenericArray<u8, D::OutputSize>,
signature: &S,
) -> Result<(), Error>;
}
+6
View File
@@ -30,6 +30,12 @@ impl Error {
}
}
/// Borrow the error's underlying cause (if available)
#[cfg(feature = "std")]
pub fn cause(&self) -> Option<&dyn StdError> {
self.cause.as_ref().map(|c| c.as_ref())
}
/// Extract the underlying cause of this error.
///
/// Panics if the error does not have a cause.
+4 -6
View File
@@ -14,19 +14,17 @@
unused_qualifications
)]
#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
#[cfg(feature = "digest")]
pub mod digest;
pub extern crate digest;
mod error;
mod prelude;
mod signature;
mod signer;
mod verifier;
pub use crate::{error::Error, signature::Signature, signer::Signer, verifier::Verifier};
#[cfg(feature = "digest")]
pub use crate::digest::Digestable;
pub use crate::{error::*, signature::*, signer::*, verifier::*};
+16
View File
@@ -22,3 +22,19 @@ pub trait Signature: AsRef<[u8]> + Debug + Sized {
self.as_slice().into()
}
}
/// Marker trait for `Signature` types computable as `S(H(m))`
///
/// - `S`: signature algorithm
/// - `H`: hash (a.k.a. digest) function
/// - `m`: message
///
/// For signature types that implement this trait, a blanket impl of
/// `Signer` will be provided for all types that `impl DigestSigner`
/// along with a corresponding impl of `Verifier` for all types that
/// `impl DigestVerifier`.
#[cfg(feature = "digest")]
pub trait DigestSignature: Signature {
/// Preferred `Digest` algorithm to use when computing this signature type.
type Digest: digest::Digest;
}
+27
View File
@@ -1,5 +1,10 @@
//! Traits for generating digital signatures
#[cfg(feature = "digest")]
use crate::{
digest::{generic_array::GenericArray, Digest},
signature::DigestSignature,
};
use crate::{error::Error, Signature};
/// Sign the provided message bytestring using `Self` (e.g. a cryptographic key
@@ -8,3 +13,25 @@ pub trait Signer<S: Signature> {
/// Sign the given message and return a digital signature
fn sign(&self, msg: &[u8]) -> Result<S, Error>;
}
/// Sign the given prehashed message `Digest` using `Self`.
#[cfg(feature = "digest")]
pub trait DigestSigner<D, S>
where
D: Digest,
S: Signature,
{
/// Sign the given prehashed message `Digest`, returning a signature.
fn sign_digest(&self, digest: GenericArray<u8, D::OutputSize>) -> Result<S, Error>;
}
#[cfg(feature = "digest")]
impl<S, T> Signer<S> for T
where
S: DigestSignature,
T: DigestSigner<S::Digest, S>,
{
fn sign(&self, msg: &[u8]) -> Result<S, Error> {
self.sign_digest(S::Digest::digest(msg))
}
}
+32
View File
@@ -1,5 +1,10 @@
//! Trait for verifying digital signatures
#[cfg(feature = "digest")]
use crate::{
digest::{generic_array::GenericArray, Digest},
signature::DigestSignature,
};
use crate::{error::Error, Signature};
/// Verify the provided message bytestring using `Self` (e.g. a public key)
@@ -10,3 +15,30 @@ pub trait Verifier<S: Signature> {
/// Returns `Error` if it is inauthentic, or otherwise returns `()`.
fn verify(&self, msg: &[u8], signature: &S) -> Result<(), Error>;
}
/// Verify the provided signature for the given prehashed message `Digest`
/// is authentic.
#[cfg(feature = "digest")]
pub trait DigestVerifier<D, S>
where
D: Digest,
S: Signature,
{
/// Verify the signature against the given `Digest`
fn verify_digest(
&self,
digest: GenericArray<u8, D::OutputSize>,
signature: &S,
) -> Result<(), Error>;
}
#[cfg(feature = "digest")]
impl<S, T> Verifier<S> for T
where
S: DigestSignature,
T: DigestVerifier<S::Digest, S>,
{
fn verify(&self, msg: &[u8], signature: &S) -> Result<(), Error> {
self.verify_digest(S::Digest::digest(msg), signature)
}
}