Skip to content

Commit 204a4e0

Browse files
authored
aead: add inout support (#1793)
Renames `AeadInPlaceDetached` to `AeadInOut`, and changes the type signature so the provided `buffer` is now an `InOutBuf`
1 parent 7b82f39 commit 204a4e0

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aead/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ arrayvec = { version = "0.7", optional = true, default-features = false }
2323
blobby = { version = "0.4.0-pre.0", optional = true }
2424
bytes = { version = "1", optional = true, default-features = false }
2525
heapless = { version = "0.8", optional = true, default-features = false }
26+
inout = { version = "0.2.0-rc.4", optional = true, default-features = false }
2627

2728
[features]
28-
default = ["rand_core"]
29+
default = ["inout", "rand_core"]
2930
alloc = []
3031
dev = ["blobby"]
3132
os_rng = ["crypto-common/os_rng", "rand_core"]

aead/src/lib.rs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ pub use crypto_common::{
3030
pub use arrayvec;
3131
#[cfg(feature = "bytes")]
3232
pub use bytes;
33-
#[cfg(feature = "heapless")]
34-
pub use heapless;
35-
3633
#[cfg(feature = "rand_core")]
3734
pub use crypto_common::rand_core;
35+
#[cfg(feature = "heapless")]
36+
pub use heapless;
37+
#[cfg(feature = "inout")]
38+
pub use inout;
3839

3940
use core::fmt;
40-
use crypto_common::array::{Array, ArraySize, typenum::Unsigned};
41+
use crypto_common::array::{Array, ArraySize};
4142

4243
#[cfg(feature = "alloc")]
4344
use alloc::vec::Vec;
4445
#[cfg(feature = "bytes")]
4546
use bytes::BytesMut;
47+
#[cfg(any(feature = "alloc", feature = "inout"))]
48+
use crypto_common::array::typenum::Unsigned;
4649
#[cfg(feature = "os_rng")]
4750
use crypto_common::rand_core::{OsError, OsRng, TryRngCore};
51+
#[cfg(feature = "inout")]
52+
use inout::InOutBuf;
4853
#[cfg(feature = "rand_core")]
4954
use rand_core::{CryptoRng, TryCryptoRng};
5055

@@ -240,23 +245,24 @@ pub trait AeadInPlace: AeadCore {
240245
}
241246

242247
/// 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(
246252
&self,
247253
nonce: &Nonce<Self>,
248254
associated_data: &[u8],
249-
buffer: &mut [u8],
255+
buffer: InOutBuf<'_, '_, u8>,
250256
) -> Result<Tag<Self>>;
251257

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
254260
/// is modified/unauthentic)
255-
fn decrypt_in_place_detached(
261+
fn decrypt_inout_detached(
256262
&self,
257263
nonce: &Nonce<Self>,
258264
associated_data: &[u8],
259-
buffer: &mut [u8],
265+
buffer: InOutBuf<'_, '_, u8>,
260266
tag: &Tag<Self>,
261267
) -> Result<()>;
262268
}
@@ -267,14 +273,41 @@ pub trait AeadInPlaceDetached: AeadCore {
267273
/// This is the common convention for AEAD algorithms.
268274
pub trait PostfixTagged {}
269275

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 {
271304
fn encrypt_in_place(
272305
&self,
273306
nonce: &Nonce<Self>,
274307
associated_data: &[u8],
275308
buffer: &mut dyn Buffer,
276309
) -> 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())?;
278311
buffer.extend_from_slice(tag.as_slice())?;
279312
Ok(())
280313
}
@@ -293,38 +326,12 @@ impl<T: AeadInPlaceDetached + PostfixTagged> AeadInPlace for T {
293326
let (msg, tag) = buffer.as_mut().split_at_mut(tag_pos);
294327
let tag = Tag::<Self>::try_from(&*tag).expect("tag length mismatch");
295328

296-
self.decrypt_in_place_detached(nonce, associated_data, msg, &tag)?;
329+
self.decrypt_inout_detached(nonce, associated_data, msg.into(), &tag)?;
297330
buffer.truncate(tag_pos);
298331
Ok(())
299332
}
300333
}
301334

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-
328335
/// AEAD payloads (message + AAD).
329336
///
330337
/// Combination of a message (plaintext or ciphertext) and

0 commit comments

Comments
 (0)