Skip to main content

reddb_server/storage/encryption/
key.rs

1//! Key Management for RedDB Encryption
2//!
3//! Handles secure storage and derivation of encryption keys.
4//! Ensures keys are zeroed out from memory when dropped.
5
6use super::pbkdf2::derive_key;
7use std::ptr;
8
9/// A securely managed encryption key
10pub struct SecureKey {
11    data: Box<[u8]>,
12}
13
14impl SecureKey {
15    /// Create a new secure key from raw bytes
16    pub fn new(data: &[u8]) -> Self {
17        Self { data: data.into() }
18    }
19
20    /// Derive a key from a password using PBKDF2-SHA256
21    /// Uses 100,000 iterations for security (OWASP recommendation)
22    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    /// Derive a key from an environment variable (e.g., REDDB_KEY)
28    /// The env var can contain a hex string or a raw passphrase.
29    /// If it's a 64-char hex string, it's treated as the raw key (32 bytes).
30    /// Otherwise, it's treated as a passphrase and KDF is applied (requires salt).
31    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        // Try hex decoding first
35        if val.len() == 64 {
36            if let Ok(bytes) = decode_hex(&val) {
37                return Ok(Self::new(&bytes));
38            }
39        }
40
41        // Fallback to KDF
42        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    /// Access the raw key bytes
50    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        // Volatile zeroing to prevent compiler optimization
72        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        // Memory fence to ensure writes happen before deallocation
79        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        // Can't easily check memory after drop safely in Rust tests without UB,
104        // but we trust the implementation logic.
105    }
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}