Skip to content

Commit db4e09e

Browse files
committed
ascon-aead128: migrate to AeadInOut
2 parents 7033841 + d009899 commit db4e09e

File tree

30 files changed

+257
-323
lines changed

30 files changed

+257
-323
lines changed

Cargo.lock

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aead-stream/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern crate alloc;
77

88
use aead::{
9-
AeadCore, AeadInPlace, Buffer, Error, Result,
9+
AeadCore, AeadInOut, Buffer, Error, Result,
1010
array::{
1111
Array, ArraySize,
1212
typenum::{U4, U5, Unsigned},
@@ -32,7 +32,7 @@ pub type NonceSize<A, S> =
3232
/// Create a new STREAM from the provided AEAD.
3333
pub trait NewStream<A>: StreamPrimitive<A>
3434
where
35-
A: AeadInPlace,
35+
A: AeadInOut,
3636
A::NonceSize: Sub<Self::NonceOverhead>,
3737
NonceSize<A, Self>: ArraySize,
3838
{
@@ -57,7 +57,7 @@ where
5757
/// Deliberately immutable and stateless to permit parallel operation.
5858
pub trait StreamPrimitive<A>
5959
where
60-
A: AeadInPlace,
60+
A: AeadInOut,
6161
A::NonceSize: Sub<Self::NonceOverhead>,
6262
NonceSize<A, Self>: ArraySize,
6363
{
@@ -165,7 +165,7 @@ macro_rules! impl_stream_object {
165165
#[derive(Debug)]
166166
pub struct $name<A, S>
167167
where
168-
A: AeadInPlace,
168+
A: AeadInOut,
169169
S: StreamPrimitive<A>,
170170
A::NonceSize: Sub<<S as StreamPrimitive<A>>::NonceOverhead>,
171171
NonceSize<A, S>: ArraySize,
@@ -179,7 +179,7 @@ macro_rules! impl_stream_object {
179179

180180
impl<A, S> $name<A, S>
181181
where
182-
A: AeadInPlace,
182+
A: AeadInOut,
183183
S: StreamPrimitive<A>,
184184
A::NonceSize: Sub<<S as StreamPrimitive<A>>::NonceOverhead>,
185185
NonceSize<A, S>: ArraySize,
@@ -344,7 +344,7 @@ pub type DecryptorLE31<A> = Decryptor<A, StreamLE31<A>>;
344344
#[derive(Debug)]
345345
pub struct StreamBE32<A>
346346
where
347-
A: AeadInPlace,
347+
A: AeadInOut,
348348
A::NonceSize: Sub<U5>,
349349
<<A as AeadCore>::NonceSize as Sub<U5>>::Output: ArraySize,
350350
{
@@ -357,7 +357,7 @@ where
357357

358358
impl<A> NewStream<A> for StreamBE32<A>
359359
where
360-
A: AeadInPlace,
360+
A: AeadInOut,
361361
A::NonceSize: Sub<U5>,
362362
<<A as AeadCore>::NonceSize as Sub<U5>>::Output: ArraySize,
363363
{
@@ -371,7 +371,7 @@ where
371371

372372
impl<A> StreamPrimitive<A> for StreamBE32<A>
373373
where
374-
A: AeadInPlace,
374+
A: AeadInOut,
375375
A::NonceSize: Sub<U5>,
376376
<<A as AeadCore>::NonceSize as Sub<U5>>::Output: ArraySize,
377377
{
@@ -405,7 +405,7 @@ where
405405

406406
impl<A> StreamBE32<A>
407407
where
408-
A: AeadInPlace,
408+
A: AeadInOut,
409409
A::NonceSize: Sub<U5>,
410410
<<A as AeadCore>::NonceSize as Sub<U5>>::Output: ArraySize,
411411
{
@@ -434,7 +434,7 @@ where
434434
#[derive(Debug)]
435435
pub struct StreamLE31<A>
436436
where
437-
A: AeadInPlace,
437+
A: AeadInOut,
438438
A::NonceSize: Sub<U4>,
439439
<<A as AeadCore>::NonceSize as Sub<U4>>::Output: ArraySize,
440440
{
@@ -447,7 +447,7 @@ where
447447

448448
impl<A> NewStream<A> for StreamLE31<A>
449449
where
450-
A: AeadInPlace,
450+
A: AeadInOut,
451451
A::NonceSize: Sub<U4>,
452452
<<A as AeadCore>::NonceSize as Sub<U4>>::Output: ArraySize,
453453
{
@@ -461,7 +461,7 @@ where
461461

462462
impl<A> StreamPrimitive<A> for StreamLE31<A>
463463
where
464-
A: AeadInPlace,
464+
A: AeadInOut,
465465
A::NonceSize: Sub<U4>,
466466
<<A as AeadCore>::NonceSize as Sub<U4>>::Output: ArraySize,
467467
{
@@ -495,7 +495,7 @@ where
495495

496496
impl<A> StreamLE31<A>
497497
where
498-
A: AeadInPlace,
498+
A: AeadInOut,
499499
A::NonceSize: Sub<U4>,
500500
<<A as AeadCore>::NonceSize as Sub<U4>>::Output: ArraySize,
501501
{

aes-gcm-siv/src/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
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
//!
@@ -48,7 +48,7 @@
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
//!
@@ -78,12 +78,12 @@
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")]
8484
pub use aes;
8585

86-
use aead::PostfixTagged;
86+
use aead::{TagPosition, inout::InOutBuf};
8787
use 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>
169168
where
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
}

aes-gcm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ subtle = { version = "2", default-features = false }
2626
zeroize = { version = "1", optional = true, default-features = false }
2727

2828
[dev-dependencies]
29-
aead = { version = "0.6.0-rc.0", features = ["dev"], default-features = false }
29+
aead = { version = "0.6.0-rc.0", features = ["alloc", "dev"], default-features = false }
3030
hex-literal = "0.4"
3131

3232
[features]

0 commit comments

Comments
 (0)