Skip to content

Commit

Permalink
Quick test, fixed chacha buffer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusTreloar committed Oct 6, 2023
1 parent 6a63a36 commit 90cb004
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 69 deletions.
5 changes: 3 additions & 2 deletions src/compression/lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where T: Encrypt
Err(e) => Err(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Encryption failed: {}", e)
format!("Encryption failed: {}", e) // TODO: better error handling
)
)
}
Expand All @@ -73,8 +73,9 @@ impl <T> Write for Lz4Compressor<T>
where T: Encrypt
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {

//println!("Dump buffer: {:?}", buf);
let len = self.encoder.write(buf)?;
println!("Comp write len: {:?}", len);

Ok(len)
}
Expand Down
59 changes: 40 additions & 19 deletions src/encryption/chachapoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,42 @@ pub struct ChaChaPoly<T, M> {
mode: PhantomData<M>
}

impl<T> ChaChaPoly<T, EncryptorMode>
where
T: Write
{
fn dump_buffer(&mut self) -> Result<Vec<u8>, Error> {
let len = std::cmp::min(8192, self.internal_buffer.len());

match self.cipher.encrypt(
// As noted in the struct def, this is will be changed
Nonce::from_slice(&self.nonce),
/*Payload{
msg: self.internal_buffer
.drain(..8192)
.as_slice(),
aad: &self.key
}*/
self.internal_buffer.drain(..len).as_slice(),
) {
Ok(n) => Ok(n),
Err(e) => {
Err(Error::new(
ErrorKind::Other,
format!("Failed to encrypt: {}", e),
))
}
}
}

}

impl <T> Encrypt for ChaChaPoly<T, EncryptorMode>
where T: Write
{
fn finalise(mut self) -> Result<(), Error> {
let buff = self.dump_buffer()?;
self.io.write_all(buff.as_slice())?;
self.io.flush()?;
Ok(())
}
Expand All @@ -119,27 +151,16 @@ where
}

fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
println!("Dump buffer enc: {:?}", buf);

self.internal_buffer.extend_from_slice(buf);

println!("Internal buffer: {:?}", self.internal_buffer.len());

if self.internal_buffer.len() > 8192 {
let enc_buf = match self.cipher.encrypt(
// As noted in the struct def, this is will be changed
Nonce::from_slice(&self.nonce),
/*Payload{
msg: self.internal_buffer
.drain(..8192)
.as_slice(),
aad: &self.key
}*/
self.internal_buffer.drain(..8192).as_slice(),
) {
Ok(n) => n,
Err(e) => {
return Err(Error::new(
ErrorKind::Other,
format!("Failed to encrypt: {}", e),
))
}
};
println!("Encrypting: {:?}", self.internal_buffer.len());

let enc_buf = self.dump_buffer()?;
// This is also implementation specific.
// As the aes is a block cipher is manages and internal buffer
// and when the buffer reaches a length greater than the blocksize
Expand Down
131 changes: 83 additions & 48 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,52 +168,87 @@ where
}
}



fn _example_usage() {

let mut _f = std::fs::File::create("test.txt").unwrap();

let mut _enc = ChaChaPolyAlgorithm::new()
.with_key(vec![0u8; 32])
.with_nonce(vec![0u8; 12])
.encryptor(_f)
.unwrap();

let mut _comp = Lz4Algorithm::new().compressor(_enc).unwrap();

let mut _signer = SignerPassthroughMethod::new().signer(_comp).unwrap();

let _p = FilePipeline::from_writer(_signer);

let mut _f2 = std::fs::File::create("test.txt").unwrap();

let _pipeline = FilePipeline::builder()
.with_encryption(
ChaChaPolyAlgorithm::new()
.with_key(vec![0u8; 32])
.with_nonce(vec![0u8; 12])
)
.with_compress_algorithm(
Lz4Algorithm::new()
)
.with_signing(
SignerPassthroughMethod::new()
)
.with_io(_f2)
.compression_pipeline()
.unwrap();
//let pipeline = FilePipeline::builder()
// .with_encryption(enc)
// .with_compress_algorithm(comp)
// .with_signing(signer)
// .compression_pipeline();

//let mut pipeline = Pipeline::new()
// .with_encryption(enc)
// .with_compress_algorithm(comp)
// .build()
// .unwrap();

//let pipeline =
mod pipeline_test{
use std::fs::File;

use crate::{
compression::{lz4::Lz4Algorithm, CompressionAlgorithm, DecompressionAlgorithm},
encryption::{chachapoly::ChaChaPolyAlgorithm, EncryptionAlgorithm, DecryptionAlgorithm},
error::{PipelineCompressionError, PipelineDecompressionError, PipelineBuildError},
signing::{passthrough::SignerPassthroughMethod, SignerMethod, VerifierMethod, Sign, Verify}, password::get_password_noconf,
};

use super::{FilePipeline, CompressionPipeline};

#[test]
fn _example_usage() {

let mut _f = match File::create("./test.out") {
Ok(f) => f,
Err(e) => panic!("Error: {:?}", e),
};

let mut _enc = match ChaChaPolyAlgorithm::new()
.with_key(get_password_noconf(256).unwrap())
.with_nonce(vec![1u8; 12])
.encryptor(_f){
Ok(e) => e,
Err(e) => panic!("Error: {:?}", e),
};

let mut _comp = match Lz4Algorithm::new().compressor(_enc) {
Ok(c) => c,
Err(e) => panic!("Error: {:?}", e),
};

let mut _signer = match SignerPassthroughMethod::new().signer(_comp) {
Ok(s) => s,
Err(e) => panic!("Error: {:?}", e),
};

let _p = FilePipeline::from_writer(_signer);

let mut input = match File::open("test.in") {
Ok(f) => f,
Err(e) => panic!("Error: {:?}", e),
};

match _p.compress(&mut input) {
Ok(Some(signature)) => println!("Success: {}", String::from_utf8_lossy(&signature)),
Ok(None) => println!("Success: no signature"),
Err(e) => println!("Error: {:?}", e),
};

let mut _f2 = std::fs::File::create("test.txt").unwrap();

let _pipeline = FilePipeline::builder()
.with_encryption(
ChaChaPolyAlgorithm::new()
.with_key(vec![0u8; 32])
.with_nonce(vec![0u8; 12])
)
.with_compress_algorithm(
Lz4Algorithm::new()
)
.with_signing(
SignerPassthroughMethod::new()
)
.with_io(_f2)
.compression_pipeline()
.unwrap();
//let pipeline = FilePipeline::builder()
// .with_encryption(enc)
// .with_compress_algorithm(comp)
// .with_signing(signer)
// .compression_pipeline();

//let mut pipeline = Pipeline::new()
// .with_encryption(enc)
// .with_compress_algorithm(comp)
// .build()
// .unwrap();

//let pipeline =
}

}

0 comments on commit 90cb004

Please sign in to comment.