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
40 changes: 40 additions & 0 deletions signature-crate/src/digest/digestable.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random thought: this pattern seems generally useful... perhaps it belongs in the digest crate?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe, but I think we first should see one-two similar use-cases first, before including it into digest.

BTW is it me or Digestable feels reverse in a sense? It's like "we can digest this signature", while it should be "this signature is calculated from message digest". How about DigestSignature? It still feels a bit off, but I don't have better ideas right now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 0ed928c which used signature::digest::Signature 😉


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)
}
}
3 changes: 2 additions & 1 deletion signature-crate/src/digest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! 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::{signer::Signer, verifier::Verifier};
pub use self::{digestable::Digestable, signer::Signer, verifier::Verifier};
6 changes: 4 additions & 2 deletions signature-crate/src/digest/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>;
}
10 changes: 8 additions & 2 deletions signature-crate/src/digest/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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>;
}
9 changes: 6 additions & 3 deletions signature-crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;