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

Handles variable-length nonce #85

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 18 additions & 23 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ pub trait NewAead {
/// This trait is intended for use with stateless AEAD algorithms. The
/// [`AeadMut`] trait provides a stateful interface.
pub trait Aead {
/// The length of a nonce.
type NonceSize: ArrayLength<u8>;
/// The maximum length of the nonce.
type TagSize: ArrayLength<u8>;
/// The upper bound amount of additional space required to support a
Expand Down Expand Up @@ -108,7 +106,7 @@ pub trait Aead {
#[cfg(feature = "alloc")]
fn encrypt<'msg, 'aad>(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
plaintext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
let payload = plaintext.into();
Expand All @@ -129,7 +127,7 @@ pub trait Aead {
/// resulting ciphertext message.
fn encrypt_in_place(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -141,7 +139,7 @@ pub trait Aead {
/// Encrypt the data in-place, returning the authentication tag
fn encrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<GenericArray<u8, Self::TagSize>, Error>;
Expand All @@ -166,7 +164,7 @@ pub trait Aead {
#[cfg(feature = "alloc")]
fn decrypt<'msg, 'aad>(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
ciphertext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
let payload = ciphertext.into();
Expand All @@ -182,7 +180,7 @@ pub trait Aead {
/// message upon success.
fn decrypt_in_place(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -194,7 +192,7 @@ pub trait Aead {
/// is modified/unauthentic)
fn decrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
tag: &GenericArray<u8, Self::TagSize>,
Expand All @@ -203,8 +201,6 @@ pub trait Aead {

/// Stateful Authenticated Encryption with Associated Data algorithm.
pub trait AeadMut {
/// The length of a nonce.
type NonceSize: ArrayLength<u8>;
/// The maximum length of the nonce.
type TagSize: ArrayLength<u8>;
/// The upper bound amount of additional space required to support a
Expand All @@ -219,7 +215,7 @@ pub trait AeadMut {
#[cfg(feature = "alloc")]
fn encrypt<'msg, 'aad>(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
plaintext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
let payload = plaintext.into();
Expand All @@ -240,7 +236,7 @@ pub trait AeadMut {
/// resulting ciphertext message.
fn encrypt_in_place(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -252,7 +248,7 @@ pub trait AeadMut {
/// Encrypt the data in-place, returning the authentication tag
fn encrypt_in_place_detached(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<GenericArray<u8, Self::TagSize>, Error>;
Expand All @@ -265,7 +261,7 @@ pub trait AeadMut {
#[cfg(feature = "alloc")]
fn decrypt<'msg, 'aad>(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
ciphertext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
let payload = ciphertext.into();
Expand All @@ -281,7 +277,7 @@ pub trait AeadMut {
/// message upon success.
fn decrypt_in_place(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -293,7 +289,7 @@ pub trait AeadMut {
/// is modified/unauthentic)
fn decrypt_in_place_detached(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
tag: &GenericArray<u8, Self::TagSize>,
Expand All @@ -303,7 +299,6 @@ pub trait AeadMut {
/// A blanket implementation of the Stateful AEAD interface for Stateless
/// AEAD implementations.
impl<Algo: Aead> AeadMut for Algo {
type NonceSize = Algo::NonceSize;
type TagSize = Algo::TagSize;
type CiphertextOverhead = Algo::CiphertextOverhead;

Expand All @@ -312,7 +307,7 @@ impl<Algo: Aead> AeadMut for Algo {
#[cfg(feature = "alloc")]
fn encrypt<'msg, 'aad>(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
plaintext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
<Self as Aead>::encrypt(self, nonce, plaintext)
Expand All @@ -321,7 +316,7 @@ impl<Algo: Aead> AeadMut for Algo {
/// Encrypt the given buffer containing a plaintext message in-place.
fn encrypt_in_place(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -331,7 +326,7 @@ impl<Algo: Aead> AeadMut for Algo {
/// Encrypt the data in-place, returning the authentication tag
fn encrypt_in_place_detached(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<GenericArray<u8, Self::TagSize>, Error> {
Expand All @@ -343,7 +338,7 @@ impl<Algo: Aead> AeadMut for Algo {
#[cfg(feature = "alloc")]
fn decrypt<'msg, 'aad>(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
ciphertext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
<Self as Aead>::decrypt(self, nonce, ciphertext)
Expand All @@ -353,7 +348,7 @@ impl<Algo: Aead> AeadMut for Algo {
/// provided authentication tag does not match the given ciphertext.
fn decrypt_in_place(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut impl Buffer,
) -> Result<(), Error> {
Expand All @@ -365,7 +360,7 @@ impl<Algo: Aead> AeadMut for Algo {
/// is modified/unauthentic)
fn decrypt_in_place_detached(
&mut self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
tag: &GenericArray<u8, Self::TagSize>,
Expand Down