feat(store): encrypt chat bodies at rest; Ctrl-Q CLEAR/QUIT
All checks were successful
ci / test (pull_request) Successful in 3m13s

Wrap a random 32-byte message key with Argon2id+ChaCha20-Poly1305.
sqlite messages.plaintext is nonce||ciphertext; identity keys stay
plaintext. Empty/wrong passphrase fails closed. Ctrl-Q types CLEAR or
QUIT then Enter (Esc cancels).
This commit is contained in:
Sirius DevOps 2026-09-10 17:19:50 -04:00
commit 6323619d80
No known key found for this signature in database
16 changed files with 461 additions and 35 deletions

View file

@ -42,7 +42,7 @@ impl BackupKeys {
}
}
fn kdf(passphrase: &str, salt: &[u8]) -> Result<[u8; 32], String> {
pub(crate) fn kdf(passphrase: &str, salt: &[u8]) -> Result<[u8; 32], String> {
if passphrase.is_empty() {
return Err("empty passphrase".into());
}
@ -94,3 +94,26 @@ pub fn open(passphrase: &str, blob: &[u8]) -> Result<BackupKeys, String> {
.map_err(|_| "wrong passphrase or corrupt backup".to_string())?;
BackupKeys::from_bytes(&pt)
}
pub(crate) fn aead_encrypt(key: &[u8; 32], plaintext: &[u8]) -> Result<Vec<u8>, String> {
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
let ct = cipher
.encrypt(&nonce, plaintext)
.map_err(|_| "encrypt failed".to_string())?;
let mut out = Vec::with_capacity(NONCE_LEN + ct.len());
out.extend_from_slice(&nonce);
out.extend_from_slice(&ct);
Ok(out)
}
pub(crate) fn aead_decrypt(key: &[u8; 32], blob: &[u8]) -> Result<Vec<u8>, String> {
if blob.len() < NONCE_LEN + 16 {
return Err("ciphertext length".into());
}
let nonce = Nonce::from_slice(&blob[..NONCE_LEN]);
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
cipher
.decrypt(nonce, &blob[NONCE_LEN..])
.map_err(|_| "wrong passphrase or corrupt".to_string())
}