Skip to content

Commit 9f4eb66

Browse files
committed
rand_core: upcoming API breaks
A recent change in the upcoming rand_core 0.10.0-rc-4 dropped the implied `CryptoRng: RngCore` from the trait. This forces any consumer to make the requirement explicit. This commit prepares for that.
1 parent 875405e commit 9f4eb66

File tree

13 files changed

+68
-51
lines changed

13 files changed

+68
-51
lines changed

cms/src/builder.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ use alloc::{
2626
vec::Vec,
2727
};
2828
use cipher::{
29-
BlockModeEncrypt, Iv, Key, KeyIvInit, block_padding::Pkcs7, crypto_common::Generate,
30-
rand_core::CryptoRng,
29+
BlockModeEncrypt, Iv, Key, KeyIvInit,
30+
block_padding::Pkcs7,
31+
crypto_common::Generate,
32+
rand_core::{CryptoRng, RngCore},
3133
};
3234
use const_oid::ObjectIdentifier;
3335
use core::{cmp::Ordering, fmt, marker::PhantomData};
@@ -437,7 +439,7 @@ impl<'s> SignedDataBuilder<'s> {
437439
S: RandomizedSigner<Signature>,
438440
S::VerifyingKey: EncodePublicKey,
439441
Signature: SignatureBitStringEncoding,
440-
R: CryptoRng + ?Sized,
442+
R: CryptoRng + RngCore + ?Sized,
441443
{
442444
let signer_info = signer_info_builder
443445
.build_with_rng::<S, Signature, R>(signer, rng)
@@ -482,7 +484,7 @@ impl<'s> SignedDataBuilder<'s> {
482484
S: AsyncRandomizedSigner<Signature>,
483485
S::VerifyingKey: EncodePublicKey,
484486
Signature: SignatureBitStringEncoding,
485-
R: CryptoRng + ?Sized,
487+
R: CryptoRng + RngCore + ?Sized,
486488
{
487489
let signer_info = signer_info_builder
488490
.build_with_rng_async::<S, Signature, R>(signer, rng)
@@ -606,7 +608,7 @@ impl<'s> SignedDataBuilder<'s> {
606608
/// formats. All implementations must implement this trait.
607609
pub trait RecipientInfoBuilder {
608610
/// Associated Rng type
609-
type Rng: CryptoRng + ?Sized;
611+
type Rng: CryptoRng + RngCore + ?Sized;
610612

611613
/// Return the recipient info type
612614
fn recipient_info_type(&self) -> RecipientInfoType;
@@ -668,9 +670,9 @@ impl<R> KeyTransRecipientInfoBuilder<R> {
668670
}
669671
}
670672

671-
impl<R: ?Sized> RecipientInfoBuilder for KeyTransRecipientInfoBuilder<R>
673+
impl<R> RecipientInfoBuilder for KeyTransRecipientInfoBuilder<R>
672674
where
673-
R: CryptoRng,
675+
R: CryptoRng + RngCore + ?Sized,
674676
{
675677
type Rng = R;
676678

@@ -739,9 +741,9 @@ impl<R> KekRecipientInfoBuilder<R> {
739741
}
740742
}
741743

742-
impl<R: ?Sized> RecipientInfoBuilder for KekRecipientInfoBuilder<R>
744+
impl<R> RecipientInfoBuilder for KekRecipientInfoBuilder<R>
743745
where
744-
R: CryptoRng,
746+
R: CryptoRng + RngCore + ?Sized,
745747
{
746748
type Rng = R;
747749

@@ -782,7 +784,7 @@ pub trait PwriEncryptor {
782784
/// including eventual parameters (e.g. the used iv).
783785
fn key_encryption_algorithm(&self) -> Result<AlgorithmIdentifierOwned>;
784786
/// Encrypt the padded content-encryption key twice following RFC 3211, § 2.3.1
785-
fn encrypt_rfc3211<R: CryptoRng + ?Sized>(
787+
fn encrypt_rfc3211<R: CryptoRng + RngCore + ?Sized>(
786788
&mut self,
787789
padded_content_encryption_key: &[u8],
788790
rng: &mut R,
@@ -830,10 +832,10 @@ where
830832
}
831833
}
832834

833-
impl<P, R: ?Sized> PasswordRecipientInfoBuilder<P, R>
835+
impl<P, R> PasswordRecipientInfoBuilder<P, R>
834836
where
835837
P: PwriEncryptor,
836-
R: CryptoRng,
838+
R: CryptoRng + RngCore + ?Sized,
837839
{
838840
/// Wrap the content-encryption key according to [RFC 3211, §2.3.1]:
839841
/// ....
@@ -874,7 +876,7 @@ where
874876
impl<P, R> RecipientInfoBuilder for PasswordRecipientInfoBuilder<P, R>
875877
where
876878
P: PwriEncryptor,
877-
R: CryptoRng + ?Sized,
879+
R: CryptoRng + RngCore + ?Sized,
878880
{
879881
type Rng = R;
880882

@@ -933,7 +935,7 @@ impl<R> OtherRecipientInfoBuilder<R> {
933935

934936
impl<R> RecipientInfoBuilder for OtherRecipientInfoBuilder<R>
935937
where
936-
R: CryptoRng + ?Sized,
938+
R: CryptoRng + RngCore + ?Sized,
937939
{
938940
type Rng = R;
939941

@@ -1017,7 +1019,7 @@ impl<'c, R> EnvelopedDataBuilder<'c, R> {
10171019

10181020
impl<'c, R> EnvelopedDataBuilder<'c, R>
10191021
where
1020-
R: CryptoRng + ?Sized,
1022+
R: CryptoRng + RngCore + ?Sized,
10211023
{
10221024
/// Add recipient info. A builder is used, which generates a `RecipientInfo` according to
10231025
/// RFC 5652 § 6.2, when `EnvelopedData` is built.
@@ -1214,7 +1216,7 @@ fn encrypt_data<R>(
12141216
rng: &mut R,
12151217
) -> Result<(Vec<u8>, Vec<u8>, AlgorithmIdentifierOwned)>
12161218
where
1217-
R: CryptoRng + ?Sized,
1219+
R: CryptoRng + RngCore + ?Sized,
12181220
{
12191221
match encryption_algorithm_identifier {
12201222
ContentEncryptionAlgorithm::Aes128Cbc => encrypt_block_mode!(

cms/src/builder/kari.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
// Super imports
1010
use super::{
11-
AlgorithmIdentifierOwned, CryptoRng, RecipientInfoBuilder, RecipientInfoType, Result,
11+
AlgorithmIdentifierOwned, CryptoRng, RecipientInfoBuilder, RecipientInfoType, Result, RngCore,
1212
UserKeyingMaterial,
1313
utils::kw::{KeyWrapAlgorithm, WrappedKey},
1414
};
@@ -250,10 +250,9 @@ where
250250
})
251251
}
252252
}
253-
impl<R: ?Sized, C, KA, KW, Enc> RecipientInfoBuilder
254-
for KeyAgreeRecipientInfoBuilder<R, C, KA, KW, Enc>
253+
impl<R, C, KA, KW, Enc> RecipientInfoBuilder for KeyAgreeRecipientInfoBuilder<R, C, KA, KW, Enc>
255254
where
256-
R: CryptoRng,
255+
R: CryptoRng + RngCore + ?Sized,
257256
KA: KeyAgreementAlgorithm + AssociatedOid,
258257
C: CurveArithmetic + AssociatedOid + PointCompression,
259258
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,

cms/tests/builder.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use pem_rfc7468::LineEnding;
2323
use pkcs5::pbes2::Pbkdf2Params;
2424
use rand::rngs::SysRng;
2525
use rsa::pkcs1::DecodeRsaPrivateKey;
26-
use rsa::rand_core::{CryptoRng, TryRngCore};
26+
use rsa::rand_core::{CryptoRng, RngCore, TryRngCore};
2727
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
2828
use rsa::{pkcs1v15, pss};
2929
use sha2::Sha256;
@@ -690,7 +690,10 @@ fn test_create_password_recipient_info() {
690690
key_derivation_params: pkcs5::pbes2::Pbkdf2Params,
691691
}
692692
impl<'a> Aes128CbcPwriEncryptor<'a> {
693-
pub fn new<R: CryptoRng + ?Sized>(challenge_password: &'a [u8], rng: &mut R) -> Self {
693+
pub fn new<R: CryptoRng + RngCore + ?Sized>(
694+
challenge_password: &'a [u8],
695+
rng: &mut R,
696+
) -> Self {
694697
let mut key_encryption_iv = [0u8; 16];
695698
rng.fill_bytes(key_encryption_iv.as_mut_slice());
696699
let key_encryption_iv = key_encryption_iv.into();
@@ -708,7 +711,7 @@ fn test_create_password_recipient_info() {
708711
}
709712
impl PwriEncryptor for Aes128CbcPwriEncryptor<'_> {
710713
const BLOCK_LENGTH_BITS: usize = 128; // AES block length
711-
fn encrypt_rfc3211<R: CryptoRng + ?Sized>(
714+
fn encrypt_rfc3211<R: CryptoRng + RngCore + ?Sized>(
712715
&mut self,
713716
padded_content_encryption_key: &[u8],
714717
_rng: &mut R,

phc/src/salt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
str::{self, FromStr},
99
};
1010
#[cfg(feature = "rand_core")]
11-
use rand_core::{CryptoRng, TryCryptoRng};
11+
use rand_core::{CryptoRng, RngCore, TryCryptoRng, TryRngCore};
1212

1313
/// Error message used with `expect` for when internal invariants are violated
1414
/// (i.e. the contents of a [`Salt`] should always be valid)
@@ -117,14 +117,14 @@ impl Salt {
117117

118118
/// Generate a random [`Salt`] from the given [`CryptoRng`].
119119
#[cfg(feature = "rand_core")]
120-
pub fn from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
120+
pub fn from_rng<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
121121
let Ok(out) = Self::try_from_rng(rng);
122122
out
123123
}
124124

125125
/// Generate a random [`Salt`] from the given [`TryCryptoRng`].
126126
#[cfg(feature = "rand_core")]
127-
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
127+
pub fn try_from_rng<R: TryCryptoRng + TryRngCore + ?Sized>(
128128
rng: &mut R,
129129
) -> core::result::Result<Self, R::Error> {
130130
let mut bytes = [0u8; Self::RECOMMENDED_LENGTH];
@@ -256,14 +256,14 @@ impl SaltString {
256256

257257
/// Generate a random B64-encoded [`SaltString`] from [`CryptoRng`].
258258
#[cfg(feature = "rand_core")]
259-
pub fn from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
259+
pub fn from_rng<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
260260
let Ok(out) = Self::try_from_rng(rng);
261261
out
262262
}
263263

264264
/// Generate a random B64-encoded [`SaltString`] from [`TryCryptoRng`].
265265
#[cfg(feature = "rand_core")]
266-
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
266+
pub fn try_from_rng<R: TryCryptoRng + TryRngCore + ?Sized>(
267267
rng: &mut R,
268268
) -> core::result::Result<Self, R::Error> {
269269
Ok(Salt::try_from_rng(rng)?.to_salt_string())

pkcs5/src/pbes2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use der::{
1919
};
2020

2121
#[cfg(feature = "rand_core")]
22-
use rand_core::CryptoRng;
22+
use rand_core::{CryptoRng, RngCore};
2323

2424
#[cfg(all(feature = "alloc", feature = "pbes2"))]
2525
use alloc::vec::Vec;
@@ -106,7 +106,7 @@ impl Parameters {
106106
/// This is currently an alias for [`Parameters::scrypt`]. See that method
107107
/// for more information.
108108
#[cfg(all(feature = "pbes2", feature = "rand_core"))]
109-
pub fn recommended<R: CryptoRng>(rng: &mut R) -> Self {
109+
pub fn recommended<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
110110
Self::scrypt(rng)
111111
}
112112

@@ -118,7 +118,7 @@ impl Parameters {
118118
/// This will use AES-256-CBC as the encryption algorithm and SHA-256 as
119119
/// the hash function for PBKDF2.
120120
#[cfg(feature = "rand_core")]
121-
pub fn pbkdf2<R: CryptoRng>(rng: &mut R) -> Self {
121+
pub fn pbkdf2<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
122122
let mut iv = [0u8; Self::DEFAULT_IV_LEN];
123123
rng.fill_bytes(&mut iv);
124124

@@ -169,7 +169,7 @@ impl Parameters {
169169
///
170170
/// [RustCrypto/formats#1205]: https://github.com/RustCrypto/formats/issues/1205
171171
#[cfg(all(feature = "pbes2", feature = "rand_core"))]
172-
pub fn scrypt<R: CryptoRng>(rng: &mut R) -> Self {
172+
pub fn scrypt<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
173173
let mut iv = [0u8; Self::DEFAULT_IV_LEN];
174174
rng.fill_bytes(&mut iv);
175175

pkcs8/src/encrypted_private_key_info.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use pkcs5::EncryptionScheme;
1212
use der::{SecretDocument, asn1::OctetString};
1313

1414
#[cfg(feature = "encryption")]
15-
use {pkcs5::pbes2, rand_core::CryptoRng};
15+
use {
16+
pkcs5::pbes2,
17+
rand_core::{CryptoRng, RngCore},
18+
};
1619

1720
#[cfg(feature = "pem")]
1821
use der::pem::PemLabel;
@@ -64,7 +67,7 @@ where
6467
/// Encrypt the given ASN.1 DER document using a symmetric encryption key
6568
/// derived from the provided password.
6669
#[cfg(feature = "encryption")]
67-
pub(crate) fn encrypt<R: CryptoRng>(
70+
pub(crate) fn encrypt<R: CryptoRng + RngCore + ?Sized>(
6871
rng: &mut R,
6972
password: impl AsRef<[u8]>,
7073
doc: &[u8],

pkcs8/src/private_key_info.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use der::{
1717

1818
#[cfg(feature = "encryption")]
1919
use {
20-
crate::EncryptedPrivateKeyInfoRef, der::zeroize::Zeroizing, pkcs5::pbes2, rand_core::CryptoRng,
20+
crate::EncryptedPrivateKeyInfoRef,
21+
der::zeroize::Zeroizing,
22+
pkcs5::pbes2,
23+
rand_core::{CryptoRng, RngCore},
2124
};
2225

2326
#[cfg(feature = "pem")]
@@ -148,7 +151,7 @@ where
148151
/// - p: 1
149152
/// - Cipher: AES-256-CBC (best available option for PKCS#5 encryption)
150153
#[cfg(feature = "encryption")]
151-
pub fn encrypt<R: CryptoRng>(
154+
pub fn encrypt<R: CryptoRng + RngCore + ?Sized>(
152155
&self,
153156
rng: &mut R,
154157
password: impl AsRef<[u8]>,

pkcs8/src/traits.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use crate::{Error, PrivateKeyInfoRef, Result};
66
use der::SecretDocument;
77

88
#[cfg(feature = "encryption")]
9-
use {crate::EncryptedPrivateKeyInfoRef, rand_core::CryptoRng};
9+
use {
10+
crate::EncryptedPrivateKeyInfoRef,
11+
rand_core::{CryptoRng, RngCore},
12+
};
1013

1114
#[cfg(feature = "pem")]
1215
use {
@@ -101,7 +104,7 @@ pub trait EncodePrivateKey {
101104
/// Create an [`SecretDocument`] containing the ciphertext of
102105
/// a PKCS#8 encoded private key encrypted under the given `password`.
103106
#[cfg(feature = "encryption")]
104-
fn to_pkcs8_encrypted_der<R: CryptoRng>(
107+
fn to_pkcs8_encrypted_der<R: CryptoRng + RngCore + ?Sized>(
105108
&self,
106109
rng: &mut R,
107110
password: impl AsRef<[u8]>,
@@ -119,7 +122,7 @@ pub trait EncodePrivateKey {
119122
/// Serialize this private key as an encrypted PEM-encoded PKCS#8 private
120123
/// key using the `provided` to derive an encryption key.
121124
#[cfg(all(feature = "encryption", feature = "pem"))]
122-
fn to_pkcs8_encrypted_pem<R: CryptoRng>(
125+
fn to_pkcs8_encrypted_pem<R: CryptoRng + RngCore + ?Sized>(
123126
&self,
124127
rng: &mut R,
125128
password: impl AsRef<[u8]>,

x509-cert/src/builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use alloc::vec;
44
use core::fmt;
55
use der::{Encode, asn1::BitString, referenced::OwnedToRef};
66
use signature::{
7-
AsyncRandomizedSigner, AsyncSigner, Keypair, RandomizedSigner, Signer, rand_core::CryptoRng,
7+
AsyncRandomizedSigner, AsyncSigner, Keypair, RandomizedSigner, Signer,
8+
rand_core::{CryptoRng, RngCore},
89
};
910
use spki::{
1011
DynSignatureAlgorithmIdentifier, EncodePublicKey, ObjectIdentifier, SignatureBitStringEncoding,
@@ -347,7 +348,7 @@ pub trait Builder: Sized {
347348
S: Keypair + DynSignatureAlgorithmIdentifier,
348349
S::VerifyingKey: EncodePublicKey,
349350
Signature: SignatureBitStringEncoding,
350-
R: CryptoRng + ?Sized,
351+
R: CryptoRng + RngCore + ?Sized,
351352
{
352353
let blob = self.finalize(signer)?;
353354

@@ -539,7 +540,7 @@ pub trait AsyncBuilder: Sized {
539540
S: Keypair + DynSignatureAlgorithmIdentifier,
540541
S::VerifyingKey: EncodePublicKey,
541542
Signature: SignatureBitStringEncoding,
542-
R: CryptoRng + ?Sized,
543+
R: CryptoRng + RngCore + ?Sized,
543544
{
544545
let blob = self.finalize(signer)?;
545546

x509-cert/src/serial_number.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use der::{
88
asn1::{self, Int},
99
};
1010
#[cfg(feature = "builder")]
11-
use {alloc::vec, signature::rand_core::CryptoRng};
11+
use {
12+
alloc::vec,
13+
signature::rand_core::{CryptoRng, RngCore},
14+
};
1215

1316
use crate::certificate::{Profile, Rfc5280};
1417

@@ -77,7 +80,7 @@ impl<P: Profile> SerialNumber<P> {
7780
/// of output from the CSPRNG. This currently defaults to a 17-bytes long serial number.
7881
///
7982
/// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/
80-
pub fn generate<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
83+
pub fn generate<R: CryptoRng + RngCore + ?Sized>(rng: &mut R) -> Self {
8184
Self::generate_with_prefix(&[], 17, rng)
8285
.expect("a random of 17 is acceptable, and rng may not fail")
8386
}
@@ -91,7 +94,7 @@ impl<P: Profile> SerialNumber<P> {
9194
/// equal or below 19 (to account for leading sign disambiguation, and the maximum length of 20).
9295
///
9396
/// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/
94-
pub fn generate_with_prefix<R: CryptoRng + ?Sized>(
97+
pub fn generate_with_prefix<R: CryptoRng + RngCore + ?Sized>(
9598
prefix: &[u8],
9699
rand_len: usize,
97100
rng: &mut R,

0 commit comments

Comments
 (0)