oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Backup Encryption Module
//!
//! Provides AES-256-GCM encryption for database backups with Argon2 key derivation.
//! Supports password-based and keyfile-based encryption for secure backup storage.

use aes_gcm::{
    aead::{Aead, AeadCore, KeyInit, OsRng},
    Aes256Gcm, Key, Nonce,
};
use argon2::{
    password_hash::{PasswordHasher, SaltString},
    Argon2,
};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

/// Encryption configuration
pub struct EncryptionConfig {
    pub password: Option<String>,
    pub keyfile: Option<PathBuf>,
    pub verify: bool,
}

/// Encryption metadata stored with the backup
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct EncryptionMetadata {
    pub version: u8,
    pub algorithm: String,
    pub key_derivation: String,
    pub salt: String,
    pub nonce: Vec<u8>,
    pub created_at: String,
}

/// Encrypt a backup file using AES-256-GCM
pub fn encrypt_backup(
    source: &Path,
    target: &Path,
    config: &EncryptionConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // Derive encryption key and get salt
    let (key, salt_b64) = derive_key_with_salt(config, None)?;

    // Read source file
    let mut source_file = File::open(source)?;
    let mut plaintext = Vec::new();
    source_file.read_to_end(&mut plaintext)?;

    // Generate random nonce (96 bits for GCM)
    let cipher = Aes256Gcm::new(&key);
    let nonce = Aes256Gcm::generate_nonce(&mut OsRng);

    // Encrypt data
    let ciphertext = cipher
        .encrypt(&nonce, plaintext.as_ref())
        .map_err(|e| format!("Encryption failed: {}", e))?;

    // Create metadata
    let metadata = EncryptionMetadata {
        version: 1,
        algorithm: "AES-256-GCM".to_string(),
        key_derivation: if config.password.is_some() {
            "Argon2id".to_string()
        } else {
            "Keyfile".to_string()
        },
        salt: salt_b64,
        nonce: nonce.to_vec(),
        created_at: chrono::Utc::now().to_rfc3339(),
    };

    // Write encrypted file with metadata
    write_encrypted_file(target, &metadata, &ciphertext)?;

    // Verify if requested
    if config.verify {
        verify_encrypted_file(target, config)?;
    }

    Ok(())
}

/// Decrypt a backup file
pub fn decrypt_backup(
    source: &Path,
    target: &Path,
    config: &EncryptionConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // Read encrypted file and metadata
    let (metadata, ciphertext) = read_encrypted_file(source)?;

    // Verify algorithm version
    if metadata.version != 1 || metadata.algorithm != "AES-256-GCM" {
        return Err(format!(
            "Unsupported encryption version or algorithm: v{} {}",
            metadata.version, metadata.algorithm
        )
        .into());
    }

    // Derive decryption key using salt from metadata
    let (key, _) = derive_key_with_salt(config, Some(&metadata.salt))?;

    // Prepare nonce
    if metadata.nonce.len() != 12 {
        return Err("Invalid nonce length".into());
    }
    let nonce = Nonce::from_slice(&metadata.nonce);

    // Decrypt data
    let cipher = Aes256Gcm::new(&key);
    let plaintext = cipher
        .decrypt(nonce, ciphertext.as_ref())
        .map_err(|e| format!("Decryption failed: {}. Check your password/keyfile.", e))?;

    // Write decrypted file
    let mut target_file = File::create(target)?;
    target_file.write_all(&plaintext)?;

    Ok(())
}

/// Derive encryption key from password or keyfile with optional salt
/// Returns (key, salt_base64)
fn derive_key_with_salt(
    config: &EncryptionConfig,
    salt_b64: Option<&str>,
) -> Result<(Key<Aes256Gcm>, String), Box<dyn std::error::Error>> {
    if let Some(ref password) = config.password {
        // Password-based key derivation
        derive_key_from_password(password, salt_b64)
    } else if let Some(ref keyfile) = config.keyfile {
        // Keyfile-based key derivation
        let key = derive_key_from_keyfile(keyfile)?;
        let salt_str = format!("keyfile:{}", keyfile.display());
        Ok((key, salt_str))
    } else {
        Err("Either password or keyfile must be provided".into())
    }
}

