diff --git a/aead/src/dyn_aead.rs b/aead/src/dyn_aead.rs index a18d38ed..88e458df 100644 --- a/aead/src/dyn_aead.rs +++ b/aead/src/dyn_aead.rs @@ -1,6 +1,6 @@ use inout::{InOutBuf, InOutBufReserved}; -use crate::{Aead, Error, Result}; +use crate::{Aead, Buffer, Error, Result}; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -55,6 +55,22 @@ pub trait DynAead: sealed::Sealed { buffer: &'out mut [u8], ) -> Result<&'out mut [u8]>; + fn encrypt_to_buffer( + &self, + nonce: &[u8], + associated_data: &[u8], + plaintext: &[u8], + buffer: &mut dyn Buffer, + ) -> Result<()>; + + fn decrypt_to_buffer( + &self, + nonce: &[u8], + associated_data: &[u8], + ciphertext: &[u8], + buffer: &mut dyn Buffer, + ) -> Result<()>; + #[cfg(feature = "alloc")] fn encrypt_to_vec( &self, @@ -138,6 +154,30 @@ impl DynAead for T { Aead::postfix_decrypt_to_buf(self, nonce, associated_data, ciphertext, buffer) } + fn encrypt_to_buffer( + &self, + nonce: &[u8], + aad: &[u8], + msg: &[u8], + buffer: &mut dyn Buffer, + ) -> Result<()> { + let nonce = nonce.try_into().map_err(|_| Error)?; + let payload = crate::Payload { aad, msg }; + Aead::encrypt_to_buffer(self, nonce, payload, buffer) + } + + fn decrypt_to_buffer( + &self, + nonce: &[u8], + aad: &[u8], + msg: &[u8], + buffer: &mut dyn Buffer, + ) -> Result<()> { + let nonce = nonce.try_into().map_err(|_| Error)?; + let payload = crate::Payload { aad, msg }; + Aead::decrypt_to_buffer(self, nonce, payload, buffer) + } + #[cfg(feature = "alloc")] fn encrypt_to_vec(&self, nonce: &[u8], aad: &[u8], msg: &[u8]) -> Result> { let nonce = nonce.try_into().map_err(|_| Error)?; diff --git a/aead/src/lib.rs b/aead/src/lib.rs index 5f10f63d..8a8b38f0 100644 --- a/aead/src/lib.rs +++ b/aead/src/lib.rs @@ -354,7 +354,7 @@ pub trait Aead { } #[inline] - fn encrypt_to_buffer<'msg, 'aad, B: Buffer>( + fn encrypt_to_buffer<'msg, 'aad, B: Buffer + ?Sized>( &self, nonce: &Nonce, pt_payload: impl Into>, @@ -374,7 +374,7 @@ pub trait Aead { } #[inline] - fn decrypt_to_buffer<'msg, 'aad, B: Buffer>( + fn decrypt_to_buffer<'msg, 'aad, B: Buffer + ?Sized>( &self, nonce: &Nonce, ct_payload: impl Into>,