//! Password-based at-rest encryption (Argon2id + XChaCha20-Poly1305). //! //! Blob format: //! ```text //! "MEOWPW" (6) ‖ version (1) ‖ m_cost KiB (u32 BE) ‖ t_cost (u32 BE) ‖ p_cost (u32 BE) //! ‖ salt (16) ‖ nonce ‖ ciphertext+tag //! ``` //! The header is authenticated as AEAD associated data. use argon2::{Algorithm, Argon2, Params, Version}; use crate::aead::{self, Key}; use crate::{CryptoError, Result}; const MAGIC: &[u8; 6] = b"MEOWPW"; const VERSION: u8 = 1; const HEADER_LEN: usize = 6 + 1 + 12 + 16; /// Argon2id cost parameters. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct KdfParams { pub m_cost_kib: u32, pub t_cost: u32, pub p_cost: u32, } impl KdfParams { /// Interactive default: 64 MiB, 3 passes. pub const DEFAULT: Self = Self { m_cost_kib: 64 * 1024, t_cost: 3, p_cost: 1, }; /// Very cheap parameters, for tests only. pub const INSECURE_TEST: Self = Self { m_cost_kib: 64, t_cost: 1, p_cost: 1, }; } /// Derive a key from a password with Argon2id. pub fn derive_key(password: &[u8], salt: &[u8; 16], p: KdfParams) -> Result { let params = Params::new(p.m_cost_kib, p.t_cost, p.p_cost, Some(32)).map_err(|_| CryptoError::Kdf)?; let mut out = [0u8; 32]; Argon2::new(Algorithm::Argon2id, Version::V0x13, params) .hash_password_into(password, salt, &mut out) .map_err(|_| CryptoError::Kdf)?; Ok(Key(out)) } /// Encrypt `plaintext` under `password`. pub fn encrypt(password: &[u8], plaintext: &[u8], p: KdfParams) -> Result> { let salt: [u8; 16] = crate::random_bytes(); let key = derive_key(password, &salt, p)?; let mut header = Vec::with_capacity(HEADER_LEN); header.extend_from_slice(MAGIC); header.push(VERSION); header.extend_from_slice(&p.m_cost_kib.to_be_bytes()); header.extend_from_slice(&p.t_cost.to_be_bytes()); header.extend_from_slice(&p.p_cost.to_be_bytes()); header.extend_from_slice(&salt); let ct = aead::encrypt(&key, plaintext, &header); Ok([header, ct].concat()) } /// Decrypt a blob produced by [`encrypt`]. pub fn decrypt(password: &[u8], blob: &[u8]) -> Result> { if blob.len() < HEADER_LEN || &blob[..6] != MAGIC || blob[6] != VERSION { return Err(CryptoError::Malformed); } let be = |i: usize| u32::from_be_bytes([blob[i], blob[i + 1], blob[i + 2], blob[i + 3]]); let p = KdfParams { m_cost_kib: be(7), t_cost: be(11), p_cost: be(15), }; // Refuse absurd parameters coming from a tampered file (DoS). if p.m_cost_kib > 4 * 1024 * 1024 || p.t_cost > 64 || p.p_cost > 16 { return Err(CryptoError::Malformed); } let mut salt = [0u8; 16]; salt.copy_from_slice(&blob[19..35]); let key = derive_key(password, &salt, p)?; aead::decrypt(&key, &blob[HEADER_LEN..], &blob[..HEADER_LEN]) } #[cfg(test)] mod tests { use super::*; #[test] fn roundtrip() { let blob = encrypt(b"pw", b"data", KdfParams::INSECURE_TEST).unwrap(); assert_eq!(decrypt(b"pw", &blob).unwrap(), b"data"); assert_eq!(decrypt(b"bad", &blob), Err(CryptoError::Decrypt)); } #[test] fn header_is_authenticated() { let mut blob = encrypt(b"pw", b"data", KdfParams::INSECURE_TEST).unwrap(); blob[20] ^= 1; // salt assert!(decrypt(b"pw", &blob).is_err()); assert_eq!(decrypt(b"pw", b"junk"), Err(CryptoError::Malformed)); } }