Skip to content

Commit af2c258

Browse files
committed
[WIP] 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 a1ade1b commit af2c258

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

aead/src/lib.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,7 @@ pub trait AeadInPlace: AeadCore {
276276
nonce: &Nonce<Self>,
277277
associated_data: &[u8],
278278
buffer: &mut dyn Buffer,
279-
) -> Result<()> {
280-
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
281-
buffer.extend_from_slice(tag.as_slice())?;
282-
Ok(())
283-
}
284-
285-
/// Encrypt the data in-place, returning the authentication tag
286-
fn encrypt_in_place_detached(
287-
&self,
288-
nonce: &Nonce<Self>,
289-
associated_data: &[u8],
290-
buffer: &mut [u8],
291-
) -> Result<Tag<Self>>;
279+
) -> Result<()>;
292280

293281
/// Decrypt the message in-place, returning an error in the event the
294282
/// provided authentication tag does not match the given ciphertext.
@@ -300,9 +288,17 @@ pub trait AeadInPlace: AeadCore {
300288
nonce: &Nonce<Self>,
301289
associated_data: &[u8],
302290
buffer: &mut dyn Buffer,
303-
) -> Result<()> {
304-
impl_decrypt_in_place!(self, nonce, associated_data, buffer)
305-
}
291+
) -> Result<()>;
292+
}
293+
294+
pub trait AeadInPlaceDetached: AeadCore {
295+
/// Encrypt the data in-place, returning the authentication tag.
296+
fn encrypt_in_place_detached(
297+
&self,
298+
nonce: &Nonce<Self>,
299+
associated_data: &[u8],
300+
buffer: &mut [u8],
301+
) -> Result<Tag<Self>>;
306302

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

315+
pub trait PostfixTagged {}
316+
317+
impl<T: AeadInPlaceDetached + PostfixTagged> AeadInPlace for T {
318+
fn encrypt_in_place(
319+
&self,
320+
nonce: &Nonce<Self>,
321+
associated_data: &[u8],
322+
buffer: &mut dyn Buffer,
323+
) -> Result<()> {
324+
let tag = self.encrypt_in_place_detached(nonce, associated_data, buffer.as_mut())?;
325+
buffer.extend_from_slice(tag.as_slice())?;
326+
Ok(())
327+
}
328+
329+
fn decrypt_in_place(
330+
&self,
331+
nonce: &Nonce<Self>,
332+
associated_data: &[u8],
333+
buffer: &mut dyn Buffer,
334+
) -> Result<()> {
335+
impl_decrypt_in_place!(self, nonce, associated_data, buffer)
336+
}
337+
}
338+
319339
/// In-place stateful AEAD trait.
320340
///
321341
/// This trait is both object safe and has no dependencies on `alloc` or `std`.

0 commit comments

Comments
 (0)