Skip to content

Commit

Permalink
chacha20poly1305: aead crate upgrade
Browse files Browse the repository at this point in the history
Upgrades to the `aead` v0.3.0-pre crate.
  • Loading branch information
tarcieri committed Jun 1, 2020
1 parent fed6ce8 commit 58f4d3f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 39 deletions.
23 changes: 15 additions & 8 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -14,11 +14,13 @@ aes = { git = "https://github.com/RustCrypto/block-ciphers" }
aead = { git = "https://github.com/RustCrypto/traits" }
block-cipher = { git = "https://github.com/RustCrypto/traits" }
crypto-mac = { git = "https://github.com/RustCrypto/traits" }
chacha20 = { git = "https://github.com/RustCrypto/stream-ciphers" }
cmac = { git = "https://github.com/RustCrypto/MACs" }
ctr = { git = "https://github.com/RustCrypto/stream-ciphers" }
dbl = { git = "https://github.com/RustCrypto/utils" }
ghash = { git = "https://github.com/RustCrypto/universal-hashes" }
pmac = { git = "https://github.com/RustCrypto/MACs" }
poly1305 = { git = "https://github.com/RustCrypto/universal-hashes" }
polyval = { git = "https://github.com/RustCrypto/universal-hashes" }
stream-cipher = { git = "https://github.com/RustCrypto/traits" }
universal-hash = { git = "https://github.com/RustCrypto/traits" }
8 changes: 4 additions & 4 deletions chacha20poly1305/Cargo.toml
Expand Up @@ -18,10 +18,10 @@ keywords = ["aead", "chacha20", "poly1305", "xchacha20", "xchacha20poly1305"]
categories = ["cryptography", "no-std"]

[dependencies]
aead = { version = "0.2", default-features = false }
chacha20 = { version = "0.3", features = ["zeroize"], optional = true }
poly1305 = "0.5"
stream-cipher = "0.3"
aead = { version = "= 0.3.0-pre", default-features = false }
chacha20 = { version = "= 0.4.0-pre", features = ["zeroize"], optional = true }
poly1305 = "= 0.6.0-pre"
stream-cipher = "= 0.4.0-pre"
zeroize = { version = "1", default-features = false }

[dev-dependencies]
Expand Down
18 changes: 13 additions & 5 deletions chacha20poly1305/src/cipher.rs
Expand Up @@ -3,9 +3,12 @@
use aead::generic_array::GenericArray;
use aead::Error;
use core::convert::TryInto;
use poly1305::{universal_hash::UniversalHash, Poly1305};
use poly1305::{
universal_hash::{NewUniversalHash, UniversalHash},
Poly1305,
};
use stream_cipher::{SyncStreamCipher, SyncStreamCipherSeek};
use zeroize::Zeroizing;
use zeroize::Zeroize;

use super::Tag;

Expand All @@ -32,9 +35,10 @@ where
/// Instantiate the underlying cipher with a particular nonce
pub(crate) fn new(mut cipher: C) -> Self {
// Derive Poly1305 key from the first 32-bytes of the ChaCha20 keystream
let mut mac_key = Zeroizing::new(poly1305::Key::default());
let mut mac_key = poly1305::Key::default();
cipher.apply_keystream(&mut *mac_key);
let mac = Poly1305::new(GenericArray::from_slice(&*mac_key));
mac_key.zeroize();

// Set ChaCha20 counter to 1
cipher.seek(BLOCK_SIZE as u64);
Expand Down Expand Up @@ -94,8 +98,12 @@ where
fn authenticate_lengths(&mut self, associated_data: &[u8], buffer: &[u8]) -> Result<(), Error> {
let associated_data_len: u64 = associated_data.len().try_into().map_err(|_| Error)?;
let buffer_len: u64 = buffer.len().try_into().map_err(|_| Error)?;
self.mac.update(&associated_data_len.to_le_bytes());
self.mac.update(&buffer_len.to_le_bytes());

let mut block = GenericArray::default();
block[..8].copy_from_slice(&associated_data_len.to_le_bytes());
block[8..].copy_from_slice(&buffer_len.to_le_bytes());
self.mac.update(&block);

Ok(())
}
}
20 changes: 10 additions & 10 deletions chacha20poly1305/src/lib.rs
Expand Up @@ -50,7 +50,7 @@
//! use chacha20poly1305::ChaCha20Poly1305; // Or `XChaCha20Poly1305`
//! use aead::{Aead, NewAead, generic_array::GenericArray};
//!
//! let key = GenericArray::clone_from_slice(b"an example very very secret key."); // 32-bytes
//! let key = GenericArray::from_slice(b"an example very very secret key."); // 32-bytes
//! let aead = ChaCha20Poly1305::new(key);
//!
//! let nonce = GenericArray::from_slice(b"unique nonce"); // 12-bytes; unique per message
Expand All @@ -76,11 +76,11 @@
//!
//! ```
//! use chacha20poly1305::ChaCha20Poly1305; // Or `XChaCha20Poly1305`
//! use aead::{Aead, NewAead};
//! use aead::{AeadInPlace, NewAead};
//! use aead::generic_array::{GenericArray, typenum::U128};
//! use aead::heapless::Vec;
//!
//! let key = GenericArray::clone_from_slice(b"an example very very secret key.");
//! let key = GenericArray::from_slice(b"an example very very secret key.");
//! let aead = ChaCha20Poly1305::new(key);
//!
//! let nonce = GenericArray::from_slice(b"unique nonce"); // 128-bits; unique per message
Expand Down Expand Up @@ -124,11 +124,11 @@ pub use aead;
pub use xchacha20poly1305::XChaCha20Poly1305;

