Skip to content

Commit fbd9a45

Browse files
committed
finish inout migration
1 parent 60c669d commit fbd9a45

File tree

24 files changed

+119
-97
lines changed

24 files changed

+119
-97
lines changed

aes-gcm-siv/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ categories = ["cryptography", "no-std"]
1717
rust-version = "1.85"
1818

1919
[dependencies]
20-
aead = { version = "0.6.0-rc.0", default-features = false }
20+
aead = { version = "0.6.0-rc.0", default-features = false, features = ["inout"] }
2121
aes = { version = "=0.9.0-pre.3", optional = true }
2222
cipher = "=0.5.0-pre.8"
2323
ctr = "0.10.0-pre.2"

aes-gcm-siv/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser};
8383
#[cfg(feature = "aes")]
8484
pub use aes;
8585

86-
use aead::{inout::InOutBuf, PostfixTagged};
86+
use aead::{PostfixTagged, inout::InOutBuf};
8787
use cipher::{
88+
BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore,
8889
array::Array,
8990
consts::{U12, U16},
90-
BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore,
9191
};
92-
use polyval::{universal_hash::UniversalHash, Polyval};
92+
use polyval::{Polyval, universal_hash::UniversalHash};
9393

9494
/// AES is optional to allow swapping in hardware-specific backends.
9595
#[cfg(feature = "aes")]
@@ -278,10 +278,10 @@ where
278278
}
279279

280280
self.polyval.update_padded(associated_data);
281-
self.polyval.update_padded(buffer);
281+
self.polyval.update_padded(buffer.get_in());
282282

283283
let tag = self.finish_tag(associated_data.len(), buffer.len());
284-
init_ctr(&self.enc_cipher, &tag).apply_keystream_partial(buffer.into());
284+
init_ctr(&self.enc_cipher, &tag).apply_keystream_partial(buffer);
285285

286286
Ok(tag)
287287
}
@@ -291,7 +291,7 @@ where
291291
pub(crate) fn decrypt_inout_detached(
292292
mut self,
293293
associated_data: &[u8],
294-
buffer: InOutBuf<'_, '_, u8>,
294+
mut buffer: InOutBuf<'_, '_, u8>,
295295
tag: &Tag,
296296
) -> Result<(), Error> {
297297
if buffer.len() as u64 > C_MAX || associated_data.len() as u64 > A_MAX {
@@ -301,8 +301,8 @@ where
301301
self.polyval.update_padded(associated_data);
302302

303303
// TODO(tarcieri): interleave decryption and authentication
304-
init_ctr(&self.enc_cipher, tag).apply_keystream_partial(buffer.into());
305-
self.polyval.update_padded(buffer);
304+
init_ctr(&self.enc_cipher, tag).apply_keystream_partial(buffer.reborrow());
305+
self.polyval.update_padded(buffer.get_in());
306306

307307
let expected_tag = self.finish_tag(associated_data.len(), buffer.len());
308308

@@ -312,7 +312,7 @@ where
312312
} else {
313313
// On MAC verify failure, re-encrypt the plaintext buffer to
314314
// prevent accidental exposure.
315-
init_ctr(&self.enc_cipher, tag).apply_keystream_partial(buffer.into());
315+
init_ctr(&self.enc_cipher, tag).apply_keystream_partial(buffer);
316316
Err(Error)
317317
}
318318
}

aes-gcm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ categories = ["cryptography", "no-std"]
1717
rust-version = "1.85"
1818

1919
[dependencies]
20-
aead = { version = "0.6.0-rc.0", default-features = false }
20+
aead = { version = "0.6.0-rc.0", default-features = false, features = ["inout"] }
2121
aes = { version = "=0.9.0-pre.3", optional = true }
2222
cipher = "=0.5.0-pre.8"
2323
ctr = "0.10.0-pre.2"