/// Derive key from password using Argon2id with optional salt
/// Returns (key, salt_base64)
fn derive_key_from_password(
    password: &str,
    salt_b64_opt: Option<&str>,
) -> Result<(Key<Aes256Gcm>, String), Box<dyn std::error::Error>> {
    use scirs2_core::random::rng;
    use scirs2_core::RngExt;

    // Get or generate salt
    let salt = if let Some(salt_b64) = salt_b64_opt {
        // Decode existing salt
        SaltString::from_b64(salt_b64).map_err(|e| format!("Failed to decode salt: {}", e))?
    } else {
        // Generate new salt
        let mut rand_gen = rng();
        let salt_bytes: Vec<u8> = (0..16).map(|_| rand_gen.random::<u8>()).collect();
        SaltString::encode_b64(&salt_bytes).map_err(|e| format!("Failed to encode salt: {}", e))?
    };

    // Use Argon2id for key derivation
    let argon2 = Argon2::default();
    let password_hash = argon2
        .hash_password(password.as_bytes(), &salt)
        .map_err(|e| format!("Failed to hash password: {}", e))?;

    // Extract the hash output (first 32 bytes for AES-256)
    let hash_output = password_hash.hash.ok_or("Failed to extract hash output")?;
    let key_bytes = hash_output.as_bytes();

    if key_bytes.len() < 32 {
        return Err("Key derivation produced insufficient bytes".into());
    }

    let key = Key::<Aes256Gcm>::from_slice(&key_bytes[..32]);
    Ok((*key, salt.to_string()))
}

/// Derive key from keyfile
fn derive_key_from_keyfile(keyfile: &Path) -> Result<Key<Aes256Gcm>, Box<dyn std::error::Error>> {
    // Read keyfile
    let mut file = File::open(keyfile)?;
    let mut key_data = Vec::new();
    file.read_to_end(&mut key_data)?;

    // Keyfile must be exactly 32 bytes for AES-256
    if key_data.len() != 32 {
        // If not exactly 32 bytes, hash it to get 32 bytes
        use ring::digest;
        let hash = digest::digest(&digest::SHA256, &key_data);
        let key = Key::<Aes256Gcm>::from_slice(hash.as_ref());
        Ok(*key)
    } else {
        let key = Key::<Aes256Gcm>::from_slice(&key_data);
        Ok(*key)
    }
}

/// Write encrypted file with metadata header
fn write_encrypted_file(
    path: &Path,
    metadata: &EncryptionMetadata,
    ciphertext: &[u8],
) -> Result<(), Box<dyn std::error::Error>> {
    let mut file = File::create(path)?;

    // Write magic header
    file.write_all(b"OXIRS_ENCRYPTED_BACKUP_V1\n")?;

    // Write metadata as JSON
    let metadata_json = serde_json::to_string(metadata)?;
    let metadata_bytes = metadata_json.as_bytes();
    file.write_all(&(metadata_bytes.len() as u32).to_le_bytes())?;
    file.write_all(metadata_bytes)?;

    // Write ciphertext
    file.write_all(ciphertext)?;

    // Ensure all data is flushed to disk
    file.flush()?;
    file.sync_all()?;

    Ok(())
}

/// Read encrypted file and extract metadata
fn read_encrypted_file(
    path: &Path,
) -> Result<(EncryptionMetadata, Vec<u8>), Box<dyn std::error::Error>> {
    let mut file = File::open(path)?;

    // Read and verify magic header (26 bytes: "OXIRS_ENCRYPTED_BACKUP_V1\n")
    let mut magic = vec![0u8; 26];
    file.read_exact(&mut magic)?;
    if magic != b"OXIRS_ENCRYPTED_BACKUP_V1\n" {
        return Err("Invalid encrypted backup file: bad magic header".into());
    }

    // Read metadata length
    let mut len_bytes = [0u8; 4];
    file.read_exact(&mut len_bytes)?;
    let metadata_len = u32::from_le_bytes(len_bytes) as usize;

    // Read metadata
    let mut metadata_bytes = vec![0u8; metadata_len];
    file.read_exact(&mut metadata_bytes)?;
    let metadata: EncryptionMetadata = serde_json::from_slice(&metadata_bytes)?;

    // Read ciphertext
    let mut ciphertext = Vec::new();
    file.read_to_end(&mut ciphertext)?;

    Ok((metadata, ciphertext))
}

/// Verify encrypted file integrity
fn verify_encrypted_file(
    path: &Path,
    _config: &EncryptionConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // Read and parse the file to ensure it's valid
    let (_metadata, _ciphertext) = read_encrypted_file(path)?;

    // Basic verification passed
    Ok(())
}

