reddb_server/storage/encryption/
key.rs1use super::pbkdf2::derive_key;
7use std::ptr;
8
9pub struct SecureKey {
11 data: Box<[u8]>,
12}
13
14impl SecureKey {
15 pub fn new(data: &[u8]) -> Self {
17 Self { data: data.into() }
18 }
19
20 pub fn from_passphrase(password: &str, salt: &[u8]) -> Self {
23 let key_data = derive_key(password.as_bytes(), salt);
24 Self::new(&key_data)
25 }
26
27 pub fn from_env(var_name: &str, salt: Option<&[u8]>) -> Result<Self, String> {
32 let val = std::env::var(var_name).map_err(|_| format!("{} not set", var_name))?;
33
34 if val.len() == 64 {
36 if let Ok(bytes) = decode_hex(&val) {
37 return Ok(Self::new(&bytes));
38 }
39 }
40
41 if let Some(s) = salt {
43 Ok(Self::from_passphrase(&val, s))
44 } else {
45 Err("Salt required for passphrase-based key derivation".to_string())
46 }
47 }
48
49 pub fn as_bytes(&self) -> &[u8] {
51 &self.data
52 }
53}
54
55fn decode_hex(s: &str) -> Result<Vec<u8>, String> {
56 if !s.len().is_multiple_of(2) {
57 return Err("Odd length".to_string());
58 }
59
60 let mut bytes = Vec::with_capacity(s.len() / 2);
61 for i in (0..s.len()).step_by(2) {
62 let byte_str = &s[i..i + 2];
63 let byte = u8::from_str_radix(byte_str, 16).map_err(|e| format!("Invalid hex: {}", e))?;
64 bytes.push(byte);
65 }
66 Ok(bytes)
67}
68
69impl Drop for SecureKey {
70 fn drop(&mut self) {
71 unsafe {
73 ptr::write_volatile(self.data.as_mut_ptr(), 0);
74 for i in 1..self.data.len() {
75 ptr::write_volatile(self.data.as_mut_ptr().add(i), 0);
76 }
77 }
78 std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
80 }
81}
82
83impl Clone for SecureKey {
84 fn clone(&self) -> Self {
85 Self::new(&self.data)
86 }
87}
88
89impl std::fmt::Debug for SecureKey {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 write!(f, "SecureKey(***)")
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_secure_key_zeroing() {
101 let key = SecureKey::new(b"secret");
102 drop(key);
103 }
106
107 #[test]
108 fn test_key_derivation() {
109 let key = SecureKey::from_passphrase("password", b"somesalt");
110 assert_eq!(key.as_bytes().len(), 32);
111 }
112}