rencfs 0.14.11

WARNING! UNDER ACTIVE DEVELOPMENT. An encrypted file system that is mounted with FUSE on Linux. It can be used to create encrypted directories.
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
use std::fs::{File, OpenOptions};
use std::io;
use std::io::{Read, Seek, Write};
use std::num::ParseIntError;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use argon2::Argon2;
use base64::alphabet::STANDARD;
use base64::engine::general_purpose::NO_PAD;
use base64::engine::GeneralPurpose;
use base64::{DecodeError, Engine};
use hex::FromHexError;
use num_format::{Locale, ToFormattedString};
use rand_chacha::rand_core::{CryptoRng, RngCore, SeedableRng};
use rand_chacha::ChaCha20Rng;
use ring::aead::{AES_256_GCM, CHACHA20_POLY1305};
use serde::{Deserialize, Serialize};
use shush_rs::{ExposeSecret, SecretString, SecretVec};
use strum_macros::{Display, EnumIter, EnumString};
use thiserror::Error;
use tracing::{debug, error, instrument};
use write::CryptoInnerWriter;

use crate::crypto::read::{CryptoRead, CryptoReadSeek, RingCryptoRead};
use crate::crypto::write::{CryptoWrite, CryptoWriteSeek, RingCryptoWrite};
use crate::encryptedfs::FsResult;
use crate::{fs_util, stream_util};

pub mod buf_mut;
pub mod read;
pub mod write;

pub static BASE64: GeneralPurpose = GeneralPurpose::new(&STANDARD, NO_PAD);

#[derive(
    Debug, Clone, Copy, EnumIter, EnumString, Display, Serialize, Deserialize, PartialEq, Eq,
)]
pub enum Cipher {
    ChaCha20Poly1305,
    Aes256Gcm,
}

impl Cipher {
    /// In bytes.
    #[must_use]
    #[allow(clippy::use_self)]
    pub fn key_len(&self) -> usize {
        match self {
            Cipher::ChaCha20Poly1305 => CHACHA20_POLY1305.key_len(),
            Cipher::Aes256Gcm => AES_256_GCM.key_len(),
        }
    }

    /// Max length (in bytes) of the plaintext that can be encrypted before becoming unsafe.
    #[must_use]
    #[allow(clippy::use_self)]
    pub const fn max_plaintext_len(&self) -> usize {
        match self {
            Cipher::ChaCha20Poly1305 => (2_usize.pow(32) - 1) * 64,
            Cipher::Aes256Gcm => (2_usize.pow(39) - 256) / 8,
        }
    }
}

