Skip to content

Commit

Permalink
Refactored to a trait based component system.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusTreloar committed Oct 6, 2023
1 parent 22d9478 commit 6a63a36
Show file tree
Hide file tree
Showing 14 changed files with 1,164 additions and 246 deletions.
4 changes: 2 additions & 2 deletions src/bin/cli_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Command {
enc
)?;

let out_file = File::create(&output).expect("Could not create file");
let out_file = File::create(output).expect("Could not create file");

let mut out_writer = BufWriter::new(out_file);

Expand Down Expand Up @@ -123,7 +123,7 @@ impl Command {
}
// Need to check if this function validates path names
// to prevent directory traversal.
unpack_files(&input, "/tmp/unpacked")?;
unpack_files(input, "/tmp/unpacked")?;

zap::decompress_directory(
"/tmp/unpacked",
Expand Down
130 changes: 130 additions & 0 deletions src/compression/lz4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use std::io::{
Write,
Read
};
use lz4_flex::frame::{
FrameEncoder,
FrameDecoder
};

use crate::{error::CompressorInitError, encryption::{Encrypt, Decrypt}};

use super::{Compress, CompressionAlgorithm, DecompressionAlgorithm, Decompress};

pub struct Lz4Algorithm {
}

impl <T> CompressionAlgorithm<T> for Lz4Algorithm
where T: Encrypt
{
type Compressor = Lz4Compressor<T>;

fn compressor(&self, io: T) -> Result<Self::Compressor, CompressorInitError> {
Ok(Lz4Compressor::new(io))
}
}

impl Lz4Algorithm {
pub fn new() -> Lz4Algorithm {
Lz4Algorithm {
}
}
}

impl Default for Lz4Algorithm {
fn default() -> Self {
Self::new()
}
}

pub struct Lz4Compressor<T>
where T: Encrypt
{
encoder: FrameEncoder<T>,
}

impl <T> Lz4Compressor<T>
where T: Encrypt
{
pub fn new(io: T) -> Self {
Lz4Compressor {
encoder: FrameEncoder::new(io),
}
}
}

impl <T> Compress for Lz4Compressor<T>
where T: Encrypt
{
fn finalise(self) -> Result<(), std::io::Error> {
match self.encoder.finish() {
Ok(w) => w.finalise(),
Err(e) => Err(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Encryption failed: {}", e)
)
)
}
}
}

impl <T> Write for Lz4Compressor<T>
where T: Encrypt
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {

let len = self.encoder.write(buf)?;

Ok(len)
}

fn flush(&mut self) -> std::io::Result<()> {
self.encoder.flush()
}
}

pub struct Lz4Deompressor<T>
where T: Decrypt
{
decoder: FrameDecoder<T>,
}

impl <T> Lz4Deompressor<T>
where T: Decrypt
{
pub fn new(io: T) -> Self {
Lz4Deompressor {
decoder: FrameDecoder::new(io),
}
}
}

impl <T> Decompress for Lz4Deompressor<T>
where T: Decrypt
{
fn finalise(self) -> Result<(), std::io::Error> {
self.decoder.into_inner().finalise()
}
}

impl <T> Read for Lz4Deompressor<T>
where T: Decrypt
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {

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

Ok(len)
}
}

impl <T> DecompressionAlgorithm<T> for Lz4Algorithm
where T: Decrypt
{
type Decompressor = Lz4Deompressor<T>;

fn decompressor(&self, io: T) -> Result<Self::Decompressor, CompressorInitError> {
Ok(Lz4Deompressor::new(io))
}
}
48 changes: 36 additions & 12 deletions src/compression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
pub mod algorithms;
pub mod lz4;

use crate::signing::{Signer, Verifier};
use crate::{signing::{Signer, Verifier}, error::CompressorInitError};

