Skip to content

Commit

Permalink
Return Result from all generate_* methods (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Nov 11, 2023
1 parent 6497790 commit eb07d10
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 65 deletions.
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"]

[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

0 comments on commit eb07d10

Please sign in to comment.