Implement challenge 18 CTR cipher mode.

This commit is contained in:
2022-07-09 11:54:55 -04:00
parent 1304ff2144
commit e70c6c470c
3 changed files with 31 additions and 28 deletions

View File

@@ -1,37 +1,29 @@
use crate::bytes::Bytes; use crate::bytes::Bytes;
// use crate::cbc; use crate::cbc;
// use openssl::symm; use openssl::symm;
fn _xor(a: &[u8], b: &[u8]) -> Vec<u8> { fn xor(a: &Vec<u8>, b: &Vec<u8>) -> Vec<u8> {
Iterator::zip(a.iter(), b.iter()) Iterator::zip(a.iter(), b.iter())
.map(|z| *(z.0) ^ *(z.1)) .map(|z| *(z.0) ^ *(z.1))
.collect() .collect()
} }
pub fn _encrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes { pub fn encrypt(key: &Bytes, nonce: u64, data: &Bytes) -> Bytes {
// let block_size = 16; decrypt(key, nonce, data)
let result: Vec<u8> = vec![];
// let mut prev_cipher: Vec<u8> = 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 decrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes { pub fn decrypt(Bytes(key): &Bytes, nonce: u64, Bytes(data): &Bytes) -> Bytes {
// XXX: does not handle padding for last block let mut counter: u64 = 0;
// let cipher_type = symm::Cipher::aes_128_ecb(); let cipher_type = symm::Cipher::aes_128_ecb();
// let block_size = cipher_type.block_size(); let block_size = cipher_type.block_size();
let result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
// let mut prev_cipher: Vec<u8> = iv.to_vec(); // first xor input is IV for cipher in data.chunks(block_size) {
// for cipher in data.chunks(block_size) { let mut keyinput = nonce.to_le_bytes().to_vec();
// let xored = cbc::decrypt_aes_128_ecb_block(key, &cipher); keyinput.append(&mut counter.to_le_bytes().to_vec());
// let data = xor(&xored, &prev_cipher); let keystream = cbc::enrypt_aes_128_ecb_block(key, &keyinput);
// result.extend(data.to_vec()); let mut data = xor(&keystream, &cipher.to_vec());
// prev_cipher = cipher.to_vec(); result.append(&mut data);
// } counter += 1;
}
Bytes(result) Bytes(result)
} }

View File

@@ -27,4 +27,5 @@ fn main() {
set2::challenge16(); set2::challenge16();
set3::challenge17(); set3::challenge17();
set3::challenge18(); set3::challenge18();
set3::challenge19();
} }

View File

@@ -97,10 +97,20 @@ pub fn challenge17() {
pub fn challenge18() { pub fn challenge18() {
let key = Bytes::from_utf8("YELLOW SUBMARINE"); 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( let cipher = BytesBase64::from_base64(
"L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==", "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==",
) )
.to_bytes(); .to_bytes();
let cleartext = ctr::decrypt(&key, &cipher).to_utf8(); let cleartext = ctr::decrypt(&key, 0, &cipher).to_utf8();
println!("[xxxx] Challenge 18: {cleartext}"); println!("[okay] Challenge 18: {cleartext}");
}
pub fn challenge19() {
println!("[xxxx] Challenge 19: TBD");
} }