Skip to content

Commit cdd6577

Browse files
committed
f Address Leo's commments
1 parent 20a8b8f commit cdd6577

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

src/util/key_obfuscator.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ impl KeyObfuscator {
7979
tag.copy_from_slice(&tag_bytes);
8080

8181
let (wrapped_nonce_bytes, wrapped_nonce_tag_bytes) = remaining.split_at(NONCE_LENGTH);
82+
8283
let mut wrapped_nonce_tag = [0u8; TAG_LENGTH];
83-
wrapped_nonce_tag.copy_from_slice(wrapped_nonce_tag_bytes);
84+
wrapped_nonce_tag.copy_from_slice(&wrapped_nonce_tag_bytes[..TAG_LENGTH]);
8485

8586
// Unwrap wrapped_nonce to get nonce.
8687
let mut wrapped_nonce = [0u8; NONCE_LENGTH];
@@ -94,10 +95,8 @@ impl KeyObfuscator {
9495
})?;
9596

9697
// Decrypt ciphertext using nonce.
97-
let cipher = ChaCha20Poly1305::new(
98-
Key::new(self.obfuscation_key.clone()),
99-
Nonce::new(wrapped_nonce),
100-
);
98+
let cipher =
99+
ChaCha20Poly1305::new(Key::new(self.obfuscation_key), Nonce::new(wrapped_nonce));
101100
let mut ciphertext = ciphertext.to_vec();
102101
cipher.decrypt(&mut ciphertext, tag, None).map_err(|_| {
103102
let msg = format!("Failed to decrypt key: {}, Invalid Tag.", obfuscated_key);
@@ -119,10 +118,7 @@ impl KeyObfuscator {
119118
&self, mut plaintext: &mut [u8], initial_nonce_material: &[u8],
120119
) -> ([u8; NONCE_LENGTH], [u8; TAG_LENGTH]) {
121120
let nonce = self.generate_synthetic_nonce(initial_nonce_material);
122-
let cipher = ChaCha20Poly1305::new(
123-
Key::new(self.obfuscation_key.clone()),
124-
Nonce::new(nonce.clone()),
125-
);
121+
let cipher = ChaCha20Poly1305::new(Key::new(self.obfuscation_key), Nonce::new(nonce));
126122
let tag = cipher.encrypt(&mut plaintext, None);
127123
(nonce, tag)
128124
}
@@ -132,10 +128,7 @@ impl KeyObfuscator {
132128
&self, mut ciphertext: &mut [u8], initial_nonce_material: &[u8], tag: [u8; TAG_LENGTH],
133129
) -> Result<(), ()> {
134130
let nonce = self.generate_synthetic_nonce(initial_nonce_material);
135-
let cipher = ChaCha20Poly1305::new(
136-
Key::new(self.obfuscation_key.clone()),
137-
Nonce::new(nonce.clone()),
138-
);
131+
let cipher = ChaCha20Poly1305::new(Key::new(self.obfuscation_key), Nonce::new(nonce));
139132
cipher.decrypt(&mut ciphertext, tag, None).map_err(|_| ())
140133
}
141134

src/util/storable_builder.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub trait EntropySource {
3131
}
3232

3333
const CHACHA20_CIPHER_NAME: &'static str = "ChaCha20Poly1305";
34+
const TAG_LENGTH: usize = 16;
35+
const NONCE_LENGTH: usize = 12;
3436

3537
impl<T: EntropySource> StorableBuilder<T> {
3638
/// Creates a [`Storable`] that can be serialized and stored as `value` in [`PutObjectRequest`].
@@ -44,13 +46,12 @@ impl<T: EntropySource> StorableBuilder<T> {
4446
pub fn build(
4547
&self, input: Vec<u8>, version: i64, data_encryption_key: &[u8; 32], aad: &[u8],
4648
) -> Storable {
47-
let mut nonce = [0u8; 12];
49+
let mut nonce = [0u8; NONCE_LENGTH];
4850
self.entropy_source.fill_bytes(&mut nonce[4..]);
4951

5052
let mut data_blob = PlaintextBlob { value: input, version }.encode_to_vec();
5153

52-
let cipher =
53-
ChaCha20Poly1305::new(Key::new(data_encryption_key.clone()), Nonce::new(nonce));
54+
let cipher = ChaCha20Poly1305::new(Key::new(*data_encryption_key), Nonce::new(nonce));
5455
let tag = cipher.encrypt(&mut data_blob, Some(aad));
5556
Storable {
5657
data: data_blob,
@@ -73,16 +74,18 @@ impl<T: EntropySource> StorableBuilder<T> {
7374
.encryption_metadata
7475
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "Invalid Metadata"))?;
7576

76-
if encryption_metadata.nonce.len() != 12 {
77+
if encryption_metadata.nonce.len() != NONCE_LENGTH {
7778
return Err(Error::new(ErrorKind::InvalidData, "Invalid Metadata"));
7879
}
79-
let mut nonce = [0u8; 12];
80+
let mut nonce = [0u8; NONCE_LENGTH];
8081
nonce.copy_from_slice(&encryption_metadata.nonce);
8182

82-
let cipher =
83-
ChaCha20Poly1305::new(Key::new(data_encryption_key.clone()), Nonce::new(nonce));
83+
let cipher = ChaCha20Poly1305::new(Key::new(*data_encryption_key), Nonce::new(nonce));
8484

85-
let mut tag = [0u8; 16];
85+
if encryption_metadata.tag.len() != TAG_LENGTH {
86+
return Err(Error::new(ErrorKind::InvalidData, "Invalid Metadata"));
87+
}
88+
let mut tag = [0u8; TAG_LENGTH];
8689
tag.copy_from_slice(&encryption_metadata.tag);
8790

8891
cipher

0 commit comments

Comments
 (0)