Skip to content

Commit

Permalink
SignDigest and VerifyDigest traits
Browse files Browse the repository at this point in the history
Support for signing and verifying prehashed message `Digest`s, for use
with signature algorithms that support Initialize-Update-Finalize usage.
  • Loading branch information
tarcieri committed Mar 24, 2019
1 parent 9fd884c commit c438c2c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
3 changes: 2 additions & 1 deletion signature-crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ keywords = ["crypto", "ecdsa", "ed25519", "signature", "signing"]
categories = ["cryptography", "no-std"]

[dependencies]
digest = { version = "0.8", optional = true, default-features = false }

[features]
default = ["std"]
default = ["digest", "std"]
alloc = []
std = ["alloc"]
8 changes: 6 additions & 2 deletions signature-crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
unused_qualifications
)]

#[cfg(feature = "digest")]
pub extern crate digest;

#[cfg(any(feature = "std", test))]
#[macro_use]
extern crate std;

mod error;
mod prelude;
mod sign;
pub mod sign;
mod signature;
pub mod verify;

pub use crate::{error::Error, sign::Sign, signature::Signature};
pub use crate::{error::Error, sign::Sign, signature::Signature, verify::Verify};
18 changes: 18 additions & 0 deletions signature-crate/src/sign/digest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! 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 crate::{error::Error, Signature};
use digest::Digest;

/// Sign the given prehashed message `Digest` using `Self`.
pub trait SignDigest<D, S>: Send + Sync
where
D: Digest,
S: Signature,
{
/// Sign the given prehashed message `Digest`, returning a signature.
fn sign(&self, digest: D) -> Result<S, Error>;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
//! Trait for generating digital signatures of message bytestrings
//! Traits for generating digital signatures
#[cfg(feature = "digest")]
pub(crate) mod digest;

#[cfg(feature = "digest")]
pub use self::digest::SignDigest;
use crate::{error::Error, Signature};

/// Sign the provided bytestring message using `Self` (e.g. a cryptographic key
/// Sign the provided message bytestring using `Self` (e.g. a cryptographic key
/// or connection to an HSM), returning a digital signature.
pub trait Sign<S: Signature>: Send + Sync {
/// Sign the given message and return a digital signature
Expand Down
19 changes: 19 additions & 0 deletions signature-crate/src/verify/digest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! 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 crate::{error::Error, Signature};
use digest::Digest;

/// Verify the provided signature for the given prehashed message `Digest`
/// is authentic.
pub trait VerifyDigest<D, S>: Send + Sync
where
D: Digest,
S: Signature,
{
/// Verify the signature against the given `Digest`
fn verify(&self, digest: D, signature: &S) -> Result<(), Error>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Trait for verifying digital signatures of message bytestrings
//! Trait for verifying digital signatures
#[cfg(feature = "digest")]
pub(crate) mod digest;

#[cfg(feature = "digest")]
pub use self::digest::VerifyDigest;
use crate::{error::Error, Signature};

/// Verify the provided message bytestring using `Self` (e.g. a public key)
Expand Down

0 comments on commit c438c2c

Please sign in to comment.