Skip to main content

cloudiful_redactor/session/
crypto.rs

1use aes_gcm_siv::aead::{Aead, KeyInit};
2use aes_gcm_siv::{Aes256GcmSiv, Nonce};
3use anyhow::{Context, Result, anyhow};
4use base64::{Engine as _, engine::general_purpose::STANDARD};
5use pbkdf2::pbkdf2_hmac_array;
6use serde::{Deserialize, Serialize};
7use sha2::Sha256;
8
9use crate::types::{RedactionSession, SessionEntrySummary};
10
11const KDF_ROUNDS: u32 = 600_000;
12const SESSION_VERSION: u32 = 1;
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub(crate) struct EncryptedSessionFile {
16    pub(crate) version: u32,
17    pub(crate) session_id: String,
18    pub(crate) fingerprint: String,
19    pub(crate) redacted_fingerprint: String,
20    pub(crate) entry_count: usize,
21    pub(crate) entries: Vec<SessionEntrySummary>,
22    pub(crate) salt_b64: String,
23    pub(crate) nonce_b64: String,
24    pub(crate) ciphertext_b64: String,
25}
26
27pub fn encrypt_session_to_string(session: &RedactionSession, passphrase: &str) -> Result<String> {
28    let mut salt = [0u8; 16];
29    let mut nonce = [0u8; 12];
30    getrandom::fill(&mut salt)
31        .map_err(|error| anyhow!("failed to generate session salt: {error}"))?;
32    getrandom::fill(&mut nonce)
33        .map_err(|error| anyhow!("failed to generate session nonce: {error}"))?;
34
35    let key = derive_key(passphrase, &salt);
36    let cipher = Aes256GcmSiv::new_from_slice(&key)
37        .map_err(|error| anyhow!("failed to initialize session cipher: {error}"))?;
38    let plaintext = serde_json::to_vec(session).context("failed to serialize session")?;
39    let ciphertext = cipher
40        .encrypt(Nonce::from_slice(&nonce), plaintext.as_ref())
41        .map_err(|_| anyhow!("failed to encrypt session"))?;
42
43    let envelope = EncryptedSessionFile {
44        version: SESSION_VERSION,
45        session_id: session.session_id.clone(),
46        fingerprint: session.fingerprint.clone(),
47        redacted_fingerprint: session.redacted_fingerprint.clone(),
48        entry_count: session.entries.len(),
49        entries: session
50            .entries
51            .iter()
52            .map(|entry| SessionEntrySummary {
53                token: entry.token.clone(),
54                kind: entry.kind,
55                replacement_hint: entry.replacement_hint.clone(),
56                occurrences: entry.occurrences,
57            })
58            .collect(),
59        salt_b64: STANDARD.encode(salt),
60        nonce_b64: STANDARD.encode(nonce),
61        ciphertext_b64: STANDARD.encode(ciphertext),
62    };
63
64    serde_json::to_string_pretty(&envelope)
65        .context("failed to serialize encrypted session envelope")
66}
67
68pub fn decrypt_session_from_str(data: &str, passphrase: &str) -> Result<RedactionSession> {
69    let envelope = parse_envelope(data)?;
70    let salt = decode_exact::<16>(&envelope.salt_b64).context("invalid encrypted session salt")?;
71    let nonce =
72        decode_exact::<12>(&envelope.nonce_b64).context("invalid encrypted session nonce")?;
73    let ciphertext = STANDARD
74        .decode(envelope.ciphertext_b64.as_bytes())
75        .context("invalid encrypted session ciphertext")?;
76
77    let key = derive_key(passphrase, &salt);
78    let cipher = Aes256GcmSiv::new_from_slice(&key)
79        .map_err(|error| anyhow!("failed to initialize session cipher: {error}"))?;
80    let plaintext = cipher
81        .decrypt(Nonce::from_slice(&nonce), ciphertext.as_ref())
82        .map_err(|_| anyhow!("failed to decrypt session"))?;
83
84    serde_json::from_slice(&plaintext).context("failed to deserialize decrypted session")
85}
86
87pub(crate) fn parse_envelope(data: &str) -> Result<EncryptedSessionFile> {
88    serde_json::from_str(data).context("failed to parse encrypted session file")
89}
90
91fn derive_key(passphrase: &str, salt: &[u8; 16]) -> [u8; 32] {
92    pbkdf2_hmac_array::<Sha256, 32>(passphrase.as_bytes(), salt, KDF_ROUNDS)
93}
94
95fn decode_exact<const N: usize>(data: &str) -> Result<[u8; N]> {
96    let decoded = STANDARD
97        .decode(data.as_bytes())
98        .context("invalid base64 data")?;
99    decoded
100        .try_into()
101        .map_err(|_| anyhow!("unexpected decoded length"))
102}