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
1 change: 1 addition & 0 deletions signature/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Re-export `rand_core`. Emit compilation error if unstable functionality
is enabled by bypassing the preview features. ([#683])
- Defined the `SignerMut` trait

[#683]: https://github.com/RustCrypto/traits/pull/683

Expand Down
27 changes: 27 additions & 0 deletions signature/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ pub trait Signer<S: Signature> {
fn try_sign(&self, msg: &[u8]) -> Result<S, Error>;
}

/// Sign the provided message bytestring using `&mut Self` (e.g., an evolving
/// cryptographic key), returning a digital signature.
pub trait SignerMut<S: Signature> {
/// Sign the given message, update the state, and return a digital signature
fn sign(&mut self, msg: &[u8]) -> S {
self.try_sign(msg).expect("signature operation failed")
}

/// Attempt to sign the given message, updating the state, and returning a
/// digital signature on success, or an error if something went wrong.
///
/// Signing can fail, e.g., if the number of time periods allowed by the
/// current key is exceeded.
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>;
}

// Blanket impl of SignerMut for all Signer types
impl<T, S> SignerMut<S> for T
where
T: Signer<S>,
S: Signature,
{
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error> {
T::try_sign(&self, msg)
}
}

/// Sign the given prehashed message [`Digest`] using `Self`.
///
/// ## Notes
Expand Down