|
|
@ -31,6 +31,7 @@ pub use source::Source;
|
|
|
|
mod tests
|
|
|
|
mod tests
|
|
|
|
{
|
|
|
|
{
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
|
|
|
|
const INPUT: &'static str = "Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!";
|
|
|
|
const INPUT: &'static str = "Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!";
|
|
|
|
|
|
|
|
|
|
|
@ -50,6 +51,20 @@ mod tests
|
|
|
|
stream
|
|
|
|
stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a source from a slice of bytes with this key and IV.
|
|
|
|
|
|
|
|
fn create_source<'a, T: ?Sized + AsRef<[u8]> +'a>(source: &'a T, key: Key, iv: IV, enc: bool) -> Source<Cursor<&'a [u8]>>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
eprintln!("({}) Key: {}, IV: {}, Input: ({}: {})", ["dec", "enc"][enc as usize], key, iv, source.as_ref().len(), source.as_ref().hex());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let stream = if enc {
|
|
|
|
|
|
|
|
Source::encrypt(Cursor::new(source.as_ref()), key, iv)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Source::decrypt(Cursor::new(source.as_ref()), key, iv)
|
|
|
|
|
|
|
|
}.expect("sink::enc");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stream
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn enc()
|
|
|
|
fn enc()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -58,6 +73,50 @@ mod tests
|
|
|
|
eprintln!("Sink ends: {:?}", enc_stream(INPUT.as_bytes(), key, iv));
|
|
|
|
eprintln!("Sink ends: {:?}", enc_stream(INPUT.as_bytes(), key, iv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn source_enc()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let (key, iv) = cha::keygen();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const INPUT: &'static [u8] = b"Hello world!";
|
|
|
|
|
|
|
|
println!("Input ({} bytes, hex): {}", INPUT.len(), INPUT.hex());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source = create_source(INPUT, key, iv, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut output = Vec::with_capacity(INPUT.len());
|
|
|
|
|
|
|
|
io::copy(&mut source, &mut output).expect("Failed to copy source to output");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("Output ({} bytes, hex): {}", output.len(), output.hex());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn source_dec()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let (key, iv) = cha::keygen();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const INPUT: &'static [u8] = b"Hello world!";
|
|
|
|
|
|
|
|
println!("Input ({} bytes, hex): {}", INPUT.len(), INPUT.hex());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source = create_source(INPUT, key, iv, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut temp = Vec::with_capacity(INPUT.len());
|
|
|
|
|
|
|
|
io::copy(&mut source, &mut temp).expect("Failed to copy source to output (encrypt)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("Encrypted ({} bytes, hex): {}", temp.len(), temp.hex());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// decrypt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source = create_source(&mut temp, key, iv, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut temp = Vec::with_capacity(INPUT.len());
|
|
|
|
|
|
|
|
io::copy(&mut source, &mut temp).expect("Failed to copy source to output (decrypt)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("Decrypted ({} bytes, hex): {}", temp.len(), temp.hex());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(INPUT, &temp[..]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn dec()
|
|
|
|
fn dec()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -78,9 +137,9 @@ mod tests
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(&dec_buffer[..], INPUT.as_bytes());
|
|
|
|
assert_eq!(&dec_buffer[..], INPUT.as_bytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks if explicit clear is actually clearing.
|
|
|
|
/// Checks if explicit clear is actually clearing.
|
|
|
|
#[cfg(feature="explicit_clear")]
|
|
|
|
#[cfg(feature="explicit_clear")]
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn remainder()
|
|
|
|
fn remainder()
|
|
|
|
{
|
|
|
|
{
|
|
|
|