Skip to content

Commit

Permalink
Encryption wrapper fully functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusTreloar committed Nov 23, 2022
1 parent a44e527 commit 8dfa70f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 44 deletions.
18 changes: 18 additions & 0 deletions bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ Direct Writer --- 846b9ba622817e229d134c38b494ffd7deb09a05

Time (mean ± σ): 1.659 s ± 0.467 s [User: 0.720 s, System: 0.292 s]
Range (min … max): 0.990 s … 2.426 s 10 runs


------------------------------------------------

After finishing the encryption wrapper:

$ du test/to_comp/
$ 311612 test/to_comp/

Encrypted -- commit after (a44e5277dc14a32001c0d094bfdc454a69cd402c)

Time (mean ± σ): 1.412 s ± 0.114 s [User: 0.968 s, System: 0.271 s]
Range (min … max): 1.236 s … 1.561 s 10 runs

Passthrough - commit after (a44e5277dc14a32001c0d094bfdc454a69cd402c)

Time (mean ± σ): 1.054 s ± 0.256 s [User: 0.658 s, System: 0.265 s]
Range (min … max): 0.861 s … 1.714 s 10 runs
2 changes: 1 addition & 1 deletion src/bin/zap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Command {
&output,
).await?;

//fs::remove_dir_all("/tmp/unpacked")
fs::remove_dir_all("/tmp/unpacked");
Ok(())
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/compression/algorithms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Internal
use super::Cleanup;

use core::panic;
// External
use std::io::{
Error,
Expand Down Expand Up @@ -36,7 +37,6 @@ where T: Write

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let len = self.inner.write(buf)?;
dbg!(buf);
Ok(len)
}
}
Expand Down Expand Up @@ -77,12 +77,10 @@ impl<T> Read for Lz4Decoder<T>
where T: Read
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
dbg!("Start");
dbg!(buf.len());
let ret = self.inner.read_until(buf);
dbg!(buf[0..8].to_vec());
dbg!(buf.len());
Ok(buf.len())

let len = self.inner.read(buf)?;

Ok(len)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/encryption/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ where T: Write
_key_len: 256,
blocksize: Cipher::aes_256_cbc().block_size(),
_iv: iv.clone(),
internal_buffer: Vec::new(),
writer: x?
}
)
Expand Down
72 changes: 45 additions & 27 deletions src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
Read,
Error,
ErrorKind,
}, fmt::format, vec
}, fmt::format, vec, os::linux::raw
};
use openssl::{
hash::{
Expand All @@ -20,7 +20,7 @@ use openssl::{
},
symm::{
Cipher,
encrypt, decrypt, Crypter
encrypt, decrypt, Crypter, Mode
}
};

Expand Down Expand Up @@ -76,6 +76,7 @@ where T: Write
_key_len: u64,
blocksize: usize,
_iv: Vec<u8>,
internal_buffer: Vec<u8>,
writer: T
}

Expand All @@ -91,7 +92,7 @@ where T: Write

let buf_len = buf.len();
let mut enc_buf = vec![0u8;buf_len+self.blocksize];
dbg!(&buf_len);

let len = match self.cipher.update(
&buf,
&mut enc_buf,
Expand All @@ -105,9 +106,9 @@ where T: Write
};

if len > 0 {
self.writer.write(&enc_buf)?;
self.writer.write(&enc_buf[0..len])?;
}

