Merge pull request #12 from RustCrypto/digestable-signatures

Digestable signatures
This commit is contained in:
Tony Arcieri
2019-04-01 07:07:10 -07:00
committed by GitHub
5 changed files with 60 additions and 8 deletions
+40
View File
@@ -0,0 +1,40 @@
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)
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
//! 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;
@@ -8,4 +9,4 @@ mod verifier;
/// trait this module depends on.
pub use ::digest::Digest;
pub use self::{signer::Signer, verifier::Verifier};
pub use self::{digestable::Digestable, signer::Signer, verifier::Verifier};
+4 -2
View File
@@ -4,7 +4,9 @@
//! For use signature algorithms that support an Initialize-Update-Finalize
//! (IUF) API, such as ECDSA or Ed25519ph.
use crate::{digest::Digest, error::Error, Signature};
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>
@@ -13,5 +15,5 @@ where
S: Signature,
{
/// Sign the given prehashed message `Digest`, returning a signature.
fn sign(&self, digest: D) -> Result<S, Error>;
fn sign_digest(&self, digest: GenericArray<u8, D::OutputSize>) -> Result<S, Error>;
}
+8 -2
View File
@@ -4,7 +4,9 @@
//! For use signature algorithms that support an Initialize-Update-Finalize
//! (IUF) API, such as ECDSA or Ed25519ph.
use crate::{digest::Digest, error::Error, Signature};
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.
@@ -14,5 +16,9 @@ where
S: Signature,
{
/// Verify the signature against the given `Digest`
fn verify(&self, digest: D, signature: &S) -> Result<(), Error>;
fn verify_digest(
&self,
digest: GenericArray<u8, D::OutputSize>,
signature: &S,
) -> Result<(), Error>;
}
+6 -3
View File
@@ -19,11 +19,14 @@
extern crate std;
#[cfg(feature = "digest")]
mod digest;
pub mod digest;
mod error;
mod prelude;
mod signature;
pub mod signer;
pub mod verifier;
mod signer;
mod verifier;
pub use crate::{error::Error, signature::Signature, signer::Signer, verifier::Verifier};
#[cfg(feature = "digest")]
pub use crate::digest::Digestable;