@@ -144,19 +144,28 @@ impl HpkeCrypto for HpkeLibcrux {
144144 aad : & [ u8 ] ,
145145 msg : & [ u8 ] ,
146146 ) -> Result < Vec < u8 > , Error > {
147- // only chacha20poly1305 is supported
148- if !matches ! ( alg, AeadAlgorithm :: ChaCha20Poly1305 ) {
149- return Err ( Error :: UnknownAeadAlgorithm ) ;
150- }
147+ let alg = aead_alg ( alg) ?;
148+
149+ use libcrux_traits:: aead:: typed_refs:: Aead as _;
151150
152- let iv = <& [ u8 ; 12 ] >:: try_from ( nonce) . map_err ( |_| Error :: AeadInvalidNonce ) ?;
151+ // set up buffer for ctxt and tag
152+ let mut msg_ctx: Vec < u8 > = alloc:: vec![ 0 ; msg. len( ) + alg. tag_len( ) ] ;
153+ let ( ctxt, tag) = msg_ctx. split_at_mut ( msg. len ( ) ) ;
153154
154- // TODO: instead, use key conversion from the libcrux-chacha20poly1305 crate, when available,
155- let key = <& [ u8 ; 32 ] >:: try_from ( key)
155+ // set up nonce
156+ let nonce = alg. new_nonce ( nonce) . map_err ( |_| Error :: AeadInvalidNonce ) ?;
157+
158+ // set up key
159+ let key = alg
160+ . new_key ( key)
156161 . map_err ( |_| Error :: CryptoLibraryError ( "AEAD invalid key length" . into ( ) ) ) ?;
157162
158- let mut msg_ctx: Vec < u8 > = alloc:: vec![ 0 ; msg. len( ) + 16 ] ;
159- libcrux_chacha20poly1305:: encrypt ( key, msg, & mut msg_ctx, aad, iv)
163+ // set up tag
164+ let tag = alg
165+ . new_tag_mut ( tag)
166+ . map_err ( |_| Error :: CryptoLibraryError ( "Invalid tag length" . into ( ) ) ) ?;
167+
168+ key. encrypt ( ctxt, tag, nonce, aad, msg)
160169 . map_err ( |_| Error :: CryptoLibraryError ( "Invalid configuration" . into ( ) ) ) ?;
161170
162171 Ok ( msg_ctx)
@@ -169,31 +178,40 @@ impl HpkeCrypto for HpkeLibcrux {
169178 aad : & [ u8 ] ,
170179 cipher_txt : & [ u8 ] ,
171180 ) -> Result < Vec < u8 > , Error > {
172- // only chacha20poly1305 is supported
173- if ! matches ! ( alg , AeadAlgorithm :: ChaCha20Poly1305 ) {
174- return Err ( Error :: UnknownAeadAlgorithm ) ;
175- }
176- if cipher_txt. len ( ) < 16 {
181+ let alg = aead_alg ( alg ) ? ;
182+
183+ use libcrux_traits :: aead :: typed_refs :: { Aead as _ , DecryptError } ;
184+
185+ if cipher_txt. len ( ) < alg . tag_len ( ) {
177186 return Err ( Error :: AeadInvalidCiphertext ) ;
178187 }
179188
180- let boundary = cipher_txt. len ( ) - 16 ;
189+ let boundary = cipher_txt. len ( ) - alg . tag_len ( ) ;
181190
191+ // set up buffers for ptext, ctext, and tag
182192 let mut ptext = alloc:: vec![ 0 ; boundary] ;
193+ let ( ctext, tag) = cipher_txt. split_at ( boundary) ;
183194
184- let iv = <& [ u8 ; 12 ] >:: try_from ( nonce) . map_err ( |_| Error :: AeadInvalidNonce ) ?;
195+ // set up nonce
196+ let nonce = alg. new_nonce ( nonce) . map_err ( |_| Error :: AeadInvalidNonce ) ?;
185197
186- // TODO: instead, use key conversion from the libcrux-chacha20poly1305 crate, when available,
187- let key = <& [ u8 ; 32 ] >:: try_from ( key)
198+ // set up key
199+ let key = alg
200+ . new_key ( key)
188201 . map_err ( |_| Error :: CryptoLibraryError ( "AEAD invalid key length" . into ( ) ) ) ?;
189- libcrux_chacha20poly1305:: decrypt ( key, & mut ptext, cipher_txt, aad, iv) . map_err (
190- |e| match e {
191- libcrux_chacha20poly1305:: AeadError :: InvalidCiphertext => {
202+
203+ // set up tag
204+ let tag = alg
205+ . new_tag ( tag)
206+ . map_err ( |_| Error :: CryptoLibraryError ( "Invalid tag length" . into ( ) ) ) ?;
207+
208+ key. decrypt ( & mut ptext, nonce, aad, ctext, tag)
209+ . map_err ( |e| match e {
210+ DecryptError :: InvalidTag => {
192211 Error :: CryptoLibraryError ( format ! ( "AEAD decryption error: {:?}" , e) )
193212 }
194213 _ => Error :: CryptoLibraryError ( "Invalid configuration" . into ( ) ) ,
195- } ,
196- ) ?;
214+ } ) ?;
197215
198216 Ok ( ptext)
199217 }
@@ -237,8 +255,7 @@ impl HpkeCrypto for HpkeLibcrux {
237255 /// Returns an error if the AEAD algorithm is not supported by this crypto provider.
238256 fn supports_aead ( alg : AeadAlgorithm ) -> Result < ( ) , Error > {
239257 match alg {
240- // Don't support Aes
241- AeadAlgorithm :: Aes128Gcm | AeadAlgorithm :: Aes256Gcm => Err ( Error :: UnknownAeadAlgorithm ) ,
258+ AeadAlgorithm :: Aes128Gcm | AeadAlgorithm :: Aes256Gcm => Ok ( ( ) ) ,
242259 AeadAlgorithm :: ChaCha20Poly1305 => Ok ( ( ) ) ,
243260 AeadAlgorithm :: HpkeExport => Ok ( ( ) ) ,
244261 }
@@ -295,6 +312,16 @@ fn kem_key_type_to_ecdh_alg(alg: KemAlgorithm) -> Result<libcrux_ecdh::Algorithm
295312 }
296313}
297314
315+ #[ inline( always) ]
316+ fn aead_alg ( alg_type : AeadAlgorithm ) -> Result < libcrux_aead:: Aead , Error > {
317+ match alg_type {
318+ AeadAlgorithm :: ChaCha20Poly1305 => Ok ( libcrux_aead:: Aead :: ChaCha20Poly1305 ) ,
319+ AeadAlgorithm :: Aes128Gcm => Ok ( libcrux_aead:: Aead :: AesGcm128 ) ,
320+ AeadAlgorithm :: Aes256Gcm => Ok ( libcrux_aead:: Aead :: AesGcm256 ) ,
321+ _ => Err ( Error :: UnknownAeadAlgorithm ) ,
322+ }
323+ }
324+
298325impl hpke_rs_crypto:: RngCore for HpkeLibcruxPrng {
299326 fn next_u32 ( & mut self ) -> u32 {
300327 self . rng . next_u32 ( )
0 commit comments