@@ -3,44 +3,41 @@ use hpke::{
33 aead:: { AeadTag , ChaCha20Poly1305 } ,
44 kdf:: HkdfSha256 ,
55 kem:: X25519HkdfSha256 ,
6- kex:: { Deserializable , KeyExchange } ,
7- setup_receiver, EncappedKey , HpkeError , Kem as KemTrait , OpModeR ,
6+ setup_receiver, Deserializable , HpkeError , Kem as KemTrait , OpModeR ,
87} ;
98use rand:: { rngs:: StdRng , SeedableRng } ;
109use serde:: { Deserialize , Serialize } ;
1110
1211type Kem = X25519HkdfSha256 ;
1312type Aead = ChaCha20Poly1305 ;
1413type Kdf = HkdfSha256 ;
15- type Kex = <Kem as KemTrait >:: Kex ;
14+
15+ type PrivateKey = <Kem as KemTrait >:: PrivateKey ;
16+ type PublicKey = <Kem as KemTrait >:: PublicKey ;
17+ type EncappedKey = <Kem as KemTrait >:: EncappedKey ;
1618
1719#[ derive( Serialize , Deserialize ) ]
1820pub struct EncryptedData {
19- encapped_key : EncappedKey < Kex > ,
21+ encapped_key : EncappedKey ,
2022 ciphertext : Vec < u8 > ,
2123 tag : AeadTag < Aead > ,
2224}
2325
2426// Generates a public-private key pair
25- pub fn generate_key_pair ( ) -> (
26- <Kex as KeyExchange >:: PrivateKey ,
27- <Kex as KeyExchange >:: PublicKey ,
28- ) {
27+ pub fn generate_key_pair ( ) -> ( PrivateKey , PublicKey ) {
2928 let mut csprng = StdRng :: from_entropy ( ) ;
3029 Kem :: gen_keypair ( & mut csprng)
3130}
3231
3332// Constructs a PrivateKey from an array of bytes
34- pub fn get_private_key_from_bytes (
35- private_key_bytes : & [ u8 ] ,
36- ) -> Result < <Kex as KeyExchange >:: PrivateKey , HpkeError > {
37- <Kex as KeyExchange >:: PrivateKey :: from_bytes ( private_key_bytes)
33+ pub fn get_private_key_from_bytes ( private_key_bytes : & [ u8 ] ) -> Result < PrivateKey , HpkeError > {
34+ PrivateKey :: from_bytes ( private_key_bytes)
3835}
3936
4037// Decrypts data with provided private key
4138pub fn decrypt_data (
4239 encrypted_data : & EncryptedData ,
43- private_key : & < Kex as KeyExchange > :: PrivateKey ,
40+ private_key : & PrivateKey ,
4441) -> Result < Vec < u8 > , HpkeError > {
4542 // Decapsulate and derive the shared secret. Create a shared AEAD context.
4643 let mut aead_ctx = setup_receiver :: < Aead , Kdf , Kem > (
@@ -52,7 +49,7 @@ pub fn decrypt_data(
5249
5350 // Decrypt ciphertext in place
5451 let mut ciphertext_copy = encrypted_data. ciphertext . clone ( ) ;
55- aead_ctx. open ( & mut ciphertext_copy, & [ ] , & encrypted_data. tag ) ?;
52+ aead_ctx. open_in_place_detached ( & mut ciphertext_copy, & [ ] , & encrypted_data. tag ) ?;
5653
5754 // Rename for clarity
5855 let plaintext = ciphertext_copy;
0 commit comments