Skip to content

Commit

Permalink
chacha20poly1305 0.8
Browse files Browse the repository at this point in the history
We switch from the c2-chacha crate to the chacha20 crate for trait
compatibility.
  • Loading branch information
str4d committed Aug 8, 2021
1 parent 8d02ba2 commit e10257f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 55 deletions.
67 changes: 27 additions & 40 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions age-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ maintenance = { status = "experimental" }
base64 = "0.13"

# - ChaCha20-Poly1305 from RFC 7539
c2-chacha = "0.3"
chacha20poly1305 = { version = "0.7", default-features = false, features = ["alloc"] }
chacha20poly1305 = { version = "0.8", default-features = false, features = ["alloc", "chacha20"] }

# - HKDF from RFC 5869 with SHA-256
hkdf = "0.11"
Expand Down
10 changes: 5 additions & 5 deletions age-core/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Primitive cryptographic operations used across various `age` components.

use chacha20poly1305::{
aead::{self, generic_array::typenum::Unsigned, Aead, NewAead},
ChaChaPoly1305,
aead::{self, generic_array::typenum::Unsigned, Aead, AeadCore, NewAead},
ChaCha20Poly1305,
};
use hkdf::Hkdf;
use sha2::Sha256;
Expand All @@ -13,7 +13,7 @@ use sha2::Sha256;
///
/// [RFC 7539]: https://tools.ietf.org/html/rfc7539
pub fn aead_encrypt(key: &[u8; 32], plaintext: &[u8]) -> Vec<u8> {
let c = ChaChaPoly1305::<c2_chacha::Ietf>::new(key.into());
let c = ChaCha20Poly1305::new(key.into());
c.encrypt(&[0; 12].into(), plaintext)
.expect("we won't overflow the ChaCha20 block counter")
}
Expand All @@ -32,11 +32,11 @@ pub fn aead_decrypt(
size: usize,
ciphertext: &[u8],
) -> Result<Vec<u8>, aead::Error> {
if ciphertext.len() != size + <ChaChaPoly1305<c2_chacha::Ietf> as Aead>::TagSize::to_usize() {
if ciphertext.len() != size + <ChaCha20Poly1305 as AeadCore>::TagSize::to_usize() {
return Err(aead::Error);
}

let c = ChaChaPoly1305::<c2_chacha::Ietf>::new(key.into());
let c = ChaCha20Poly1305::new(key.into());
c.decrypt(&[0; 12].into(), ciphertext)
}

Expand Down
3 changes: 1 addition & 2 deletions age/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ age-core = { version = "0.6.0", path = "../age-core" }
base64 = "0.13"

# - ChaCha20-Poly1305 from RFC 7539
c2-chacha = "0.3"
chacha20poly1305 = { version = "0.7", default-features = false, features = ["alloc"] }
chacha20poly1305 = { version = "0.8", default-features = false, features = ["alloc", "chacha20"] }

# - X25519 from RFC 7748
x25519-dalek = "1"
Expand Down
10 changes: 4 additions & 6 deletions age/src/primitives/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use chacha20poly1305::{
aead::{generic_array::GenericArray, Aead, NewAead},
ChaChaPoly1305,
ChaCha20Poly1305,
};
use pin_project::pin_project;
use secrecy::{ExposeSecret, SecretVec};
Expand All @@ -24,9 +24,7 @@ const CHUNK_SIZE: usize = 64 * 1024;
const TAG_SIZE: usize = 16;
const ENCRYPTED_CHUNK_SIZE: usize = CHUNK_SIZE + TAG_SIZE;

pub(crate) struct PayloadKey(
pub(crate) GenericArray<u8, <ChaChaPoly1305<c2_chacha::Ietf> as NewAead>::KeySize>,
);
pub(crate) struct PayloadKey(pub(crate) GenericArray<u8, <ChaCha20Poly1305 as NewAead>::KeySize>);

impl Drop for PayloadKey {
fn drop(&mut self) {
Expand Down Expand Up @@ -89,14 +87,14 @@ struct EncryptedChunk {
///
/// [STREAM]: https://eprint.iacr.org/2015/189.pdf
pub(crate) struct Stream {
aead: ChaChaPoly1305<c2_chacha::Ietf>,
aead: ChaCha20Poly1305,
nonce: Nonce,
}

impl Stream {
fn new(key: PayloadKey) -> Self {
Stream {
aead: ChaChaPoly1305::new(&key.0),
aead: ChaCha20Poly1305::new(&key.0),
nonce: Nonce::default(),
}
}
Expand Down

0 comments on commit e10257f

Please sign in to comment.