Skip to content

Commit

Permalink
implement Clone for LessSafeKey
Browse files Browse the repository at this point in the history
OpeningKey and SealingKey intentionally avoid implementing Copy and
Clone, because they're attached to a fixed nonce sequence that should be
unique. LessSafeKey isn't attached to a nonce sequence, though, and
making it Clone lets callers avoid repeating key setup work.
  • Loading branch information
oconnor663-zoom committed Jan 22, 2021
1 parent 27200d4 commit 47a236b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl core::fmt::Debug for UnboundKey {
}

#[allow(clippy::large_enum_variant, variant_size_differences)]
#[derive(Clone)]
enum KeyInner {
AesGcm(aes_gcm::Key),
ChaCha20Poly1305(chacha20_poly1305::Key),
Expand All @@ -425,6 +426,16 @@ impl UnboundKey {
pub fn algorithm(&self) -> &'static Algorithm {
self.algorithm
}

// LessSafeKey implements Clone using this method, but UnboundKey itself doesn't publicly
// implement Clone, because that would somewhat defeat the purpose of BoundKey.
fn clone_internal(&self) -> Self {
Self {
inner: self.inner.clone(),
algorithm: self.algorithm,
cpu_features: self.cpu_features,
}
}
}

impl From<hkdf::Okm<'_, &'static Algorithm>> for UnboundKey {
Expand Down Expand Up @@ -558,6 +569,15 @@ impl core::fmt::Debug for LessSafeKey {
}
}

impl Clone for LessSafeKey {
fn clone(&self) -> Self {
Self {
// UnboundKey doesn't publicly implement Clone.
key: self.key.clone_internal(),
}
}
}

/// An AEAD Algorithm.
pub struct Algorithm {
init: fn(key: &[u8], cpu_features: cpu::Features) -> Result<KeyInner, error::Unspecified>,
Expand Down
2 changes: 2 additions & 0 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use super::{counter, iv::Iv, quic::Sample, Block, Direction, BLOCK_LEN};
use crate::{bits::BitLength, c, cpu, endian::*, error, polyfill};

#[derive(Clone)]
pub(crate) struct Key {
inner: AES_KEY,
cpu_features: cpu::Features,
Expand Down Expand Up @@ -322,6 +323,7 @@ impl Key {

// Keep this in sync with AES_KEY in aes.h.
#[repr(C)]
#[derive(Clone)]
pub(super) struct AES_KEY {
pub rd_key: [u32; 4 * (MAX_ROUNDS + 1)],
pub rounds: c::uint,
Expand Down
1 change: 1 addition & 0 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub static AES_256_GCM: aead::Algorithm = aead::Algorithm {
max_input_len: AES_GCM_MAX_INPUT_LEN,
};

#[derive(Clone)]
pub struct Key {
gcm_key: gcm::Key, // First because it has a large alignment requirement.
aes_key: aes::Key,
Expand Down
1 change: 1 addition & 0 deletions src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{counter, iv::Iv, quic::Sample, BLOCK_LEN};
use crate::{c, endian::*};

#[repr(transparent)]
#[derive(Clone)]
pub struct Key([LittleEndian<u32>; KEY_LEN / 4]);

impl From<[u8; KEY_LEN]> for Key {
Expand Down
1 change: 1 addition & 0 deletions src/aead/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::cpu;
#[cfg(not(target_arch = "aarch64"))]
mod gcm_nohw;

#[derive(Clone)]
pub struct Key(HTable);

impl Key {
Expand Down
33 changes: 33 additions & 0 deletions tests/aead_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,39 @@ fn test_aead_key_debug() {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_aead_lesssafekey_clone() {
let key_bytes = [0; 32];
let nonce = [0; aead::NONCE_LEN];

let key1 =
aead::LessSafeKey::new(aead::UnboundKey::new(&aead::AES_256_GCM, &key_bytes).unwrap());
// Clone key1.
let key2 = key1.clone();

// LessSafeKey doesn't support AsRef or PartialEq, so instead just check that both keys produce
// the same encrypted output.
let mut buf1 = [0; 32];
let tag1 = key1
.seal_in_place_separate_tag(
aead::Nonce::try_assume_unique_for_key(&nonce).unwrap(),
aead::Aad::empty(),
&mut buf1,
)
.unwrap();
let mut buf2 = [0; 32];
let tag2 = key2
.seal_in_place_separate_tag(
aead::Nonce::try_assume_unique_for_key(&nonce).unwrap(),
aead::Aad::empty(),
&mut buf2,
)
.unwrap();
assert_eq!(tag1.as_ref(), tag2.as_ref());
assert_eq!(buf1, buf2);
}

fn make_key<K: aead::BoundKey<OneNonceSequence>>(
algorithm: &'static aead::Algorithm,
key: &[u8],
Expand Down

0 comments on commit 47a236b

Please sign in to comment.