use self::cipher::Cipher;
use aead::generic_array::{
typenum::{U0, U12, U16, U32},
GenericArray,
use aead::{
consts::{U0, U12, U16, U32},
generic_array::GenericArray,
AeadInPlace, Error, NewAead,
};
use aead::{Aead, Error, NewAead};
use core::marker::PhantomData;
use stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
use zeroize::Zeroize;
Expand Down Expand Up @@ -176,15 +176,15 @@ where
{
type KeySize = U32;

fn new(key: GenericArray<u8, U32>) -> Self {
fn new(key: &GenericArray<u8, U32>) -> Self {
Self {
key,
key: *key,
stream_cipher: PhantomData,
}
}
}

impl<C> Aead for ChaChaPoly1305<C>
impl<C> AeadInPlace for ChaChaPoly1305<C>
where
C: NewStreamCipher<KeySize = U32, NonceSize = U12> + SyncStreamCipher + SyncStreamCipherSeek,
{
Expand Down
16 changes: 8 additions & 8 deletions chacha20poly1305/src/xchacha20poly1305.rs
Expand Up @@ -3,11 +3,11 @@
//! See [`XChaCha20Poly1305`] documentation for usage.

use crate::{cipher::Cipher, Tag};
use aead::generic_array::{
typenum::{U0, U16, U24, U32},
GenericArray,
use aead::{
consts::{U0, U16, U24, U32},
generic_array::GenericArray,
AeadInPlace, Error, NewAead,
};
use aead::{Aead, Error, NewAead};
use chacha20::{stream_cipher::NewStreamCipher, XChaCha20};
use zeroize::Zeroize;

Expand Down Expand Up @@ -44,7 +44,7 @@ use zeroize::Zeroize;
/// use chacha20poly1305::XChaCha20Poly1305;
/// use aead::{Aead, NewAead, generic_array::GenericArray};
///
/// let key = GenericArray::clone_from_slice(b"an example very very secret key."); // 32-bytes
/// let key = GenericArray::from_slice(b"an example very very secret key."); // 32-bytes
/// let aead = XChaCha20Poly1305::new(key);
///
/// let nonce = GenericArray::from_slice(b"extra long unique nonce!"); // 24-bytes; unique
Expand All @@ -61,12 +61,12 @@ pub struct XChaCha20Poly1305 {
impl NewAead for XChaCha20Poly1305 {
type KeySize = U32;

fn new(key: GenericArray<u8, U32>) -> Self {
XChaCha20Poly1305 { key }
fn new(key: &GenericArray<u8, U32>) -> Self {
XChaCha20Poly1305 { key: *key }
}
}

impl Aead for XChaCha20Poly1305 {
impl AeadInPlace for XChaCha20Poly1305 {
type NonceSize = U24;
type TagSize = U16;
type CiphertextOverhead = U0;
Expand Down
8 changes: 4 additions & 4 deletions chacha20poly1305/tests/lib.rs
Expand Up @@ -11,7 +11,7 @@ macro_rules! impl_tests {
aad: $aad,
};

let ciphertext = <$cipher>::new(*key).encrypt(nonce, payload).unwrap();
let ciphertext = <$cipher>::new(key).encrypt(nonce, payload).unwrap();

let tag_begins = ciphertext.len() - 16;
assert_eq!($ciphertext, &ciphertext[..tag_begins]);
Expand All @@ -30,7 +30,7 @@ macro_rules! impl_tests {
aad: $aad,
};

let plaintext = <$cipher>::new(*key).decrypt(nonce, payload).unwrap();
let plaintext = <$cipher>::new(key).decrypt(nonce, payload).unwrap();

assert_eq!($plaintext, plaintext.as_slice());
}
Expand All @@ -51,7 +51,7 @@ macro_rules! impl_tests {
aad: $aad,
};

let cipher = <$cipher>::new(*key);
let cipher = <$cipher>::new(key);
assert!(cipher.decrypt(nonce, payload).is_err());
}
};
Expand Down Expand Up @@ -115,7 +115,7 @@ mod chacha20 {

#[test]
fn clone_impl() {
let _ = ChaCha20Poly1305::new(GenericArray::clone_from_slice(KEY)).clone();
let _ = ChaCha20Poly1305::new(GenericArray::from_slice(KEY)).clone();
}
}

Expand Down

0 comments on commit 58f4d3f

Please sign in to comment.