3434//! This crate has an optional `alloc` feature which can be disabled in e.g.
3535//! microcontroller environments that don't have a heap.
3636//!
37- //! The [`AeadInPlace ::encrypt_in_place`] and [`AeadInPlace ::decrypt_in_place`]
37+ //! The [`AeadInOut ::encrypt_in_place`] and [`AeadInOut ::decrypt_in_place`]
3838//! methods accept any type that impls the [`aead::Buffer`] trait which
3939//! contains the plaintext for encryption or ciphertext for decryption.
4040//!
4848#![ cfg_attr( not( all( feature = "os_rng" , feature = "heapless" ) ) , doc = "```ignore" ) ]
4949//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
5050//! use aes_gcm_siv::{
51- //! aead::{AeadInPlace , KeyInit, rand_core::OsRng, heapless::Vec},
51+ //! aead::{AeadInOut , KeyInit, rand_core::OsRng, heapless::Vec},
5252//! Aes256GcmSiv, Nonce, // Or `Aes128GcmSiv`
5353//! };
5454//!
7878//! provide an impl of [`aead::Buffer`] for `bytes::BytesMut` (re-exported from the
7979//! [`aead`] crate as [`aead::bytes::BytesMut`]).
8080
81- pub use aead:: { self , AeadCore , AeadInPlaceDetached , Error , Key , KeyInit , KeySizeUser } ;
81+ pub use aead:: { self , AeadCore , AeadInOut , Error , Key , KeyInit , KeySizeUser } ;
8282
8383#[ cfg( feature = "aes" ) ]
8484pub use aes;
8585
86- use aead:: PostfixTagged ;
86+ use aead:: { TagPosition , inout :: InOutBuf } ;
8787use cipher:: {
8888 BlockCipherEncrypt , BlockSizeUser , InnerIvInit , StreamCipherCore ,
8989 array:: Array ,
@@ -161,32 +161,31 @@ where
161161{
162162 type NonceSize = U12 ;
163163 type TagSize = U16 ;
164+ const TAG_POSITION : TagPosition = TagPosition :: Postfix ;
164165}
165166
166- impl < Aes > PostfixTagged for AesGcmSiv < Aes > { }
167-
168- impl < Aes > AeadInPlaceDetached for AesGcmSiv < Aes >
167+ impl < Aes > AeadInOut for AesGcmSiv < Aes >
169168where
170169 Aes : BlockSizeUser < BlockSize = U16 > + BlockCipherEncrypt + KeyInit ,
171170{
172- fn encrypt_in_place_detached (
171+ fn encrypt_inout_detached (
173172 & self ,
174173 nonce : & Nonce ,
175174 associated_data : & [ u8 ] ,
176- buffer : & mut [ u8 ] ,
175+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
177176 ) -> Result < Tag , Error > {
178177 Cipher :: < Aes > :: new ( & self . key_generating_key , nonce)
179- . encrypt_in_place_detached ( associated_data, buffer)
178+ . encrypt_inout_detached ( associated_data, buffer)
180179 }
181180
182- fn decrypt_in_place_detached (
181+ fn decrypt_inout_detached (
183182 & self ,
184183 nonce : & Nonce ,
185184 associated_data : & [ u8 ] ,
186- buffer : & mut [ u8 ] ,
185+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
187186 tag : & Tag ,
188187 ) -> Result < ( ) , Error > {
189- Cipher :: < Aes > :: new ( & self . key_generating_key , nonce) . decrypt_in_place_detached (
188+ Cipher :: < Aes > :: new ( & self . key_generating_key , nonce) . decrypt_inout_detached (
190189 associated_data,
191190 buffer,
192191 tag,
@@ -268,30 +267,30 @@ where
268267 }
269268
270269 /// Encrypt the given message in-place, returning the authentication tag.
271- pub ( crate ) fn encrypt_in_place_detached (
270+ pub ( crate ) fn encrypt_inout_detached (
272271 mut self ,
273272 associated_data : & [ u8 ] ,
274- buffer : & mut [ u8 ] ,
273+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
275274 ) -> Result < Tag , Error > {
276275 if buffer. len ( ) as u64 > P_MAX || associated_data. len ( ) as u64 > A_MAX {
277276 return Err ( Error ) ;
278277 }
279278
280279 self . polyval . update_padded ( associated_data) ;
281- self . polyval . update_padded ( buffer) ;
280+ self . polyval . update_padded ( buffer. get_in ( ) ) ;
282281
283282 let tag = self . finish_tag ( associated_data. len ( ) , buffer. len ( ) ) ;
284- init_ctr ( & self . enc_cipher , & tag) . apply_keystream_partial ( buffer. into ( ) ) ;
283+ init_ctr ( & self . enc_cipher , & tag) . apply_keystream_partial ( buffer) ;
285284
286285 Ok ( tag)
287286 }
288287
289288 /// Decrypt the given message, first authenticating ciphertext integrity
290289 /// and returning an error if it's been tampered with.
291- pub ( crate ) fn decrypt_in_place_detached (
290+ pub ( crate ) fn decrypt_inout_detached (
292291 mut self ,
293292 associated_data : & [ u8 ] ,
294- buffer : & mut [ u8 ] ,
293+ mut buffer : InOutBuf < ' _ , ' _ , u8 > ,
295294 tag : & Tag ,
296295 ) -> Result < ( ) , Error > {
297296 if buffer. len ( ) as u64 > C_MAX || associated_data. len ( ) as u64 > A_MAX {
@@ -301,8 +300,8 @@ where
301300 self . polyval . update_padded ( associated_data) ;
302301
303302 // TODO(tarcieri): interleave decryption and authentication
304- init_ctr ( & self . enc_cipher , tag) . apply_keystream_partial ( buffer. into ( ) ) ;
305- self . polyval . update_padded ( buffer) ;
303+ init_ctr ( & self . enc_cipher , tag) . apply_keystream_partial ( buffer. reborrow ( ) ) ;
304+ self . polyval . update_padded ( buffer. get_out ( ) ) ;
306305
307306 let expected_tag = self . finish_tag ( associated_data. len ( ) , buffer. len ( ) ) ;
308307
@@ -312,7 +311,7 @@ where
312311 } else {
313312 // On MAC verify failure, re-encrypt the plaintext buffer to
314313 // prevent accidental exposure.
315- init_ctr ( & self . enc_cipher , tag) . apply_keystream_partial ( buffer. into ( ) ) ;
314+ init_ctr ( & self . enc_cipher , tag) . apply_keystream_partial ( buffer) ;
316315 Err ( Error )
317316 }
318317 }
0 commit comments