Skip to content

Require RngCore instead of CryptoRngCore for various random methods#710

Merged
tarcieri merged 2 commits into
RustCrypto:masterfrom
fjarri:rng-core
Dec 7, 2024
Merged

Require RngCore instead of CryptoRngCore for various random methods#710
tarcieri merged 2 commits into
RustCrypto:masterfrom
fjarri:rng-core

Conversation

@fjarri

@fjarri fjarri commented Dec 7, 2024

Copy link
Copy Markdown
Contributor

Relaxes CryptoRngCore requirement to RngCore. Fixes #137

@AaronFeickert AaronFeickert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many doc comments that reference cryptographically-secure generation, and are no longer accurate with the relaxed trait bound.

Comment thread src/non_zero.rs
{
/// Generate a random `NonZero<T>`.
fn random(mut rng: &mut impl CryptoRngCore) -> Self {
fn random(mut rng: &mut impl RngCore) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to move aspects of the internal comment to the doc comment now that a CSPRNG is no longer guaranteed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this doc comment and allowed the Random::random() one take over.

Comment thread src/traits.rs
pub trait Random: Sized {
/// Generate a cryptographically secure random value.
fn random(rng: &mut impl CryptoRngCore) -> Self;
fn random(rng: &mut impl RngCore) -> Self;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment is no longer accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note

Comment thread src/traits.rs
///
/// A wrapper for [`RandomBits::try_random_bits`] that panics on error.
fn random_bits(rng: &mut impl CryptoRngCore, bit_length: u32) -> Self {
fn random_bits(rng: &mut impl RngCore, bit_length: u32) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment is no longer accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added notes for the methods in this trait

Comment thread src/traits.rs
rng: &mut impl CryptoRngCore,
bit_length: u32,
) -> Result<Self, RandomBitsError>;
fn try_random_bits(rng: &mut impl RngCore, bit_length: u32) -> Result<Self, RandomBitsError>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment is no longer accurate.

Comment thread src/traits.rs
/// A wrapper for [`RandomBits::try_random_bits_with_precision`] that panics on error.
fn random_bits_with_precision(
rng: &mut impl CryptoRngCore,
rng: &mut impl RngCore,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment is no longer accurate.

Comment thread src/traits.rs
/// CSRNG, where previous outputs are unrelated to subsequent
/// outputs and do not reveal information about the RNG's internal state.
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self;
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be very careful about this doc comment, as the user may not be aware of what constitutes a CSPRNG.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At any rate, the top-line comment is no longer accurate.

Comment thread src/uint/boxed/rand.rs
/// underlying random number generator is truly a CSRNG, where previous outputs are unrelated to
/// subsequent outputs and do not reveal information about the RNG's internal state.
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self {
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be very careful about this doc comment, as the user may not be aware of what constitutes a CSPRNG.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At any rate, the top-line comment is no longer accurate.

Comment thread src/uint/rand.rs
impl<const LIMBS: usize> Random for Uint<LIMBS> {
/// Generate a cryptographically secure random [`Uint`].
fn random(mut rng: &mut impl CryptoRngCore) -> Self {
fn random(mut rng: &mut impl RngCore) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment is no longer accurate.

Comment thread src/uint/rand.rs
/// CSRNG, where previous outputs are unrelated to subsequent
/// outputs and do not reveal information about the RNG's internal state.
fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self {
fn random_mod(rng: &mut impl RngCore, modulus: &NonZero<Self>) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See earlier comments.

@fjarri

fjarri commented Dec 7, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for checking, that's what happens when you make a PR late at night

@tarcieri tarcieri merged commit e87d7f4 into RustCrypto:master Dec 7, 2024
@fjarri

fjarri commented Dec 7, 2024

Copy link
Copy Markdown
Contributor Author

I was in the process of adding a little more stuff to the docs... sorry, should have switched it to the draft form

@fjarri

fjarri commented Dec 7, 2024

Copy link
Copy Markdown
Contributor Author

Basically just the additional lines

/// To find a CSRNG, look for RNGs implementing the marker trait [`rand_core::CryptoRngCore`].

Maybe it would be too much hand-holding anyway.

@tarcieri

tarcieri commented Dec 7, 2024

Copy link
Copy Markdown
Member

That seems okay if you want to submit a followup

@AaronFeickert

Copy link
Copy Markdown
Contributor

Does this change mean that random number generation functions using rejection sampling should be explicitly tagged with a _vartime suffix? Previously, their variable-time nature was guaranteed not to leak anything about output values. This is no longer a guarantee given the relaxed trait bound. The alternative would be to retain the existing naming, but update the documentation to carefully note this.

@tarcieri

tarcieri commented Dec 8, 2024

Copy link
Copy Markdown
Member

Ugh, I mean it was always "vartime". I agree that now there are more footguns.

@AaronFeickert

Copy link
Copy Markdown
Contributor

Made some documentation updates in #711 toward this.

@fjarri fjarri deleted the rng-core branch December 8, 2024 18:31
@tarcieri tarcieri mentioned this pull request Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there a reason random() and random_mod() require CryptoRng?

3 participants