Skip to content
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
16 changes: 0 additions & 16 deletions signature-crate/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,3 @@ 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;
}
31 changes: 16 additions & 15 deletions signature-crate/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Traits for generating digital signatures

#[cfg(feature = "digest")]
use crate::{
digest::{generic_array::GenericArray, Digest},
signature::DigestSignature,
};
use crate::digest::{generic_array::GenericArray, Digest};
use crate::{error::Error, Signature};

/// Sign the provided message bytestring using `Self` (e.g. a cryptographic key
Expand All @@ -30,7 +27,22 @@ where
D: Digest,
S: Signature,
{
/// Sign the computed digest of the given message.
///
/// Panics in the event of a signing error.
fn sign_msg_digest(&self, msg: &[u8]) -> S {
self.try_sign_msg_digest(msg)
.expect("signature operation failed")
}

/// Attempt to sign the computed digest of the given message.
fn try_sign_msg_digest(&self, msg: &[u8]) -> Result<S, Error> {
self.try_sign_digest(D::digest(msg))
}

/// Sign the given prehashed message `Digest`, returning a signature.
///
/// Panics in the event of a signing error.
fn sign_digest(&self, digest: GenericArray<u8, D::OutputSize>) -> S {
self.try_sign_digest(digest)
.expect("signature operation failed")
Expand All @@ -40,14 +52,3 @@ where
/// digital signature on success, or an error if something went wrong.
fn try_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 try_sign(&self, msg: &[u8]) -> Result<S, Error> {
self.try_sign_digest(S::Digest::digest(msg))
}
}
23 changes: 7 additions & 16 deletions signature-crate/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Trait for verifying digital signatures

#[cfg(feature = "digest")]
use crate::{
digest::{generic_array::GenericArray, Digest},
signature::DigestSignature,
};
use crate::digest::{generic_array::GenericArray, Digest};
use crate::{error::Error, Signature};

/// Verify the provided message bytestring using `Self` (e.g. a public key)
Expand All @@ -24,21 +21,15 @@ where
D: Digest,
S: Signature,
{
/// Verify the signature against the given `Digest`
/// Verify the signature against the computed `Digest` output.
fn verify_msg_digest(&self, msg: &[u8], signature: &S) -> Result<(), Error> {
self.verify_digest(D::digest(msg), signature)
}

/// Verify the signature against the given `Digest` output.
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)
}
}