/// Generate a new keyfile with random 256-bit key
pub fn generate_keyfile(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    use scirs2_core::random::rng;
    use scirs2_core::RngExt;
    let mut rand_gen = rng();

    // Generate 32 random bytes (256 bits)
    let key_bytes: Vec<u8> = (0..32).map(|_| rand_gen.random::<u8>()).collect();

    // Write to file
    let mut file = File::create(path)?;
    file.write_all(&key_bytes)?;

    // Set restrictive permissions on Unix systems
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(path)?.permissions();
        perms.set_mode(0o600); // Read/write for owner only
        fs::set_permissions(path, perms)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::temp_dir;

    #[test]
    fn test_password_encryption_decryption() {
        let temp_dir = temp_dir();
        let source = temp_dir.join("test_source.txt");
        let encrypted = temp_dir.join("test_encrypted.oxirs");
        let decrypted = temp_dir.join("test_decrypted.txt");

        // Create test file
        let test_data = b"This is a test backup file with sensitive data!";
        fs::write(&source, test_data).unwrap();

        // Encrypt
        let encrypt_config = EncryptionConfig {
            password: Some("test_password_123".to_string()),
            keyfile: None,
            verify: true,
        };
        encrypt_backup(&source, &encrypted, &encrypt_config).unwrap();

        // Verify encrypted file exists and is different
        assert!(encrypted.exists());
        let encrypted_data = fs::read(&encrypted).unwrap();
        assert_ne!(encrypted_data.as_slice(), test_data);

        // Decrypt
        let decrypt_config = EncryptionConfig {
            password: Some("test_password_123".to_string()),
            keyfile: None,
            verify: false,
        };
        decrypt_backup(&encrypted, &decrypted, &decrypt_config).unwrap();

        // Verify decrypted data matches original
        let decrypted_data = fs::read(&decrypted).unwrap();
        assert_eq!(decrypted_data.as_slice(), test_data);

        // Cleanup
        let _ = fs::remove_file(source);
        let _ = fs::remove_file(encrypted);
        let _ = fs::remove_file(decrypted);
    }

    #[test]
    fn test_keyfile_generation_and_encryption() {
        let temp_dir = temp_dir();
        let keyfile = temp_dir.join("test_keyfile.key");
        let source = temp_dir.join("test_source2.txt");
        let encrypted = temp_dir.join("test_encrypted2.oxirs");
        let decrypted = temp_dir.join("test_decrypted2.txt");

        // Generate keyfile
        generate_keyfile(&keyfile).unwrap();
        assert!(keyfile.exists());
        let key_data = fs::read(&keyfile).unwrap();
        assert_eq!(key_data.len(), 32);

        // Create test file
        let test_data = b"Keyfile-based encryption test data!";
        fs::write(&source, test_data).unwrap();

        // Encrypt with keyfile
        let encrypt_config = EncryptionConfig {
            password: None,
            keyfile: Some(keyfile.clone()),
            verify: true,
        };
        encrypt_backup(&source, &encrypted, &encrypt_config).unwrap();

        // Decrypt with keyfile
        let decrypt_config = EncryptionConfig {
            password: None,
            keyfile: Some(keyfile.clone()),
            verify: false,
        };
        decrypt_backup(&encrypted, &decrypted, &decrypt_config).unwrap();

        // Verify
        let decrypted_data = fs::read(&decrypted).unwrap();
        assert_eq!(decrypted_data.as_slice(), test_data);

        // Cleanup
        let _ = fs::remove_file(keyfile);
        let _ = fs::remove_file(source);
        let _ = fs::remove_file(encrypted);
        let _ = fs::remove_file(decrypted);
    }

    #[test]
    fn test_wrong_password_fails() {
        let temp_dir = temp_dir();
        let source = temp_dir.join("test_source3.txt");
        let encrypted = temp_dir.join("test_encrypted3.oxirs");
        let decrypted = temp_dir.join("test_decrypted3.txt");

        // Create and encrypt
        fs::write(&source, b"Secret data").unwrap();
        let encrypt_config = EncryptionConfig {
            password: Some("correct_password".to_string()),
            keyfile: None,
            verify: false,
        };
        encrypt_backup(&source, &encrypted, &encrypt_config).unwrap();

        // Try to decrypt with wrong password
        let decrypt_config = EncryptionConfig {
            password: Some("wrong_password".to_string()),
            keyfile: None,
            verify: false,
        };
        let result = decrypt_backup(&encrypted, &decrypted, &decrypt_config);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Decryption failed"));

        // Cleanup
        let _ = fs::remove_file(source);
        let _ = fs::remove_file(encrypted);
    }
}