Skip to content

Commit

Permalink
[WIP] aead: factor apart AeadInPlace/*Detached
Browse files Browse the repository at this point in the history
Factors apart the detached methods of `AeadInPlace` into a separate
`AeadInPlaceDetached` trait, which itself can now more easily be further
refactored (by adding e.g. `inout` support).

Also adds a `PostfixTagged` trait which is used to gate the blanket
impls.
  • Loading branch information
tarcieri committed Nov 16, 2024
1 parent a1ade1b commit af2c258
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,7 @@ pub trait AeadInPlace: AeadCore {
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<()> {
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
buffer.extend_from_slice(tag.as_slice())?;
Ok(())
}

/// Encrypt the data in-place, returning the authentication tag
fn encrypt_in_place_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag<Self>>;
) -> Result<()>;

/// Decrypt the message in-place, returning an error in the event the
/// provided authentication tag does not match the given ciphertext.
Expand All @@ -300,9 +288,17 @@ pub trait AeadInPlace: AeadCore {
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<()> {
impl_decrypt_in_place!(self, nonce, associated_data, buffer)
}
) -> Result<()>;
}

pub trait AeadInPlaceDetached: AeadCore {
/// Encrypt the data in-place, returning the authentication tag.
fn encrypt_in_place_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag<Self>>;

/// Decrypt the message in-place, returning an error in the event the provided
/// authentication tag does not match the given ciphertext (i.e. ciphertext
Expand All @@ -316,6 +312,30 @@ pub trait AeadInPlace: AeadCore {
) -> Result<()>;
}

pub trait PostfixTagged {}

impl<T: AeadInPlaceDetached + PostfixTagged> AeadInPlace for T {
fn encrypt_in_place(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<()> {
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
buffer.extend_from_slice(tag.as_slice())?;
Ok(())
}

fn decrypt_in_place(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<()> {
impl_decrypt_in_place!(self, nonce, associated_data, buffer)
}
}

/// In-place stateful AEAD trait.
///
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
Expand Down

0 comments on commit af2c258

Please sign in to comment.