#[derive(Debug, Error)]
pub enum Error {
    // #[error("cryptostream error: {source}")]
    // OpenSsl {
    //     #[from]
    //     source: ErrorStack,
    //     // backtrace: Backtrace,
    // },
    #[error("IO error: {source}")]
    Io {
        #[from]
        source: io::Error,
        // backtrace: Backtrace,
    },
    #[error("from hex error: {source}")]
    FromHexError {
        #[from]
        source: FromHexError,
        // backtrace: Backtrace,
    },
    #[error("hex decode: {source}")]
    DecodeError {
        #[from]
        source: DecodeError,
        // backtrace: Backtrace,
    },
    #[error("parse int: {source}")]
    ParseIntError {
        #[from]
        source: ParseIntError,
        // backtrace: Backtrace,
    },
    #[error("serialize error: {source}")]
    SerializeError {
        #[from]
        source: bincode::Error,
        // backtrace: Backtrace,
    },
    #[error("generic error: {0}")]
    Generic(&'static str),
    #[error("generic error: {0}")]
    GenericString(String),
}

pub type Result<T> = std::result::Result<T, Error>;

/// Creates an encrypted writer
pub fn create_write<W: CryptoInnerWriter + Send + Sync + 'static>(
    writer: W,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> impl CryptoWrite<W> {
    create_ring_write(writer, cipher, key)
}

/// Creates an encrypted writer with seek
pub fn create_write_seek<W: CryptoInnerWriter + Seek + Read + Send + Sync + 'static>(
    writer: W,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> impl CryptoWriteSeek<W> {
    create_ring_write_seek(writer, cipher, key)
}

fn create_ring_write<W: CryptoInnerWriter + Send + Sync>(
    writer: W,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> RingCryptoWrite<W> {
    let algorithm = match cipher {
        Cipher::ChaCha20Poly1305 => &CHACHA20_POLY1305,
        Cipher::Aes256Gcm => &AES_256_GCM,
    };
    RingCryptoWrite::new(writer, false, algorithm, key)
}

fn create_ring_write_seek<W: CryptoInnerWriter + Seek + Read + Send + Sync>(
    writer: W,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> RingCryptoWrite<W> {
    let algorithm = match cipher {
        Cipher::ChaCha20Poly1305 => &CHACHA20_POLY1305,
        Cipher::Aes256Gcm => &AES_256_GCM,
    };
    RingCryptoWrite::new(writer, true, algorithm, key)
}

fn create_ring_read<R: Read + Send + Sync>(
    reader: R,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> RingCryptoRead<R> {
    let algorithm = match cipher {
        Cipher::ChaCha20Poly1305 => &CHACHA20_POLY1305,
        Cipher::Aes256Gcm => &AES_256_GCM,
    };
    RingCryptoRead::new(reader, algorithm, key)
}

fn create_ring_read_seek<R: Read + Seek + Send + Sync>(
    reader: R,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> RingCryptoRead<R> {
    let algorithm = match cipher {
        Cipher::ChaCha20Poly1305 => &CHACHA20_POLY1305,
        Cipher::Aes256Gcm => &AES_256_GCM,
    };
    RingCryptoRead::new_seek(reader, algorithm, key)
}

/// Creates an encrypted reader
pub fn create_read<R: Read + Send + Sync>(
    reader: R,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> impl CryptoRead<R> {
    create_ring_read(reader, cipher, key)
}

/// Creates an encrypted reader with seek
pub fn create_read_seek<R: Read + Seek + Send + Sync>(
    reader: R,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> impl CryptoReadSeek<R> {
    create_ring_read_seek(reader, cipher, key)
}

#[allow(clippy::missing_errors_doc)]
pub fn encrypt(s: &SecretString, cipher: Cipher, key: &SecretVec<u8>) -> Result<String> {
    let mut cursor = io::Cursor::new(vec![]);
    let mut writer = create_write(cursor, cipher, key);
    writer.write_all(s.expose_secret().as_bytes())?;
    cursor = writer.finish()?;
    let v = cursor.into_inner();
    Ok(BASE64.encode(v))
}

#[allow(clippy::missing_panics_doc)]
#[allow(clippy::missing_errors_doc)]
pub fn decrypt(s: &str, cipher: Cipher, key: &SecretVec<u8>) -> Result<SecretString> {
    let vec = BASE64.decode(s)?;
    let cursor = io::Cursor::new(vec);

    let mut reader = create_read(cursor, cipher, key);
    let mut decrypted = String::new();
    reader.read_to_string(&mut decrypted)?;
    Ok(SecretString::new(Box::new(decrypted)))
}

#[allow(clippy::missing_errors_doc)]
pub fn decrypt_file_name(name: &str, cipher: Cipher, key: &SecretVec<u8>) -> Result<SecretString> {
    let name = String::from(name).replace('|', "/");
    decrypt(&name, cipher, key)
}

#[instrument(skip(password, salt))]
#[allow(clippy::missing_errors_doc)]
pub fn derive_key(password: &SecretString, cipher: Cipher, salt: &[u8]) -> Result<SecretVec<u8>> {
    let mut dk = vec![];
    let key_len = cipher.key_len();
    dk.resize(key_len, 0);
    Argon2::default()
        .hash_password_into(password.expose_secret().as_bytes(), salt, &mut dk)
        .map_err(|err| Error::GenericString(err.to_string()))?;
    Ok(SecretVec::new(Box::new(dk)))
}

#[allow(clippy::missing_errors_doc)]
pub fn encrypt_file_name(
    name: &SecretString,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> FsResult<String> {
    let secret_string = name.expose_secret();

    match secret_string.as_str() {
        "$." | "$.." => Ok(secret_string.clone()),
        "." | ".." => Ok(format!("${secret_string}")),
        _ => {
            let secret = SecretString::from_str(&secret_string)
                .map_err(|err| Error::GenericString(err.to_string()))?;
            let mut encrypted = encrypt(&secret, cipher, key)?;
            encrypted = encrypted.replace('/', "|");

            Ok(encrypted)
        }
    }
}

#[allow(clippy::missing_errors_doc)]
#[must_use]
pub fn hash_file_name(name: &SecretString) -> String {
    if *name.expose_secret() == "$." || *name.expose_secret() == "$.." {
        name.expose_secret().clone()
    } else if *name.expose_secret() == "." || *name.expose_secret() == ".." {
        format!("${}", name.expose_secret())
    } else {
        hex::encode(hash_secret_string(name))
    }
}

#[must_use]
pub fn hash(data: &[u8]) -> [u8; 32] {
    let mut hasher = blake3::Hasher::new();
    hasher.update(data);
    hasher.finalize().into()
}

#[allow(clippy::missing_panics_doc)]
pub fn hash_reader<R: Read + ?Sized>(r: &mut R) -> io::Result<[u8; 32]> {
    let mut hasher = blake3::Hasher::new();
    let mut reader = io::BufReader::new(r);
    io::copy(&mut reader, &mut hasher)?;
    Ok(hasher.finalize().into())
}

#[must_use]
pub fn hash_secret_string(data: &SecretString) -> [u8; 32] {
    hash(data.expose_secret().as_bytes())
}

#[must_use]
pub fn hash_secret_vec(data: &SecretVec<u8>) -> [u8; 32] {
    hash(&data.expose_secret())
}

/// Copy from `pos` position in file `len` bytes
#[instrument(skip(w, key), fields(pos = pos.to_formatted_string(& Locale::en), len = len.to_formatted_string(& Locale::en)))]
#[allow(clippy::missing_errors_doc)]
pub fn copy_from_file_exact(
    file: PathBuf,
    pos: u64,
    len: u64,
    cipher: Cipher,
    key: &SecretVec<u8>,
    w: &mut impl Write,
) -> io::Result<()> {
    debug!("");
    copy_from_file(file, pos, len, cipher, key, w, false)?;
    Ok(())
}

#[allow(clippy::missing_errors_doc)]
pub fn copy_from_file(
    file: PathBuf,
    pos: u64,
    len: u64,
    cipher: Cipher,
    key: &SecretVec<u8>,
    w: &mut impl Write,
    stop_on_eof: bool,
) -> io::Result<u64> {
    if len == 0 || file.metadata()?.len() == 0 {
        // no-op
        return Ok(0);
    }
    // create a new reader by reading from the beginning of the file
    let mut reader = create_read(OpenOptions::new().read(true).open(file)?, cipher, key);
    // move read position to the write position
    let pos2 = stream_util::seek_forward(&mut reader, pos, stop_on_eof)?;
    if pos2 < pos {
        return if stop_on_eof {
            Ok(0)
        } else {
            Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "unexpected eof",
            ))
        };
    }

    // copy the rest of the file
    let len = stream_util::copy(&mut reader, w, len, stop_on_eof)?;
    Ok(len)
}

#[must_use]
pub fn create_rng() -> impl RngCore + CryptoRng {
    ChaCha20Rng::from_entropy()
}

pub fn serialize_encrypt_into<W, T>(
    writer: W,
    value: &T,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> Result<W>
where
    W: CryptoInnerWriter + Send + Sync + 'static,
    T: serde::Serialize + ?Sized,
{
    let mut writer = create_write(writer, cipher, key);
    bincode::serialize_into(&mut writer, value)?;
    let writer = writer.finish()?;
    Ok(writer)
}

pub fn atomic_serialize_encrypt_into<T>(
    file: &Path,
    value: &T,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> Result<()>
where
    T: serde::Serialize + ?Sized,
{
    let parent = file.parent().ok_or(Error::Generic("file has no parent"))?;
    let mut file = fs_util::open_atomic_write(file)?;
    file = serialize_encrypt_into(file, value, cipher, key)?;
    file.commit()?;
    File::open(parent)?.sync_all()?;
    Ok(())
}

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

    use rand_core::RngCore;
    use shush_rs::{ExposeSecret, SecretString, SecretVec};
    use std::{
        fs::File,
        io::{self, Write},
        path::{Path, PathBuf},
    };
    use tempfile::{tempdir, TempDir};

    fn create_encrypted_file(
        content: &str,
        cipher: Cipher,
        key: &SecretVec<u8>,
    ) -> (TempDir, PathBuf) {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");

        let mut file = File::create(file_path.clone()).unwrap();
        file.write_all(content.as_bytes()).unwrap();
        file.flush().unwrap();

        let encrypted_file_path = Path::new(&file_path).to_path_buf().with_extension("enc");
        if encrypted_file_path.exists() {
            std::fs::remove_file(&encrypted_file_path).unwrap();
        }

        let mut file = File::open(file_path).unwrap();
        let mut writer = create_write(
            File::create(encrypted_file_path.clone()).unwrap(),
            cipher,
            key,
        );
        io::copy(&mut file, &mut writer).unwrap();
        writer.finish().unwrap();

        (temp_dir, encrypted_file_path)
    }

    fn secret_key(cipher: Cipher) -> SecretVec<u8> {
        let mut key = vec![0; cipher.key_len()];
        create_rng().fill_bytes(&mut key);
        SecretVec::new(Box::new(key))
    }

    #[test]
    fn test_simple_encrypt_and_decrypt() {
        let secret = SecretString::from_str("Test secret").unwrap();

        for &cipher in &[Cipher::ChaCha20Poly1305, Cipher::Aes256Gcm] {
            let key = secret_key(cipher);

            let encrypted = encrypt(&secret, cipher, &key).unwrap();
            let decrypted = decrypt(&encrypted, cipher, &key).unwrap();
            assert_eq!(decrypted.expose_secret(), secret.expose_secret());
        }
    }

    #[test]
    fn test_encrypt_and_decrypt_file_name() {
        let secret_name = SecretString::from_str("testfile.txt").unwrap();

        for &cipher in &[Cipher::ChaCha20Poly1305, Cipher::Aes256Gcm] {
            let key = secret_key(cipher);
            let encrypted = encrypt_file_name(&secret_name, cipher, &key).unwrap();
            let decrypted = decrypt_file_name(&encrypted, cipher, &key).unwrap();
            assert_eq!(decrypted.expose_secret(), secret_name.expose_secret());
        }

        let secret_name = SecretString::from_str("testfile\\With/slash.txt").unwrap();

        for &cipher in &[Cipher::ChaCha20Poly1305, Cipher::Aes256Gcm] {
            let key = secret_key(cipher);
            let encrypted = encrypt_file_name(&secret_name, cipher, &key).unwrap();
            let decrypted = decrypt_file_name(&encrypted, cipher, &key).unwrap();
            assert_eq!(decrypted.expose_secret(), secret_name.expose_secret());
        }
    }

    #[test]
    fn test_encrypt_and_decrypt_file_name_invalid_cipher() {
        let key = secret_key(Cipher::ChaCha20Poly1305);
        let secret_name = SecretString::from_str("testfile.txt").unwrap();

        let encrypted = encrypt_file_name(&secret_name, Cipher::ChaCha20Poly1305, &key).unwrap();
        let result = decrypt_file_name(&encrypted, Cipher::Aes256Gcm, &key);
        assert!(result.is_err());
    }

    #[test]
    fn test_derive_key() {
        let password = SecretString::from_str("password").unwrap();
        let salt = b"salt_of_pass";

        for &cipher in &[Cipher::ChaCha20Poly1305, Cipher::Aes256Gcm] {
            let derived_key = derive_key(&password, cipher, salt).unwrap();
            assert_eq!(derived_key.expose_secret().len(), cipher.key_len());
        }
    }

    #[test]
    fn test_derive_key_consistency() {
        let password = SecretString::from_str("password").unwrap();
        let salt = b"random_salt";

        let derived_key_1 = derive_key(&password, Cipher::ChaCha20Poly1305, salt).unwrap();
        let derived_key_2 = derive_key(&password, Cipher::ChaCha20Poly1305, salt).unwrap();

        assert_eq!(derived_key_1.expose_secret(), derived_key_2.expose_secret());
    }

    #[test]
    fn test_derive_key_empty_salt() {
        let empty_password = SecretString::from_str("password").unwrap();
        let empty_salt = b"";

        let result = derive_key(&empty_password, Cipher::ChaCha20Poly1305, empty_salt);

        // Salt is too small
        assert!(result.is_err());
    }

    #[test]
    fn test_derive_key_uniqueness() {
        let password = SecretString::from_str("password").unwrap();
        let salts = vec![b"random_salt1", b"random_salt2", b"random_salt3"];

        let mut derived_keys = std::collections::HashSet::new();
        for salt in salts.clone() {
            let derived_key = derive_key(&password, Cipher::ChaCha20Poly1305, salt).unwrap();
            derived_keys.insert(derived_key.expose_secret().clone());
        }

        assert_eq!(derived_keys.len(), salts.len());
    }

    #[test]
    fn test_encrypt_decrypt() {
        for &cipher in &[Cipher::ChaCha20Poly1305, Cipher::Aes256Gcm] {
            let key = secret_key(cipher);

            let data = SecretString::from_str("A").unwrap();
            let encrypted = encrypt(&data, cipher, &key).unwrap();
            let decrypted = decrypt(&encrypted, cipher, &key).unwrap();
            assert_eq!(decrypted.expose_secret(), data.expose_secret());

            let large_data = SecretString::from_str("A".repeat(1024 * 1024).as_str()).unwrap(); // 1 MB
            let encrypted = encrypt(&large_data, cipher, &key).unwrap();
            let decrypted = decrypt(&encrypted, cipher, &key).unwrap();
            assert_eq!(decrypted.expose_secret(), large_data.expose_secret());
        }
    }

    #[test]
    fn test_encrypt_decrypt_empty_string() {
        let key = SecretVec::from(vec![0; 32]);
        let secret = SecretString::new(Box::new(String::new()));

        let encrypted = encrypt(&secret, Cipher::ChaCha20Poly1305, &key).unwrap();
        let decrypted = decrypt(&encrypted, Cipher::ChaCha20Poly1305, &key).unwrap();

        assert_eq!(*decrypted.expose_secret(), "");
    }

    #[test]
    fn test_hash_file_name_special_cases() {
        let expected = "$.".to_owned();
        let name = SecretString::new(Box::new(expected.clone()));
        let result = hash_file_name(&name);
        assert_eq!(result, expected);

        let expected = "$..".to_owned();
        let name = SecretString::new(Box::new(expected.clone()));
        let result = hash_file_name(&name);
        assert_eq!(result, expected);

        let input = ".".to_owned();
        let expected = "$.".to_owned();
        let name = SecretString::new(Box::new(input));
        let result = hash_file_name(&name);
        assert_eq!(result, expected);

        let input = "..".to_owned();
        let expected = "$..".to_owned();
        let name = SecretString::new(Box::new(input));
        let result = hash_file_name(&name);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_hash_file_name_regular_case() {
        let name = SecretString::new(Box::new("filename.txt".to_owned()));
        let result = hash_file_name(&name);
        let expected_hash = hex::encode(hash_secret_string(&name));
        assert_eq!(result, expected_hash);
    }

    #[test]
    fn test_hash_secret_string() {
        let secret = SecretString::new(Box::new("hash this secret".to_owned()));
        let expected_hash_hex = "d820cbf278fc742d8ec30e43947674689cd06d5aa9b71a2f9afe162a4ce408dc";

        let hash_hex = hex::encode(hash_secret_string(&secret));
        assert_eq!(hash_hex, expected_hash_hex);
    }

    #[test]
    fn test_copy_from_file_exact() {
        let cipher = Cipher::ChaCha20Poly1305;
        let key = secret_key(cipher);

        let content = "Hello World!";
        let (_temp_dir, file_path) = create_encrypted_file(content, cipher, &key);

        let mut output = Vec::new();
        copy_from_file_exact(
            file_path.clone(),
            0,
            content.len() as u64,
            cipher,
            &key,
            &mut output,
        )
        .unwrap();
        assert_eq!(&output, content.as_bytes());
    }

    #[test]
    fn test_copy_from_file_exact_zero_length() {
        let key = SecretVec::from(vec![0; 32]);
        let (_temp_dir, file_path) =
            create_encrypted_file("Hello, world!", Cipher::ChaCha20Poly1305, &key);

        let mut output = Vec::new();
        let result = copy_from_file_exact(
            file_path.clone(),
            0,
            0,
            Cipher::ChaCha20Poly1305,
            &key,
            &mut output,
        );

        assert!(result.is_ok());
        assert!(output.is_empty());
    }

    #[test]
    fn test_copy_from_file_exact_position_beyond_eof() {
        let cipher = Cipher::ChaCha20Poly1305;
        let key = secret_key(cipher);

        let content = "Hello, world!";
        let (_temp_dir, file_path) = create_encrypted_file(content, Cipher::ChaCha20Poly1305, &key);

        let mut output = Vec::new();
        let result = copy_from_file_exact(
            file_path.clone(),
            content.len() as u64 + 1,
            10,
            Cipher::ChaCha20Poly1305,
            &key,
            &mut output,
        );

        assert!(result.is_err());
    }
}