3232//! This crate has an optional `alloc` feature which can be disabled in e.g.
3333//! microcontroller environments that don't have a heap.
3434//!
35- //! The [`AeadInPlace ::encrypt_in_place`] and [`AeadInPlace ::decrypt_in_place`]
35+ //! The [`AeadInOut ::encrypt_in_place`] and [`AeadInOut ::decrypt_in_place`]
3636//! methods accept any type that impls the [`aead::Buffer`] trait which
3737//! contains the plaintext for encryption or ciphertext for decryption.
3838//!
4646//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
4747//! # #[cfg(all(feature = "os_rng", feature = "heapless"))] {
4848//! use belt_dwp::{
49- //! aead::{AeadInPlace, AeadInPlaceDetached , KeyInit, heapless::Vec},
49+ //! aead::{AeadInOut , KeyInit, heapless::Vec},
5050//! Nonce, BeltDwp
5151//! };
5252//!
7373//! [`aead::Buffer`] for `arrayvec::ArrayVec` (re-exported from the [`aead`] crate as
7474//! [`aead::arrayvec::ArrayVec`]).
7575
76- pub use aead:: { self , AeadCore , AeadInPlace , Error , Key , KeyInit , KeySizeUser } ;
76+ pub use aead:: { self , AeadCore , AeadInOut , Error , Key , KeyInit , KeySizeUser } ;
7777pub use belt_block:: BeltBlock ;
7878
7979use aead:: consts:: { U8 , U16 } ;
80- use aead:: { AeadInPlaceDetached , PostfixTagged } ;
80+ use aead:: { TagPosition , inout :: InOutBuf } ;
8181use belt_block:: cipher:: crypto_common:: InnerUser ;
8282use belt_block:: cipher:: { Block , BlockCipherEncrypt , StreamCipher } ;
8383use belt_ctr:: cipher:: InnerIvInit ;
@@ -128,15 +128,15 @@ where
128128 }
129129}
130130
131- impl < C > AeadInPlaceDetached for Dwp < C >
131+ impl < C > AeadInOut for Dwp < C >
132132where
133133 C : BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 > ,
134134{
135- fn encrypt_in_place_detached (
135+ fn encrypt_inout_detached (
136136 & self ,
137137 nonce : & Nonce ,
138138 associated_data : & [ u8 ] ,
139- buffer : & mut [ u8 ] ,
139+ mut buffer : InOutBuf < ' _ , ' _ , u8 > ,
140140 ) -> aead:: Result < Tag > {
141141 let sizes_block = get_sizes_block ( associated_data. len ( ) , buffer. len ( ) ) ;
142142
@@ -165,8 +165,8 @@ where
165165 // 4.2 𝑌𝑖 ← 𝑋𝑖 ⊕ Lo(belt-block(𝑠, 𝐾), |𝑋𝑖|)
166166 // 4.3 𝑡 ← 𝑡 ⊕ (𝑌𝑖 ‖ 0^{128−|𝑌𝑖|})
167167 // 4.4 𝑡 ← 𝑡 * 𝑟.
168- enc_cipher. apply_keystream ( buffer) ;
169- ghash. update_padded ( buffer) ;
168+ enc_cipher. apply_keystream_inout ( buffer. reborrow ( ) ) ;
169+ ghash. update_padded ( buffer. get_out ( ) ) ;
170170
171171 // 5. 𝑡 ← 𝑡 ⊕ (⟨|𝐼|⟩_64 ‖ ⟨|𝑋|⟩_64)
172172 ghash. xor_s ( & sizes_block) ;
@@ -178,11 +178,11 @@ where
178178 Ok ( Tag :: try_from ( & tag[ ..8 ] ) . expect ( "Tag is always 8 bytes" ) )
179179 }
180180
181- fn decrypt_in_place_detached (
181+ fn decrypt_inout_detached (
182182 & self ,
183183 nonce : & Nonce ,
184184 associated_data : & [ u8 ] ,
185- buffer : & mut [ u8 ] ,
185+ buffer : InOutBuf < ' _ , ' _ , u8 > ,
186186 tag : & Tag ,
187187 ) -> aead:: Result < ( ) > {
188188 let sizes_block = get_sizes_block ( associated_data. len ( ) , buffer. len ( ) ) ;
@@ -206,7 +206,7 @@ where
206206 // 4. For 𝑖 = 1, 2, . . . , 𝑛 do:
207207 // 4.1 𝑡 ← 𝑡 ⊕ (𝑌𝑖 ‖ 0^{128−|𝑌𝑖|})
208208 // 4.2 𝑡 ← 𝑡 * 𝑟.
209- ghash. update_padded ( buffer) ;
209+ ghash. update_padded ( buffer. get_in ( ) ) ;
210210
211211 // 5. 𝑡 ← 𝑡 ⊕ (⟨|𝐼|⟩_64 ‖ ⟨|𝑋|⟩_64)
212212 ghash. xor_s ( & sizes_block) ;
@@ -223,22 +223,21 @@ where
223223 // 8.2. 𝑋𝑖 ← 𝑌𝑖 ⊕ Lo(belt-block(𝑠, 𝐾), |𝑌𝑖|)
224224 let core = BeltCtrCore :: inner_iv_init ( & self . cipher , nonce) ;
225225 let mut enc_cipher = BeltCtr :: from_core ( core) ;
226- enc_cipher. apply_keystream ( buffer) ;
226+ enc_cipher. apply_keystream_inout ( buffer) ;
227227 Ok ( ( ) )
228228 } else {
229229 Err ( Error )
230230 }
231231 }
232232}
233233
234- impl < C > PostfixTagged for Dwp < C > where C : BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 > { }
235-
236234impl < C > AeadCore for Dwp < C >
237235where
238236 C : BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 > ,
239237{
240238 type NonceSize = C :: BlockSize ;
241239 type TagSize = U8 ;
240+ const TAG_POSITION : TagPosition = TagPosition :: Postfix ;
242241}
243242
244243/// Get the sizes block for the GHASH
0 commit comments