@@ -11,8 +11,8 @@ use crate::crypto::chacha20poly1305::ChaCha20Poly1305;
11
11
///
12
12
/// It provides client-side deterministic encryption of given keys using ChaCha20-Poly1305.
13
13
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 ] ,
16
16
}
17
17
18
18
impl KeyObfuscator {
@@ -24,6 +24,7 @@ impl KeyObfuscator {
24
24
}
25
25
}
26
26
27
+ const KEY_LENGTH : usize = 32 ;
27
28
const TAG_LENGTH : usize = 16 ;
28
29
const NONCE_LENGTH : usize = 12 ;
29
30
@@ -137,20 +138,28 @@ impl KeyObfuscator {
137
138
138
139
/// Derives the obfuscation and hashing keys from the master key.
139
140
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 ] ) {
142
143
let prk = Self :: hkdf ( obfuscation_master_key, "pseudo_random_key" . as_bytes ( ) ) ;
143
144
let k1 = Self :: hkdf ( & prk, "obfuscation_key" . as_bytes ( ) ) ;
144
145
let k2 = Self :: hkdf ( & prk, & [ & k1[ ..] , "hashing_key" . as_bytes ( ) ] . concat ( ) ) ;
145
146
( k1, k2)
146
147
}
147
- fn hkdf ( initial_key_material : & [ u8 ] , salt : & [ u8 ] ) -> [ u8 ; 32 ] {
148
+ fn hkdf ( initial_key_material : & [ u8 ] , salt : & [ u8 ] ) -> [ u8 ; KEY_LENGTH ] {
148
149
let mut engine = HmacEngine :: < sha256:: Hash > :: new ( salt) ;
149
150
engine. input ( initial_key_material) ;
150
151
Hmac :: from_engine ( engine) . to_byte_array ( )
151
152
}
152
153
}
153
154
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
+
154
163
#[ cfg( test) ]
155
164
mod tests {
156
165
use crate :: util:: key_obfuscator:: KeyObfuscator ;
0 commit comments