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

optimization: reusing AEAD Cipher instance #79

Merged
merged 1 commit into from Mar 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -19,7 +19,6 @@ rust-version = "1.56"
generic-array = { version = "0.14", default-features = false }
opaque-debug = "0.3"
ring = { version = "0.16", default-features = false }
zeroize = { version = "1", default-features = false }

# optional features
aead = { version = "0.4", optional = true, default-features = false }
Expand Down
28 changes: 9 additions & 19 deletions src/aead.rs
Expand Up @@ -12,27 +12,27 @@ use aead::{
use ring::aead::{
Aad, LessSafeKey as Key, Nonce, UnboundKey, AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305,
};
use zeroize::Zeroize;

/// Authentication tags
pub type Tag = GenericArray<u8, U16>;

/// AES-GCM with a 128-bit key
pub struct Aes128Gcm(GenericArray<u8, U16>);
pub struct Aes128Gcm(Cipher);

/// AES-GCM with a 256-bit key
pub struct Aes256Gcm(GenericArray<u8, U32>);
pub struct Aes256Gcm(Cipher);

/// ChaCha20Poly1305
pub struct ChaCha20Poly1305(GenericArray<u8, U32>);
pub struct ChaCha20Poly1305(Cipher);

macro_rules! impl_aead {
($cipher:ty, $algorithm:expr, $key_size:ty) => {
impl NewAead for $cipher {
type KeySize = $key_size;

fn new(key: &GenericArray<u8, Self::KeySize>) -> Self {
Self(*key)
let key = UnboundKey::new(&$algorithm, key.as_slice()).unwrap();
Self(Cipher::new(key))
}
}

Expand All @@ -49,12 +49,8 @@ macro_rules! impl_aead {
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag, Error> {
let key = UnboundKey::new(&$algorithm, self.0.as_slice()).unwrap();
Cipher::new(key).encrypt_in_place_detached(
nonce.as_slice(),
associated_data,
buffer,
)
self.0
.encrypt_in_place_detached(nonce.as_slice(), associated_data, buffer)
}

fn decrypt_in_place(
Expand All @@ -63,8 +59,8 @@ macro_rules! impl_aead {
associated_data: &[u8],
buffer: &mut dyn Buffer,
) -> Result<(), Error> {
let key = UnboundKey::new(&$algorithm, self.0.as_slice()).unwrap();
Cipher::new(key).decrypt_in_place(nonce.as_slice(), associated_data, buffer)
self.0
.decrypt_in_place(nonce.as_slice(), associated_data, buffer)
}

fn decrypt_in_place_detached(
Expand All @@ -77,12 +73,6 @@ macro_rules! impl_aead {
unimplemented!(); // ring does not allow us to implement this API
}
}

impl Drop for $cipher {
fn drop(&mut self) {
self.0.zeroize();
}
}
zonyitoo marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down