Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto-common: change generate_* to support getrandom #1371

Merged
merged 1 commit into from
Oct 31, 2023

Conversation

tarcieri
Copy link
Member

This commit splits the existing generate_* functions into two different variants:

  • fn generate_*: accepts no parameters and uses OsRng automatically. Gated on the getrandom feature.
  • fn generate_*_with_rng: accepts a &mut impl CryptoRngCore parameter. Gated on the rand_core.

Previously all of the generate_* methods were parameterized on a CryptoRngCore.

Splitting them up like this makes it very easy for users to do the right thing, which is use getrandom/OsRng, but without the need to document how to import OsRng into their code everywhere.

Retaining the parameterized versions as *_with_rng allows users to pass a custom RNG where it makes sense, for example rand::thread_rng, or potentially an embedded peripheral/entropy pool.

This commit splits the existing `generate_*` functions into two
different variants:

- `fn generate_*`: accepts no parameters and uses `OsRng` automatically.
  Gated on the `getrandom` feature.
- `fn generate_*_with_rng`: accepts a `&mut impl CryptoRngCore`
  parameter. Gated on the `rand_core`.

Previously all of the `generate_*` methods were parameterized on a
`CryptoRngCore`.

Splitting them up like this makes it very easy for users to do the right
thing, which is use `getrandom`/`OsRng`, but without the need to
document how to import `OsRng` into their code everywhere.

Retaining the parameterized versions as `*_with_rng` allows users to
pass a custom RNG where it makes sense, for example `rand::thread_rng`,
or potentially an embedded peripheral/entropy pool.
@tarcieri tarcieri merged commit 128d4e6 into master Oct 31, 2023
9 checks passed
@tarcieri tarcieri deleted the crypto-common/getrandom-rng-support branch October 31, 2023 19:20
/// Generate random key using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key(mut rng: impl CryptoRngCore) -> Key<Self> {
fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
Copy link
Member Author

Choose a reason for hiding this comment

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

@newpavlov should we also make these methods fallible and call try_fill_bytes?

Copy link
Member

Choose a reason for hiding this comment

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

It may be worth to add separate try_* variants and add a note to the generate_* method docs about potential (improbable) panics.

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.

2 participants