use super::file_common;
use crate::Result;
use crate::error::KeyRingError;
use zeroize::Zeroize;
#[cfg(feature = "memory")]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum CipherAlgorithm {
#[cfg(not(feature = "fips"))]
#[default]
ChaCha20Poly1305,
#[cfg_attr(feature = "fips", default)]
Aes256Gcm,
}
#[cfg(feature = "memory")]
impl Zeroize for CipherAlgorithm {
fn zeroize(&mut self) {
*self = CipherAlgorithm::default();
}
}
#[cfg(feature = "memory")]
impl file_common::CipherParam for CipherAlgorithm {
fn parse(value: &str) -> Result<Self> {
match value {
#[cfg(not(feature = "fips"))]
"chacha20poly1305" | "chacha20" => Ok(CipherAlgorithm::ChaCha20Poly1305),
"aes256gcm" | "aes" => Ok(CipherAlgorithm::Aes256Gcm),
other => Err(unknown_cipher(other, "chacha20poly1305")),
}
}
}
#[cfg(any(feature = "encrypted-vfs", feature = "turso-encrypted-io"))]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum PageCipher {
#[cfg(not(feature = "fips"))]
#[default]
XChaCha20Poly1305,
#[cfg_attr(feature = "fips", default)]
Aes256Gcm,
}
#[cfg(any(feature = "encrypted-vfs", feature = "turso-encrypted-io"))]
impl Zeroize for PageCipher {
fn zeroize(&mut self) {
*self = PageCipher::default();
}
}
#[cfg(any(feature = "encrypted-vfs", feature = "turso-encrypted-io"))]
impl file_common::CipherParam for PageCipher {
fn parse(value: &str) -> Result<Self> {
match value {
#[cfg(not(feature = "fips"))]
"xchacha20poly1305" | "xchacha20" => Ok(PageCipher::XChaCha20Poly1305),
"aes256gcm" | "aes" => Ok(PageCipher::Aes256Gcm),
other => Err(unknown_cipher(other, "xchacha20poly1305")),
}
}
}
#[cfg(not(feature = "fips"))]
fn unknown_cipher(name: &str, chacha: &str) -> KeyRingError {
KeyRingError::GeneralError {
msg: format!("unknown cipher '{}'; use {} or aes256gcm", name, chacha),
}
}
#[cfg(feature = "fips")]
fn unknown_cipher(name: &str, _chacha: &str) -> KeyRingError {
KeyRingError::GeneralError {
msg: format!(
"cipher '{}' is not allowed in a fips build; use aes256gcm",
name
),
}
}