11use std:: fmt;
22
33use chacha20poly1305:: {
4- aead:: { Aead , AeadCore , KeyInit , OsRng , generic_array :: GenericArray } ,
5- XChaCha20Poly1305 , XNonce ,
4+ aead:: { generic_array :: typenum :: Unsigned , Aead , AeadCore , KeyInit , OsRng } ,
5+ XChaCha20Poly1305 , XNonce
66} ;
7+ use hkdf:: Hkdf ;
8+ use sha2:: Sha256 ;
79use cookie:: Key ;
810use serde:: { de, ser, Deserialize , Serialize } ;
911
1012use crate :: request:: { Outcome , Request , FromRequest } ;
1113
12- const NONCE_LEN : usize = 24 ; // 192-bit
13- const KEY_LEN : usize = 32 ;
14+ const INFO_STRING : & [ u8 ] = b"secret_key_data_encryption" ;
1415
1516#[ derive( Debug ) ]
1617pub enum Error {
@@ -212,21 +213,18 @@ impl SecretKey {
212213 /// assert_eq!(decrypted, plaintext);
213214 /// ```
214215 pub fn encrypt < T : AsRef < [ u8 ] > > ( & self , value : T ) -> Result < Vec < u8 > , Error > {
215- // Convert the encryption key to a fixed-length array
216- let key: [ u8 ; KEY_LEN ] = self . key
217- . encryption ( )
218- . try_into ( )
219- . map_err ( |_| Error :: KeyLengthError ) ?;
220-
221- let cipher = XChaCha20Poly1305 :: new ( GenericArray :: from_slice ( & key) ) ;
222216 let nonce = XChaCha20Poly1305 :: generate_nonce ( & mut OsRng ) ;
223217
218+ let ( mut prk, hk) = Hkdf :: < Sha256 > :: extract ( Some ( & nonce) , self . key . encryption ( ) ) ;
219+ hk. expand ( INFO_STRING , & mut prk) . map_err ( |_| Error :: KeyLengthError ) ?;
220+ let cipher = XChaCha20Poly1305 :: new ( & prk) ;
221+
224222 let ciphertext = cipher
225223 . encrypt ( & nonce, value. as_ref ( ) )
226224 . map_err ( |_| Error :: EncryptionError ) ?;
227225
228226 // Prepare a vector to hold the nonce and ciphertext
229- let mut encrypted_data = Vec :: with_capacity ( NONCE_LEN + ciphertext. len ( ) ) ;
227+ let mut encrypted_data = Vec :: with_capacity ( nonce . len ( ) + ciphertext. len ( ) ) ;
230228 encrypted_data. extend_from_slice ( nonce. as_slice ( ) ) ;
231229 encrypted_data. extend_from_slice ( & ciphertext) ;
232230
@@ -240,21 +238,18 @@ impl SecretKey {
240238 let encrypted = encrypted. as_ref ( ) ;
241239
242240 // Check if the length of decoded data is at least the length of the nonce
243- if encrypted. len ( ) <= NONCE_LEN {
241+ let nonce_len = <XChaCha20Poly1305 as AeadCore >:: NonceSize :: USIZE ;
242+ if encrypted. len ( ) <= nonce_len {
244243 return Err ( Error :: EncryptedDataLengthError ) ;
245244 }
246245
247246 // Split the decoded data into nonce and ciphertext
248- let ( nonce, ciphertext) = encrypted. split_at ( NONCE_LEN ) ;
247+ let ( nonce, ciphertext) = encrypted. split_at ( nonce_len ) ;
249248 let nonce = XNonce :: from_slice ( nonce) ;
250249
251- // Convert the encryption key to a fixed-length array
252- let key: [ u8 ; KEY_LEN ] = self . key
253- . encryption ( )
254- . try_into ( )
255- . map_err ( |_| Error :: KeyLengthError ) ?;
256-
257- let cipher = XChaCha20Poly1305 :: new ( GenericArray :: from_slice ( & key) ) ;
250+ let ( mut prk, hk) = Hkdf :: < Sha256 > :: extract ( Some ( & nonce) , self . key . encryption ( ) ) ;
251+ hk. expand ( INFO_STRING , & mut prk) . map_err ( |_| Error :: KeyLengthError ) ?;
252+ let cipher = XChaCha20Poly1305 :: new ( & prk) ;
258253
259254 // Decrypt the ciphertext using the nonce
260255 let decrypted = cipher. decrypt ( nonce, ciphertext)
0 commit comments