diff --git a/src/bytes.rs b/src/bytes.rs index 36e8e87..5dc0b98 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -1,4 +1,5 @@ #![allow(dead_code)] +use rand::Rng; use std::fmt::Write; // need to import this trait #[derive(PartialEq, PartialOrd, Debug)] @@ -15,6 +16,19 @@ impl Bytes { String::from(std::str::from_utf8(&v).unwrap()) } + pub fn random(length: usize) -> Bytes { + Bytes( + (0..length) + .map(|_| rand::thread_rng().gen_range(0..255)) + .collect(), + ) + } + + pub fn random_range(lower: usize, upper: usize) -> Bytes { + let length: usize = rand::thread_rng().gen_range(lower..upper); + Bytes::random(length) + } + pub fn from_hex(s: &str) -> Bytes { if s.len() % 2 != 0 { panic!("Input string has uneven number of characters"); diff --git a/src/main.rs b/src/main.rs index 029e3b0..601a922 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,4 +17,5 @@ fn main() { set2::challenge9(); set2::challenge10(); set2::challenge11(); + set2::challenge12(); } diff --git a/src/set2.rs b/src/set2.rs index ee6cb35..e6e7326 100644 --- a/src/set2.rs +++ b/src/set2.rs @@ -37,17 +37,10 @@ pub fn challenge11() { CBC, ECB, } - fn random_bytes(count: usize) -> Vec { - (0..count) - .map(|_| rand::thread_rng().gen_range(0..255)) - .collect() - } fn pad_data(mut v: Vec) -> Bytes { - let pre_count: usize = rand::thread_rng().gen_range(5..10); - let post_count: usize = rand::thread_rng().gen_range(5..10); - let mut pre_pad = random_bytes(pre_count); - let mut post_pad = random_bytes(post_count); + let mut pre_pad = Bytes::random_range(5, 10).0; + let mut post_pad = Bytes::random_range(5, 10).0; pre_pad.append(&mut v); pre_pad.append(&mut post_pad); Bytes(pre_pad) @@ -56,7 +49,7 @@ pub fn challenge11() { fn encryption_oracle(Bytes(data): &Bytes) -> (Bytes, EncryptionType) { // Write a function that encrypts data under an unknown key --- that is, a // function that generates a random key and encrypts under it. - let key = Bytes(random_bytes(16)); + let key = Bytes::random(16); // Under the hood, have the function append 5-10 bytes (count chosen randomly) // before the plaintext and 5-10 bytes after the plaintext. let padded_data = pad_data(data.to_vec()); @@ -67,7 +60,7 @@ pub fn challenge11() { let (data, encryption_type) = if zero_or_one == 1 { (ecb::encrypt(&key, &padded_data), EncryptionType::ECB) } else { - let iv = Bytes(random_bytes(16)); + let iv = Bytes::random(16); (cbc::encrypt(&key, &iv, &padded_data), EncryptionType::CBC) }; (data, encryption_type) @@ -107,3 +100,17 @@ pub fn challenge11() { } run_oracle(10); } + +pub fn challenge12() { + let key = Bytes::random(16); + fn encrypt(Bytes(data): &Bytes, key: &Bytes) -> Bytes { + let mut data = data.to_vec(); + let mut b = Bytes::from_utf8("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK"); + data.append(&mut b.0); + let cipher = ecb::encrypt(&key, &Bytes(data)); + cipher + } + let text = Bytes::from_utf8("aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd"); + let cipher = encrypt(&text, &key); + println!("[xxxx] Challenge 12: {}", cipher.0.len()); +}