Skip to content

Commit d60f55e

Browse files
committed
aead: factor apart AeadInPlace/*Detached
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.
1 parent fbb4321 commit d60f55e

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

aead/src/lib.rs

+48-23
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub trait Aead: AeadCore {
206206
) -> Result<Vec<u8>>;
207207
}
208208

209-
/// In-place stateless AEAD trait.
209+
/// In-place AEAD trait.
210210
///
211211
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
212212
pub trait AeadInPlace: AeadCore {
@@ -224,25 +224,61 @@ pub trait AeadInPlace: AeadCore {
224224
nonce: &Nonce<Self>,
225225
associated_data: &[u8],
226226
buffer: &mut dyn Buffer,
227-
) -> Result<()> {
228-
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
229-
buffer.extend_from_slice(tag.as_slice())?;
230-
Ok(())
231-
}
227+
) -> Result<()>;
228+
229+
/// Decrypt the message in-place, returning an error in the event the
230+
/// provided authentication tag does not match the given ciphertext.
231+
///
232+
/// The buffer will be truncated to the length of the original plaintext
233+
/// message upon success.
234+
fn decrypt_in_place(
235+
&self,
236+
nonce: &Nonce<Self>,
237+
associated_data: &[u8],
238+
buffer: &mut dyn Buffer,
239+
) -> Result<()>;
240+
}
232241

233-
/// Encrypt the data in-place, returning the authentication tag
242+
/// In-place AEAD trait which handles the authentication tag as a return value/separate parameter.
243+
pub trait AeadInPlaceDetached: AeadCore {
244+
/// Encrypt the data in-place, returning the authentication tag.
234245
fn encrypt_in_place_detached(
235246
&self,
236247
nonce: &Nonce<Self>,
237248
associated_data: &[u8],
238249
buffer: &mut [u8],
239250
) -> Result<Tag<Self>>;
240251

241-
/// Decrypt the message in-place, returning an error in the event the
242-
/// provided authentication tag does not match the given ciphertext.
243-
///
244-
/// The buffer will be truncated to the length of the original plaintext
245-
/// message upon success.
252+
/// Decrypt the message in-place, returning an error in the event the provided
253+
/// authentication tag does not match the given ciphertext (i.e. ciphertext
254+
/// is modified/unauthentic)
255+
fn decrypt_in_place_detached(
256+
&self,
257+
nonce: &Nonce<Self>,
258+
associated_data: &[u8],
259+
buffer: &mut [u8],
260+
tag: &Tag<Self>,
261+
) -> Result<()>;
262+
}
263+
264+
/// Marker trait for AEAD algorithms which append the authentication tag to the end of the
265+
/// ciphertext message.
266+
///
267+
/// This is the common convention for AEAD algorithms.
268+
pub trait PostfixTagged {}
269+
270+
impl<T: AeadInPlaceDetached + PostfixTagged> AeadInPlace for T {
271+
fn encrypt_in_place(
272+
&self,
273+
nonce: &Nonce<Self>,
274+
associated_data: &[u8],
275+
buffer: &mut dyn Buffer,
276+
) -> Result<()> {
277+
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
278+
buffer.extend_from_slice(tag.as_slice())?;
279+
Ok(())
280+
}
281+
246282
fn decrypt_in_place(
247283
&self,
248284
nonce: &Nonce<Self>,
@@ -261,17 +297,6 @@ pub trait AeadInPlace: AeadCore {
261297
buffer.truncate(tag_pos);
262298
Ok(())
263299
}
264-
265-
/// Decrypt the message in-place, returning an error in the event the provided
266-
/// authentication tag does not match the given ciphertext (i.e. ciphertext
267-
/// is modified/unauthentic)
268-
fn decrypt_in_place_detached(
269-
&self,
270-
nonce: &Nonce<Self>,
271-
associated_data: &[u8],
272-
buffer: &mut [u8],
273-
tag: &Tag<Self>,
274-
) -> Result<()>;
275300
}
276301

277302
#[cfg(feature = "alloc")]

0 commit comments

Comments
 (0)