Skip to content

Commit

Permalink
signature: use R: CryptoRngCore + ?Sized for RNG params
Browse files Browse the repository at this point in the history
As discussed in #1148, adopts a generic paramater `R` for RNGs, and also
adds a `?Sized` bound which permits the use of trait objects.
  • Loading branch information
tarcieri committed Dec 10, 2022
1 parent 28ec035 commit 5e0e3e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions signature/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub trait RandomizedPrehashSigner<S> {
///
/// Allowed lengths are algorithm-dependent and up to a particular
/// implementation to decide.
fn sign_prehash_with_rng(
fn sign_prehash_with_rng<R: CryptoRngCore + ?Sized>(
&self,
rng: &mut impl CryptoRngCore,
rng: &mut R,
prehash: &[u8],
) -> Result<S, Error>;
}
Expand Down
17 changes: 12 additions & 5 deletions signature/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub trait DigestSigner<D: Digest, S> {
#[cfg_attr(docsrs, doc(cfg(feature = "rand-preview")))]
pub trait RandomizedSigner<S> {
/// Sign the given message and return a digital signature
fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
fn sign_with_rng<R: CryptoRngCore + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
Expand All @@ -97,7 +97,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(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<S, Error>;
fn try_sign_with_rng<R: CryptoRngCore + ?Sized>(
&self,
rng: &mut R,
msg: &[u8],
) -> Result<S, Error>;
}

/// Combination of [`DigestSigner`] and [`RandomizedSigner`] with support for
Expand All @@ -109,13 +113,16 @@ 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(&self, rng: &mut impl CryptoRngCore, digest: D) -> S {
fn sign_digest_with_rng<R: CryptoRngCore + ?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(&self, rng: &mut impl CryptoRngCore, digest: D)
-> Result<S, Error>;
fn try_sign_digest_with_rng<R: CryptoRngCore + ?Sized>(
&self,
rng: &mut R,
digest: D,
) -> Result<S, Error>;
}

0 comments on commit 5e0e3e3

Please sign in to comment.