cryptex 3.0.0

Store and retrieve secrets in OS keyrings, encrypted SQLite or Turso files, in-memory and browser keyrings, or under a YubiHSM 2 or Nitrokey 3
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
//! The AEAD algorithm choices for cryptex's own encryption.
//!
//! A `fips` build offers only AES-256-GCM.

use super::file_common;
use crate::Result;
use crate::error::KeyRingError;
use zeroize::Zeroize;

/// Selects the AEAD algorithm the in-memory keyrings seal with.
///
/// `Aes256Gcm` uses AES-NI hardware acceleration on x86/x86_64/AArch64 when
/// available; on hardware without AES-NI it falls back to a constant-time
/// software implementation that is slower than ChaCha20-Poly1305.
#[cfg(feature = "memory")]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum CipherAlgorithm {
    /// ChaCha20-Poly1305 (default; not in `fips` builds)
    #[cfg(not(feature = "fips"))]
    #[default]
    ChaCha20Poly1305,
    /// AES-256-GCM (the default in `fips` builds)
    #[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")),
        }
    }
}

/// Selects the AEAD algorithm for page encryption in the encrypted VFS and
/// the Turso encrypted IO keyrings.
///
/// Both store a random nonce with every page. XChaCha20-Poly1305's 192-bit
/// nonce makes a repeat negligible for any number of writes; AES-256-GCM's
/// 96-bit nonce is good for about 2^32 page writes under one key, so rekey a
/// long-lived AES keyring now and then.
#[cfg(any(feature = "encrypted-vfs", feature = "turso-encrypted-io"))]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum PageCipher {
    /// XChaCha20-Poly1305 (default; not in `fips` builds)
    #[cfg(not(feature = "fips"))]
    #[default]
    XChaCha20Poly1305,
    /// AES-256-GCM (the default in `fips` builds)
    #[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
        ),
    }
}