diff --git a/crypto-common/Cargo.toml b/crypto-common/Cargo.toml index beb671fa..38496ad5 100644 --- a/crypto-common/Cargo.toml +++ b/crypto-common/Cargo.toml @@ -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"] @@ -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 diff --git a/crypto-common/src/lib.rs b/crypto-common/src/lib.rs index 6fd536b4..5fb3a814 100644 --- a/crypto-common/src/lib.rs +++ b/crypto-common/src/lib.rs @@ -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 = ByteArray<::BlockSize>; @@ -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::generate_key_with_rng(&mut OsRng) + fn generate_key() -> Result, getrandom::Error> { + let mut key = Key::::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::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, rand_core::Error> { + fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Result, rand_core::Error> { let mut key = Key::::default(); rng.try_fill_bytes(&mut key)?; Ok(key) @@ -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::generate_key_with_rng(&mut OsRng) + fn generate_key() -> Result, getrandom::Error> { + let mut key = Key::::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::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, rand_core::Error> { + fn generate_key_with_rng(rng: &mut impl CryptoRngCore) -> Result, rand_core::Error> { let mut key = Key::::default(); rng.try_fill_bytes(&mut key)?; Ok(key) @@ -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::generate_iv_with_rng(&mut OsRng) + fn generate_iv() -> Result, getrandom::Error> { + let mut iv = Iv::::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::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, rand_core::Error> { + fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Result, rand_core::Error> { let mut iv = Iv::::default(); rng.try_fill_bytes(&mut iv)?; Ok(iv) @@ -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, Iv) { - Self::generate_key_iv_with_rng(&mut OsRng) + fn generate_key_iv() -> Result<(Key, Iv), 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, Iv) { - 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, Iv), 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)) } } @@ -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::generate_iv_with_rng(&mut OsRng) + fn generate_iv() -> Result, getrandom::Error> { + let mut iv = Iv::::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::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, rand_core::Error> { + fn generate_iv_with_rng(rng: &mut impl CryptoRngCore) -> Result, rand_core::Error> { let mut iv = Iv::::default(); rng.try_fill_bytes(&mut iv)?; Ok(iv)