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 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 committed Apr 21, 2021
1 parent 9accd87 commit 3a7e084
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ where
impl<A> Eq for Aad<A> where A: Eq {}

#[allow(clippy::large_enum_variant, variant_size_differences)]
#[derive(Clone)]
enum KeyInner {
AesGcm(aes_gcm::Key),
ChaCha20Poly1305(chacha20_poly1305::Key),
Expand Down
2 changes: 2 additions & 0 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
};
use core::ops::RangeFrom;

#[derive(Clone)]
pub(crate) struct Key {
inner: AES_KEY,
cpu_features: cpu::Features,
Expand Down Expand Up @@ -323,6 +324,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 @@ -40,6 +40,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 @@ -32,6 +32,7 @@ mod fallback;

use core::ops::RangeFrom;

#[derive(Clone)]
pub struct Key {
words: [u32; KEY_LEN / 4],
cpu_features: cpu::Features,
Expand Down
1 change: 1 addition & 0 deletions src/aead/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::ops::BitXorAssign;
#[cfg(not(target_arch = "aarch64"))]
mod gcm_nohw;

#[derive(Clone)]
pub struct Key {
h_table: HTable,
cpu_features: cpu::Features,
Expand Down
1 change: 1 addition & 0 deletions src/aead/less_safe_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use core::ops::RangeFrom;
/// `NonceSequence` cannot reasonably be used.
///
/// Prefer to use `OpeningKey`/`SealingKey` and `NonceSequence` when practical.
#[derive(Clone)]
pub struct LessSafeKey {
inner: KeyInner,
algorithm: &'static Algorithm,
Expand Down
34 changes: 33 additions & 1 deletion tests/aead_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! test_aead {
$(
#[allow(non_snake_case)]
mod $alg { // Provide a separate namespace for each algorithm's test.
use super::super::{*, test};
use super::super::*;

test_known_answer!(
$alg,
Expand Down Expand Up @@ -509,6 +509,38 @@ 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 =
aead::LessSafeKey::new(aead::UnboundKey::new(&aead::AES_256_GCM, &key_bytes).unwrap());
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 3a7e084

Please sign in to comment.