From a54bd0d7fbe7ea3543ff36eafbfbbb1b309aaccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 18 Nov 2024 05:56:45 +0300 Subject: [PATCH] Add `Buffer`-based methods to `DynAead` --- aead/src/dyn_aead.rs | 42 +++++++++++++++++++++++++++++++++++++++++- aead/src/lib.rs | 4 ++-- 2 files changed, 43 insertions(+), 3 deletions(-) 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>,