diff --git a/src/crypto.rs b/src/crypto.rs index 69389ec..76b78c7 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,4 +1,5 @@ use crate::bytes::Bytes; +use rand::Rng; pub use openssl::symm::{Cipher, Crypter, Mode}; @@ -21,6 +22,14 @@ impl Aes { None } + pub fn new_random_128(pad: bool) -> Aes { + let key: Vec = rand::thread_rng() + .sample_iter(rand::distributions::Standard) + .take(16) + .collect(); + Self::new(&key, pad).unwrap() + } + pub fn cbc(&self, data: Vec, iv: &[u8], mode: Mode) -> Option> { if (!self.pad) && (data.len() % Self::BLOCK_SIZE != 0) { return None; @@ -106,7 +115,6 @@ impl Aes { } } - fn update(crypter: &mut Crypter, chunk: &[u8], output: &mut [u8]) { assert!( Self::BLOCK_SIZE diff --git a/src/main.rs b/src/main.rs index 7638b03..4c71aef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,9 +165,8 @@ fn q11() { let black_box = |mut data: Vec| -> (bool, Vec) { let mut rng = rand::thread_rng(); let dist = rand::distributions::Standard; - let key: Vec = rng.sample_iter(&dist).take(16).collect(); let iv: Vec = rng.sample_iter(&dist).take(16).collect(); - let aes = crypto::Aes::new(&key, true).unwrap(); + let aes = crypto::Aes::new_random_128(true); let mut start: Vec = rng.sample_iter(&dist).take(rng.gen_range(5, 11)).collect(); let mut end: Vec = rng.sample_iter(&dist).take(rng.gen_range(5, 11)).collect(); @@ -191,12 +190,8 @@ fn q11() { fn q12() { println!("Running q12"); - let key: Vec = rand::thread_rng() - .sample_iter(rand::distributions::Standard) - .take(16) - .collect(); let plaintext: Vec = Bytes::from_base64("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK").into(); - let aes = crate::crypto::Aes::new(&key, true).unwrap(); + let aes = crate::crypto::Aes::new_random_128(true); let blackbox = |mut data: Vec| -> Vec { data.append(&mut plaintext.clone()); @@ -252,11 +247,6 @@ fn q12() { fn q13() { println!("Running q13"); - let key: Vec = rand::thread_rng() - .sample_iter(rand::distributions::Standard) - .take(16) - .collect(); - let parse = |x: &str| -> std::collections::HashMap { let mut out = std::collections::HashMap::new(); for pair in x.split(|y| y == '&') { @@ -275,14 +265,13 @@ fn q13() { ) }; + let aes = crypto::Aes::new_random_128(true); let encrypt = |profile: &str| -> Vec { let profile: Vec = profile.bytes().collect(); - let aes = crypto::Aes::new(&key, true).unwrap(); aes.ecb(profile, Mode::Encrypt).unwrap() }; let is_admin = |profile: Vec| -> bool { - let aes = crypto::Aes::new(&key, true).unwrap(); let profile = aes.ecb(profile, Mode::Decrypt).unwrap(); let profile = String::from_utf8(profile).unwrap(); parse(&profile)["role"] == "admin" @@ -306,12 +295,8 @@ fn q13() { fn q14() { println!("Running q14"); - let key: Vec = rand::thread_rng() - .sample_iter(rand::distributions::Standard) - .take(16) - .collect(); let plaintext: Vec = Bytes::from_base64("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK").into(); - let aes = crate::crypto::Aes::new(&key, true).unwrap(); + let aes = crate::crypto::Aes::new_random_128(true); let prefix: Vec = rand::thread_rng() .sample_iter(rand::distributions::Standard) .take(rand::thread_rng().gen_range(5, 11))