Skip to content

Commit a593e51

Browse files
authored
ssh-key: use &mut impl CryptoRngCore for RNGs (#67)
Alias added in `rand_core` v0.6.4 with a blanket impl for `CryptoRng + RngCore`. Using a mut reference avoids reborrowing. For more context, see RustCrypto/traits#1148.
1 parent 9ee7302 commit a593e51

File tree

5 files changed

+14
-19
lines changed

5 files changed

+14
-19
lines changed

ssh-key/src/certificate/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{public, Result, Signature, SigningKey};
55
use alloc::{string::String, vec::Vec};
66

77
#[cfg(feature = "rand_core")]
8-
use rand_core::{CryptoRng, RngCore};
8+
use rand_core::CryptoRngCore;
99

1010
#[cfg(feature = "std")]
1111
use std::time::SystemTime;
@@ -159,7 +159,7 @@ impl Builder {
159159
#[cfg(feature = "rand_core")]
160160
#[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))]
161161
pub fn new_with_random_nonce(
162-
mut rng: impl CryptoRng + RngCore,
162+
rng: &mut impl CryptoRngCore,
163163
public_key: impl Into<public::KeyData>,
164164
valid_after: u64,
165165
valid_before: u64,

ssh-key/src/kdf.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ use encoding::{CheckedSum, Decode, Encode, Reader, Writer};
99
use alloc::vec::Vec;
1010

1111
#[cfg(feature = "encryption")]
12-
use {
13-
crate::Cipher,
14-
bcrypt_pbkdf::bcrypt_pbkdf,
15-
rand_core::{CryptoRng, RngCore},
16-
zeroize::Zeroizing,
17-
};
12+
use {crate::Cipher, bcrypt_pbkdf::bcrypt_pbkdf, rand_core::CryptoRngCore, zeroize::Zeroizing};
1813

1914
/// Default number of rounds to use for bcrypt-pbkdf.
2015
#[cfg(feature = "encryption")]
@@ -47,7 +42,7 @@ impl Kdf {
4742
/// Initialize KDF configuration for the given algorithm.
4843
#[cfg(feature = "encryption")]
4944
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
50-
pub fn new(algorithm: KdfAlg, mut rng: impl CryptoRng + RngCore) -> Result<Self> {
45+
pub fn new(algorithm: KdfAlg, rng: &mut impl CryptoRngCore) -> Result<Self> {
5146
let mut salt = vec![0u8; DEFAULT_SALT_SIZE];
5247
rng.fill_bytes(&mut salt);
5348

ssh-key/src/private/dsa.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use subtle::{Choice, ConstantTimeEq};
77
use zeroize::Zeroize;
88

99
#[cfg(all(feature = "dsa", feature = "rand_core"))]
10-
use rand_core::{CryptoRng, RngCore};
10+
use rand_core::CryptoRngCore;
1111

1212
/// Digital Signature Algorithm (DSA) private key.
1313
///
@@ -150,9 +150,9 @@ impl DsaKeypair {
150150
/// Generate a random DSA private key.
151151
#[cfg(all(feature = "dsa", feature = "rand_core"))]
152152
#[cfg_attr(docsrs, doc(cfg(all(feature = "dsa", feature = "rand_core"))))]
153-
pub fn random(mut rng: impl CryptoRng + RngCore) -> Result<Self> {
154-
let components = dsa::Components::generate(&mut rng, Self::KEY_SIZE);
155-
dsa::SigningKey::generate(&mut rng, components).try_into()
153+
pub fn random(rng: &mut impl CryptoRngCore) -> Result<Self> {
154+
let components = dsa::Components::generate(rng, Self::KEY_SIZE);
155+
dsa::SigningKey::generate(rng, components).try_into()
156156
}
157157
}
158158

ssh-key/src/private/ed25519.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use subtle::{Choice, ConstantTimeEq};
99
use zeroize::{Zeroize, Zeroizing};
1010

1111
#[cfg(feature = "rand_core")]
12-
use rand_core::{CryptoRng, RngCore};
12+
use rand_core::CryptoRngCore;
1313

1414
/// Ed25519 private key.
1515
// TODO(tarcieri): use `ed25519::PrivateKey`? (doesn't exist yet)
@@ -23,7 +23,7 @@ impl Ed25519PrivateKey {
2323
/// Generate a random Ed25519 private key.
2424
#[cfg(feature = "rand_core")]
2525
#[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))]
26-
pub fn random(mut rng: impl CryptoRng + RngCore) -> Self {
26+
pub fn random(rng: &mut impl CryptoRngCore) -> Self {
2727
let mut key_bytes = [0u8; Self::BYTE_SIZE];
2828
rng.fill_bytes(&mut key_bytes);
2929
Self(key_bytes)
@@ -165,7 +165,7 @@ impl Ed25519Keypair {
165165
/// Generate a random Ed25519 private keypair.
166166
#[cfg(feature = "ed25519")]
167167
#[cfg_attr(docsrs, doc(cfg(feature = "ed25519")))]
168-
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
168+
pub fn random(rng: &mut impl CryptoRngCore) -> Self {
169169
Ed25519PrivateKey::random(rng).into()
170170
}
171171

ssh-key/src/private/rsa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use zeroize::Zeroize;
88

99
#[cfg(feature = "rsa")]
1010
use {
11-
rand_core::{CryptoRng, RngCore},
11+
rand_core::CryptoRngCore,
1212
rsa::{pkcs1v15, PublicKeyParts},
1313
sha2::{digest::const_oid::AssociatedOid, Digest},
1414
};
@@ -109,9 +109,9 @@ impl RsaKeypair {
109109
/// Generate a random RSA keypair of the given size.
110110
#[cfg(feature = "rsa")]
111111
#[cfg_attr(docsrs, doc(cfg(feature = "rsa")))]
112-
pub fn random(mut rng: impl CryptoRng + RngCore, bit_size: usize) -> Result<Self> {
112+
pub fn random(rng: &mut impl CryptoRngCore, bit_size: usize) -> Result<Self> {
113113
if bit_size >= Self::MIN_KEY_SIZE {
114-
rsa::RsaPrivateKey::new(&mut rng, bit_size)?.try_into()
114+
rsa::RsaPrivateKey::new(rng, bit_size)?.try_into()
115115
} else {
116116
Err(Error::Crypto)
117117
}

0 commit comments

Comments
 (0)