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

Return Result from all generate_* methods #1379

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crypto-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.56"
rust-version = "1.65"
documentation = "https://docs.rs/crypto-common"
repository = "https://github.com/RustCrypto/traits"
keywords = ["crypto", "traits"]
Expand All @@ -17,10 +17,10 @@ hybrid-array = "=0.2.0-pre.5"

# optional dependencies
rand_core = { version = "0.6.4", optional = true }
getrandom = { version = "0.2", optional = true }

[features]
std = []
getrandom = ["rand_core/getrandom"]
tarcieri marked this conversation as resolved.
Show resolved Hide resolved

[package.metadata.docs.rs]
all-features = true
Expand Down
90 changes: 27 additions & 63 deletions crypto-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ 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 @@ -162,23 +160,16 @@ pub trait KeyInit: KeySizeUser + Sized {
/// 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)
fn generate_key() -> Result<Key<Self>, getrandom::Error> {
let mut key = Key::<Self>::default();
getrandom::getrandom(&mut key)?;
Ok(key)
}

/// Generate random key using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Key<Self> {
Self::try_generate_key_with_rng(rng).expect("RNG failure")
}

/// Generate random key using the provided [`CryptoRngCore`], returning an error on RNG failure.
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<Key<Self>, rand_core::Error> {
fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Result<Key<Self>, rand_core::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
Expand All @@ -201,23 +192,16 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
/// 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)
fn generate_key() -> Result<Key<Self>, getrandom::Error> {
let mut key = Key::<Self>::default();
getrandom::getrandom(&mut key)?;
Ok(key)
}

/// Generate random key using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Key<Self> {
Self::try_generate_key_with_rng(rng).expect("RNG failure")
}

/// Generate random key using the provided [`CryptoRngCore`], returning an error on RNG failure.
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<Key<Self>, rand_core::Error> {
fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Result<Key<Self>, rand_core::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
Expand All @@ -226,23 +210,16 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
/// 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)
fn generate_iv() -> Result<Iv<Self>, getrandom::Error> {
let mut iv = Iv::<Self>::default();
getrandom::getrandom(&mut iv)?;
Ok(iv)
}

/// Generate random IV using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Iv<Self> {
Self::try_generate_iv_with_rng(rng).expect("RNG failure")
}

/// Generate random IV using the provided [`CryptoRngCore`], returning an error on RNG failure.
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_iv_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<Iv<Self>, rand_core::Error> {
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Result<Iv<Self>, rand_core::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
Expand All @@ -251,26 +228,20 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
/// 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)
fn generate_key_iv() -> Result<(Key<Self>, Iv<Self>), getrandom::Error> {
let key = Self::generate_key()?;
let iv = Self::generate_iv()?;
Ok((key, iv))
}

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

/// Generate random key and IV using the provided [`CryptoRngCore`], returning an error on RNG
/// failure.
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_iv_with_rng(
fn generate_key_iv_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<(Key<Self>, Iv<Self>), rand_core::Error> {
let key = Self::try_generate_key_with_rng(rng)?;
let iv = Self::try_generate_iv_with_rng(rng)?;
let key = Self::generate_key_with_rng(rng)?;
let iv = Self::generate_iv_with_rng(rng)?;
Ok((key, iv))
}
}
Expand Down Expand Up @@ -301,23 +272,16 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
/// 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)
fn generate_iv() -> Result<Iv<Self>, getrandom::Error> {
let mut iv = Iv::<Self>::default();
getrandom::getrandom(&mut iv)?;
Ok(iv)
}

/// Generate random IV using the provided [`CryptoRngCore`].
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Iv<Self> {
Self::try_generate_iv_with_rng(rng).expect("RNG failure")
}

/// Generate random IV using the provided [`CryptoRngCore`], returning an error on RNG failure.
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_iv_with_rng(
rng: &mut impl CryptoRngCore,
) -> Result<Iv<Self>, rand_core::Error> {
fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Result<Iv<Self>, rand_core::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
Expand Down