@@ -30,21 +30,26 @@ pub use crypto_common::{
30
30
pub use arrayvec;
31
31
#[ cfg( feature = "bytes" ) ]
32
32
pub use bytes;
33
- #[ cfg( feature = "heapless" ) ]
34
- pub use heapless;
35
-
36
33
#[ cfg( feature = "rand_core" ) ]
37
34
pub use crypto_common:: rand_core;
35
+ #[ cfg( feature = "heapless" ) ]
36
+ pub use heapless;
37
+ #[ cfg( feature = "inout" ) ]
38
+ pub use inout;
38
39
39
40
use core:: fmt;
40
- use crypto_common:: array:: { Array , ArraySize , typenum :: Unsigned } ;
41
+ use crypto_common:: array:: { Array , ArraySize } ;
41
42
42
43
#[ cfg( feature = "alloc" ) ]
43
44
use alloc:: vec:: Vec ;
44
45
#[ cfg( feature = "bytes" ) ]
45
46
use bytes:: BytesMut ;
47
+ #[ cfg( any( feature = "alloc" , feature = "inout" ) ) ]
48
+ use crypto_common:: array:: typenum:: Unsigned ;
46
49
#[ cfg( feature = "os_rng" ) ]
47
50
use crypto_common:: rand_core:: { OsError , OsRng , TryRngCore } ;
51
+ #[ cfg( feature = "inout" ) ]
52
+ use inout:: InOutBuf ;
48
53
#[ cfg( feature = "rand_core" ) ]
49
54
use rand_core:: { CryptoRng , TryCryptoRng } ;
50
55
@@ -240,23 +245,24 @@ pub trait AeadInPlace: AeadCore {
240
245
}
241
246
242
247
/// 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.
245
- fn encrypt_in_place_detached (
248
+ #[ cfg( feature = "inout" ) ]
249
+ pub trait AeadInOut : AeadCore {
250
+ /// Encrypt the data in the provided [`InOutBuf`], returning the authentication tag.
251
+ fn encrypt_inout_detached (
246
252
& self ,
247
253
nonce : & Nonce < Self > ,
248
254
associated_data : & [ u8 ] ,
249
- buffer : & mut [ u8 ] ,
255
+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
250
256
) -> Result < Tag < Self > > ;
251
257
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
258
+ /// Decrypt the data in the provided [`InOutBuf`] , returning an error in the event the
259
+ /// provided authentication tag is invalid for the given ciphertext (i.e. ciphertext
254
260
/// is modified/unauthentic)
255
- fn decrypt_in_place_detached (
261
+ fn decrypt_inout_detached (
256
262
& self ,
257
263
nonce : & Nonce < Self > ,
258
264
associated_data : & [ u8 ] ,
259
- buffer : & mut [ u8 ] ,
265
+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
260
266
tag : & Tag < Self > ,
261
267
) -> Result < ( ) > ;
262
268
}
@@ -267,14 +273,41 @@ pub trait AeadInPlaceDetached: AeadCore {
267
273
/// This is the common convention for AEAD algorithms.
268
274
pub trait PostfixTagged { }
269
275
270
- impl < T : AeadInPlaceDetached + PostfixTagged > AeadInPlace for T {
276
+ #[ cfg( feature = "alloc" ) ]
277
+ impl < Alg : AeadInPlace > Aead for Alg {
278
+ fn encrypt < ' msg , ' aad > (
279
+ & self ,
280
+ nonce : & Nonce < Self > ,
281
+ plaintext : impl Into < Payload < ' msg , ' aad > > ,
282
+ ) -> Result < Vec < u8 > > {
283
+ let payload = plaintext. into ( ) ;
284
+ let mut buffer = Vec :: with_capacity ( payload. msg . len ( ) + Self :: TagSize :: to_usize ( ) ) ;
285
+ buffer. extend_from_slice ( payload. msg ) ;
286
+ self . encrypt_in_place ( nonce, payload. aad , & mut buffer) ?;
287
+ Ok ( buffer)
288
+ }
289
+
290
+ fn decrypt < ' msg , ' aad > (
291
+ & self ,
292
+ nonce : & Nonce < Self > ,
293
+ ciphertext : impl Into < Payload < ' msg , ' aad > > ,
294
+ ) -> Result < Vec < u8 > > {
295
+ let payload = ciphertext. into ( ) ;
296
+ let mut buffer = Vec :: from ( payload. msg ) ;
297
+ self . decrypt_in_place ( nonce, payload. aad , & mut buffer) ?;
298
+ Ok ( buffer)
299
+ }
300
+ }
301
+
302
+ #[ cfg( feature = "inout" ) ]
303
+ impl < T : AeadInOut + PostfixTagged > AeadInPlace for T {
271
304
fn encrypt_in_place (
272
305
& self ,
273
306
nonce : & Nonce < Self > ,
274
307
associated_data : & [ u8 ] ,
275
308
buffer : & mut dyn Buffer ,
276
309
) -> Result < ( ) > {
277
- let tag = self . encrypt_in_place_detached ( nonce, associated_data, buffer. as_mut ( ) ) ?;
310
+ let tag = self . encrypt_inout_detached ( nonce, associated_data, buffer. as_mut ( ) . into ( ) ) ?;
278
311
buffer. extend_from_slice ( tag. as_slice ( ) ) ?;
279
312
Ok ( ( ) )
280
313
}
@@ -293,38 +326,12 @@ impl<T: AeadInPlaceDetached + PostfixTagged> AeadInPlace for T {
293
326
let ( msg, tag) = buffer. as_mut ( ) . split_at_mut ( tag_pos) ;
294
327
let tag = Tag :: < Self > :: try_from ( & * tag) . expect ( "tag length mismatch" ) ;
295
328
296
- self . decrypt_in_place_detached ( nonce, associated_data, msg, & tag) ?;
329
+ self . decrypt_inout_detached ( nonce, associated_data, msg. into ( ) , & tag) ?;
297
330
buffer. truncate ( tag_pos) ;
298
331
Ok ( ( ) )
299
332
}
300
333
}
301
334
302
- #[ cfg( feature = "alloc" ) ]
303
- impl < Alg : AeadInPlace > Aead for Alg {
304
- fn encrypt < ' msg , ' aad > (
305
- & self ,
306
- nonce : & Nonce < Self > ,
307
- plaintext : impl Into < Payload < ' msg , ' aad > > ,
308
- ) -> Result < Vec < u8 > > {
309
- let payload = plaintext. into ( ) ;
310
- let mut buffer = Vec :: with_capacity ( payload. msg . len ( ) + Self :: TagSize :: to_usize ( ) ) ;
311
- buffer. extend_from_slice ( payload. msg ) ;
312
- self . encrypt_in_place ( nonce, payload. aad , & mut buffer) ?;
313
- Ok ( buffer)
314
- }
315
-
316
- fn decrypt < ' msg , ' aad > (
317
- & self ,
318
- nonce : & Nonce < Self > ,
319
- ciphertext : impl Into < Payload < ' msg , ' aad > > ,
320
- ) -> Result < Vec < u8 > > {
321
- let payload = ciphertext. into ( ) ;
322
- let mut buffer = Vec :: from ( payload. msg ) ;
323
- self . decrypt_in_place ( nonce, payload. aad , & mut buffer) ?;
324
- Ok ( buffer)
325
- }
326
- }
327
-
328
335
/// AEAD payloads (message + AAD).
329
336
///
330
337
/// Combination of a message (plaintext or ciphertext) and
0 commit comments