Skip to content

Commit fe03c1f

Browse files
authored
optimization: reusing AEAD Cipher instance (#79)
Preventing the Key being copied every time when calling encrypt_* and decrypt_*
1 parent d814c95 commit fe03c1f

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

Cargo.lock

Lines changed: 10 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ rust-version = "1.56"
1919
generic-array = { version = "0.14", default-features = false }
2020
opaque-debug = "0.3"
2121
ring = { version = "0.16", default-features = false }
22-
zeroize = { version = "1", default-features = false }
2322

2423
# optional features
2524
aead = { version = "0.4", optional = true, default-features = false }

src/aead.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ use aead::{
1212
use ring::aead::{
1313
Aad, LessSafeKey as Key, Nonce, UnboundKey, AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305,
1414
};
15-
use zeroize::Zeroize;
1615

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

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

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

2625
/// ChaCha20Poly1305
27-
pub struct ChaCha20Poly1305(GenericArray<u8, U32>);
26+
pub struct ChaCha20Poly1305(Cipher);
2827

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

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

@@ -49,12 +49,8 @@ macro_rules! impl_aead {
4949
associated_data: &[u8],
5050
buffer: &mut [u8],
5151
) -> Result<Tag, Error> {
52-
let key = UnboundKey::new(&$algorithm, self.0.as_slice()).unwrap();
53-
Cipher::new(key).encrypt_in_place_detached(
54-
nonce.as_slice(),
55-
associated_data,
56-
buffer,
57-
)
52+
self.0
53+
.encrypt_in_place_detached(nonce.as_slice(), associated_data, buffer)
5854
}
5955

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

7066
fn decrypt_in_place_detached(
@@ -77,12 +73,6 @@ macro_rules! impl_aead {
7773
unimplemented!(); // ring does not allow us to implement this API
7874
}
7975
}
80-
81-
impl Drop for $cipher {
82-
fn drop(&mut self) {
83-
self.0.zeroize();
84-
}
85-
}
8676
};
8777
}
8878

0 commit comments

Comments
 (0)