Skip to content

Commit

Permalink
signature: relax Sized requirements on rng (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo authored Feb 22, 2025
1 parent ad7adbb commit f17b962
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions signature/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait DigestSigner<D: Digest, S> {
#[cfg(feature = "rand_core")]
pub trait RandomizedSigner<S> {
/// Sign the given message and return a digital signature
fn sign_with_rng<R: CryptoRng>(&self, rng: &mut R, msg: &[u8]) -> S {
fn sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
Expand All @@ -96,7 +96,11 @@ pub trait RandomizedSigner<S> {
///
/// The main intended use case for signing errors is when communicating
/// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
fn try_sign_with_rng<R: TryCryptoRng>(&self, rng: &mut R, msg: &[u8]) -> Result<S, Error>;
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
msg: &[u8],
) -> Result<S, Error>;
}

/// Combination of [`DigestSigner`] and [`RandomizedSigner`] with support for
Expand All @@ -106,15 +110,18 @@ pub trait RandomizedDigestSigner<D: Digest, S> {
/// Sign the given prehashed message `Digest`, returning a signature.
///
/// Panics in the event of a signing error.
fn sign_digest_with_rng<R: CryptoRng>(&self, rng: &mut R, digest: D) -> S {
fn sign_digest_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, digest: D) -> S {
self.try_sign_digest_with_rng(rng, digest)
.expect("signature operation failed")
}

/// Attempt to sign the given prehashed message `Digest`, returning a
/// digital signature on success, or an error if something went wrong.
fn try_sign_digest_with_rng<R: TryCryptoRng>(&self, rng: &mut R, digest: D)
-> Result<S, Error>;
fn try_sign_digest_with_rng<R: TryCryptoRng + ?Sized>(
&self,
rng: &mut R,
digest: D,
) -> Result<S, Error>;
}

/// Sign the provided message bytestring using `&mut Self` (e.g. an evolving
Expand All @@ -123,7 +130,7 @@ pub trait RandomizedDigestSigner<D: Digest, S> {
#[cfg(feature = "rand_core")]
pub trait RandomizedSignerMut<S> {
/// Sign the given message, update the state, and return a digital signature.
fn sign_with_rng<R: CryptoRng>(&mut self, rng: &mut R, msg: &[u8]) -> S {
fn sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
Expand All @@ -133,13 +140,21 @@ pub trait RandomizedSignerMut<S> {
///
/// Signing can fail, e.g., if the number of time periods allowed by the
/// current key is exceeded.
fn try_sign_with_rng<R: TryCryptoRng>(&mut self, rng: &mut R, msg: &[u8]) -> Result<S, Error>;
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
&mut self,
rng: &mut R,
msg: &[u8],
) -> Result<S, Error>;
}

/// Blanket impl of [`RandomizedSignerMut`] for all [`RandomizedSigner`] types.
#[cfg(feature = "rand_core")]
impl<S, T: RandomizedSigner<S>> RandomizedSignerMut<S> for T {
fn try_sign_with_rng<R: TryCryptoRng>(&mut self, rng: &mut R, msg: &[u8]) -> Result<S, Error> {
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
&mut self,
rng: &mut R,
msg: &[u8],
) -> Result<S, Error> {
T::try_sign_with_rng(self, rng, msg)
}
}
Expand Down

0 comments on commit f17b962

Please sign in to comment.