From 55e59ce3645d7841c6ef8a56c0a00d7b8732bc4f Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 3 Jan 2026 10:59:11 -0700 Subject: [PATCH] crypto-common: add deprecated `generate*` to `KeyIvInit` I was myself confused by the breakages removing these methods caused the other week: https://github.com/RustCrypto/formats/pull/2140#issuecomment-3694361097 If it's confusing me as the person who made the changes, no doubt it will confuse others, as it's something of a counterintuitive migration (though ultimately for the best). This adds back the methods trying to mostly preserve the type signatures from `crypto-common` v0.1, and deprecates them, along with providing documentation for what to do instead. --- crypto-common/src/lib.rs | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/crypto-common/src/lib.rs b/crypto-common/src/lib.rs index 54a1926af..d9919980d 100644 --- a/crypto-common/src/lib.rs +++ b/crypto-common/src/lib.rs @@ -30,6 +30,9 @@ use hybrid_array::{ typenum::{Diff, Sum, Unsigned}, }; +#[cfg(feature = "rand_core")] +use rand_core::CryptoRng; + /// Block on which [`BlockSizeUser`] implementors operate. pub type Block = Array::BlockSize>; @@ -178,6 +181,22 @@ pub trait KeyInit: KeySizeUser + Sized { .map(Self::new) .map_err(|_| InvalidLength) } + + /// DEPRECATED: generate random key using the provided [`CryptoRng`]. + /// + /// Instead, you can now use the [`Generate`] trait directly with the [`Key`] type: + /// + /// ```ignore + /// let key = Key::generate_from_rng(rng); + /// ``` + #[deprecated( + since = "0.2.0", + note = "use the `Generate` trait impl on `Key` instead" + )] + #[cfg(feature = "rand_core")] + fn generate_key(rng: &mut R) -> Key { + Key::::generate_from_rng(rng) + } } /// Types which can be initialized from a key and initialization vector (nonce). @@ -205,6 +224,57 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized { let iv = <&Iv>::try_from(iv).map_err(|_| InvalidLength)?; Ok(Self::new(key, iv)) } + + /// DEPRECATED: generate random key using the provided [`CryptoRng`]. + /// + /// Instead, you can now use the [`Generate`] trait directly with the [`Key`] type: + /// + /// ```ignore + /// let key = Key::generate_from_rng(rng); + /// ``` + #[deprecated( + since = "0.2.0", + note = "use the `Generate` trait impl on `Key` instead" + )] + #[cfg(feature = "rand_core")] + fn generate_key(rng: &mut R) -> Key { + Key::::generate_from_rng(rng) + } + + /// DEPRECATED: generate random IV using the provided [`CryptoRng`]. + /// + /// Instead, you can now use the [`Generate`] trait directly with the [`Iv`] type: + /// + /// ```ignore + /// let iv = Iv::generate_from_rng(rng); + /// ``` + #[deprecated( + since = "0.2.0", + note = "use the `Generate` trait impl on `Iv` instead" + )] + #[cfg(feature = "rand_core")] + fn generate_iv(rng: &mut R) -> Iv { + Iv::::generate_from_rng(rng) + } + + /// DEPRECATED: generate random key and IV using the provided [`CryptoRng`]. + /// + /// Instead, you can now use the [`Generate`] trait directly with the [`Key`] and [`Iv`] types: + /// + /// ```ignore + /// let key = Key::generate_from_rng(rng); + /// let iv = Iv::generate_from_rng(rng); + /// ``` + #[deprecated( + since = "0.2.0", + note = "use the `Generate` trait impls on `Key` and `Iv` instead" + )] + #[cfg(feature = "rand_core")] + fn generate_key_iv(rng: &mut R) -> (Key, Iv) { + let key = Key::::generate_from_rng(rng); + let iv = Iv::::generate_from_rng(rng); + (key, iv) + } } /// Types which can be fallibly initialized from a key.