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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions crypto-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ pub use hybrid_array::typenum;

use core::fmt;
use hybrid_array::{typenum::Unsigned, Array, ArraySize, ByteArray};

#[cfg(feature = "rand_core")]
use rand_core::CryptoRngCore;
#[cfg(feature = "getrandom")]
use rand_core::OsRng;

/// Block on which [`BlockSizeUser`] implementors operate.
pub type Block<B> = ByteArray<<B as BlockSizeUser>::BlockSize>;
Expand Down Expand Up @@ -156,10 +159,17 @@ pub trait KeyInit: KeySizeUser + Sized {
.map_err(|_| InvalidLength)
}

/// Generate random key using the operating system's secure RNG.
#[cfg(feature = "getrandom")]
#[inline]
fn generate_key() -> Key<Self> {
Self::generate_key_with_rng(&mut OsRng)
}

/// 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.

key
Expand All @@ -179,29 +189,53 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
Ok(Self::new(key, iv))
}

/// Generate random key using the operating system's secure RNG.
#[cfg(feature = "getrandom")]
#[inline]
fn generate_key() -> Key<Self> {
Self::generate_key_with_rng(&mut OsRng)
}

/// 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);
key
}

/// Generate random IV using the operating system's secure RNG.
#[cfg(feature = "getrandom")]
#[inline]
fn generate_iv() -> Iv<Self> {
Self::generate_iv_with_rng(&mut OsRng)
}

/// Generate random IV using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv(mut rng: impl CryptoRngCore) -> Iv<Self> {
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
}

/// Generate random key and nonce using the provided [`CryptoRngCore`].
/// Generate random key and IV using the operating system's secure RNG.
#[cfg(feature = "getrandom")]
#[inline]
fn generate_key_iv() -> (Key<Self>, Iv<Self>) {
Self::generate_key_iv_with_rng(&mut OsRng)
}

/// Generate random key and IV using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_iv(mut rng: impl CryptoRngCore) -> (Key<Self>, Iv<Self>) {
(Self::generate_key(&mut rng), Self::generate_iv(&mut rng))
fn generate_key_iv_with_rng(rng: &mut impl CryptoRngCore) -> (Key<Self>, Iv<Self>) {
(
Self::generate_key_with_rng(rng),
Self::generate_iv_with_rng(rng),
)
}
}

Expand All @@ -228,10 +262,17 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
Ok(Self::inner_iv_init(inner, iv))
}

/// Generate random IV using the operating system's secure RNG.
#[cfg(feature = "getrandom")]
#[inline]
fn generate_iv() -> Iv<Self> {
Self::generate_iv_with_rng(&mut OsRng)
}

/// Generate random IV using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv(mut rng: impl CryptoRngCore) -> Iv<Self> {
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
Expand Down