feistel0 encryption and decryption confirmed working

This commit is contained in:
Christoph J. Scherr 2023-05-10 17:55:48 +02:00
parent 4ddf244ee9
commit ad7cdd48b1
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
2 changed files with 27 additions and 11 deletions

View File

@ -86,7 +86,7 @@ pub fn inner(input: u16, key: u16, verbose: bool) -> u16 {
} }
/// Boilerplate KSA, returns the same given values everytime. /// Boilerplate KSA, returns the same given values everytime.
pub fn key_scheduler(key: u32) -> Vec<u16> { pub fn key_scheduler(_key: u32) -> Vec<u16> {
return vec![0xdead, 0xc0ff, 0xee5a] return vec![0xdead, 0xc0ff, 0xee5a]
} }
@ -130,37 +130,45 @@ fn test_decrypt() {
let plaintext = 0xb00b00; let plaintext = 0xb00b00;
let ciphertext = encrypt(plaintext, keys.clone(), true); let ciphertext = encrypt(plaintext, keys.clone(), true);
let deciphertext = decrypt(ciphertext, keys.clone(), true); let deciphertext = decrypt(ciphertext, keys.clone(), true);
assert_eq!(ciphertext, deciphertext); assert_eq!(plaintext, deciphertext);
} }
/// decrypt a given plaintext with a given key vec /// decrypt a given plaintext with a given key vec
pub fn decrypt(ciphertext: u32, mut keys: Vec<u16>, verbose: bool) -> u32 { pub fn decrypt(ciphertext: u32, mut keys: Vec<u16>, verbose: bool) -> u32 {
assert_eq!(keys.len(), ROUNDS as usize); assert_eq!(keys.len(), ROUNDS as usize);
let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom");
let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom");
if verbose {
println!("input:\n{:08x}\n{:04x}\n {:04x}", ciphertext, lef, rig);
}
// swap lef rig // swap lef rig
let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom");
let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom");
let tmp = rig; let tmp = rig;
rig = lef; rig = lef;
lef = tmp; lef = tmp;
let mut ciphertext: u32 = 0;
ciphertext |= (lef as u32) << 16;
ciphertext |= rig as u32;
if verbose {
println!("input:\n{:08x}\n{:04x}\n {:04x}", ciphertext, lef, rig);
}
// reverse keys // reverse keys
keys.reverse(); keys.reverse();
// do the thing // do the thing
encrypt(ciphertext, keys, verbose); ciphertext = encrypt(ciphertext, keys, verbose);
// swap lef rig back // swap lef rig back
let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom");
let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom");
let tmp = rig; let tmp = rig;
rig = lef; rig = lef;
lef = tmp; lef = tmp;
let mut plaintext: u32 = 0; let mut plaintext: u32 = 0;
plaintext |= (lef as u32) << 16; plaintext |= (lef as u32) << 16;
plaintext |= rig as u32; plaintext |= rig as u32;
if verbose {
println!("input:\n{:08x}\n{:04x}\n {:04x}", plaintext, lef, rig);
}
if verbose { if verbose {
println!("returning plaintext:\n{:08x}", plaintext); println!("returning plaintext:\n{:08x}", plaintext);
} }

View File

@ -136,9 +136,11 @@ struct Feistel0SBOXArgs {
#[derive(Args, Clone, Debug, PartialEq, Eq)] #[derive(Args, Clone, Debug, PartialEq, Eq)]
struct Feistel0Args{ struct Feistel0Args{
#[clap(value_parser=maybe_hex::<u32>)] #[clap(value_parser=maybe_hex::<u32>)]
plaintext: u32, input: u32,
#[clap(value_parser=maybe_hex::<u32>)] #[clap(value_parser=maybe_hex::<u32>)]
key: u32, key: u32,
#[arg(short, long, default_value_t = false)]
decrypt: bool,
} }
/*************************************************************************************************/ /*************************************************************************************************/
@ -215,7 +217,13 @@ pub fn main() {
} }
AlgoActions::Feistel0(alg_fe0_args) => { AlgoActions::Feistel0(alg_fe0_args) => {
let keys = algo::feistel0::key_scheduler(alg_fe0_args.key); let keys = algo::feistel0::key_scheduler(alg_fe0_args.key);
let result: u32 = algo::feistel0::encrypt(alg_fe0_args.plaintext, keys, args.verbose); let result: u32;
if alg_fe0_args.decrypt {
result = algo::feistel0::decrypt(alg_fe0_args.input, keys, args.verbose);
}
else {
result = algo::feistel0::encrypt(alg_fe0_args.input, keys, args.verbose);
}
if args.machine { if args.machine {
println!("{}", result) println!("{}", result)
} }