diff --git a/Cargo.lock b/Cargo.lock index 43fb66bd..ed8d4b9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,13 +6,14 @@ version = 4 [[package]] name = "aead" version = "0.6.0-rc.0" -source = "git+https://github.com/RustCrypto/traits.git#1548d2a7d7ce71a278a783d19d94b59b0103ab15" +source = "git+https://github.com/RustCrypto/traits.git#204a4e030fa98863429ccd3797e12f9e7c45dc33" dependencies = [ "arrayvec", "blobby 0.4.0-pre.0", "bytes", "crypto-common", "heapless", + "inout", ] [[package]] @@ -38,7 +39,6 @@ name = "aes-gcm" version = "0.11.0-pre.2" dependencies = [ "aead", - "aead-stream", "aes", "cipher", "ctr", @@ -53,7 +53,6 @@ name = "aes-gcm-siv" version = "0.12.0-pre.2" dependencies = [ "aead", - "aead-stream", "aes", "cipher", "ctr", @@ -67,7 +66,6 @@ name = "aes-siv" version = "0.8.0-pre.2" dependencies = [ "aead", - "aead-stream", "aes", "blobby 0.3.1", "cipher", @@ -100,7 +98,6 @@ name = "ascon-aead" version = "0.4.2" dependencies = [ "aead", - "aead-stream", "ascon", "hex-literal", "subtle", @@ -151,7 +148,6 @@ name = "ccm" version = "0.5.0" dependencies = [ "aead", - "aead-stream", "aes", "cipher", "ctr", @@ -181,7 +177,6 @@ name = "chacha20poly1305" version = "0.11.0-pre.2" dependencies = [ "aead", - "aead-stream", "chacha20", "cipher", "poly1305", @@ -221,7 +216,7 @@ dependencies = [ [[package]] name = "crypto-common" version = "0.2.0-rc.2" -source = "git+https://github.com/RustCrypto/traits.git#1548d2a7d7ce71a278a783d19d94b59b0103ab15" +source = "git+https://github.com/RustCrypto/traits.git#204a4e030fa98863429ccd3797e12f9e7c45dc33" dependencies = [ "hybrid-array", "rand_core", @@ -249,7 +244,6 @@ name = "deoxys" version = "0.1.0" dependencies = [ "aead", - "aead-stream", "aes", "hex-literal", "subtle", @@ -272,7 +266,6 @@ name = "eax" version = "0.5.0" dependencies = [ "aead", - "aead-stream", "aes", "cipher", "cmac", diff --git a/aes-gcm-siv/src/lib.rs b/aes-gcm-siv/src/lib.rs index 7f2ca194..37700513 100644 --- a/aes-gcm-siv/src/lib.rs +++ b/aes-gcm-siv/src/lib.rs @@ -78,18 +78,18 @@ //! provide an impl of [`aead::Buffer`] for `bytes::BytesMut` (re-exported from the //! [`aead`] crate as [`aead::bytes::BytesMut`]). -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser}; +pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser}; #[cfg(feature = "aes")] pub use aes; -use aead::PostfixTagged; +use aead::{inout::InOutBuf, PostfixTagged}; use cipher::{ - BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore, array::Array, consts::{U12, U16}, + BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore, }; -use polyval::{Polyval, universal_hash::UniversalHash}; +use polyval::{universal_hash::UniversalHash, Polyval}; /// AES is optional to allow swapping in hardware-specific backends. #[cfg(feature = "aes")] @@ -165,28 +165,28 @@ where impl PostfixTagged for AesGcmSiv {} -impl AeadInPlaceDetached for AesGcmSiv +impl AeadInOut for AesGcmSiv where Aes: BlockSizeUser + BlockCipherEncrypt + KeyInit, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result { Cipher::::new(&self.key_generating_key, nonce) - .encrypt_in_place_detached(associated_data, buffer) + .encrypt_inout_detached(associated_data, buffer) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { - Cipher::::new(&self.key_generating_key, nonce).decrypt_in_place_detached( + Cipher::::new(&self.key_generating_key, nonce).decrypt_inout_detached( associated_data, buffer, tag, @@ -268,10 +268,10 @@ where } /// Encrypt the given message in-place, returning the authentication tag. - pub(crate) fn encrypt_in_place_detached( + pub(crate) fn encrypt_inout_detached( mut self, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result { if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX { return Err(Error); @@ -288,10 +288,10 @@ where /// Decrypt the given message, first authenticating ciphertext integrity /// and returning an error if it's been tampered with. - pub(crate) fn decrypt_in_place_detached( + pub(crate) fn decrypt_inout_detached( mut self, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { if buffer.len() as u64 > C_MAX || associated_data.len() as u64 > A_MAX { diff --git a/aes-gcm/src/lib.rs b/aes-gcm/src/lib.rs index 6b307d68..9b5e3d4e 100644 --- a/aes-gcm/src/lib.rs +++ b/aes-gcm/src/lib.rs @@ -98,26 +98,26 @@ //! provide an impl of [`aead::Buffer`] for `bytes::BytesMut` (re-exported from the //! [`aead`] crate as [`aead::bytes::BytesMut`]). -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser}; +pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser}; #[cfg(feature = "aes")] pub use aes; -use aead::PostfixTagged; +use aead::{inout::InOutBuf, PostfixTagged}; use cipher::{ - BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore, array::{Array, ArraySize}, consts::U16, + BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore, }; use core::marker::PhantomData; -use ghash::{GHash, universal_hash::UniversalHash}; +use ghash::{universal_hash::UniversalHash, GHash}; #[cfg(feature = "zeroize")] use zeroize::Zeroize; #[cfg(feature = "aes")] -use aes::{Aes128, Aes256, cipher::consts::U12}; +use aes::{cipher::consts::U12, Aes128, Aes256}; /// Maximum length of associated data. pub const A_MAX: u64 = 1 << 36; @@ -260,17 +260,17 @@ impl PostfixTagged for AesGcm { } -impl AeadInPlaceDetached for AesGcm +impl AeadInOut for AesGcm where Aes: BlockSizeUser + BlockCipherEncrypt, NonceSize: ArraySize, TagSize: self::TagSize, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX { return Err(Error); @@ -286,11 +286,11 @@ where Ok(Tag::try_from(&full_tag[..TagSize::to_usize()]).expect("tag size mismatch")) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { if buffer.len() as u64 > C_MAX || associated_data.len() as u64 > A_MAX { diff --git a/aes-gcm/tests/aes128gcm.rs b/aes-gcm/tests/aes128gcm.rs index 1e0f82e0..819a631b 100644 --- a/aes-gcm/tests/aes128gcm.rs +++ b/aes-gcm/tests/aes128gcm.rs @@ -7,7 +7,7 @@ mod common; use self::common::TestVector; use aes_gcm::Aes128Gcm; -use aes_gcm::aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; +use aes_gcm::aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; use hex_literal::hex; /// NIST CAVS vectors diff --git a/aes-gcm/tests/aes256gcm.rs b/aes-gcm/tests/aes256gcm.rs index e56e5110..7f0ea0b5 100644 --- a/aes-gcm/tests/aes256gcm.rs +++ b/aes-gcm/tests/aes256gcm.rs @@ -7,7 +7,7 @@ mod common; use self::common::TestVector; use aes_gcm::Aes256Gcm; -use aes_gcm::aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; +use aes_gcm::aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; use hex_literal::hex; /// NIST CAVS vectors diff --git a/aes-gcm/tests/common/mod.rs b/aes-gcm/tests/common/mod.rs index 5833b250..c7d217ae 100644 --- a/aes-gcm/tests/common/mod.rs +++ b/aes-gcm/tests/common/mod.rs @@ -77,7 +77,7 @@ macro_rules! tests { } #[test] - fn decrypt_in_place_detached_modified() { + fn decrypt_inout_detached_modified() { let vector = &$vectors.iter().last().unwrap(); let key = Array(*vector.key); let nonce = Array(*vector.nonce); @@ -92,7 +92,7 @@ macro_rules! tests { let cipher = <$aead>::new(&key); assert!( cipher - .decrypt_in_place_detached(&nonce, &[], &mut buffer, &tag) + .decrypt_inout_detached(&nonce, &[], &mut buffer, &tag) .is_err() ); diff --git a/aes-siv/src/lib.rs b/aes-siv/src/lib.rs index 9fd9e289..592e03ab 100644 --- a/aes-siv/src/lib.rs +++ b/aes-siv/src/lib.rs @@ -83,15 +83,14 @@ extern crate alloc; pub mod siv; -pub use aead::{ - self, AeadCore, AeadInPlace, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser, -}; +pub use aead::{self, AeadCore, AeadInOut, AeadInPlace, Error, Key, KeyInit, KeySizeUser}; use crate::siv::Siv; use aead::{ Buffer, array::Array, consts::{U1, U16, U32, U64}, + inout::InOutBuf, }; use aes::{Aes128, Aes256}; use cipher::{BlockCipherEncrypt, BlockSizeUser, array::ArraySize, typenum::IsGreaterOrEqual}; @@ -241,7 +240,7 @@ where } } -impl AeadInPlaceDetached for SivAead +impl AeadInOut for SivAead where Self: KeySizeUser, Siv: KeyInit + KeySizeUser::KeySize>, @@ -250,24 +249,24 @@ where ::KeySize: Add, NonceSize: ArraySize + IsGreaterOrEqual, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Array, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { Siv::::new(&self.key) - .encrypt_in_place_detached([associated_data, nonce.as_slice()], buffer) + .encrypt_inout_detached([associated_data, nonce.as_slice()], buffer) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Array, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Array, ) -> Result<(), Error> { - Siv::::new(&self.key).decrypt_in_place_detached( + Siv::::new(&self.key).decrypt_inout_detached( [associated_data, nonce.as_slice()], buffer, tag, diff --git a/aes-siv/src/siv.rs b/aes-siv/src/siv.rs index 7880ec2e..8aec3289 100644 --- a/aes-siv/src/siv.rs +++ b/aes-siv/src/siv.rs @@ -72,6 +72,7 @@ use crate::Tag; use aead::{ Buffer, Error, array::{Array, ArraySize, typenum::U16}, + inout::InOutBuf, }; use aes::{Aes128, Aes256}; use cipher::{ @@ -209,10 +210,10 @@ where // Make room in the buffer for the SIV tag. It needs to be prepended. buffer.extend_from_slice(Tag::default().as_slice())?; - // TODO(tarcieri): add offset param to `encrypt_in_place_detached` + // TODO(tarcieri): add offset param to `encrypt_inout_detached` buffer.as_mut().copy_within(..pt_len, IV_SIZE); - let tag = self.encrypt_in_place_detached(headers, &mut buffer.as_mut()[IV_SIZE..])?; + let tag = self.encrypt_inout_detached(headers, &mut buffer.as_mut()[IV_SIZE..])?; buffer.as_mut()[..IV_SIZE].copy_from_slice(tag.as_slice()); Ok(()) } @@ -223,10 +224,10 @@ where /// /// Returns [`Error`] if `plaintext.len()` is less than `M::OutputSize`. /// Returns [`Error`] if `headers.len()` is greater than [`MAX_HEADERS`]. - pub fn encrypt_in_place_detached( + pub fn encrypt_inout_detached( &mut self, headers: I, - plaintext: &mut [u8], + plaintext: InOutBuf<'_, '_, u8>, ) -> Result where I: IntoIterator, @@ -270,11 +271,11 @@ where } let siv_tag = Tag::try_from(&buffer.as_ref()[..IV_SIZE]).expect("tag size mismatch"); - self.decrypt_in_place_detached(headers, &mut buffer.as_mut()[IV_SIZE..], &siv_tag)?; + self.decrypt_inout_detached(headers, &mut buffer.as_mut()[IV_SIZE..], &siv_tag)?; let pt_len = buffer.len() - IV_SIZE; - // TODO(tarcieri): add offset param to `encrypt_in_place_detached` + // TODO(tarcieri): add offset param to `encrypt_inout_detached` buffer.as_mut().copy_within(IV_SIZE.., 0); buffer.truncate(pt_len); Ok(()) @@ -286,10 +287,10 @@ where /// # Errors /// /// Returns [`Error`] if the ciphertext is not authentic - pub fn decrypt_in_place_detached( + pub fn decrypt_inout_detached( &mut self, headers: I, - ciphertext: &mut [u8], + ciphertext: InOutBuf<'_, '_, u8>, siv_tag: &Tag, ) -> Result<(), Error> where diff --git a/aes-siv/tests/aead.rs b/aes-siv/tests/aead.rs index ea61a96a..d0d8ffd0 100644 --- a/aes-siv/tests/aead.rs +++ b/aes-siv/tests/aead.rs @@ -32,7 +32,7 @@ macro_rules! tests { } #[test] - fn encrypt_in_place_detached() { + fn encrypt_inout_detached() { for vector in $vectors { let key = Array(*vector.key); let nonce = Array(*vector.nonce); @@ -40,7 +40,7 @@ macro_rules! tests { let cipher = <$aead>::new(&key); let tag = cipher - .encrypt_in_place_detached(&nonce, vector.aad, &mut buffer) + .encrypt_inout_detached(&nonce, vector.aad, &mut buffer) .unwrap(); let (expected_tag, expected_ciphertext) = vector.ciphertext.split_at(16); assert_eq!(expected_tag, &tag[..]); @@ -67,7 +67,7 @@ macro_rules! tests { } #[test] - fn decrypt_in_place_detached() { + fn decrypt_inout_detached() { for vector in $vectors { let key = Array(*vector.key); let nonce = Array(*vector.nonce); @@ -75,7 +75,7 @@ macro_rules! tests { let mut buffer = vector.ciphertext[16..].to_vec(); <$aead>::new(&key) - .decrypt_in_place_detached(&nonce, vector.aad, &mut buffer, &tag) + .decrypt_inout_detached(&nonce, vector.aad, &mut buffer, &tag) .unwrap(); assert_eq!(vector.plaintext, buffer.as_slice()); @@ -108,7 +108,7 @@ macro_rules! tests { mod aes128cmacsivaead { use super::TestVector; use aes_siv::Aes128SivAead; - use aes_siv::aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; + use aes_siv::aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; /// AES-128-CMAC-SIV test vectors const TEST_VECTORS: &[TestVector<[u8; 32]>] = &[TestVector { @@ -132,7 +132,7 @@ mod aes128cmacsivaead { mod aes128pmacsivaead { use super::TestVector; use aes_siv::Aes128PmacSivAead; - use aes_siv::aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; + use aes_siv::aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; /// AES-128-PMAC-SIV test vectors const AES_128_PMAC_SIV_TEST_VECTORS: &[TestVector<[u8; 32]>] = &[TestVector { diff --git a/ascon-aead/src/lib.rs b/ascon-aead/src/lib.rs index 34c867fc..52201699 100644 --- a/ascon-aead/src/lib.rs +++ b/ascon-aead/src/lib.rs @@ -106,8 +106,9 @@ pub use zeroize; pub use aead::{self, Error, Key, Nonce, Tag}; use aead::{ - AeadCore, AeadInPlaceDetached, KeyInit, KeySizeUser, PostfixTagged, + AeadCore, AeadInOut, KeyInit, KeySizeUser, PostfixTagged, consts::{U16, U20}, + inout::InOutBuf, }; mod asconcore; @@ -142,12 +143,12 @@ impl AeadCore for Ascon

{ impl PostfixTagged for Ascon

{} -impl AeadInPlaceDetached for Ascon

{ - fn encrypt_in_place_detached( +impl AeadInOut for Ascon

{ + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { if (buffer.len() as u64) .checked_add(associated_data.len() as u64) @@ -160,11 +161,11 @@ impl AeadInPlaceDetached for Ascon

{ Ok(core.encrypt_inplace(buffer, associated_data)) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { if (buffer.len() as u64) @@ -205,28 +206,28 @@ impl AeadCore for Ascon128 { impl PostfixTagged for Ascon128 {} -impl AeadInPlaceDetached for Ascon128 { +impl AeadInOut for Ascon128 { #[inline(always)] - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { self.0 - .encrypt_in_place_detached(nonce, associated_data, buffer) + .encrypt_inout_detached(nonce, associated_data, buffer) } #[inline(always)] - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { self.0 - .decrypt_in_place_detached(nonce, associated_data, buffer, tag) + .decrypt_inout_detached(nonce, associated_data, buffer, tag) } } @@ -257,28 +258,28 @@ impl AeadCore for Ascon128a { impl PostfixTagged for Ascon128a {} -impl AeadInPlaceDetached for Ascon128a { +impl AeadInOut for Ascon128a { #[inline(always)] - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { self.0 - .encrypt_in_place_detached(nonce, associated_data, buffer) + .encrypt_inout_detached(nonce, associated_data, buffer) } #[inline(always)] - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { self.0 - .decrypt_in_place_detached(nonce, associated_data, buffer, tag) + .decrypt_inout_detached(nonce, associated_data, buffer, tag) } } @@ -308,27 +309,27 @@ impl AeadCore for Ascon80pq { impl PostfixTagged for Ascon80pq {} -impl AeadInPlaceDetached for Ascon80pq { +impl AeadInOut for Ascon80pq { #[inline(always)] - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { self.0 - .encrypt_in_place_detached(nonce, associated_data, buffer) + .encrypt_inout_detached(nonce, associated_data, buffer) } #[inline(always)] - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { self.0 - .decrypt_in_place_detached(nonce, associated_data, buffer, tag) + .decrypt_inout_detached(nonce, associated_data, buffer, tag) } } diff --git a/ascon-aead/tests/kats_test.rs b/ascon-aead/tests/kats_test.rs index 3684244a..6fa63537 100644 --- a/ascon-aead/tests/kats_test.rs +++ b/ascon-aead/tests/kats_test.rs @@ -3,11 +3,11 @@ use ascon_aead::{ Ascon80pq, Ascon128, Ascon128a, - aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, Tag}, + aead::{Aead, AeadInOut, KeyInit, Payload, Tag}, }; use hex_literal::hex; -fn run_tv( +fn run_tv( key: &[u8], nonce: &[u8], plaintext: &[u8], @@ -40,7 +40,7 @@ fn run_tv( let bad_tag = Tag::::default(); let mut buf = ciphertext[..ciphertext.len() - bad_tag.len()].to_vec(); - let res = core.decrypt_in_place_detached(nonce, associated_data, &mut buf, &bad_tag); + let res = core.decrypt_inout_detached(nonce, associated_data, &mut buf, &bad_tag); assert!(res.is_err()); assert!(buf.iter().all(|b| *b == 0)); } diff --git a/benches/src/ascon-aead.rs b/benches/src/ascon-aead.rs index b36ad373..753f2261 100644 --- a/benches/src/ascon-aead.rs +++ b/benches/src/ascon-aead.rs @@ -1,6 +1,6 @@ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use ascon_aead::aead::{AeadInPlaceDetached, KeyInit}; +use ascon_aead::aead::{AeadInOut, KeyInit}; use ascon_aead::{Ascon128, Ascon128a, Ascon80pq}; const KB: usize = 1024; @@ -10,7 +10,7 @@ type Benchmarker = Criterion; #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] type Benchmarker = Criterion; -fn bench(name: &str, c: &mut Benchmarker) { +fn bench(name: &str, c: &mut Benchmarker) { let mut group = c.benchmark_group(name); let nonce = black_box(Default::default()); let cipher = black_box(A::new(&Default::default())); @@ -18,15 +18,15 @@ fn bench(name: &str, c: &mut Benchmarker) { let mut buf = vec![0u8; 16 * KB]; for size in [KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB] { let buf = &mut buf[..size]; - let tag = cipher.encrypt_in_place_detached(&nonce, b"", buf).unwrap(); + let tag = cipher.encrypt_inout_detached(&nonce, b"", buf).unwrap(); group.throughput(Throughput::Bytes(size as u64)); group.bench_function(BenchmarkId::new("encrypt-128", size), |b| { - b.iter(|| cipher.encrypt_in_place_detached(&nonce, b"", buf)) + b.iter(|| cipher.encrypt_inout_detached(&nonce, b"", buf)) }); group.bench_function(BenchmarkId::new("decrypt-128", size), |b| { - b.iter(|| cipher.decrypt_in_place_detached(&nonce, b"", buf, &tag)) + b.iter(|| cipher.decrypt_inout_detached(&nonce, b"", buf, &tag)) }); } diff --git a/ccm/src/lib.rs b/ccm/src/lib.rs index bd51999b..a9200aa6 100644 --- a/ccm/src/lib.rs +++ b/ccm/src/lib.rs @@ -42,12 +42,13 @@ //! [aead]: https://docs.rs/aead //! [1]: https://en.wikipedia.org/wiki/Authenticated_encryption -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser, consts}; +pub use aead::{self, consts, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser}; use aead::{ - PostfixTagged, - array::{Array, ArraySize, typenum::Unsigned}, + array::{typenum::Unsigned, Array, ArraySize}, consts::U16, + inout::InOutBuf, + PostfixTagged, }; use cipher::{ Block, BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipher, StreamCipherSeek, @@ -221,17 +222,17 @@ where { } -impl AeadInPlaceDetached for Ccm +impl AeadInOut for Ccm where C: BlockSizeUser + BlockCipherEncrypt, M: ArraySize + TagSize, N: ArraySize + NonceSize, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, adata: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { let mut full_tag = self.calc_mac(nonce, adata, buffer)?; @@ -252,11 +253,11 @@ where Ok(Tag::try_from(&full_tag[..M::to_usize()]).expect("tag size mismatch")) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, adata: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { let ext_nonce = Self::extend_nonce(nonce); diff --git a/ccm/tests/mod.rs b/ccm/tests/mod.rs index 10ce7276..aa393c7e 100644 --- a/ccm/tests/mod.rs +++ b/ccm/tests/mod.rs @@ -1,6 +1,6 @@ #![cfg(feature = "alloc")] -use aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; +use aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; use aes::{Aes128, Aes192, Aes256}; use ccm::{ Ccm, @@ -19,11 +19,11 @@ fn test_data_len_check() { let c = Cipher::new(&key); let mut buf1 = [1; u16::MAX as usize]; - let res = c.encrypt_in_place_detached(&nonce, &[], &mut buf1); + let res = c.encrypt_inout_detached(&nonce, &[], &mut buf1); assert!(res.is_ok()); let mut buf2 = [1; u16::MAX as usize + 1]; - let res = c.encrypt_in_place_detached(&nonce, &[], &mut buf2); + let res = c.encrypt_inout_detached(&nonce, &[], &mut buf2); assert!(res.is_err()); } diff --git a/chacha20poly1305/src/cipher.rs b/chacha20poly1305/src/cipher.rs index 76bb4bf8..b04a2058 100644 --- a/chacha20poly1305/src/cipher.rs +++ b/chacha20poly1305/src/cipher.rs @@ -50,7 +50,7 @@ where } /// Encrypt the given message in-place, returning the authentication tag - pub(crate) fn encrypt_in_place_detached( + pub(crate) fn encrypt_inout_detached( mut self, associated_data: &[u8], buffer: &mut [u8], @@ -72,7 +72,7 @@ where /// Decrypt the given message, first authenticating ciphertext integrity /// and returning an error if it's been tampered with. - pub(crate) fn decrypt_in_place_detached( + pub(crate) fn decrypt_inout_detached( mut self, associated_data: &[u8], buffer: &mut [u8], diff --git a/chacha20poly1305/src/lib.rs b/chacha20poly1305/src/lib.rs index c42e057d..a021df7f 100644 --- a/chacha20poly1305/src/lib.rs +++ b/chacha20poly1305/src/lib.rs @@ -139,7 +139,7 @@ mod cipher; -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, KeyInit, KeySizeUser, consts}; +pub use aead::{self, AeadCore, AeadInOut, Error, KeyInit, KeySizeUser, consts}; use self::cipher::Cipher; use ::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek}; @@ -147,6 +147,7 @@ use aead::{ PostfixTagged, array::{Array, ArraySize}, consts::{U12, U16, U24, U32}, + inout::InOutBuf, }; use core::marker::PhantomData; @@ -254,32 +255,28 @@ where { } -impl AeadInPlaceDetached for ChaChaPoly1305 +impl AeadInOut for ChaChaPoly1305 where C: KeyIvInit + StreamCipher + StreamCipherSeek, N: ArraySize, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &aead::Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result { - Cipher::new(C::new(&self.key, nonce)).encrypt_in_place_detached(associated_data, buffer) + Cipher::new(C::new(&self.key, nonce)).encrypt_inout_detached(associated_data, buffer) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &aead::Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { - Cipher::new(C::new(&self.key, nonce)).decrypt_in_place_detached( - associated_data, - buffer, - tag, - ) + Cipher::new(C::new(&self.key, nonce)).decrypt_inout_detached(associated_data, buffer, tag) } } diff --git a/deoxys/src/lib.rs b/deoxys/src/lib.rs index 2ea45a76..fe68704a 100644 --- a/deoxys/src/lib.rs +++ b/deoxys/src/lib.rs @@ -110,12 +110,13 @@ mod deoxys_bc; /// Operation modes for Deoxys. mod modes; -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser, consts}; +pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser, consts}; use aead::{ PostfixTagged, array::{Array, ArraySize}, consts::U16, + inout::InOutBuf, }; use core::marker::PhantomData; @@ -268,16 +269,16 @@ where { } -impl AeadInPlaceDetached for Deoxys +impl AeadInOut for Deoxys where M: DeoxysMode, B: DeoxysBcType, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result { Ok(Tag::from(M::encrypt_in_place( nonce, @@ -287,11 +288,11 @@ where ))) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { M::decrypt_in_place(nonce, associated_data, buffer, tag, &self.subkeys) diff --git a/eax/src/lib.rs b/eax/src/lib.rs index c4d6cf03..e619c305 100644 --- a/eax/src/lib.rs +++ b/eax/src/lib.rs @@ -97,7 +97,7 @@ //! # { //! use aes::Aes256; //! use eax::Eax; -//! use eax::aead::{AeadInPlaceDetached, KeyInit, array::Array}; +//! use eax::aead::{AeadInOut, KeyInit, array::Array}; //! use eax::aead::heapless::Vec; //! use eax::aead::consts::{U8, U128}; //! @@ -110,7 +110,7 @@ //! buffer.extend_from_slice(b"plaintext message"); //! //! // Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext -//! let tag = cipher.encrypt_in_place_detached(nonce, b"", &mut buffer).expect("encryption failure!"); +//! let tag = cipher.encrypt_inout_detached(nonce, b"", &mut buffer).expect("encryption failure!"); //! //! // The tag has only 8 bytes, compared to the usual 16 bytes //! assert_eq!(tag.len(), 8); @@ -119,15 +119,15 @@ //! assert_ne!(&buffer, b"plaintext message"); //! //! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext -//! cipher.decrypt_in_place_detached(nonce, b"", &mut buffer, &tag).expect("decryption failure!"); +//! cipher.decrypt_inout_detached(nonce, b"", &mut buffer, &tag).expect("decryption failure!"); //! assert_eq!(&buffer, b"plaintext message"); //! # } //! ``` -pub use aead::{self, AeadCore, AeadInPlaceDetached, Error, Key, KeyInit, KeySizeUser}; +pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser}; pub use cipher; -use aead::PostfixTagged; +use aead::{PostfixTagged, inout::InOutBuf}; use cipher::{ BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipherCore, array::Array, consts::U16, crypto_common::OutputSizeUser, typenum::Unsigned, @@ -219,16 +219,16 @@ where { } -impl AeadInPlaceDetached for Eax +impl AeadInOut for Eax where Cipher: BlockSizeUser + BlockCipherEncrypt + Clone + KeyInit, M: TagSize, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result, Error> { if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX { return Err(Error); @@ -267,11 +267,11 @@ where Ok(tag) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { if buffer.len() as u64 > C_MAX || associated_data.len() as u64 > A_MAX { diff --git a/ocb3/src/lib.rs b/ocb3/src/lib.rs index a6808ff9..261a863e 100644 --- a/ocb3/src/lib.rs +++ b/ocb3/src/lib.rs @@ -14,11 +14,11 @@ pub mod consts { } pub use aead::{ - self, AeadCore, AeadInPlaceDetached, Error, KeyInit, KeySizeUser, + self, AeadCore, AeadInOut, Error, KeyInit, KeySizeUser, array::{Array, AssocArraySize}, }; -use aead::{PostfixTagged, array::ArraySize}; +use aead::{PostfixTagged, array::ArraySize, inout::InOutBuf}; use cipher::{ BlockCipherDecrypt, BlockCipherEncrypt, BlockSizeUser, consts::{U12, U16}, @@ -198,17 +198,17 @@ where { } -impl AeadInPlaceDetached for Ocb3 +impl AeadInOut for Ocb3 where Cipher: BlockSizeUser + BlockCipherEncrypt + BlockCipherDecrypt, NonceSize: sealed::NonceSizes, TagSize: sealed::TagSizes, { - fn encrypt_in_place_detached( + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> aead::Result> { if (buffer.len() > P_MAX) || (associated_data.len() > A_MAX) { unimplemented!() @@ -263,11 +263,11 @@ where Ok(tag) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &aead::Tag, ) -> aead::Result<()> { let expected_tag = self.decrypt_in_place_return_tag(nonce, associated_data, buffer); diff --git a/ocb3/tests/kats.rs b/ocb3/tests/kats.rs index 7181c5c9..b58069d7 100644 --- a/ocb3/tests/kats.rs +++ b/ocb3/tests/kats.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] use aead::{ - AeadInPlaceDetached, KeyInit, + AeadInOut, KeyInit, consts::{U8, U12}, }; use aes::{Aes128, Aes192, Aes256}; @@ -38,7 +38,7 @@ macro_rules! rfc7253_wider_variety { let N = num2str96(3 * i + 1); let mut buffer = S.clone(); let tag = ocb - .encrypt_in_place_detached(N.as_slice().try_into().unwrap(), &S, &mut buffer) + .encrypt_inout_detached(N.as_slice().try_into().unwrap(), &S, &mut buffer) .unwrap(); ciphertext.append(&mut buffer); ciphertext.append(&mut tag.as_slice().to_vec()); @@ -48,7 +48,7 @@ macro_rules! rfc7253_wider_variety { let N = num2str96(3 * i + 2); let mut buffer = S.clone(); let tag = ocb - .encrypt_in_place_detached(N.as_slice().try_into().unwrap(), &[], &mut buffer) + .encrypt_inout_detached(N.as_slice().try_into().unwrap(), &[], &mut buffer) .unwrap(); ciphertext.append(&mut buffer); ciphertext.append(&mut tag.as_slice().to_vec()); @@ -57,7 +57,7 @@ macro_rules! rfc7253_wider_variety { // C = C || OCB-ENCRYPT(K,N,S,) let N = num2str96(3 * i + 3); let tag = ocb - .encrypt_in_place_detached(N.as_slice().try_into().unwrap(), &S, &mut []) + .encrypt_inout_detached(N.as_slice().try_into().unwrap(), &S, &mut []) .unwrap(); ciphertext.append(&mut tag.as_slice().to_vec()); } @@ -75,7 +75,7 @@ macro_rules! rfc7253_wider_variety { // Output : OCB-ENCRYPT(K,N,C,) let N = num2str96(385); let tag = ocb - .encrypt_in_place_detached(N.as_slice().try_into().unwrap(), &ciphertext, &mut []) + .encrypt_inout_detached(N.as_slice().try_into().unwrap(), &ciphertext, &mut []) .unwrap(); assert_eq!(tag.as_slice(), hex!($expected)) diff --git a/xaes-256-gcm/src/lib.rs b/xaes-256-gcm/src/lib.rs index a89ff81e..362eb574 100644 --- a/xaes-256-gcm/src/lib.rs +++ b/xaes-256-gcm/src/lib.rs @@ -56,7 +56,7 @@ pub use aes_gcm; use core::ops::{Div, Mul}; use aead::{ - AeadCore, AeadInPlaceDetached, Error, KeyInit, KeySizeUser, PostfixTagged, array::Array, + AeadCore, AeadInOut, Error, KeyInit, KeySizeUser, PostfixTagged, array::Array, inout::InOutBuf, }; use aes::Aes256; use aes_gcm::Aes256Gcm; @@ -128,12 +128,12 @@ impl KeyInit for Xaes256Gcm { impl PostfixTagged for Xaes256Gcm {} -impl AeadInPlaceDetached for Xaes256Gcm { - fn encrypt_in_place_detached( +impl AeadInOut for Xaes256Gcm { + fn encrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, ) -> Result { if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX { return Err(Error); @@ -141,14 +141,14 @@ impl AeadInPlaceDetached for Xaes256Gcm { let (n1, n) = nonce.split_ref::<>::Output>(); let k = self.derive_key(n1); - Aes256Gcm::new(&k).encrypt_in_place_detached(n, associated_data, buffer) + Aes256Gcm::new(&k).encrypt_inout_detached(n, associated_data, buffer) } - fn decrypt_in_place_detached( + fn decrypt_inout_detached( &self, nonce: &Nonce, associated_data: &[u8], - buffer: &mut [u8], + buffer: InOutBuf<'_, '_, u8>, tag: &Tag, ) -> Result<(), Error> { if buffer.len() as u64 > C_MAX || associated_data.len() as u64 > A_MAX { @@ -157,7 +157,7 @@ impl AeadInPlaceDetached for Xaes256Gcm { let (n1, n) = nonce.split_ref::<>::Output>(); let k = self.derive_key(n1); - Aes256Gcm::new(&k).decrypt_in_place_detached(n, associated_data, buffer, tag) + Aes256Gcm::new(&k).decrypt_inout_detached(n, associated_data, buffer, tag) } } diff --git a/xaes-256-gcm/tests/xaes256gcm.rs b/xaes-256-gcm/tests/xaes256gcm.rs index 1bb04ce1..62b85d7e 100644 --- a/xaes-256-gcm/tests/xaes256gcm.rs +++ b/xaes-256-gcm/tests/xaes256gcm.rs @@ -4,7 +4,7 @@ #[path = "../../aes-gcm/tests/common/mod.rs"] mod common; -use aes_gcm::aead::{Aead, AeadInPlaceDetached, KeyInit, Payload, array::Array}; +use aes_gcm::aead::{Aead, AeadInOut, KeyInit, Payload, array::Array}; use common::TestVector; use hex_literal::hex; use xaes_256_gcm::Xaes256Gcm;