// External
use std::{
io::{
Error,
Read,
Write,
copy,
ErrorKind,
}
use std::io::{
Error,
Read,
Write,
copy,
ErrorKind,
};
use lz4_flex::frame::{
FrameEncoder,
Expand All @@ -27,7 +26,7 @@ V: Write
// Unwrap our output
let mut out = output?;
copy(&mut input, &mut out)?;
out.cleanup()
out.finalise()
}

pub fn decompress<T, U, V>(input: Result<T, Error>, mut output: U) -> Result<bool, Error>
Expand All @@ -39,7 +38,7 @@ V: Read
// Unwrap our input
let mut inp = input?;
copy(&mut inp, &mut output)?;
inp.cleanup()
inp.finalise()
}

// This and Decoder will end up being the generic compression
Expand Down Expand Up @@ -75,7 +74,7 @@ where T: Write+Cleanup<U>
Err(e) => Err(
Error::new(
ErrorKind::Other,
format!("Encryption failed: {}", e.to_string())
format!("Encryption failed: {}", e)
)
)
}
Expand Down Expand Up @@ -108,4 +107,29 @@ where T: Read+Cleanup<U>
}
}

pub struct CompressionMode;
pub struct DecompressionMode;

pub trait Compress: Write {
fn finalise(self) -> Result<(), Error>;
}
pub trait Decompress: Read {
fn finalise(self) -> Result<(), Error>;
}

pub trait CompressionAlgorithm<T>
where T: Write
{
type Compressor: Compress;

fn compressor(&self, writer: T) -> Result<Self::Compressor, CompressorInitError>;
}

pub trait DecompressionAlgorithm<T>
where T: Read
{
type Decompressor: Decompress;

fn decompressor(&self, reader: T) -> Result<Self::Decompressor, CompressorInitError>;
}

19 changes: 6 additions & 13 deletions src/encryption/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ use std::io::{
Error,
ErrorKind
};
use chacha20poly1305::{consts::{B1, B0}, ChaChaPoly1305};
use chacha20poly1305::ChaCha20Poly1305;
use aes_gcm::{
AesGcm,
aes::{Aes256, cipher::{typenum::{UInt, UTerm}, StreamCipherCoreWrapper}},
Aes256Gcm,
AeadCore,
KeyInit,
aead::{
Aead
}
};

/// This file holds all of the functions that return a Writer/Reader
Expand All @@ -34,10 +27,10 @@ use aes_gcm::{
/// When adding encryption methods, there is currently some boilerplate in the lib and bin files.
/// Future versions will work to minimize this.
pub fn chacha20poly1305<'a, T>(
pub fn chacha20poly1305<T>(
psk: Vec<u8>,
nonce:Vec<u8>,
) -> Box<dyn Fn(Result<T, Error>) -> Result<Encryptor<T, ChaChaPolyEncryptor>, Error>>
) -> impl Fn(Result<T, Error>) -> Result<Encryptor<T, ChaChaPolyEncryptor>, Error>
where T: Write
{
Box::new( move | x | Ok(
Expand All @@ -62,7 +55,7 @@ where T: Write
pub fn aes256<'a, T>(
psk: Vec<u8>,
nonce:Vec<u8>,
) -> Box<dyn Fn(Result<T, Error>) -> Result<Encryptor<T, AesGcmEncryptor>, Error>>
) -> impl Fn(Result<T, Error>) -> Result<Encryptor<T, AesGcmEncryptor>, Error>
where T: Write
{
Box::new( move | x | Ok(
Expand All @@ -84,7 +77,7 @@ where T: Write
) )
}

pub fn encryption_passthrough<'a, T>() -> Box< dyn Fn(Result<T, Error>) -> Result<Encryptor<T, ()>, Error> >
pub fn encryption_passthrough<'a, T>() -> impl Fn(Result<T, Error>) -> Result<Encryptor<T, ()>, Error>
where T: Write
{
Box::new( move | x | Ok(
Expand Down Expand Up @@ -117,7 +110,7 @@ where T: Read
))
}
),
key: psk.clone(),
_key: psk.clone(),
nonce: nonce.clone(),
internal_buffer: Vec::new(),
reader: x?
Expand All @@ -131,7 +124,7 @@ where T: Read
Box::new( move | x | Ok(
Decryptor {
cipher: None,
key: vec![],
_key: vec![],
nonce: vec![],
internal_buffer: Vec::new(),
reader: x?
Expand Down
Loading

0 comments on commit 6a63a36

Please sign in to comment.