Skip to content

Commit

Permalink
aead: factor apart AeadInPlace/*Detached (#1714)
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 authored Feb 28, 2025
1 parent 3fa125f commit d39ad30
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub trait Aead: AeadCore {
) -> Result<Vec<u8>>;
}

/// In-place stateless AEAD trait.
/// In-place AEAD trait.
///
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
pub trait AeadInPlace: AeadCore {
Expand All @@ -224,25 +224,61 @@ 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(())
}
) -> Result<()>;

/// Decrypt the message in-place, returning an error in the event the
/// provided authentication tag does not match the given ciphertext.
///
/// The buffer will be truncated to the length of the original plaintext
/// message upon success.
fn decrypt_in_place(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<()>;
}

/// Encrypt the data in-place, returning the authentication tag
/// In-place AEAD trait which handles the authentication tag as a return value/separate parameter.
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.
///
/// The buffer will be truncated to the length of the original plaintext
/// message upon success.
/// Decrypt the message in-place, returning an error in the event the provided
/// authentication tag does not match the given ciphertext (i.e. ciphertext
/// is modified/unauthentic)
fn decrypt_in_place_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut [u8],
tag: &Tag<Self>,
) -> Result<()>;
}

/// Marker trait for AEAD algorithms which append the authentication tag to the end of the
/// ciphertext message.
///
/// This is the common convention for AEAD algorithms.
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>,
Expand All @@ -261,17 +297,6 @@ pub trait AeadInPlace: AeadCore {
buffer.truncate(tag_pos);
Ok(())
}

/// Decrypt the message in-place, returning an error in the event the provided
/// authentication tag does not match the given ciphertext (i.e. ciphertext
/// is modified/unauthentic)
fn decrypt_in_place_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: &mut [u8],
tag: &Tag<Self>,
) -> Result<()>;
}

#[cfg(feature = "alloc")]
Expand Down

0 comments on commit d39ad30

Please sign in to comment.