This commit is contained in:
2026-05-02 16:20:20 +02:00
parent 1ae56389fc
commit 5e51b4bfe1
5 changed files with 434 additions and 143 deletions
+9 -9
View File
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only
use chacha20poly1305::{aead::stream, KeyInit, XChaCha20Poly1305};
use rand::{rngs::OsRng, RngCore};
use chacha20poly1305::{KeyInit, XChaCha20Poly1305, aead::stream};
use rand::{RngCore, rngs::OsRng};
use crate::error::*;
use crate::reader::ReadInfo;
use crate::reader::ReadInfoChunk;
use crate::utils::BUFSIZE;
use crate::utils::*;
@@ -32,7 +32,7 @@ pub fn encrypt<S: AsRef<str>>(
let read_result = f_plain.read_ahead(&mut buf)?;
match read_result {
ReadInfo::NormalChunk(n) => {
ReadInfoChunk::Normal(n) => {
assert_eq!(n, BUFSIZE);
assert_eq!(buf.len(), BUFSIZE);
eprintln!("[encrypt]: read normal chunk");
@@ -42,14 +42,14 @@ pub fn encrypt<S: AsRef<str>>(
// we shrink it to the BUFSIZE in order to read the correct size
buf.truncate(BUFSIZE);
}
ReadInfo::LastChunk(n) => {
ReadInfoChunk::Last(n) => {
eprintln!("[encrypt]: read last chunk");
buf.truncate(n);
stream_encryptor.encrypt_last_in_place(&[], &mut buf)?;
f_encrypted.write_all(&buf)?;
break;
}
ReadInfo::EmptyChunk => {
ReadInfoChunk::Empty => {
eprintln!("[encrypt]: read empty chunk");
panic!("[ERROR] Empty Chunk while reading");
}
@@ -79,21 +79,21 @@ pub fn decrypt<S: AsRef<str>>(
let read_result = f_encrypted.read_ahead(&mut buf)?;
match read_result {
ReadInfo::NormalChunk(n) => {
ReadInfoChunk::Normal(n) => {
assert_eq!(n, BUFSIZE + 16);
eprintln!("[decrypt]: read normal chunk");
stream_decryptor.decrypt_next_in_place(&[], &mut buf)?;
f_plain.write_all(&buf)?;
buf.resize(BUFSIZE + 16, 0);
}
ReadInfo::LastChunk(n) => {
ReadInfoChunk::Last(n) => {
eprintln!("[decrypt]: read last chunk");
buf.truncate(n);
stream_decryptor.decrypt_last_in_place(&[], &mut buf)?;
f_plain.write_all(&buf)?;
break;
}
ReadInfo::EmptyChunk => {
ReadInfoChunk::Empty => {
eprintln!("[decrypt]: read empty chunk");
panic!("Empty Chunk while reading");
}
+1
View File
@@ -3,6 +3,7 @@
use chacha20poly1305::aead;
use std::io;
#[allow(dead_code)]
#[derive(Debug)]
pub enum FcryError {
Io(io::Error),
+12 -12
View File
@@ -3,10 +3,10 @@
use std::io;
use std::io::{BufRead, Read};
pub enum ReadInfo {
NormalChunk(usize),
LastChunk(usize),
EmptyChunk,
pub enum ReadInfoChunk {
Normal(usize),
Last(usize),
Empty,
}
pub struct AheadReader {
@@ -46,7 +46,7 @@ impl AheadReader {
Ok(total)
}
pub fn read_ahead(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
pub fn read_ahead(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// 1st read
if self.bufsz == 0 {
eprintln!("[reader] first read");
@@ -62,11 +62,11 @@ impl AheadReader {
self.inner.read_exact(userbuf)
}
fn first_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
fn first_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// 1st read directly to userbuf (we have no cached data yet)
let n = self.read_until_full(userbuf)?;
if n == 0 {
return Ok(ReadInfo::EmptyChunk);
return Ok(ReadInfoChunk::Empty);
}
// 2nd read directly into our internal buf
@@ -75,13 +75,13 @@ impl AheadReader {
self.buf = tmp;
self.bufsz = n2;
if n2 == 0 {
return Ok(ReadInfo::LastChunk(n));
return Ok(ReadInfoChunk::Last(n));
}
Ok(ReadInfo::NormalChunk(n))
Ok(ReadInfoChunk::Normal(n))
}
fn normal_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfo> {
fn normal_read(&mut self, userbuf: &mut [u8]) -> io::Result<ReadInfoChunk> {
// copy internal buf to userbuf
userbuf.copy_from_slice(&self.buf);
let userbuf_sz = self.bufsz;
@@ -92,9 +92,9 @@ impl AheadReader {
self.buf = tmp;
self.bufsz = n2;
if n2 == 0 {
return Ok(ReadInfo::LastChunk(userbuf_sz));
return Ok(ReadInfoChunk::Last(userbuf_sz));
}
Ok(ReadInfo::NormalChunk(userbuf_sz))
Ok(ReadInfoChunk::Normal(userbuf_sz))
}
}