Skip to content

Commit 03ca9d9

Browse files
committed
Zeroize KeyObfuscator keys on Drop
.. to make sure the values don't linger in memory.
1 parent 6873274 commit 03ca9d9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/util/key_obfuscator.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::crypto::chacha20poly1305::ChaCha20Poly1305;
1111
///
1212
/// It provides client-side deterministic encryption of given keys using ChaCha20-Poly1305.
1313
pub struct KeyObfuscator {
14-
obfuscation_key: [u8; 32],
15-
hashing_key: [u8; 32],
14+
obfuscation_key: [u8; KEY_LENGTH],
15+
hashing_key: [u8; KEY_LENGTH],
1616
}
1717

1818
impl KeyObfuscator {
@@ -24,6 +24,7 @@ impl KeyObfuscator {
2424
}
2525
}
2626

27+
const KEY_LENGTH: usize = 32;
2728
const TAG_LENGTH: usize = 16;
2829
const NONCE_LENGTH: usize = 12;
2930

@@ -137,20 +138,28 @@ impl KeyObfuscator {
137138

138139
/// Derives the obfuscation and hashing keys from the master key.
139140
fn derive_obfuscation_and_hashing_keys(
140-
obfuscation_master_key: &[u8; 32],
141-
) -> ([u8; 32], [u8; 32]) {
141+
obfuscation_master_key: &[u8; KEY_LENGTH],
142+
) -> ([u8; KEY_LENGTH], [u8; KEY_LENGTH]) {
142143
let prk = Self::hkdf(obfuscation_master_key, "pseudo_random_key".as_bytes());
143144
let k1 = Self::hkdf(&prk, "obfuscation_key".as_bytes());
144145
let k2 = Self::hkdf(&prk, &[&k1[..], "hashing_key".as_bytes()].concat());
145146
(k1, k2)
146147
}
147-
fn hkdf(initial_key_material: &[u8], salt: &[u8]) -> [u8; 32] {
148+
fn hkdf(initial_key_material: &[u8], salt: &[u8]) -> [u8; KEY_LENGTH] {
148149
let mut engine = HmacEngine::<sha256::Hash>::new(salt);
149150
engine.input(initial_key_material);
150151
Hmac::from_engine(engine).to_byte_array()
151152
}
152153
}
153154

155+
impl Drop for KeyObfuscator {
156+
fn drop(&mut self) {
157+
// Zeroize the owned keys
158+
self.obfuscation_key.copy_from_slice(&[0u8; KEY_LENGTH]);
159+
self.hashing_key.copy_from_slice(&[0u8; KEY_LENGTH]);
160+
}
161+
}
162+
154163
#[cfg(test)]
155164
mod tests {
156165
use crate::util::key_obfuscator::KeyObfuscator;

0 commit comments

Comments
 (0)