@@ -15,8 +15,11 @@ use aes::{
1515 Aes128 ,
1616 cipher:: { BlockCipherEncrypt , KeyInit } ,
1717} ;
18- use rand:: { CryptoRng , Rng , RngCore , SeedableRng } ;
19- use rand_core:: block:: { BlockRng , BlockRngCore , CryptoBlockRng } ;
18+ use rand:: { RngExt , SeedableRng } ;
19+ use rand_core:: {
20+ TryCryptoRng , TryRng ,
21+ block:: { BlockRng , Generator } ,
22+ } ;
2023
2124use crate :: { AES_PAR_BLOCKS , Block } ;
2225
@@ -28,23 +31,25 @@ use crate::{AES_PAR_BLOCKS, Block};
2831#[ derive( Clone , Debug ) ]
2932pub struct AesRng ( BlockRng < AesRngCore > ) ;
3033
31- impl RngCore for AesRng {
34+ impl TryRng for AesRng {
35+ type Error = core:: convert:: Infallible ;
36+
3237 #[ inline]
33- fn next_u32 ( & mut self ) -> u32 {
34- self . 0 . next_u32 ( )
38+ fn try_next_u32 ( & mut self ) -> Result < u32 , Self :: Error > {
39+ Ok ( self . 0 . next_word ( ) )
3540 }
3641
3742 #[ inline]
38- fn next_u64 ( & mut self ) -> u64 {
39- self . 0 . next_u64 ( )
43+ fn try_next_u64 ( & mut self ) -> Result < u64 , Self :: Error > {
44+ Ok ( self . 0 . next_u64_from_u32 ( ) )
4045 }
4146
4247 #[ inline]
43- fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
48+ fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
4449 let block_size = mem:: size_of :: < aes:: Block > ( ) ;
4550 let block_len = dest. len ( ) / block_size * block_size;
4651 let ( block_bytes, rest_bytes) = dest. split_at_mut ( block_len) ;
47- // fast path so we don't unnecessarily copy u32 from BlockRngCore ::generate into
52+ // fast path so we don't unnecessarily copy u32 from Generator ::generate into
4853 // dest
4954 let blocks = bytemuck:: cast_slice_mut :: < _ , aes:: Block > ( block_bytes) ;
5055 for chunk in blocks. chunks_mut ( AES_PAR_BLOCKS ) {
@@ -55,7 +60,8 @@ impl RngCore for AesRng {
5560 self . 0 . core . aes . encrypt_blocks ( chunk) ;
5661 }
5762 // handle the tail
58- self . 0 . fill_bytes ( rest_bytes)
63+ self . 0 . fill_bytes ( rest_bytes) ;
64+ Ok ( ( ) )
5965 }
6066}
6167
@@ -64,11 +70,11 @@ impl SeedableRng for AesRng {
6470
6571 #[ inline]
6672 fn from_seed ( seed : Self :: Seed ) -> Self {
67- AesRng ( BlockRng :: < AesRngCore > :: from_seed ( seed) )
73+ AesRng ( BlockRng :: new ( AesRngCore :: from_seed ( seed) ) )
6874 }
6975}
7076
71- impl CryptoRng for AesRng { }
77+ impl TryCryptoRng for AesRng { }
7278
7379impl AesRng {
7480 /// Create a new random number generator using a random seed from
@@ -107,15 +113,14 @@ impl std::fmt::Debug for AesRngCore {
107113 }
108114}
109115
110- impl BlockRngCore for AesRngCore {
111- type Item = u32 ;
112- // This is equivalent to `[Block; 9]`
113- type Results = hidden:: ParBlockWrapper ;
116+ impl Generator for AesRngCore {
117+ // This is equivalent to `[aes::Block; AES_PAR_BLOCKS]`
118+ type Output = [ u32 ; AES_PAR_BLOCKS * std:: mem:: size_of :: < u32 > ( ) ] ;
114119
115120 // Compute `E(state)` nine times, where `state` is a counter.
116121 #[ inline]
117- fn generate ( & mut self , results : & mut Self :: Results ) {
118- let blocks = bytemuck:: cast_slice_mut :: < _ , aes:: Block > ( results. as_mut ( ) ) ;
122+ fn generate ( & mut self , results : & mut Self :: Output ) {
123+ let blocks = bytemuck:: cast_slice_mut :: < _ , aes:: Block > ( results) ;
119124 blocks. iter_mut ( ) . for_each ( |blk| {
120125 // aes::Block is a type alias to Array, but type aliases can't be used as
121126 // constructors
@@ -126,32 +131,6 @@ impl BlockRngCore for AesRngCore {
126131 }
127132}
128133
129- mod hidden {
130- /// Equivalent to [aes::Block; 9] (which is the parralel block size for the
131- /// aes-ni backend). Since size 36 arrays don't impl Default we write a
132- /// wrapper.
133- #[ derive( Copy , Clone ) ]
134- pub struct ParBlockWrapper ( [ u32 ; 36 ] ) ;
135-
136- impl Default for ParBlockWrapper {
137- fn default ( ) -> Self {
138- Self ( [ 0 ; 36 ] )
139- }
140- }
141-
142- impl AsMut < [ u32 ] > for ParBlockWrapper {
143- fn as_mut ( & mut self ) -> & mut [ u32 ] {
144- & mut self . 0
145- }
146- }
147-
148- impl AsRef < [ u32 ] > for ParBlockWrapper {
149- fn as_ref ( & self ) -> & [ u32 ] {
150- & self . 0
151- }
152- }
153- }
154-
155134impl SeedableRng for AesRngCore {
156135 type Seed = Block ;
157136
@@ -165,8 +144,6 @@ impl SeedableRng for AesRngCore {
165144 }
166145}
167146
168- impl CryptoBlockRng for AesRngCore { }
169-
170147impl From < AesRngCore > for AesRng {
171148 #[ inline]
172149 fn from ( core : AesRngCore ) -> Self {
0 commit comments