aes-gcm/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ where
270270
&self,
271271
nonce: &Nonce<NonceSize>,
272272
associated_data: &[u8],
273-
buffer: InOutBuf<'_, '_, u8>,
273+
mut buffer: InOutBuf<'_, '_, u8>,
274274
) -> Result<Tag<TagSize>, Error> {
275275
if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX {
276276
return Err(Error);
@@ -280,9 +280,9 @@ where
280280

281281
// TODO(tarcieri): interleave encryption with GHASH
282282
// See: <https://github.com/RustCrypto/AEADs/issues/74>
283-
ctr.apply_keystream_partial(buffer.into());
283+
ctr.apply_keystream_partial(buffer.reborrow());
284284

285-
let full_tag = self.compute_tag(mask, associated_data, buffer);
285+
let full_tag = self.compute_tag(mask, associated_data, buffer.get_in());
286286
Ok(Tag::try_from(&full_tag[..TagSize::to_usize()]).expect("tag size mismatch"))
287287
}
288288

@@ -301,11 +301,11 @@ where
301301

302302
// TODO(tarcieri): interleave encryption with GHASH
303303
// See: <https://github.com/RustCrypto/AEADs/issues/74>
304-
let expected_tag = self.compute_tag(mask, associated_data, buffer);
304+
let expected_tag = self.compute_tag(mask, associated_data, buffer.get_in());
305305

306306
use subtle::ConstantTimeEq;
307307
if expected_tag[..TagSize::to_usize()].ct_eq(tag).into() {
308-
ctr.apply_keystream_partial(buffer.into());
308+
ctr.apply_keystream_partial(buffer);
309309
Ok(())
310310
} else {
311311
Err(Error)

aes-gcm/tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ macro_rules! tests {
9292
let cipher = <$aead>::new(&key);
9393
assert!(
9494
cipher
95-
.decrypt_inout_detached(&nonce, &[], &mut buffer, &tag)
95+
.decrypt_inout_detached(&nonce, &[], (buffer.as_mut_slice()).into(), &tag)
9696
.is_err()
9797
);
9898

aes-siv/src/siv.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ where
213213
// TODO(tarcieri): add offset param to `encrypt_inout_detached`
214214
buffer.as_mut().copy_within(..pt_len, IV_SIZE);
215215

216-
let tag = self.encrypt_inout_detached(headers, &mut buffer.as_mut()[IV_SIZE..])?;
216+
let tag = self.encrypt_inout_detached(headers, (&mut buffer.as_mut()[IV_SIZE..]).into())?;
217217
buffer.as_mut()[..IV_SIZE].copy_from_slice(tag.as_slice());
218218
Ok(())
219219
}
@@ -227,15 +227,15 @@ where
227227
pub fn encrypt_inout_detached<I, T>(
228228
&mut self,
229229
headers: I,
230-
plaintext: InOutBuf<'_, '_, u8>,
230+
mut plaintext: InOutBuf<'_, '_, u8>,
231231
) -> Result<Tag, Error>
232232
where
233233
I: IntoIterator<Item = T>,
234234
T: AsRef<[u8]>,
235235
{
236236
// Compute the synthetic IV for this plaintext
237-
let siv_tag = s2v(&mut self.mac, headers, plaintext)?;
238-
self.xor_with_keystream(siv_tag, plaintext);
237+
let siv_tag = s2v(&mut self.mac, headers, plaintext.get_in())?;
238+
self.xor_with_keystream(siv_tag, plaintext.get_out());
239239
Ok(siv_tag)
240240
}
241241

@@ -271,7 +271,7 @@ where
271271
}
272272

273273
let siv_tag = Tag::try_from(&buffer.as_ref()[..IV_SIZE]).expect("tag size mismatch");
274-
self.decrypt_inout_detached(headers, &mut buffer.as_mut()[IV_SIZE..], &siv_tag)?;
274+
self.decrypt_inout_detached(headers, (&mut buffer.as_mut()[IV_SIZE..]).into(), &siv_tag)?;
275275

276276
let pt_len = buffer.len() - IV_SIZE;
277277

@@ -290,22 +290,22 @@ where
290290
pub fn decrypt_inout_detached<I, T>(
291291
&mut self,
292292
headers: I,
293-
ciphertext: InOutBuf<'_, '_, u8>,
293+
mut ciphertext: InOutBuf<'_, '_, u8>,
294294
siv_tag: &Tag,
295295
) -> Result<(), Error>
296296
where
297297
I: IntoIterator<Item = T>,
298298
T: AsRef<[u8]>,
299299
{
300-
self.xor_with_keystream(*siv_tag, ciphertext);
301-
let computed_siv_tag = s2v(&mut self.mac, headers, ciphertext)?;
300+
self.xor_with_keystream(*siv_tag, ciphertext.get_out());
301+
let computed_siv_tag = s2v(&mut self.mac, headers, ciphertext.get_in())?;
302302

303303
// Note: `CtOutput` provides constant-time equality
304304
if CtOutput::<M>::new(computed_siv_tag) == CtOutput::new(*siv_tag) {
305305
Ok(())
306306
} else {
307307
// Re-encrypt the decrypted plaintext to avoid revealing it
308-
self.xor_with_keystream(*siv_tag, ciphertext);
308+
self.xor_with_keystream(*siv_tag, ciphertext.get_out());
309309
Err(Error)
310310
}
311311
}

aes-siv/tests/aead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ macro_rules! tests {
4040

4141
let cipher = <$aead>::new(&key);
4242
let tag = cipher
43-
.encrypt_inout_detached(&nonce, vector.aad, &mut buffer)
43+
.encrypt_inout_detached(&nonce, vector.aad, buffer.as_mut_slice().into())
4444
.unwrap();
4545
let (expected_tag, expected_ciphertext) = vector.ciphertext.split_at(16);
4646
assert_eq!(expected_tag, &tag[..]);
@@ -75,7 +75,7 @@ macro_rules! tests {
7575
let mut buffer = vector.ciphertext[16..].to_vec();
7676

7777
<$aead>::new(&key)
78-
.decrypt_inout_detached(&nonce, vector.aad, &mut buffer, &tag)
78+
.decrypt_inout_detached(&nonce, vector.aad, buffer.as_mut_slice().into(), &tag)
7979
.unwrap();
8080

8181
assert_eq!(vector.plaintext, buffer.as_slice());

ascon-aead/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ categories = ["cryptography", "no-std"]
1515
rust-version = "1.85"
1616

1717
[dependencies]
18-
aead = { version = "0.6.0-rc.0", default-features = false }
18+
aead = { version = "0.6.0-rc.0", default-features = false, features = ["inout"] }
1919
subtle = { version = "2", default-features = false }
2020
zeroize = { version = "1.6", optional = true, default-features = false, features = ["derive"] }
2121
ascon = "0.4"

ascon-aead/src/asconcore.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use aead::{
5-
Error,
6-
array::{Array, ArraySize, typenum::Unsigned},
5+
array::{typenum::Unsigned, Array, ArraySize},
76
consts::{U16, U20},
7+
inout::InOutBuf,
8+
Error,
89
};
9-
use ascon::{State, pad};
10+
use ascon::{pad, State};
1011
use subtle::ConstantTimeEq;
1112

1213
/// Clear bytes from a 64 bit word.
@@ -337,30 +338,30 @@ impl<'a, P: Parameters> AsconCore<'a, P> {
337338
tag
338339
}
339340

340-
pub(crate) fn encrypt_inplace(
341+
pub(crate) fn encrypt_inout(
341342
&mut self,
342-
message: &mut [u8],
343+
mut message: InOutBuf<'_, '_, u8>,
343344
associated_data: &[u8],
344345
) -> Array<u8, U16> {
345346
self.process_associated_data(associated_data);
346-
self.process_encrypt_inplace(message);
347+
self.process_encrypt_inplace(message.get_out());
347348
Array::from(self.process_final())
348349
}
349350

350-
pub(crate) fn decrypt_inplace(
351+
pub(crate) fn decrypt_inout(
351352
&mut self,
352-
ciphertext: &mut [u8],
353+
mut ciphertext: InOutBuf<'_, '_, u8>,
353354
associated_data: &[u8],
354355
expected_tag: &Array<u8, U16>,
355356
) -> Result<(), Error> {
356357
self.process_associated_data(associated_data);
357-
self.process_decrypt_inplace(ciphertext);
358+
self.process_decrypt_inplace(ciphertext.get_out());
358359

359360
let tag = self.process_final();
360361
if bool::from(tag.ct_eq(expected_tag)) {
361362
Ok(())
362363
} else {
363-
ciphertext.fill(0);
364+
ciphertext.get_out().fill(0);
364365
Err(Error)
365366
}
366367
}

ascon-aead/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ pub use zeroize;
106106

107107
pub use aead::{self, Error, Key, Nonce, Tag};
108108
use aead::{
109-
AeadCore, AeadInOut, KeyInit, KeySizeUser, PostfixTagged,
110109
consts::{U16, U20},
111110
inout::InOutBuf,
111+
AeadCore, AeadInOut, KeyInit, KeySizeUser, PostfixTagged,
112112
};
113113

114114
mod asconcore;
115115

116-
use asconcore::{AsconCore, Parameters, Parameters80pq, Parameters128, Parameters128a};
116+
use asconcore::{AsconCore, Parameters, Parameters128, Parameters128a, Parameters80pq};
117117

118118
/// Ascon generic over some Parameters
119119
///
@@ -158,7 +158,7 @@ impl<P: Parameters> AeadInOut for Ascon<P> {
158158
}
159159

160160
let mut core = AsconCore::<P>::new(&self.key, nonce);
161-
Ok(core.encrypt_inplace(buffer, associated_data))
161+
Ok(core.encrypt_inout(buffer, associated_data))
162162
}
163163

164164
fn decrypt_inout_detached(
@@ -176,7 +176,7 @@ impl<P: Parameters> AeadInOut for Ascon<P> {
176176
}
177177

178178
let mut core = AsconCore::<P>::new(&self.key, nonce);
179-
core.decrypt_inplace(buffer, associated_data, tag)
179+
core.decrypt_inout(buffer, associated_data, tag)
180180
}
181181
}
182182

0 commit comments

Comments
 (0)