11use  crate :: handshake:: binder:: PskBinder ; 
22use  crate :: handshake:: finished:: Finished ; 
33use  crate :: { TlsError ,  config:: TlsCipherSuite } ; 
4- #[ cfg( feature = "key-cache" ) ]  
5- use  core:: cell:: OnceCell ; 
64use  digest:: OutputSizeUser ; 
75use  digest:: generic_array:: ArrayLength ; 
86use  hmac:: { Mac ,  SimpleHmac } ; 
@@ -137,11 +135,8 @@ where
137135{ 
138136    traffic_secret :  Secret < CipherSuite > , 
139137    counter :  u64 , 
140- 
141-     #[ cfg( feature = "key-cache" ) ]  
142-     key :  OnceCell < KeyArray < CipherSuite > > , 
143-     #[ cfg( feature = "key-cache" ) ]  
144-     iv :  OnceCell < IvArray < CipherSuite > > , 
138+     key :  KeyArray < CipherSuite > , 
139+     iv :  IvArray < CipherSuite > , 
145140} 
146141
147142impl < CipherSuite >  KeyScheduleState < CipherSuite > 
@@ -152,57 +147,24 @@ where
152147        Self  { 
153148            traffic_secret :  Secret :: Uninitialized , 
154149            counter :  0 , 
155-             #[ cfg( feature = "key-cache" ) ]  
156-             key :  OnceCell :: new ( ) , 
157-             #[ cfg( feature = "key-cache" ) ]  
158-             iv :  OnceCell :: new ( ) , 
150+             key :  KeyArray :: < CipherSuite > :: default ( ) , 
151+             iv :  IvArray :: < CipherSuite > :: default ( ) , 
159152        } 
160153    } 
161154
162155    #[ inline]  
163-     pub  fn  get_key ( & self )  -> Result < KeyArray < CipherSuite > ,  TlsError >  { 
164-         #[ cfg( feature = "key-cache" ) ]  
165-         if  let  Some ( k)  = self . key . get ( )  { 
166-             Ok ( k. clone ( ) ) 
167-         }  else  { 
168-             let  k = self . get_key_impl ( ) ?; 
169-             let  _ = self . key . set ( k. clone ( ) ) ; 
170-             Ok ( k) 
171-         } 
172- 
173-         #[ cfg( not( feature = "key-cache" ) ) ]  
174-         self . get_key_impl ( ) 
156+     pub  fn  get_key ( & self )  -> Result < & KeyArray < CipherSuite > ,  TlsError >  { 
157+         Ok ( & self . key ) 
175158    } 
176159
177160    #[ inline]  
178-     pub  fn  get_iv ( & self )  -> Result < IvArray < CipherSuite > ,  TlsError >  { 
179-         #[ cfg( feature = "key-cache" ) ]  
180-         if  let  Some ( k)  = self . iv . get ( )  { 
181-             Ok ( k. clone ( ) ) 
182-         }  else  { 
183-             let  k = self . get_iv_impl ( ) ?; 
184-             let  _ = self . iv . set ( k. clone ( ) ) ; 
185-             Ok ( k) 
186-         } 
187- 
188-         #[ cfg( not( feature = "key-cache" ) ) ]  
189-         self . get_iv_impl ( ) 
190-     } 
191- 
192-     fn  get_key_impl ( & self )  -> Result < KeyArray < CipherSuite > ,  TlsError >  { 
193-         self . traffic_secret 
194-             . make_expanded_hkdf_label ( b"key" ,  ContextType :: None ) 
161+     pub  fn  get_iv ( & self )  -> Result < & IvArray < CipherSuite > ,  TlsError >  { 
162+         Ok ( & self . iv ) 
195163    } 
196164
197-     fn  get_iv_impl ( & self )  -> Result < IvArray < CipherSuite > ,  TlsError >  { 
198-         self . traffic_secret 
199-             . make_expanded_hkdf_label ( b"iv" ,  ContextType :: None ) 
200-     } 
201- 
202- 
203165    pub  fn  get_nonce ( & self )  -> Result < IvArray < CipherSuite > ,  TlsError >  { 
204166        let  iv = self . get_iv ( ) ?; 
205-         Ok ( KeySchedule :: < CipherSuite > :: get_nonce ( self . counter ,  & iv) ) 
167+         Ok ( KeySchedule :: < CipherSuite > :: get_nonce ( self . counter ,  iv) ) 
206168    } 
207169
208170    fn  calculate_traffic_secret ( 
@@ -216,11 +178,12 @@ where
216178            Hkdf :: < CipherSuite > :: from_prk ( & secret) . map_err ( |_| TlsError :: InternalError ) ?; 
217179
218180        self . traffic_secret . replace ( traffic_secret) ; 
219-         #[ cfg( feature = "key-cache" ) ]  
220-         { 
221-             self . key  = OnceCell :: new ( ) ; 
222-             self . iv  = OnceCell :: new ( ) ; 
223-         } 
181+         self . key  = self 
182+             . traffic_secret 
183+             . make_expanded_hkdf_label ( b"key" ,  ContextType :: None ) ?; 
184+         self . iv  = self 
185+             . traffic_secret 
186+             . make_expanded_hkdf_label ( b"iv" ,  ContextType :: None ) ?; 
224187        self . counter  = 0 ; 
225188        Ok ( ( ) ) 
226189    } 
@@ -449,7 +412,7 @@ where
449412        self . state . increment_counter ( ) ; 
450413    } 
451414
452-     pub ( crate )  fn  get_key ( & self )  -> Result < KeyArray < CipherSuite > ,  TlsError >  { 
415+     pub ( crate )  fn  get_key ( & self )  -> Result < & KeyArray < CipherSuite > ,  TlsError >  { 
453416        self . state . get_key ( ) 
454417    } 
455418
@@ -496,7 +459,7 @@ where
496459        & mut  self . transcript_hash 
497460    } 
498461
499-     pub ( crate )  fn  get_key ( & self )  -> Result < KeyArray < CipherSuite > ,  TlsError >  { 
462+     pub ( crate )  fn  get_key ( & self )  -> Result < & KeyArray < CipherSuite > ,  TlsError >  { 
500463        self . state . get_key ( ) 
501464    } 
502465
0 commit comments