Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions aead/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aead"
version = "0.4.3" # Also update html_root_url in lib.rs when bumping this
version = "0.5.0-pre" # Also update html_root_url in lib.rs when bumping this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: breaking change

description = """
Traits for Authenticated Encryption with Associated Data (AEAD) algorithms,
such as AES-GCM as ChaCha20Poly1305, which provide a high-level API
Expand All @@ -15,18 +15,20 @@ keywords = ["crypto", "encryption"]
categories = ["cryptography", "no-std"]

[dependencies]
crypto-common = { version = "0.1", path = "../crypto-common" }
generic-array = { version = "0.14", default-features = false }

# optional dependencies
blobby = { version = "0.3", optional = true }
bytes = { version = "1", optional = true, default-features = false }
heapless = { version = "0.7", optional = true, default-features = false }
rand_core = { version = "0.6", optional = true }

[features]
default = ["rand_core"]
alloc = []
std = ["alloc", "rand_core/std"]
std = ["alloc", "crypto-common/std"]
dev = ["blobby"]
rand_core = ["crypto-common/rand_core"]
stream = []

[package.metadata.docs.rs]
Expand Down
45 changes: 3 additions & 42 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#![forbid(unsafe_code, clippy::unwrap_used)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_root_url = "https://docs.rs/aead/0.4.3"
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms)]

Expand All @@ -37,6 +36,7 @@ pub mod dev;
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
pub mod stream;

pub use crypto_common::{Key, KeyInit, KeySizeUser};
pub use generic_array::{self, typenum::consts};

#[cfg(feature = "bytes")]
Expand All @@ -49,7 +49,7 @@ pub use heapless;

#[cfg(feature = "rand_core")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))]
pub use rand_core;
pub use crypto_common::rand_core;

use core::fmt;
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
Expand All @@ -60,9 +60,6 @@ use alloc::vec::Vec;
#[cfg(feature = "bytes")]
use bytes::BytesMut;

#[cfg(feature = "rand_core")]
use rand_core::{CryptoRng, RngCore};

/// Error type.
///
/// This type is deliberately opaque as to avoid potential side-channel
Expand All @@ -82,48 +79,12 @@ impl fmt::Display for Error {
#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// Key for a [`NewAead`] algorithm
// TODO(tarcieri): make this a struct and zeroize on drop?
pub type Key<A> = GenericArray<u8, <A as NewAead>::KeySize>;

/// Nonce: single-use value for ensuring ciphertexts are unique
pub type Nonce<A> = GenericArray<u8, <A as AeadCore>::NonceSize>;

/// Tag: authentication code which ensures ciphertexts are authentic
pub type Tag<A> = GenericArray<u8, <A as AeadCore>::TagSize>;

/// Instantiate either a stateless [`Aead`] or stateful [`AeadMut`] algorithm.
pub trait NewAead {
/// The size of the key array required by this algorithm.
type KeySize: ArrayLength<u8>;

/// Create a new AEAD instance with the given key.
fn new(key: &Key<Self>) -> Self;

/// Create new AEAD instance from key given as a byte slice..
///
/// Default implementation will accept only keys with length equal to `KeySize`.
fn new_from_slice(key: &[u8]) -> Result<Self>
where
Self: Sized,
{
if key.len() != Self::KeySize::to_usize() {
Err(Error)
} else {
Ok(Self::new(GenericArray::from_slice(key)))
}
}

/// Generate a random key for this AEAD using the provided [`CryptoRng`].
#[cfg(feature = "rand_core")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand_core")))]
fn generate_key(mut rng: impl CryptoRng + RngCore) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
key
}
}

/// Authenticated Encryption with Associated Data (AEAD) algorithm core trait.
///
/// Defines nonce, tag, and overhead sizes that are consumed by various other
Expand Down
8 changes: 4 additions & 4 deletions 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, NewAead, Result};
use crate::{AeadCore, AeadInPlace, Buffer, Error, Key, KeyInit, Result};
use core::ops::{AddAssign, Sub};
use generic_array::{
typenum::{Unsigned, U4, U5},
Expand Down Expand Up @@ -76,7 +76,7 @@ where
/// Create a new STREAM with the given key and nonce.
fn new(key: &Key<A>, nonce: &Nonce<A, Self>) -> Self
where
A: NewAead,
A: KeyInit,
Self: Sized,
{
Self::from_aead(A::new(key), nonce)
Expand Down Expand Up @@ -227,7 +227,7 @@ macro_rules! impl_stream_object {
#[doc = "object from the given AEAD key and nonce."]
pub fn new(key: &Key<A>, nonce: &Nonce<A, S>) -> Self
where
A: NewAead,
A: KeyInit,
S: NewStream<A>,
{
Self::from_stream_primitive(S::new(key, nonce))
Expand All @@ -238,7 +238,7 @@ macro_rules! impl_stream_object {
#[doc = "object from the given AEAD primitive."]
pub fn from_aead(aead: A, nonce: &Nonce<A, S>) -> Self
where
A: NewAead,
A: KeyInit,
S: NewStream<A>,
{
Self::from_stream_primitive(S::from_aead(aead, nonce))
Expand Down
2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2021"
rust-version = "1.57"

[dependencies]
aead = { version = "0.4", optional = true, path = "../aead" }
aead = { version = "=0.5.0-pre", optional = true, path = "../aead" }
cipher = { version = "0.4", optional = true }
digest = { version = "0.10", optional = true }
elliptic-curve = { version = "0.12", optional = true, path = "../elliptic-curve" }
Expand Down