Skip to content

Commit

Permalink
inout: Rename aead::InOut to inout::overlapping::Overlapping.
Browse files Browse the repository at this point in the history
Take a step towards `Overlapping` being used by more things.
  • Loading branch information
briansmith committed Dec 31, 2024
1 parent 0a00284 commit 7d6cb12
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub use self::{
sealing_key::SealingKey,
unbound_key::UnboundKey,
};
use inout::InOut;

/// A sequences of unique nonces.
///
Expand Down Expand Up @@ -176,7 +175,6 @@ mod chacha;
mod chacha20_poly1305;
pub mod chacha20_poly1305_openssh;
mod gcm;
mod inout;
mod less_safe_key;
mod nonce;
mod opening_key;
Expand Down
9 changes: 6 additions & 3 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{nonce::Nonce, quic::Sample, InOut, NONCE_LEN};
use super::{nonce::Nonce, quic::Sample, NONCE_LEN};
use crate::{
constant_time,
cpu::{self, GetFeature as _},
error,
inout::overlapping,
polyfill::unwrap_const,
};
use cfg_if::cfg_if;
Expand All @@ -32,6 +33,8 @@ pub(super) mod fallback;
pub(super) mod hw;
pub(super) mod vp;

pub type Overlapping<'o> = overlapping::Overlapping<'o>;

cfg_if! {
if #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] {
pub(super) use ffi::AES_KEY;
Expand Down Expand Up @@ -161,7 +164,7 @@ pub(super) trait EncryptBlock {
}

pub(super) trait EncryptCtr32 {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter);
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter);
}

