Skip to content

Commit 1578fe3

Browse files
authored
Bump signature to v2.0.0-pre.2; use impl CryptoRngCore (#579)
As discussed in RustCrypto/traits#1148, this uses `&mut impl CryptoRngCore` as the API for passing CSRNGs. This removes the need for a generic parameter in the type signature while also keeping syntax to a minimum. The traits in `signature` v2.0.0-pre.2 switched to these APIs. See RustCrypto/traits#1147.
1 parent aa59217 commit 1578fe3

17 files changed

+42
-53
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsa/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ digest = "0.10"
1919
num-bigint = { package = "num-bigint-dig", version = "0.8", default-features = false, features = ["prime", "rand", "zeroize"] }
2020
num-traits = { version = "0.2", default-features = false }
2121
pkcs8 = { version = "0.9", default-features = false, features = ["alloc"] }
22-
rand = { version = "0.8", default-features = false }
2322
rfc6979 = { version = "0.3", path = "../rfc6979" }
2423
sha2 = { version = "0.10", default-features = false }
25-
signature = { version = "=2.0.0-pre.0", default-features = false, features = ["alloc", "digest-preview", "rand-preview"] }
24+
signature = { version = "=2.0.0-pre.2", default-features = false, features = ["alloc", "digest-preview", "rand-preview"] }
2625
zeroize = { version = "1.5", default-features = false }
2726

2827
[dev-dependencies]

dsa/examples/sign.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ fn main() {
1111
let signing_key = SigningKey::generate(&mut rng, components);
1212
let verifying_key = signing_key.verifying_key();
1313

14-
let signature = signing_key
15-
.sign_digest_with_rng(rand::thread_rng(), Sha1::new().chain_update(b"hello world"));
14+
let signature = signing_key.sign_digest_with_rng(
15+
&mut rand::thread_rng(),
16+
Sha1::new().chain_update(b"hello world"),
17+
);
1618

1719
let signing_key_bytes = signing_key.to_pkcs8_pem(LineEnding::LF).unwrap();
1820
let verifying_key_bytes = verifying_key.to_public_key_pem(LineEnding::LF).unwrap();

dsa/src/components.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{size::KeySize, two};
66
use num_bigint::BigUint;
77
use num_traits::Zero;
88
use pkcs8::der::{self, asn1::UIntRef, DecodeValue, Encode, Header, Reader, Sequence, Tag};
9-
use rand::{CryptoRng, RngCore};
9+
use signature::rand_core::CryptoRngCore;
1010

1111
/// The common components of an DSA keypair
1212
///
@@ -35,10 +35,7 @@ impl Components {
3535
}
3636

3737
/// Generate a new pair of common components
38-
pub fn generate<R>(rng: &mut R, key_size: KeySize) -> Self
39-
where
40-
R: CryptoRng + RngCore + ?Sized,
41-
{
38+
pub fn generate(rng: &mut impl CryptoRngCore, key_size: KeySize) -> Self {
4239
let (p, q, g) = crate::generate::common_components(rng, key_size);
4340
Self::from_components(p, q, g).expect("[Bug] Newly generated components considered invalid")
4441
}

dsa/src/generate/mod.rs renamed to dsa/src/generate.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::two;
22
use num_bigint::{BigUint, RandPrime};
33
use num_traits::Pow;
4-
use rand::{CryptoRng, RngCore};
4+
use signature::rand_core::CryptoRngCore;
55

66
mod components;
77
mod keypair;
@@ -24,9 +24,6 @@ fn calculate_bounds(size: u32) -> (BigUint, BigUint) {
2424
///
2525
/// This wrapper function mainly exists to enforce the [`CryptoRng`](rand::CryptoRng) requirement (I might otherwise forget it)
2626
#[inline]
27-
fn generate_prime<R>(bit_length: usize, rng: &mut R) -> BigUint
28-
where
29-
R: CryptoRng + RngCore + ?Sized,
30-
{
27+
fn generate_prime(bit_length: usize, rng: &mut impl CryptoRngCore) -> BigUint {
3128
rng.gen_prime(bit_length)
3229
}

dsa/src/generate/components.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use num_bigint::{prime::probably_prime, BigUint, RandBigInt};
1111
use num_traits::One;
12-
use rand::{CryptoRng, RngCore};
12+
use signature::rand_core::CryptoRngCore;
1313

1414
/// Numbers of miller-rabin rounds performed to determine primality
1515
const MR_ROUNDS: usize = 64;
@@ -19,10 +19,10 @@ const MR_ROUNDS: usize = 64;
1919
/// # Returns
2020
///
2121
/// Tuple of three `BigUint`s. Ordered like this `(p, q, g)`
22-
pub fn common<R>(rng: &mut R, KeySize { l, n }: KeySize) -> (BigUint, BigUint, BigUint)
23-
where
24-
R: CryptoRng + RngCore + ?Sized,
25-
{
22+
pub fn common(
23+
rng: &mut impl CryptoRngCore,
24+
KeySize { l, n }: KeySize,
25+
) -> (BigUint, BigUint, BigUint) {
2626
// Calculate the lower and upper bounds of p and q
2727
let (p_min, p_max) = calculate_bounds(l);
2828
let (q_min, q_max) = calculate_bounds(n);

dsa/src/generate/keypair.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
use crate::{generate::components, Components, SigningKey, VerifyingKey};
66
use num_bigint::{BigUint, RandBigInt};
77
use num_traits::One;
8-
use rand::{CryptoRng, RngCore};
8+
use signature::rand_core::CryptoRngCore;
99

1010
/// Generate a new keypair
1111
#[inline]
12-
pub fn keypair<R>(rng: &mut R, components: Components) -> SigningKey
13-
where
14-
R: CryptoRng + RngCore + ?Sized,
15-
{
12+
pub fn keypair(rng: &mut impl CryptoRngCore, components: Components) -> SigningKey {
1613
let x = rng.gen_biguint_range(&BigUint::one(), components.q());
1714
let y = components::public(&components, &x);
1815

dsa/src/generate/secret_number.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use core::cmp::min;
88
use digest::{core_api::BlockSizeUser, Digest, FixedOutputReset};
99
use num_bigint::{BigUint, ModInverse, RandBigInt};
1010
use num_traits::{One, Zero};
11-
use rand::{CryptoRng, RngCore};
1211
use rfc6979::HmacDrbg;
12+
use signature::rand_core::CryptoRngCore;
1313
use zeroize::Zeroize;
1414

1515
/// Reduce the hash into an RFC-6979 appropriate form
@@ -69,10 +69,10 @@ where
6969
///
7070
/// Secret number k and its modular multiplicative inverse with q
7171
#[inline]
72-
pub fn secret_number<R>(rng: &mut R, components: &Components) -> Option<(BigUint, BigUint)>
73-
where
74-
R: CryptoRng + RngCore + ?Sized,
75-
{
72+
pub fn secret_number(
73+
rng: &mut impl CryptoRngCore,
74+
components: &Components,
75+
) -> Option<(BigUint, BigUint)> {
7676
let q = components.q();
7777
let n = q.bits();
7878

dsa/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl SignatureEncoding for Signature {
157157
type Repr = Box<[u8]>;
158158

159159
fn to_bytes(&self) -> Box<[u8]> {
160-
self.to_boxed_slice()
160+
SignatureEncoding::to_vec(self).into_boxed_slice()
161161
}
162162

163163
fn to_vec(&self) -> Vec<u8> {

dsa/src/signing_key.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use pkcs8::{
1414
der::{asn1::UIntRef, AnyRef, Decode, Encode},
1515
AlgorithmIdentifier, DecodePrivateKey, EncodePrivateKey, PrivateKeyInfo, SecretDocument,
1616
};
17-
use rand::{CryptoRng, RngCore};
1817
use signature::{
1918
hazmat::{PrehashSigner, RandomizedPrehashSigner},
19+
rand_core::CryptoRngCore,
2020
DigestSigner, RandomizedDigestSigner, Signer,
2121
};
2222
use zeroize::{Zeroize, Zeroizing};
@@ -50,10 +50,7 @@ impl SigningKey {
5050

5151
/// Generate a new DSA keypair
5252
#[inline]
53-
pub fn generate<R>(rng: &mut R, components: Components) -> SigningKey
54-
where
55-
R: CryptoRng + RngCore + ?Sized,
56-
{
53+
pub fn generate(rng: &mut impl CryptoRngCore, components: Components) -> SigningKey {
5754
crate::generate::keypair(rng, components)
5855
}
5956

@@ -117,7 +114,7 @@ impl PrehashSigner<Signature> for SigningKey {
117114
impl RandomizedPrehashSigner<Signature> for SigningKey {
118115
fn sign_prehash_with_rng(
119116
&self,
120-
mut rng: impl CryptoRng + RngCore,
117+
mut rng: &mut impl CryptoRngCore,
121118
prehash: &[u8],
122119
) -> Result<Signature, signature::Error> {
123120
let components = self.verifying_key.components();
@@ -147,7 +144,7 @@ where
147144
{
148145
fn try_sign_digest_with_rng(
149146
&self,
150-
mut rng: impl CryptoRng + RngCore,
147+
mut rng: &mut impl CryptoRngCore,
151148
digest: D,
152149
) -> Result<Signature, signature::Error> {
153150
let ks = crate::generate::secret_number(&mut rng, self.verifying_key().components())

dsa/tests/signature.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn decode_encode_signature() {
7070
fn sign_message() {
7171
let signing_key = generate_deterministic_keypair();
7272
let generated_signature =
73-
signing_key.sign_digest_with_rng(seeded_csprng(), Sha256::new().chain_update(MESSAGE));
73+
signing_key.sign_digest_with_rng(&mut seeded_csprng(), Sha256::new().chain_update(MESSAGE));
7474

7575
let expected_signature =
7676
Signature::from_der(MESSAGE_SIGNATURE_CRATE_ASN1).expect("Failed to decode signature");

dsa/tests/private_key.rs renamed to dsa/tests/signing_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn sign_and_verify() {
4747
let verifying_key = signing_key.verifying_key();
4848

4949
let signature =
50-
signing_key.sign_digest_with_rng(rand::thread_rng(), Sha1::new().chain_update(DATA));
50+
signing_key.sign_digest_with_rng(&mut rand::thread_rng(), Sha1::new().chain_update(DATA));
5151

5252
assert!(verifying_key
5353
.verify_digest(Sha1::new().chain_update(DATA), &signature)
File renamed without changes.

ecdsa/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rust-version = "1.57"
1717

1818
[dependencies]
1919
elliptic-curve = { version = "0.12", default-features = false, features = ["digest", "sec1"] }
20-
signature = { version = "=2.0.0-pre.0", default-features = false, features = ["rand-preview"] }
20+
signature = { version = "=2.0.0-pre.2", default-features = false, features = ["rand-preview"] }
2121

2222
# optional dependencies
2323
der = { version = "0.6", optional = true }

ecdsa/src/der.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ where
230230
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
231231
{
232232
fn from(signature: Signature<C>) -> Box<[u8]> {
233-
signature.to_boxed_slice()
233+
signature.to_vec().into_boxed_slice()
234234
}
235235
}
236236

ecdsa/src/sign.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use elliptic_curve::{
1818
use signature::{
1919
digest::{core_api::BlockSizeUser, Digest, FixedOutput, FixedOutputReset},
2020
hazmat::PrehashSigner,
21-
rand_core::{CryptoRng, RngCore},
21+
rand_core::CryptoRngCore,
2222
DigestSigner, RandomizedDigestSigner, RandomizedSigner, Signer,
2323
};
2424

@@ -68,7 +68,7 @@ where
6868
SignatureSize<C>: ArrayLength<u8>,
6969
{
7070
/// Generate a cryptographically random [`SigningKey`].
71-
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
71+
pub fn random(rng: &mut impl CryptoRngCore) -> Self {
7272
NonZeroScalar::<C>::random(rng).into()
7373
}
7474

@@ -171,7 +171,7 @@ where
171171
/// entropy from an RNG.
172172
fn try_sign_digest_with_rng(
173173
&self,
174-
mut rng: impl CryptoRng + RngCore,
174+
rng: &mut impl CryptoRngCore,
175175
msg_digest: D,
176176
) -> Result<Signature<C>> {
177177
let mut ad = FieldBytes::<C>::default();
@@ -190,7 +190,7 @@ where
190190
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + Reduce<C::UInt> + SignPrimitive<C>,
191191
SignatureSize<C>: ArrayLength<u8>,
192192
{
193-
fn try_sign_with_rng(&self, rng: impl CryptoRng + RngCore, msg: &[u8]) -> Result<Signature<C>> {
193+
fn try_sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> Result<Signature<C>> {
194194
self.try_sign_digest_with_rng(rng, C::Digest::new_with_prefix(msg))
195195
}
196196
}
@@ -242,7 +242,7 @@ where
242242
{
243243
fn try_sign_digest_with_rng(
244244
&self,
245-
rng: impl CryptoRng + RngCore,
245+
rng: &mut impl CryptoRngCore,
246246
msg_digest: D,
247247
) -> Result<der::Signature<C>> {
248248
RandomizedDigestSigner::<D, Signature<C>>::try_sign_digest_with_rng(self, rng, msg_digest)
@@ -263,7 +263,7 @@ where
263263
{
264264
fn try_sign_with_rng(
265265
&self,
266-
rng: impl CryptoRng + RngCore,
266+
rng: &mut impl CryptoRngCore,
267267
msg: &[u8],
268268
) -> Result<der::Signature<C>> {
269269
RandomizedSigner::<Signature<C>>::try_sign_with_rng(self, rng, msg).map(Into::into)

ed25519/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2021"
1717
rust-version = "1.56"
1818

1919
[dependencies]
20-
signature = { version = "=2.0.0-pre.0", default-features = false }
20+
signature = { version = "=2.0.0-pre.2", default-features = false }
2121

2222
# optional dependencies
2323
pkcs8 = { version = "0.9", optional = true }

0 commit comments

Comments
 (0)