diff --git a/src/ctr.rs b/src/ctr.rs index a26c967..603b0d4 100644 --- a/src/ctr.rs +++ b/src/ctr.rs @@ -1,37 +1,29 @@ use crate::bytes::Bytes; -// use crate::cbc; -// use openssl::symm; +use crate::cbc; +use openssl::symm; -fn _xor(a: &[u8], b: &[u8]) -> Vec { +fn xor(a: &Vec, b: &Vec) -> Vec { Iterator::zip(a.iter(), b.iter()) .map(|z| *(z.0) ^ *(z.1)) .collect() } -pub fn _encrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes { - // let block_size = 16; - let result: Vec = vec![]; - // let mut prev_cipher: Vec = iv.to_vec(); // first xor input is IV - // for data in data.chunks(block_size) { - // let xored = xor(&prev_cipher, data); - // let mut cipher = cbc::enrypt_aes_128_ecb_block(key, &xored); - // prev_cipher = cipher.to_vec(); // cipher is input for next xor - // result.append(&mut cipher); - // } - Bytes(result) +pub fn encrypt(key: &Bytes, nonce: u64, data: &Bytes) -> Bytes { + decrypt(key, nonce, data) } -pub fn decrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes { - // XXX: does not handle padding for last block - // let cipher_type = symm::Cipher::aes_128_ecb(); - // let block_size = cipher_type.block_size(); - let result: Vec = vec![]; - // let mut prev_cipher: Vec = iv.to_vec(); // first xor input is IV - // for cipher in data.chunks(block_size) { - // let xored = cbc::decrypt_aes_128_ecb_block(key, &cipher); - // let data = xor(&xored, &prev_cipher); - // result.extend(data.to_vec()); - // prev_cipher = cipher.to_vec(); - // } +pub fn decrypt(Bytes(key): &Bytes, nonce: u64, Bytes(data): &Bytes) -> Bytes { + let mut counter: u64 = 0; + let cipher_type = symm::Cipher::aes_128_ecb(); + let block_size = cipher_type.block_size(); + let mut result: Vec = vec![]; + for cipher in data.chunks(block_size) { + let mut keyinput = nonce.to_le_bytes().to_vec(); + keyinput.append(&mut counter.to_le_bytes().to_vec()); + let keystream = cbc::enrypt_aes_128_ecb_block(key, &keyinput); + let mut data = xor(&keystream, &cipher.to_vec()); + result.append(&mut data); + counter += 1; + } Bytes(result) } diff --git a/src/main.rs b/src/main.rs index edb91b3..70dd938 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,4 +27,5 @@ fn main() { set2::challenge16(); set3::challenge17(); set3::challenge18(); + set3::challenge19(); } diff --git a/src/set3.rs b/src/set3.rs index ba46f66..02e7f3b 100644 --- a/src/set3.rs +++ b/src/set3.rs @@ -97,10 +97,20 @@ pub fn challenge17() { pub fn challenge18() { let key = Bytes::from_utf8("YELLOW SUBMARINE"); + + let cleartext = Bytes::from_utf8("Let's see if we can get the party started hard my friends."); + let cipher = ctr::encrypt(&key, 42351234, &cleartext); + let roundtrip = ctr::encrypt(&key, 42351234, &cipher); + assert_eq!(cleartext, roundtrip); + let cipher = BytesBase64::from_base64( "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==", ) .to_bytes(); - let cleartext = ctr::decrypt(&key, &cipher).to_utf8(); - println!("[xxxx] Challenge 18: {cleartext}"); + let cleartext = ctr::decrypt(&key, 0, &cipher).to_utf8(); + println!("[okay] Challenge 18: {cleartext}"); +} + +pub fn challenge19() { + println!("[xxxx] Challenge 19: TBD"); }