Skip to content

Commit 4303b14

Browse files
authored
crypto-common: add deprecated generate* to Key(Iv)Init (#2162)
I was myself confused by the breakages removing these methods caused the other week: RustCrypto/formats#2140 (comment) 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.
1 parent d51c293 commit 4303b14

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

crypto-common/src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ use hybrid_array::{
3030
typenum::{Diff, Sum, Unsigned},
3131
};
3232

33+
#[cfg(feature = "rand_core")]
34+
use rand_core::CryptoRng;
35+
3336
/// Block on which [`BlockSizeUser`] implementors operate.
3437
pub type Block<B> = Array<u8, <B as BlockSizeUser>::BlockSize>;
3538

@@ -178,6 +181,22 @@ pub trait KeyInit: KeySizeUser + Sized {
178181
.map(Self::new)
179182
.map_err(|_| InvalidLength)
180183
}
184+
185+
/// DEPRECATED: generate random key using the provided [`CryptoRng`].
186+
///
187+
/// Instead, you can now use the [`Generate`] trait directly with the [`Key`] type:
188+
///
189+
/// ```ignore
190+
/// let key = Key::generate_from_rng(rng);
191+
/// ```
192+
#[deprecated(
193+
since = "0.2.0",
194+
note = "use the `Generate` trait impl on `Key` instead"
195+
)]
196+
#[cfg(feature = "rand_core")]
197+
fn generate_key<R: CryptoRng>(rng: &mut R) -> Key<Self> {
198+
Key::<Self>::generate_from_rng(rng)
199+
}
181200
}
182201

183202
/// Types which can be initialized from a key and initialization vector (nonce).
@@ -205,6 +224,57 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
205224
let iv = <&Iv<Self>>::try_from(iv).map_err(|_| InvalidLength)?;
206225
Ok(Self::new(key, iv))
207226
}
227+
228+
/// DEPRECATED: generate random key using the provided [`CryptoRng`].
229+
///
230+
/// Instead, you can now use the [`Generate`] trait directly with the [`Key`] type:
231+
///
232+
/// ```ignore
233+
/// let key = Key::generate_from_rng(rng);
234+
/// ```
235+
#[deprecated(
236+
since = "0.2.0",
237+
note = "use the `Generate` trait impl on `Key` instead"
238+
)]
239+
#[cfg(feature = "rand_core")]
240+
fn generate_key<R: CryptoRng>(rng: &mut R) -> Key<Self> {
241+
Key::<Self>::generate_from_rng(rng)
242+
}
243+
244+
/// DEPRECATED: generate random IV using the provided [`CryptoRng`].
245+
///
246+
/// Instead, you can now use the [`Generate`] trait directly with the [`Iv`] type:
247+
///
248+
/// ```ignore
249+
/// let iv = Iv::generate_from_rng(rng);
250+
/// ```
251+
#[deprecated(
252+
since = "0.2.0",
253+
note = "use the `Generate` trait impl on `Iv` instead"
254+
)]
255+
#[cfg(feature = "rand_core")]
256+
fn generate_iv<R: CryptoRng>(rng: &mut R) -> Iv<Self> {
257+
Iv::<Self>::generate_from_rng(rng)
258+
}
259+
260+
/// DEPRECATED: generate random key and IV using the provided [`CryptoRng`].
261+
///
262+
/// Instead, you can now use the [`Generate`] trait directly with the [`Key`] and [`Iv`] types:
263+
///
264+
/// ```ignore
265+
/// let key = Key::generate_from_rng(rng);
266+
/// let iv = Iv::generate_from_rng(rng);
267+
/// ```
268+
#[deprecated(
269+
since = "0.2.0",
270+
note = "use the `Generate` trait impls on `Key` and `Iv` instead"
271+
)]
272+
#[cfg(feature = "rand_core")]
273+
fn generate_key_iv<R: CryptoRng>(rng: &mut R) -> (Key<Self>, Iv<Self>) {
274+
let key = Key::<Self>::generate_from_rng(rng);
275+
let iv = Iv::<Self>::generate_from_rng(rng);
276+
(key, iv)
277+
}
208278
}
209279

210280
/// Types which can be fallibly initialized from a key.

0 commit comments

Comments
 (0)