#[allow(dead_code)]
Expand All @@ -181,7 +184,7 @@ fn encrypt_iv_xor_block_using_encrypt_block(
#[allow(dead_code)]
fn encrypt_iv_xor_block_using_ctr32(key: &impl EncryptCtr32, iv: Iv, mut block: Block) -> Block {
let mut ctr = Counter(iv.0); // This is OK because we're only encrypting one block.
key.ctr32_encrypt_within(InOut::in_place(&mut block), &mut ctr);
key.ctr32_encrypt_within(Overlapping::in_place(&mut block), &mut ctr);
block
}

Expand Down
4 changes: 2 additions & 2 deletions src/aead/aes/bs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#![cfg(target_arch = "arm")]

use super::{Counter, InOut, AES_KEY};
use super::{Counter, Overlapping, AES_KEY};

/// SAFETY:
/// * The caller must ensure that if blocks > 0 then either `input` and
Expand All @@ -27,7 +27,7 @@ use super::{Counter, InOut, AES_KEY};
/// * Upon returning, `blocks` blocks will have been read from `input` and
/// written to `output`.
pub(super) unsafe fn ctr32_encrypt_blocks_with_vpaes_key(
in_out: InOut<'_>,
in_out: Overlapping<'_>,
vpaes_key: &AES_KEY,
ctr: &mut Counter,
) {
Expand Down
4 changes: 2 additions & 2 deletions src/aead/aes/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{Block, Counter, EncryptBlock, EncryptCtr32, InOut, Iv, KeyBytes, AES_KEY};
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
use crate::error;

#[derive(Clone)]
Expand All @@ -38,7 +38,7 @@ impl EncryptBlock for Key {
}

impl EncryptCtr32 for Key {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter) {
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
unsafe { ctr32_encrypt_blocks!(aes_nohw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
}
}
4 changes: 2 additions & 2 deletions src/aead/aes/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{Block, InOut, KeyBytes, BLOCK_LEN};
use super::{Block, KeyBytes, Overlapping, BLOCK_LEN};
use crate::{bits::BitLength, c, error};
use core::num::{NonZeroU32, NonZeroUsize};

Expand Down Expand Up @@ -167,7 +167,7 @@ impl AES_KEY {
key: &AES_KEY,
ivec: &Counter,
),
in_out: InOut<'_>,
in_out: Overlapping<'_>,
ctr: &mut Counter,
) {
let (input, output, len) = in_out.into_input_output_len();
Expand Down
4 changes: 2 additions & 2 deletions src/aead/aes/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#![cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]

use super::{Block, Counter, EncryptBlock, EncryptCtr32, InOut, Iv, KeyBytes, AES_KEY};
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
use crate::{cpu, error};

#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -55,7 +55,7 @@ impl EncryptBlock for Key {
}

impl EncryptCtr32 for Key {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter) {
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
#[cfg(target_arch = "x86_64")]
let _: cpu::Features = cpu::features();
unsafe { ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
Expand Down
18 changes: 10 additions & 8 deletions src/aead/aes/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
target_arch = "x86_64"
))]

use super::{Block, Counter, EncryptBlock, EncryptCtr32, InOut, Iv, KeyBytes, AES_KEY};
use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
use crate::{cpu, error};

#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
Expand Down Expand Up @@ -56,14 +56,14 @@ impl EncryptBlock for Key {

#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
impl EncryptCtr32 for Key {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter) {
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
unsafe { ctr32_encrypt_blocks!(vpaes_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
}
}

#[cfg(target_arch = "arm")]
impl EncryptCtr32 for Key {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter) {
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
use super::{bs, BLOCK_LEN};

let in_out = {
Expand All @@ -84,9 +84,11 @@ impl EncryptCtr32 for Key {
0
};
let bsaes_in_out_len = bsaes_blocks * BLOCK_LEN;
let bs_in_out =
InOut::overlapping(&mut in_out[..(src.start + bsaes_in_out_len)], src.clone())
.unwrap();
let bs_in_out = Overlapping::overlapping(
&mut in_out[..(src.start + bsaes_in_out_len)],
src.clone(),
)
.unwrap();

// SAFETY:
// * self.inner was initialized with `vpaes_set_encrypt_key` above,
Expand All @@ -95,7 +97,7 @@ impl EncryptCtr32 for Key {
bs::ctr32_encrypt_blocks_with_vpaes_key(bs_in_out, &self.inner, ctr);
}

InOut::overlapping(&mut in_out[bsaes_in_out_len..], src).unwrap()
Overlapping::overlapping(&mut in_out[bsaes_in_out_len..], src).unwrap()
};

// SAFETY:
Expand All @@ -120,7 +122,7 @@ impl EncryptBlock for Key {

#[cfg(target_arch = "x86")]
impl EncryptCtr32 for Key {
fn ctr32_encrypt_within(&self, in_out: InOut<'_>, ctr: &mut Counter) {
fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
super::super::shift::shift_full_blocks(in_out, |input| {
self.encrypt_iv_xor_block(ctr.increment(), *input)
});
Expand Down
16 changes: 9 additions & 7 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{
aes::{self, Counter, BLOCK_LEN, ZERO_BLOCK},
gcm, shift, Aad, InOut, Nonce, Tag,
aes::{self, Counter, Overlapping, BLOCK_LEN, ZERO_BLOCK},
gcm, shift, Aad, Nonce, Tag,
};
use crate::{
cpu, error,
Expand Down Expand Up @@ -160,7 +160,8 @@ pub(super) fn seal(
}
};
let (whole, remainder) = slice::as_chunks_mut(ramaining);
aes_key.ctr32_encrypt_within(InOut::in_place(slice::flatten_mut(whole)), &mut ctr);
aes_key
.ctr32_encrypt_within(Overlapping::in_place(slice::flatten_mut(whole)), &mut ctr);
auth.update_blocks(whole);
seal_finish(aes_key, auth, remainder, ctr, tag_iv)
}
Expand Down Expand Up @@ -240,7 +241,7 @@ fn seal_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks +
let (whole, remainder) = slice::as_chunks_mut(in_out);

for chunk in whole.chunks_mut(CHUNK_BLOCKS) {
aes_key.ctr32_encrypt_within(InOut::in_place(slice::flatten_mut(chunk)), &mut ctr);
aes_key.ctr32_encrypt_within(Overlapping::in_place(slice::flatten_mut(chunk)), &mut ctr);
auth.update_blocks(chunk);
}

Expand Down Expand Up @@ -275,7 +276,7 @@ pub(super) fn open(
src: RangeFrom<usize>,
) -> Result<Tag, error::Unspecified> {
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
let in_out = InOut::overlapping(in_out_slice, src.clone())?;
let in_out = Overlapping::overlapping(in_out_slice, src.clone())?;

let mut ctr = Counter::one(nonce);
let tag_iv = ctr.increment();
Expand Down Expand Up @@ -331,7 +332,8 @@ pub(super) fn open(
let whole_len = slice::flatten(whole).len();

// Decrypt any remaining whole blocks.
let whole = InOut::overlapping(&mut in_out[..(src.start + whole_len)], src.clone())?;
let whole =
Overlapping::overlapping(&mut in_out[..(src.start + whole_len)], src.clone())?;
aes_key.ctr32_encrypt_within(whole, &mut ctr);

let in_out = match in_out.get_mut(whole_len..) {
Expand Down Expand Up @@ -448,7 +450,7 @@ fn open_strided<A: aes::EncryptBlock + aes::EncryptCtr32, G: gcm::UpdateBlocks +
}
auth.update_blocks(ciphertext);

let chunk = InOut::overlapping(
let chunk = Overlapping::overlapping(
&mut in_out[output..][..(chunk_len + in_prefix_len)],
in_prefix_len..,
)?;
Expand Down
14 changes: 7 additions & 7 deletions src/aead/inout.rs → src/inout/overlapping/overlapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@
use crate::error;
use core::ops::RangeFrom;

pub struct InOut<'i> {
in_out: &'i mut [u8],
pub struct Overlapping<'o> {
in_out: &'o mut [u8],
src: RangeFrom<usize>,
}

impl<'i> InOut<'i> {
pub fn in_place(in_out: &'i mut [u8]) -> Self {
impl<'o> Overlapping<'o> {
pub fn in_place(in_out: &'o mut [u8]) -> Self {
Self { in_out, src: 0.. }
}

pub fn overlapping(in_out: &'i mut [u8], src: RangeFrom<usize>) -> Result<Self, SrcIndexError> {
pub fn overlapping(in_out: &'o mut [u8], src: RangeFrom<usize>) -> Result<Self, SrcIndexError> {
match in_out.get(src.clone()) {
Some(_) => Ok(Self { in_out, src }),
None => Err(SrcIndexError::new(src)),
}
}

#[cfg(any(target_arch = "arm", target_arch = "x86"))]
pub fn into_slice_src_mut(self) -> (&'i mut [u8], RangeFrom<usize>) {
pub fn into_slice_src_mut(self) -> (&'o mut [u8], RangeFrom<usize>) {
(self.in_out, self.src)
}
}

impl InOut<'_> {
impl Overlapping<'_> {
pub fn len(&self) -> usize {
self.in_out[self.src.clone()].len()
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ mod ec;
pub mod error;
pub mod hkdf;
pub mod hmac;
mod inout;
mod limb;
pub mod pbkdf2;
pub mod pkcs8;
Expand Down

0 comments on commit 7d6cb12

Please sign in to comment.