use secrecy::{ExposeSecret, SecretBox};
pub struct UnlockKey {
password: SecretBox<Vec<u8>>,
}
impl UnlockKey {
pub fn from_passphrase(passphrase: String) -> Self {
Self {
password: SecretBox::new(Box::new(passphrase.into_bytes())),
}
}
pub fn expose_bytes(&self) -> &[u8] {
self.password.expose_secret().as_slice()
}
pub fn calculate_effective_key(&self, _challenge: &[u8]) -> Result<Vec<u8>, crate::LuksError> {
Ok(self.password.expose_secret().to_vec())
}
}
impl From<String> for UnlockKey {
fn from(passphrase: String) -> Self {
Self::from_passphrase(passphrase)
}
}
impl From<&str> for UnlockKey {
fn from(passphrase: &str) -> Self {
Self::from_passphrase(passphrase.to_string())
}
}
pub struct VolumeKey(SecretBox<Vec<u8>>);
impl VolumeKey {
pub fn new(bytes: Vec<u8>) -> Result<Self, crate::LuksError> {
if bytes.len() != crate::AES128_KEY_SIZE * 2 && bytes.len() != crate::AES256_KEY_SIZE * 2 {
return Err(crate::LuksError::InvalidHeader(format!(
"Invalid volume key size: expected {} or {}, got {}",
crate::AES128_KEY_SIZE * 2,
crate::AES256_KEY_SIZE * 2,
bytes.len()
)));
}
Ok(Self(SecretBox::new(Box::new(bytes))))
}
pub fn expose_bytes(&self) -> &[u8] {
self.0.expose_secret().as_slice()
}
}