use super::pbkdf2::derive_key;
use std::ptr;
pub struct SecureKey {
data: Box<[u8]>,
}
impl SecureKey {
pub fn new(data: &[u8]) -> Self {
Self { data: data.into() }
}
pub fn from_passphrase(password: &str, salt: &[u8]) -> Self {
let key_data = derive_key(password.as_bytes(), salt);
Self::new(&key_data)
}
pub fn from_env(var_name: &str, salt: Option<&[u8]>) -> Result<Self, String> {
let val = std::env::var(var_name).map_err(|_| format!("{} not set", var_name))?;
if val.len() == 64 {
if let Ok(bytes) = decode_hex(&val) {
return Ok(Self::new(&bytes));
}
}
if let Some(s) = salt {
Ok(Self::from_passphrase(&val, s))
} else {
Err("Salt required for passphrase-based key derivation".to_string())
}
}
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
}
fn decode_hex(s: &str) -> Result<Vec<u8>, String> {
if !s.len().is_multiple_of(2) {
return Err("Odd length".to_string());
}
let mut bytes = Vec::with_capacity(s.len() / 2);
for i in (0..s.len()).step_by(2) {
let byte_str = &s[i..i + 2];
let byte = u8::from_str_radix(byte_str, 16).map_err(|e| format!("Invalid hex: {}", e))?;
bytes.push(byte);
}
Ok(bytes)
}
impl Drop for SecureKey {
fn drop(&mut self) {
unsafe {
ptr::write_volatile(self.data.as_mut_ptr(), 0);
for i in 1..self.data.len() {
ptr::write_volatile(self.data.as_mut_ptr().add(i), 0);
}
}
std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
}
}
impl Clone for SecureKey {
fn clone(&self) -> Self {
Self::new(&self.data)
}
}
impl std::fmt::Debug for SecureKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SecureKey(***)")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_secure_key_zeroing() {
let key = SecureKey::new(b"secret");
drop(key);
}
#[test]
fn test_key_derivation() {
let key = SecureKey::from_passphrase("password", b"somesalt");
assert_eq!(key.as_bytes().len(), 32);
}
}