Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding detached tag mode to stream #1189

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 117 additions & 1 deletion aead/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#![allow(clippy::upper_case_acronyms)]

use crate::{AeadCore, AeadInPlace, Buffer, Error, Key, KeyInit, Result};
use crate::{AeadCore, AeadInPlace, Buffer, Error, Key, KeyInit, Result, Tag};
use core::ops::{AddAssign, Sub};
use generic_array::{
typenum::{Unsigned, U4, U5},
Expand Down Expand Up @@ -128,6 +128,25 @@ where
buffer: &mut dyn Buffer,
) -> Result<()>;

/// Encrypt an AEAD message in-place at the given position in the STREAM. Return tag separately.
fn encrypt_in_place_detached(
&self,
position: Self::Counter,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<Tag<A>>;

/// Decrypt an AEAD message in-place at the given position in the STREAM. Accept tag separately.
fn decrypt_in_place_detached(
&self,
position: Self::Counter,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
tag: &Tag<A>,
) -> Result<()>;

/// Encrypt the given plaintext payload, and return the resulting
/// ciphertext as a vector of bytes.
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -346,6 +365,29 @@ impl_stream_object!(
"ℰ STREAM encryptor"
);

impl<A, S> Encryptor<A, S>
where
A: AeadInPlace,
S: StreamPrimitive<A>,
A::NonceSize: Sub<<S as StreamPrimitive<A>>::NonceOverhead>,
NonceSize<A, S>: ArrayLength<u8>,
{
#[doc = "Use the underlying AEAD to encrypt"]
#[doc = "the last AEAD message in this STREAM in-place,"]
#[doc = "consuming the "]
#[doc = "ℰ STREAM encryptor"]
#[doc = "object in order to prevent further use."]
#[doc = "Returns the tag separately."]
pub fn encrypt_last_in_place_detached(
self,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<Tag<A>> {
self.stream
.encrypt_in_place_detached(self.position, true, associated_data, buffer)
}
}

impl_stream_object!(
Decryptor,
decrypt_next,
Expand All @@ -358,6 +400,30 @@ impl_stream_object!(
"𝒟 STREAM decryptor"
);

impl<A, S> Decryptor<A, S>
where
A: AeadInPlace,
S: StreamPrimitive<A>,
A::NonceSize: Sub<<S as StreamPrimitive<A>>::NonceOverhead>,
NonceSize<A, S>: ArrayLength<u8>,
{
#[doc = "Use the underlying AEAD to decrypt"]
#[doc = "the last AEAD message in this STREAM in-place,"]
#[doc = "consuming the "]
#[doc = "𝒟 STREAM decryptor"]
#[doc = "object in order to prevent further use."]
#[doc = "Accepts the tag separately."]
pub fn decrypt_last_in_place_detached(
self,
associated_data: &[u8],
buffer: &mut dyn Buffer,
tag: &Tag<A>,
) -> Result<()> {
self.stream
.decrypt_in_place_detached(self.position, true, associated_data, buffer, tag)
}
}

/// The original "Rogaway-flavored" STREAM as described in the paper
/// [Online Authenticated-Encryption and its Nonce-Reuse Misuse-Resistance][1].
///
Expand Down Expand Up @@ -424,6 +490,31 @@ where
let nonce = self.aead_nonce(position, last_block);
self.aead.decrypt_in_place(&nonce, associated_data, buffer)
}

fn encrypt_in_place_detached(
&self,
position: u32,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<Tag<A>> {
let nonce = self.aead_nonce(position, last_block);
self.aead
.encrypt_in_place_detached(&nonce, associated_data, buffer.as_mut())
}

fn decrypt_in_place_detached(
&self,
position: Self::Counter,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
tag: &Tag<A>,
) -> Result<()> {
let nonce = self.aead_nonce(position, last_block);
self.aead
.decrypt_in_place_detached(&nonce, associated_data, buffer.as_mut(), tag)
}
}

impl<A> StreamBE32<A>
Expand Down Expand Up @@ -503,6 +594,18 @@ where
self.aead.encrypt_in_place(&nonce, associated_data, buffer)
}

fn encrypt_in_place_detached(
&self,
position: Self::Counter,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<Tag<A>> {
let nonce = self.aead_nonce(position, last_block)?;
self.aead
.encrypt_in_place_detached(&nonce, associated_data, buffer.as_mut())
}

fn decrypt_in_place(
&self,
position: Self::Counter,
Expand All @@ -513,6 +616,19 @@ where
let nonce = self.aead_nonce(position, last_block)?;
self.aead.decrypt_in_place(&nonce, associated_data, buffer)
}

fn decrypt_in_place_detached(
&self,
position: Self::Counter,
last_block: bool,
associated_data: &[u8],
buffer: &mut dyn Buffer,
tag: &Tag<A>,
) -> Result<()> {
let nonce = self.aead_nonce(position, last_block)?;
self.aead
.decrypt_in_place_detached(&nonce, associated_data, buffer.as_mut(), tag)
}
}

impl<A> StreamLE31<A>
Expand Down