@@ -206,7 +206,7 @@ pub trait Aead: AeadCore {
206
206
) -> Result < Vec < u8 > > ;
207
207
}
208
208
209
- /// In-place stateless AEAD trait.
209
+ /// In-place AEAD trait.
210
210
///
211
211
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
212
212
pub trait AeadInPlace : AeadCore {
@@ -224,25 +224,61 @@ pub trait AeadInPlace: AeadCore {
224
224
nonce : & Nonce < Self > ,
225
225
associated_data : & [ u8 ] ,
226
226
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
+ }
232
241
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.
234
245
fn encrypt_in_place_detached (
235
246
& self ,
236
247
nonce : & Nonce < Self > ,
237
248
associated_data : & [ u8 ] ,
238
249
buffer : & mut [ u8 ] ,
239
250
) -> Result < Tag < Self > > ;
240
251
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
+
246
282
fn decrypt_in_place (
247
283
& self ,
248
284
nonce : & Nonce < Self > ,
@@ -261,17 +297,6 @@ pub trait AeadInPlace: AeadCore {
261
297
buffer. truncate ( tag_pos) ;
262
298
Ok ( ( ) )
263
299
}
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 < ( ) > ;
275
300
}
276
301
277
302
#[ cfg( feature = "alloc" ) ]
0 commit comments