Skip to content

Commit

Permalink
signature: use &mut impl CryptoRngCore RNG arguments (#1147)
Browse files Browse the repository at this point in the history
`rand_core` v0.6.4 added an auto-impl'd `CryptoRngCore` marker trait for
types which impl `CryptoRng + RngCore` which is slightly more convenient
and less verbose.

This commit changes to using `&mut impl CryptoRngCore` as proposed
in #1087. This hopefully strikes a balance between least surprise and
minimal required syntax, namely &mut references are reusable and don't
require knowledge of the blanket impl of `RngCore` for `&mut R: RngCore`
  • Loading branch information
tarcieri committed Nov 2, 2022
1 parent 796894f commit 45f1bab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion signature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["cryptography", "no-std"]

[dependencies]
digest = { version = "0.10.3", optional = true, default-features = false }
rand_core = { version = "0.6", optional = true, default-features = false }
rand_core = { version = "0.6.4", optional = true, default-features = false }
derive = { package = "signature_derive", version = "=2.0.0-pre.0", optional = true, path = "derive" }

[dev-dependencies]
Expand Down
15 changes: 6 additions & 9 deletions signature/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::error::Error;
use crate::digest::Digest;

#[cfg(feature = "rand-preview")]
use crate::rand_core::{CryptoRng, RngCore};
use crate::rand_core::CryptoRngCore;

/// Sign the provided message bytestring using `Self` (e.g. a cryptographic key
/// or connection to an HSM), returning a digital signature.
Expand Down 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: impl CryptoRng + RngCore, msg: &[u8]) -> S {
fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
self.try_sign_with_rng(rng, msg)
.expect("signature operation failed")
}
Expand All @@ -97,7 +97,7 @@ 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: impl CryptoRng + RngCore, msg: &[u8]) -> Result<S, Error>;
fn try_sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<S, Error>;
}

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

0 comments on commit 45f1bab

Please sign in to comment.