You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
|
|
#[macro_use] extern crate hex_literal;
|
|
|
|
mod ext; #[macro_use] use ext::*;
|
|
|
|
mod key;
|
|
mod cha;
|
|
mod stream;
|
|
|
|
use key::{Key, IV};
|
|
|
|
fn encrypt((key, iv): &(Key, IV), input: impl AsRef<[u8]>) -> Result<String, openssl::error::ErrorStack>
|
|
{
|
|
let input = input.as_ref();
|
|
let mut output = vec![0u8; input.len()];
|
|
|
|
eprintln!("(enc) Key: {}, IV: {}, Input: ({}, {})", key, iv, input.len(), input.hex());
|
|
|
|
let mut enc = cha::encrypter(key, iv)?;
|
|
|
|
let n = enc.update(&input[..], &mut output[..])?;
|
|
eprintln!("(enc) Written {} bytes", n);
|
|
|
|
println!(">> {}", (&output[..n]).hex());
|
|
assert!(enc.finalize(&mut output[..n])? == 0);
|
|
println!(">> {}", (&output[..n]).hex());
|
|
|
|
Ok(base64::encode(&output[..n]))
|
|
}
|
|
|
|
fn decrypt((key, iv): &(Key, IV), input: impl AsRef<str>) -> Result<Vec<u8>, openssl::error::ErrorStack>
|
|
{
|
|
let input = base64::decode(input.as_ref()).expect("invalid base64");
|
|
let mut output = vec![0u8; input.len()];
|
|
|
|
eprintln!("(dec) Key: {}, IV: {}, Input: ({}, {})", key, iv, input.len(), input.hex());
|
|
|
|
let mut dec = cha::decrypter(key, iv)?;
|
|
|
|
let n = dec.update(&input[..], &mut output[..])?;
|
|
eprintln!("(dec) Written {} bytes", n);
|
|
|
|
println!(">> {}", (&output[..n]).hex());
|
|
assert!(dec.finalize(&mut output[..n])? == 0);
|
|
// assert!(dec.finalize(&mut output[..n])? == 0);
|
|
println!(">> {}", (&output[..n]).hex());
|
|
|
|
output.truncate(n);
|
|
Ok(output)
|
|
}
|
|
|
|
fn main() {
|
|
let input = std::env::args().nth(1).unwrap_or({
|
|
let mut input = [0u8; 16];
|
|
getrandom::getrandom(&mut input[..]).expect("rng fatal");
|
|
khash::generate(&Default::default(), input).expect("kana-hash fatal")
|
|
//input.hex().into()
|
|
});
|
|
|
|
let key = cha::keygen();
|
|
let enc = encrypt(&key, &input).expect("encrypt");
|
|
println!("{}", enc);
|
|
let dec = decrypt(&key, enc).expect("decrypt");
|
|
|
|
let output = std::str::from_utf8(&dec[..]).unwrap();
|
|
println!("{:?}", output);
|
|
assert_eq!(output, &input[..]);
|
|
}
|