Ok(buf_len)
}
}
Expand All @@ -118,7 +119,8 @@ T: Write
{
fn cleanup(mut self) -> Result<T, Error> {
let mut enc_buf = vec![0u8;self.blocksize];
self.cipher.finalize(&mut enc_buf)?;
let len = self.cipher.finalize(&mut enc_buf)?;
dbg!(len);
self.writer.write(&mut enc_buf)?;
self.writer.flush()?;
Ok(self.writer)
Expand All @@ -141,28 +143,44 @@ where T: Read
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {

let mut enc_buf = vec![0u8;buf.len()];
if self.internal_buffer.len() == 0{
let mut raw_buf = vec![0u8;8192];

let read_len = self.reader.read(&mut raw_buf)?;

let mut dec_buf = vec![0u8;read_len+self.blocksize];

if read_len > 0 {
match self.cipher.update(
&mut raw_buf[0..read_len],
&mut dec_buf
) {
Ok(l) => {
self.internal_buffer.extend_from_slice(&mut dec_buf[0..l]);
let mut fin_buf = vec![0u8; 32];
if read_len < 8192 {
let len = self.cipher.finalize(&mut fin_buf)?;
self.internal_buffer.extend_from_slice(&mut fin_buf[0..len]);
}
},
Err(e) => return Err(
Error::new(
ErrorKind::Other,
format!("Failed to decrypt: {}", e.to_string()))
)
}
} else {
return Ok(0);
}
}
let cpy_len = std::cmp::min(buf.len(), self.internal_buffer.len());
buf[..cpy_len].clone_from_slice(
self.internal_buffer
.drain(..cpy_len)
.as_slice()
);

dbg!("here");
self.reader.read_exact(&mut enc_buf)?;
dbg!(&enc_buf);
dbg!(buf.len());
let mut pt_buf = vec![0u8;enc_buf.len()+self.blocksize+self.internal_buffer.len()];
pt_buf[0..self.internal_buffer.len()].swap_with_slice(&mut self.internal_buffer);
let len = match self.cipher.update(
&enc_buf,
&mut pt_buf,
) {
Ok(n) => n,
Err(e) => return Err(
Error::new(
ErrorKind::Other,
format!("Failed to decrypt: {}", e.to_string()))
)
};
buf.swap_with_slice(&mut pt_buf[0..buf.len()]);
self.internal_buffer.append(&mut pt_buf[buf.len()..].to_vec());
Ok(buf.len())
Ok(cpy_len)
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use compression::{Cleanup, compress, decompress, algorithms::lz4_decoder};
use encryption::{algorithm::{aes256, aes256_r}, convert_pw_to_key, Encryptor};
use crate::{compression::algorithms::Lz4Encoder};
use crate::{compression::algorithms::Lz4Encoder, encryption::{encryption_passthrough, EncryptionPassthrough}};
use internal::{process_unit, build_writer};
use signing::signers::{signer_passthrough, SignerPassthrough, verifier_passthrough};
use walkdir::WalkDir;
Expand All @@ -24,7 +24,6 @@ pub async fn compress_directory(
) -> io::Result<()>
{
let psk: Vec<u8> = convert_pw_to_key("password".to_owned(), 256).unwrap();

let mut task_list = Vec::with_capacity(800);

for entry in WalkDir::new(input_folder_path) {
Expand Down Expand Up @@ -61,21 +60,22 @@ pub async fn compress_directory(
let compress_task = tokio::spawn(async move {

let writer = build_writer(
aes256(
pass.clone(),
pass
),
//aes256(
// pass.clone(),
// pass
//),
encryption_passthrough,
lz4_encoder,
signer_passthrough
);

/*let writer = build_writer(
encryption_passthrough,
writer_algorithm,
signer_passthrough
);*/

dbg!(compress::<std::fs::File, SignerPassthrough<Lz4Encoder<Encryptor<fs::File>>>, fs::File>(
dbg!(compress::<std::fs::File, SignerPassthrough<Lz4Encoder<EncryptionPassthrough<fs::File>>>, fs::File>(
fs::File::open(entry_path).expect("Failed to open input file"),
writer(fs::File::create(output_path))
));
Expand All @@ -97,7 +97,7 @@ pub async fn decompress_directory(
) -> io::Result<()>
{
let psk: Vec<u8> = convert_pw_to_key("password".to_owned(), 256).unwrap();

let mut task_list = Vec::with_capacity(800);

for entry in WalkDir::new(input_folder_path) {
Expand Down

0 comments on commit 8dfa70f

Please sign in to comment.