Skip to content

Commit

Permalink
Add Buffer-based methods to DynAead
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 18, 2024
1 parent 10de5f7 commit a54bd0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
42 changes: 41 additions & 1 deletion aead/src/dyn_aead.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -138,6 +154,30 @@ impl<T: Aead> 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<Vec<u8>> {
let nonce = nonce.try_into().map_err(|_| Error)?;
Expand Down
4 changes: 2 additions & 2 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>,
pt_payload: impl Into<Payload<'msg, 'aad>>,
Expand All @@ -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<Self>,
ct_payload: impl Into<Payload<'msg, 'aad>>,
Expand Down

0 comments on commit a54bd0d

Please sign in to comment.