1212/// Hazardous materials.
1313pub mod hazmat;
1414
15- #[ cfg( feature = "getrandom" ) ]
16- pub use getrandom;
17-
15+ /// Secure random generation.
1816#[ cfg( feature = "rand_core" ) ]
19- pub use rand_core ;
17+ mod generate ;
2018
2119pub use hybrid_array as array;
2220pub use hybrid_array:: typenum;
2321
22+ #[ cfg( feature = "rand_core" ) ]
23+ pub use { generate:: Generate , rand_core} ;
24+ #[ cfg( feature = "getrandom" ) ]
25+ pub use { getrandom, getrandom:: Error as RngError } ;
26+
2427use core:: fmt;
2528use hybrid_array:: {
2629 Array , ArraySize ,
2730 typenum:: { Diff , Sum , Unsigned } ,
2831} ;
2932
30- #[ cfg( feature = "rand_core" ) ]
31- use rand_core:: { CryptoRng , TryCryptoRng } ;
32-
3333/// Block on which [`BlockSizeUser`] implementors operate.
3434pub type Block < B > = Array < u8 , <B as BlockSizeUser >:: BlockSize > ;
3535
@@ -178,35 +178,6 @@ pub trait KeyInit: KeySizeUser + Sized {
178178 . map ( Self :: new)
179179 . map_err ( |_| InvalidLength )
180180 }
181-
182- /// Generate random key using the operating system's secure RNG.
183- #[ cfg( feature = "getrandom" ) ]
184- #[ inline]
185- fn generate_key ( ) -> Result < Key < Self > , getrandom:: Error > {
186- let mut key = Key :: < Self > :: default ( ) ;
187- getrandom:: fill ( & mut key) ?;
188- Ok ( key)
189- }
190-
191- /// Generate random key using the provided [`CryptoRng`].
192- #[ cfg( feature = "rand_core" ) ]
193- #[ inline]
194- fn generate_key_with_rng < R : CryptoRng + ?Sized > ( rng : & mut R ) -> Key < Self > {
195- let mut key = Key :: < Self > :: default ( ) ;
196- rng. fill_bytes ( & mut key) ;
197- key
198- }
199-
200- /// Generate random key using the provided [`TryCryptoRng`].
201- #[ cfg( feature = "rand_core" ) ]
202- #[ inline]
203- fn try_generate_key_with_rng < R : TryCryptoRng + ?Sized > (
204- rng : & mut R ,
205- ) -> Result < Key < Self > , R :: Error > {
206- let mut key = Key :: < Self > :: default ( ) ;
207- rng. try_fill_bytes ( & mut key) ?;
208- Ok ( key)
209- }
210181}
211182
212183/// Types which can be initialized from key and initialization vector (nonce).
@@ -234,93 +205,6 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
234205 let iv = <& Iv < Self > >:: try_from ( iv) . map_err ( |_| InvalidLength ) ?;
235206 Ok ( Self :: new ( key, iv) )
236207 }
237-
238- /// Generate random key using the operating system's secure RNG.
239- #[ cfg( feature = "getrandom" ) ]
240- #[ inline]
241- fn generate_key ( ) -> Result < Key < Self > , getrandom:: Error > {
242- let mut key = Key :: < Self > :: default ( ) ;
243- getrandom:: fill ( & mut key) ?;
244- Ok ( key)
245- }
246-
247- /// Generate random key using the provided [`CryptoRng`].
248- #[ cfg( feature = "rand_core" ) ]
249- #[ inline]
250- fn generate_key_with_rng < R : CryptoRng + ?Sized > ( rng : & mut R ) -> Key < Self > {
251- let mut key = Key :: < Self > :: default ( ) ;
252- rng. fill_bytes ( & mut key) ;
253- key
254- }
255-
256- /// Generate random key using the provided [`TryCryptoRng`].
257- #[ cfg( feature = "rand_core" ) ]
258- #[ inline]
259- fn try_generate_key_with_rng < R : TryCryptoRng + ?Sized > (
260- rng : & mut R ,
261- ) -> Result < Key < Self > , R :: Error > {
262- let mut key = Key :: < Self > :: default ( ) ;
263- rng. try_fill_bytes ( & mut key) ?;
264- Ok ( key)
265- }
266-
267- /// Generate random IV using the operating system's secure RNG.
268- #[ cfg( feature = "getrandom" ) ]
269- #[ inline]
270- fn generate_iv ( ) -> Result < Iv < Self > , getrandom:: Error > {
271- let mut iv = Iv :: < Self > :: default ( ) ;
272- getrandom:: fill ( & mut iv) ?;
273- Ok ( iv)
274- }
275-
276- /// Generate random IV using the provided [`CryptoRng`].
277- #[ cfg( feature = "rand_core" ) ]
278- #[ inline]
279- fn generate_iv_with_rng < R : CryptoRng + ?Sized > ( rng : & mut R ) -> Iv < Self > {
280- let mut iv = Iv :: < Self > :: default ( ) ;
281- rng. fill_bytes ( & mut iv) ;
282- iv
283- }
284-
285- /// Generate random IV using the provided [`TryCryptoRng`].
286- #[ cfg( feature = "rand_core" ) ]
287- #[ inline]
288- fn try_generate_iv_with_rng < R : TryCryptoRng + ?Sized > (
289- rng : & mut R ,
290- ) -> Result < Iv < Self > , R :: Error > {
291- let mut iv = Iv :: < Self > :: default ( ) ;
292- rng. try_fill_bytes ( & mut iv) ?;
293- Ok ( iv)
294- }
295-
296- /// Generate random key and IV using the operating system's secure RNG.
297- #[ cfg( feature = "getrandom" ) ]
298- #[ inline]
299- fn generate_key_iv ( ) -> Result < ( Key < Self > , Iv < Self > ) , getrandom:: Error > {
300- let key = Self :: generate_key ( ) ?;
301- let iv = Self :: generate_iv ( ) ?;
302- Ok ( ( key, iv) )
303- }
304-
305- /// Generate random key and IV using the provided [`CryptoRng`].
306- #[ cfg( feature = "rand_core" ) ]
307- #[ inline]
308- fn generate_key_iv_with_rng < R : CryptoRng + ?Sized > ( rng : & mut R ) -> ( Key < Self > , Iv < Self > ) {
309- let key = Self :: generate_key_with_rng ( rng) ;
310- let iv = Self :: generate_iv_with_rng ( rng) ;
311- ( key, iv)
312- }
313-
314- /// Generate random key and IV using the provided [`TryCryptoRng`].
315- #[ cfg( feature = "rand_core" ) ]
316- #[ inline]
317- fn try_generate_key_iv_with_rng < R : TryCryptoRng + ?Sized > (
318- rng : & mut R ,
319- ) -> Result < ( Key < Self > , Iv < Self > ) , R :: Error > {
320- let key = Self :: try_generate_key_with_rng ( rng) ?;
321- let iv = Self :: try_generate_iv_with_rng ( rng) ?;
322- Ok ( ( key, iv) )
323- }
324208}
325209
326210/// Types which can be initialized from another type (usually block ciphers).
@@ -345,35 +229,6 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
345229 let iv = <& Iv < Self > >:: try_from ( iv) . map_err ( |_| InvalidLength ) ?;
346230 Ok ( Self :: inner_iv_init ( inner, iv) )
347231 }
348-
349- /// Generate random IV using the operating system's secure RNG.
350- #[ cfg( feature = "getrandom" ) ]
351- #[ inline]
352- fn generate_iv ( ) -> Result < Iv < Self > , getrandom:: Error > {
353- let mut iv = Iv :: < Self > :: default ( ) ;
354- getrandom:: fill ( & mut iv) ?;
355- Ok ( iv)
356- }
357-
358- /// Generate random IV using the provided [`CryptoRng`].
359- #[ cfg( feature = "rand_core" ) ]
360- #[ inline]
361- fn generate_iv_with_rng < R : CryptoRng + ?Sized > ( rng : & mut R ) -> Iv < Self > {
362- let mut iv = Iv :: < Self > :: default ( ) ;
363- rng. fill_bytes ( & mut iv) ;
364- iv
365- }
366-
367- /// Generate random IV using the provided [`TryCryptoRng`].
368- #[ cfg( feature = "rand_core" ) ]
369- #[ inline]
370- fn try_generate_iv_with_rng < R : TryCryptoRng + ?Sized > (
371- rng : & mut R ,
372- ) -> Result < Iv < Self > , R :: Error > {
373- let mut iv = Iv :: < Self > :: default ( ) ;
374- rng. try_fill_bytes ( & mut iv) ?;
375- Ok ( iv)
376- }
377232}
378233
379234/// Trait for loading current IV state.
0 commit comments