Skip to content

Commit

Permalink
implemented issue #1 allow multiple reads in parallel but only exclus…
Browse files Browse the repository at this point in the history
…ive write
  • Loading branch information
radumarias committed Apr 24, 2024
1 parent 1071cf1 commit 8d96e38
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 120 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "encryptedfs"
description = "An encrypted file system that mounts with FUSE on Linux. It can be used to create encrypted directories."
version = "0.1.25"
version = "0.1.26"
edition = "2021"
license = "Apache-2.0"
authors = ["Radu Marias <[email protected]>"]
Expand Down Expand Up @@ -37,6 +37,7 @@ strum = "0.26.2"
strum_macros = "0.26.2"
rpassword = "7.3.1"
cryptostream = "0.3.2"
weak-table = "0.3.2"

[package.metadata.aur]
depends = ["fuse3"]
20 changes: 10 additions & 10 deletions examples/aes_stream_with_writer_seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<E: BlockEncryptor, R:Read + Seek, W: Write + Seek> AesWriter<E, R, W> {
///
/// [be]: https://docs.rs/rust-crypto/0.2.36/crypto/symmetriccipher/trait.BlockEncryptor.html
pub fn new(mut writer: W, mut reader: R, enc: E, first_write: bool) -> Result<AesWriter<E, R, W>> {
let mut iv = vec![0u8; enc.block_size()];
let mut iv = vec![0_u8; enc.block_size()];
if first_write {
OsRng::new()?.fill_bytes(&mut iv);
writer.write_all(&iv)?;
Expand All @@ -235,7 +235,7 @@ impl<E: BlockEncryptor, R:Read + Seek, W: Write + Seek> AesWriter<E, R, W> {

self.reader.as_mut().unwrap().seek(SeekFrom::Start(self.block_size as u64))?;
self.reader.as_mut().unwrap().seek(SeekFrom::Current((writer_size - self.block_size as u64) as i64))?;
let mut iv = vec![0u8; self.block_size];
let mut iv = vec![0_u8; self.block_size];
self.reader.as_mut().unwrap().read_exact(&mut iv)?;
self.enc.reset(&iv);

Expand All @@ -249,17 +249,17 @@ impl<E: BlockEncryptor, R:Read + Seek, W: Write + Seek> AesWriter<E, R, W> {

// reset CbcDecryptor
self.writer.as_mut().unwrap().seek(SeekFrom::Start((block_num - 1) * self.block_size as u64))?;
let mut iv = vec![0u8; self.block_size];
let mut iv = vec![0_u8; self.block_size];
self.reader.as_mut().unwrap().seek(SeekFrom::Start((block_num - 1) * self.block_size as u64))?;
self.reader.as_mut().unwrap().read_exact(&mut iv)?;
self.writer.as_mut().unwrap().seek(SeekFrom::Current(iv.len() as i64))?;
self.enc.reset(&iv);

// skip remaining
let mut skip = vec![0u8; block_offset as usize];
let mut skip = vec![0_u8; block_offset as usize];
self.reader.as_mut().unwrap().read_exact(&mut skip)?;
let mut read_buf = RefReadBuffer::new(skip.as_slice());
let mut out = [0u8; BUFFER_SIZE];
let mut out = [0_u8; BUFFER_SIZE];
let mut write_buf = RefWriteBuffer::new(&mut out);

loop {
Expand Down Expand Up @@ -287,7 +287,7 @@ impl<E: BlockEncryptor, R:Read + Seek, W: Write + Seek> AesWriter<E, R, W> {
/// finished and padding added.
fn encrypt_write(&mut self, buf: &[u8], eof: bool) -> Result<usize> {
let mut read_buf = RefReadBuffer::new(buf);
let mut out = [0u8; BUFFER_SIZE];
let mut out = [0_u8; BUFFER_SIZE];
let mut write_buf = RefWriteBuffer::new(&mut out);
loop {
let res = self.enc.encrypt(&mut read_buf, &mut write_buf, eof)
Expand Down Expand Up @@ -471,7 +471,7 @@ impl<D: BlockDecryptor, R: Read> AesReader<D, R> {
///
/// [bd]: https://docs.rs/rust-crypto/0.2.36/crypto/symmetriccipher/trait.BlockDecryptor.html
pub fn new(mut reader: R, dec: D) -> Result<AesReader<D, R>> {
let mut iv = vec![0u8; dec.block_size()];
let mut iv = vec![0_u8; dec.block_size()];
reader.read_exact(&mut iv)?;
Ok(AesReader {
reader,
Expand All @@ -484,7 +484,7 @@ impl<D: BlockDecryptor, R: Read> AesReader<D, R> {

/// Reads at max BUFFER_SIZE bytes, handles potential eof and returns the buffer as Vec<u8>
fn fill_buf(&mut self) -> Result<Vec<u8>> {
let mut eof_buffer = vec![0u8; BUFFER_SIZE];
let mut eof_buffer = vec![0_u8; BUFFER_SIZE];
let read = self.reader.read(&mut eof_buffer)?;
self.eof = read == 0;
eof_buffer.truncate(read);
Expand Down Expand Up @@ -568,12 +568,12 @@ impl<D: BlockDecryptor, R: Read + Seek> AesReader<D, R> {
let block_offset = offset % self.block_size as u64;
// reset CbcDecryptor
self.reader.seek(SeekFrom::Start((block_num - 1) * self.block_size as u64))?;
let mut iv = vec![0u8; self.block_size];
let mut iv = vec![0_u8; self.block_size];
self.reader.read_exact(&mut iv)?;
self.dec.reset(&iv);
self.buffer = Vec::new();
self.eof = false;
let mut skip = vec![0u8; block_offset as usize];
let mut skip = vec![0_u8; block_offset as usize];
self.read_exact(&mut skip)?;
// subtract IV
Ok(offset - 16)
Expand Down
4 changes: 2 additions & 2 deletions examples/cryptostream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let mut decryptor =
read::Decryptor::new(src.as_slice(), Cipher::aes_128_cbc(), &key, &iv).unwrap();

let mut decrypted = [0u8; 1024]; // a buffer to decrypt into
let mut decrypted = [0_u8; 1024]; // a buffer to decrypt into
let mut bytes_decrypted = 0;

loop {
Expand Down Expand Up @@ -56,7 +56,7 @@ fn main() {
let mut decryptor =
read::Decryptor::new(file, Cipher::aes_128_cbc(), &key, &iv).unwrap();

let mut decrypted = [0u8; 1024]; // a buffer to decrypt into
let mut decrypted = [0_u8; 1024]; // a buffer to decrypt into
let mut bytes_decrypted = 0;
loop {
// Just read from the `Decryptor` as if it were any other `Read` impl,
Expand Down
2 changes: 1 addition & 1 deletion examples/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let n = 600_000;
// Expected value of generated key

// let mut key1 = [0u8; 20];
// let mut key1 = [0_u8; 20];
// pbkdf2_hmac::<Sha256>(password, salt, n, &mut key1);
// println!("{:?}", key1);

Expand Down
Loading

0 comments on commit 8d96e38

Please sign in to comment.