From f1f522df90e4af23f442067e269463710193148d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 9 Apr 2019 00:06:18 -0400 Subject: get this compiling again --- tests/set1.rs | 33 ++++++++++------ tests/set2.rs | 124 +++++++++++++++++++++++++++++----------------------------- tests/set3.rs | 98 +++++++++++++++++++++++----------------------- tests/set4.rs | 112 ++++++++++++++++++++++++++++------------------------ tests/util.rs | 30 +++++++------- 5 files changed, 209 insertions(+), 188 deletions(-) (limited to 'tests') diff --git a/tests/set1.rs b/tests/set1.rs index c9eece7..e1dacd1 100644 --- a/tests/set1.rs +++ b/tests/set1.rs @@ -6,10 +6,12 @@ use serialize::hex::FromHex; mod util; #[test] -fn problem_1 () { +fn problem_1() { let hex = "49276d206b696c6c696e6720796f757220627261\ 696e206c696b65206120706f69736f6e6f757320\ - 6d757368726f6f6d".from_hex().unwrap(); + 6d757368726f6f6d" + .from_hex() + .unwrap(); let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEg\ cG9pc29ub3VzIG11c2hyb29t"; let got = matasano::to_base64(&hex[..]); @@ -17,7 +19,7 @@ fn problem_1 () { } #[test] -fn problem_2 () { +fn problem_2() { let bytes1 = "1c0111001f010100061a024b53535009181c".from_hex().unwrap(); let bytes2 = "686974207468652062756c6c277320657965".from_hex().unwrap(); let expected = "746865206b696420646f6e277420706c6179".from_hex().unwrap(); @@ -26,16 +28,18 @@ fn problem_2 () { } #[test] -fn problem_3 () { +fn problem_3() { let ciphertext = "1b37373331363f78151b7f2b783431333d783978\ - 28372d363c78373e783a393b3736".from_hex().unwrap(); + 28372d363c78373e783a393b3736" + .from_hex() + .unwrap(); let plaintext = b"Cooking MC's like a pound of bacon"; let got = matasano::crack_single_byte_xor(&ciphertext[..]); assert_eq!(got, &plaintext[..]); } #[test] -fn problem_4 () { +fn problem_4() { let possibles = util::read_as_hex_lines("data/4.txt"); let plaintext = b"Now that the party is jumping\n"; let got = matasano::find_single_byte_xor_encrypted_string(&possibles[..]); @@ -43,20 +47,22 @@ fn problem_4 () { } #[test] -fn problem_5 () { +fn problem_5() { let plaintext = b"Burning 'em, if you ain't quick and nimble\n\ I go crazy when I hear a cymbal"; let key = b"ICE"; let ciphertext = "0b3637272a2b2e63622c2e69692a23693a2a3c63\ 24202d623d63343c2a26226324272765272a282b\ 2f20430a652e2c652a3124333a653e2b2027630c\ - 692b20283165286326302e27282f".from_hex().unwrap(); + 692b20283165286326302e27282f" + .from_hex() + .unwrap(); let got = matasano::repeating_key_xor(plaintext, key); assert_eq!(got, ciphertext); } #[test] -fn problem_6 () { +fn problem_6() { let ciphertext = util::read_as_base64("data/6.txt"); let plaintext = util::read("data/6.out.txt"); let key = matasano::crack_repeating_key_xor(&ciphertext[..]); @@ -65,16 +71,16 @@ fn problem_6 () { } #[test] -fn problem_7 () { +fn problem_7() { let ciphertext = util::read_as_base64("data/7.txt"); let key = b"YELLOW SUBMARINE"; let plaintext = util::read("data/7.out.txt"); let got = matasano::decrypt_aes_128_ecb(&ciphertext[..], key); - assert_eq!(got, Some(plaintext)); + assert_eq!(got, plaintext); } #[test] -fn problem_8 () { +fn problem_8() { let possibles = util::read_as_hex_lines("data/8.txt"); let ciphertext = "d880619740a8a19b7840a8a31c810a3d08649af7\ 0dc06f4fd5d2d69c744cd283e2dd052f6b641dbf\ @@ -84,7 +90,8 @@ fn problem_8 () { 8d6aecd566489154789a6b0308649af70dc06f4f\ d5d2d69c744cd283d403180c98c8f6db1f2a3f9c\ 4040deb0ab51b29933f2c123c58386b06fba186a" - .from_hex().unwrap(); + .from_hex() + .unwrap(); let got = matasano::find_aes_128_ecb_encrypted_string(&possibles[..]); assert_eq!(got, ciphertext); } diff --git a/tests/set2.rs b/tests/set2.rs index 84f55b5..d5cc5d5 100644 --- a/tests/set2.rs +++ b/tests/set2.rs @@ -1,6 +1,6 @@ extern crate matasano; -extern crate rustc_serialize as serialize; extern crate rand; +extern crate rustc_serialize as serialize; use std::borrow::ToOwned; use std::collections::HashMap; @@ -11,14 +11,14 @@ use serialize::base64::FromBase64; mod util; #[test] -fn problem_9 () { +fn problem_9() { let block = b"YELLOW SUBMARINE"; let got = matasano::pad_pkcs7(block, 20); assert_eq!(got, b"YELLOW SUBMARINE\x04\x04\x04\x04"); } #[test] -fn problem_10 () { +fn problem_10() { let ciphertext = util::read_as_base64("data/10.txt"); let key = b"YELLOW SUBMARINE"; let plaintext = util::read("data/10.out.txt"); @@ -27,16 +27,17 @@ fn problem_10 () { } #[test] -fn problem_11 () { - static mut last_mode: matasano::BlockCipherMode = matasano::BlockCipherMode::ECB; +fn problem_11() { + static mut LAST_MODE: matasano::BlockCipherMode = + matasano::BlockCipherMode::ECB; - fn random_padding (input: &[u8]) -> Vec { + fn random_padding(input: &[u8]) -> Vec { let front_padding: Vec = rand::thread_rng() - .gen_iter() + .sample_iter(&rand::distributions::Standard) .take(rand::thread_rng().gen_range(5, 10)) .collect(); let back_padding: Vec = rand::thread_rng() - .gen_iter() + .sample_iter(&rand::distributions::Standard) .take(rand::thread_rng().gen_range(5, 10)) .collect(); return front_padding @@ -44,47 +45,48 @@ fn problem_11 () { .chain(input.iter()) .chain(back_padding.iter()) .map(|x| *x) - .collect() + .collect(); } - fn random_encrypter (input: &[u8]) -> Vec { + fn random_encrypter(input: &[u8]) -> Vec { let key = util::random_aes_128_key(); let padded_input = random_padding(input); if util::coinflip() { unsafe { - last_mode = matasano::BlockCipherMode::ECB; + LAST_MODE = matasano::BlockCipherMode::ECB; } return matasano::encrypt_aes_128_ecb(&padded_input[..], &key[..]); - } - else { + } else { unsafe { - last_mode = matasano::BlockCipherMode::CBC; + LAST_MODE = matasano::BlockCipherMode::CBC; } let iv = util::random_aes_128_key(); - return matasano::encrypt_aes_128_cbc(&padded_input[..], &key[..], &iv[..]); + return matasano::encrypt_aes_128_cbc( + &padded_input[..], + &key[..], + &iv[..], + ); } } for _ in 0..100 { let got = matasano::detect_ecb_cbc(&random_encrypter, 16); - let expected = unsafe { &last_mode }; + let expected = unsafe { &LAST_MODE }; assert_eq!(&got, expected); } } #[test] -fn problem_12 () { +fn problem_12() { let padding = b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ - LCBJIGp1c3QgZHJvdmUgYnkK".from_base64().unwrap(); + LCBJIGp1c3QgZHJvdmUgYnkK" + .from_base64() + .unwrap(); let fixed_padding = |input: &[u8]| -> Vec { - return input - .iter() - .chain(padding.iter()) - .map(|x| *x) - .collect() + return input.iter().chain(padding.iter()).map(|x| *x).collect(); }; let key = util::random_aes_128_key(); @@ -98,8 +100,8 @@ fn problem_12 () { } #[test] -fn problem_13 () { - fn profile_for (email: &str) -> String { +fn problem_13() { + fn profile_for(email: &str) -> String { let mut params = HashMap::new(); params.insert("email", email); params.insert("uid", "10"); @@ -112,40 +114,42 @@ fn problem_13 () { matasano::encrypt_aes_128_ecb(profile_for(email).as_bytes(), &key[..]) }; let decrypter = |ciphertext: &[u8]| -> Option> { - let plaintext = matasano::decrypt_aes_128_ecb(ciphertext, &key[..]).unwrap(); + let plaintext = matasano::decrypt_aes_128_ecb(ciphertext, &key[..]); let plaintext_str = std::str::from_utf8(&plaintext[..]).unwrap(); if let Some(params) = matasano::parse_query_string(plaintext_str) { return Some( params - .into_iter() - .map(|(k, v)| (k.to_owned(), v.to_owned())) - .collect() + .into_iter() + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect(), ); - } - else { + } else { return None; } }; - let (email, ciphertexts) = matasano::crack_querystring_aes_128_ecb(&encrypter); + let (email, ciphertexts) = + matasano::crack_querystring_aes_128_ecb(&encrypter); let mut expected = HashMap::new(); expected.insert("email".to_owned(), email); expected.insert("uid".to_owned(), "10".to_owned()); expected.insert("role".to_owned(), "admin".to_owned()); - assert!(ciphertexts.iter().any(|ciphertext| { - decrypter(ciphertext).map(|params| params == expected).unwrap_or(false) - })); + assert!(ciphertexts.iter().any(|ciphertext| decrypter(ciphertext) + .map(|params| params == expected) + .unwrap_or(false))); } #[test] -fn problem_14 () { +fn problem_14() { let padding = b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ - LCBJIGp1c3QgZHJvdmUgYnkK".from_base64().unwrap(); + LCBJIGp1c3QgZHJvdmUgYnkK" + .from_base64() + .unwrap(); let front_padding: Vec = rand::thread_rng() - .gen_iter() + .sample_iter(&rand::distributions::Standard) .take(rand::thread_rng().gen_range(1, 100)) .collect(); let fixed_padding = |input: &[u8]| -> Vec { @@ -154,7 +158,7 @@ fn problem_14 () { .chain(input.iter()) .chain(padding.iter()) .map(|x| *x) - .collect() + .collect(); }; let key = util::random_aes_128_key(); @@ -163,36 +167,25 @@ fn problem_14 () { return matasano::encrypt_aes_128_ecb(&padded_input[..], &key[..]); }; - let got = matasano::crack_padded_aes_128_ecb_with_prefix(&random_encrypter); + let got = + matasano::crack_padded_aes_128_ecb_with_prefix(&random_encrypter); assert_eq!(got, padding); } #[test] -fn problem_15 () { +fn problem_15() { assert_eq!( matasano::unpad_pkcs7(b"ICE ICE BABY\x04\x04\x04\x04"), Some(&b"ICE ICE BABY"[..]) ); - assert_eq!( - matasano::unpad_pkcs7(b"ICE ICE BABY\x05\x05\x05\x05"), - None - ); - assert_eq!( - matasano::unpad_pkcs7(b"ICE ICE BABY\x01\x02\x03\x04"), - None - ); - assert_eq!( - matasano::unpad_pkcs7(b"ICE ICE BABY\x00"), - None - ); - assert_eq!( - matasano::unpad_pkcs7(b"\x04\x04\x04\x04"), - Some(&b""[..]) - ); + assert_eq!(matasano::unpad_pkcs7(b"ICE ICE BABY\x05\x05\x05\x05"), None); + assert_eq!(matasano::unpad_pkcs7(b"ICE ICE BABY\x01\x02\x03\x04"), None); + assert_eq!(matasano::unpad_pkcs7(b"ICE ICE BABY\x00"), None); + assert_eq!(matasano::unpad_pkcs7(b"\x04\x04\x04\x04"), Some(&b""[..])); } #[test] -fn problem_16 () { +fn problem_16() { let key = util::random_aes_128_key(); let iv = util::random_aes_128_key(); let prefix = "comment1=cooking%20MCs;userdata="; @@ -200,7 +193,10 @@ fn problem_16 () { let admin = ";admin=true;"; let escape = |input: &str| { - input.replace("%", "%25").replace(";", "%3B").replace("=", "%3D") + input + .replace("%", "%25") + .replace(";", "%3B") + .replace("=", "%3D") }; let encode = |input: &str| -> Vec { @@ -211,11 +207,17 @@ fn problem_16 () { .chain(suffix.as_bytes().iter()) .map(|x| *x) .collect(); - return matasano::encrypt_aes_128_cbc(&plaintext[..], &key[..], &iv[..]); + return matasano::encrypt_aes_128_cbc( + &plaintext[..], + &key[..], + &iv[..], + ); }; let verify = |ciphertext: &[u8]| -> bool { - let plaintext = matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]).unwrap(); + let plaintext = + matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]) + .unwrap(); return (0..(plaintext.len() - admin.len())).any(|i| { plaintext .iter() diff --git a/tests/set3.rs b/tests/set3.rs index 77b3371..4cbd9e2 100644 --- a/tests/set3.rs +++ b/tests/set3.rs @@ -1,16 +1,14 @@ extern crate matasano; -extern crate rustc_serialize as serialize; extern crate rand; +extern crate rustc_serialize as serialize; -use std::ascii::AsciiExt; - -use rand::{Rng, SeedableRng}; +use rand::{FromEntropy, Rng}; use serialize::base64::FromBase64; mod util; #[test] -fn problem_17 () { +fn problem_17() { let strings = [ &b"MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc="[..], &b"MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic="[..], @@ -25,24 +23,21 @@ fn problem_17 () { ]; let key = util::random_aes_128_key(); - static mut chosen_plaintext_idx: usize = 0; + static mut CHOSEN_PLAINTEXT_IDX: usize = 0; let encrypter = || { let idx = rand::thread_rng().gen_range(0, strings.len()); let plaintext = strings[idx].from_base64().unwrap(); - unsafe { chosen_plaintext_idx = idx }; + unsafe { CHOSEN_PLAINTEXT_IDX = idx }; let iv = util::random_aes_128_key(); return ( iv, - matasano::encrypt_aes_128_cbc(&plaintext[..], &key[..], &iv[..]) + matasano::encrypt_aes_128_cbc(&plaintext[..], &key[..], &iv[..]), ); }; let validator = |iv: &[u8], ciphertext: &[u8]| { - let plaintext = matasano::decrypt_aes_128_cbc( - ciphertext, - &key[..], - &iv[..] - ); + let plaintext = + matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]); return plaintext.is_some(); }; @@ -51,24 +46,23 @@ fn problem_17 () { let plaintext = matasano::crack_cbc_padding_oracle( &iv[..], &ciphertext[..], - &validator + &validator, ); - let idx = unsafe { chosen_plaintext_idx.clone() }; + let idx = unsafe { CHOSEN_PLAINTEXT_IDX.clone() }; let expected = strings[idx].from_base64().unwrap(); assert_eq!(plaintext, expected); } } #[test] -fn problem_18 () { +fn problem_18() { let ciphertext = b"L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syL\ - XzhPweyyMTJULu/6/kXX0KSvoOLSFQ==".from_base64().unwrap(); - let plaintext = &b"Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby "[..]; - let got = matasano::aes_128_ctr( - &ciphertext[..], - b"YELLOW SUBMARINE", - 0 - ); + XzhPweyyMTJULu/6/kXX0KSvoOLSFQ==" + .from_base64() + .unwrap(); + let plaintext = + &b"Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby "[..]; + let got = matasano::aes_128_ctr(&ciphertext[..], b"YELLOW SUBMARINE", 0); assert_eq!(got, plaintext); } @@ -83,8 +77,8 @@ fn problem_18 () { // } #[test] -fn problem_20 () { - fn normalize (line_list: Vec>, len: usize) -> Vec> { +fn problem_20() { + fn normalize(line_list: Vec>, len: usize) -> Vec> { line_list .iter() .map(|line| line.to_ascii_lowercase()) @@ -99,20 +93,19 @@ fn problem_20 () { .collect(); let expected = util::read_as_lines("data/20.out.txt"); - let plaintexts = matasano::crack_fixed_nonce_ctr_statistically( - ciphertexts - ); + let plaintexts = + matasano::crack_fixed_nonce_ctr_statistically(ciphertexts); - assert_eq!( - normalize(plaintexts, 27), - normalize(expected, 27) - ); + assert_eq!(normalize(plaintexts, 27), normalize(expected, 27)); } #[test] -fn problem_21 () { - let mut mt = matasano::MersenneTwister::from_seed(0x12345678); - let got: Vec = mt.gen_iter().take(10).collect(); +fn problem_21() { + let mut mt = matasano::MersenneTwister::from_u32(0x12345678); + let got: Vec = mt + .sample_iter(&rand::distributions::Standard) + .take(10) + .collect(); let expected: Vec = vec![ 0xC6979343, 0x0962D2FA, 0xA73A24A4, 0xE118A180, 0xB5475ABB, 0x64613C7C, 0x6F32F4DB, 0xF27BF199, 0x464DD8DC, 0x95C1FED6, @@ -121,20 +114,24 @@ fn problem_21 () { } #[test] -fn problem_22 () { +fn problem_22() { // std::thread::sleep_ms(rand::thread_rng().gen_range(40, 1000) * 1000); let seed = util::now(); - let mut mt = matasano::MersenneTwister::from_seed(seed); + let mut mt = matasano::MersenneTwister::from_u32(seed); // std::thread::sleep_ms(rand::thread_rng().gen_range(40, 1000) * 1000); let output: u32 = mt.gen(); - let got = matasano::recover_mersenne_twister_seed_from_time(output).unwrap(); + let got = + matasano::recover_mersenne_twister_seed_from_time(output).unwrap(); assert_eq!(got, seed); } #[test] -fn problem_23 () { - let mut mt: matasano::MersenneTwister = rand::thread_rng().gen(); - let outputs: Vec = mt.gen_iter().take(624).collect(); +fn problem_23() { + let mut mt = matasano::MersenneTwister::from_entropy(); + let outputs: Vec = mt + .sample_iter(&rand::distributions::Standard) + .take(624) + .collect(); let mut mt2 = matasano::clone_mersenne_twister_from_output(&outputs[..]); for _ in 1..1000 { assert_eq!(mt.gen::(), mt2.gen::()); @@ -142,27 +139,32 @@ fn problem_23 () { } #[test] -fn problem_24 () { +fn problem_24() { let key: u16 = rand::thread_rng().gen(); let fixed_suffix = b"AAAAAAAAAAAAAA"; let plaintext: Vec = rand::thread_rng() - .gen_iter() + .sample_iter(&rand::distributions::Standard) .take(rand::thread_rng().gen_range(0, 32)) .chain(fixed_suffix.iter().map(|x| *x)) .collect(); - let ciphertext = matasano::mt19937_stream_cipher(&plaintext[..], key as u32); + let ciphertext = + matasano::mt19937_stream_cipher(&plaintext[..], key as u32); let got = matasano::recover_16_bit_mt19937_key( &ciphertext[..], &fixed_suffix[..], - ).unwrap(); + ) + .unwrap(); assert_eq!(got, key); } #[test] -fn problem_24_part_2 () { +fn problem_24_part_2() { let seed = util::now(); - let mut mt = matasano::MersenneTwister::from_seed(seed); - let token: Vec = mt.gen_iter().take(16).collect(); + let mut mt = matasano::MersenneTwister::from_u32(seed); + let token: Vec = mt + .sample_iter(&rand::distributions::Standard) + .take(16) + .collect(); let got = matasano::recover_mt19937_key_from_time(&token[..]).unwrap(); assert_eq!(got, seed); } diff --git a/tests/set4.rs b/tests/set4.rs index d6fbeb5..5f5916d 100644 --- a/tests/set4.rs +++ b/tests/set4.rs @@ -6,7 +6,7 @@ use rand::Rng; mod util; #[test] -fn problem_25 () { +fn problem_25() { let key = util::random_aes_128_key(); let nonce: u64 = rand::thread_rng().gen(); let plaintext = util::read("data/25.txt"); @@ -16,15 +16,13 @@ fn problem_25 () { let block_start_number = offset / 16; let block_start = block_start_number * 16; let block_end_number = (offset + newtext.len() - 1) / 16; - let block_end = std::cmp::min( - (block_end_number + 1) * 16, - ciphertext.len() - ); + let block_end = + std::cmp::min((block_end_number + 1) * 16, ciphertext.len()); let mut plaintext = matasano::aes_128_ctr_with_counter( &ciphertext[block_start..block_end], &key[..], nonce, - (offset / 16) as u64 + (offset / 16) as u64, ); for i in 0..newtext.len() { plaintext[offset - block_start + i] = newtext[i]; @@ -33,7 +31,7 @@ fn problem_25 () { &plaintext[..], &key[..], nonce, - (offset / 16) as u64 + (offset / 16) as u64, ); return ciphertext @@ -45,12 +43,13 @@ fn problem_25 () { .collect(); }; - let got = matasano::crack_aes_128_ctr_random_access(&ciphertext[..], edit); + let got = + matasano::crack_aes_128_ctr_random_access(&ciphertext[..], edit); assert_eq!(&got[..], &plaintext[..]); } #[test] -fn problem_26 () { +fn problem_26() { let key = util::random_aes_128_key(); let nonce = rand::thread_rng().gen(); let prefix = "comment1=cooking%20MCs;userdata="; @@ -58,7 +57,10 @@ fn problem_26 () { let admin = ";admin=true;"; let escape = |input: &str| { - input.replace("%", "%25").replace(";", "%3B").replace("=", "%3D") + input + .replace("%", "%25") + .replace(";", "%3B") + .replace("=", "%3D") }; let encode = |input: &str| -> Vec { @@ -88,7 +90,7 @@ fn problem_26 () { } #[test] -fn problem_27 () { +fn problem_27() { let key = util::random_aes_128_key(); let iv = key; let prefix = "comment1=cooking%20MCs;userdata="; @@ -96,7 +98,10 @@ fn problem_27 () { let admin = ";admin=true;"; let escape = |input: &str| { - input.replace("%", "%25").replace(";", "%3B").replace("=", "%3D") + input + .replace("%", "%25") + .replace(";", "%3B") + .replace("=", "%3D") }; let encode = |input: &str| -> Vec { @@ -107,24 +112,27 @@ fn problem_27 () { .chain(suffix.as_bytes().iter()) .map(|x| *x) .collect(); - return matasano::encrypt_aes_128_cbc(&plaintext[..], &key[..], &iv[..]); + return matasano::encrypt_aes_128_cbc( + &plaintext[..], + &key[..], + &iv[..], + ); }; let verify = |ciphertext: &[u8]| -> Result> { - let plaintext = matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]).unwrap(); + let plaintext = + matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]) + .unwrap(); if plaintext.iter().any(|&c| c < 32 || c > 126) { return Err(plaintext); - } - else { - return Ok( - (0..(plaintext.len() - admin.len())).any(|i| { - plaintext - .iter() - .skip(i) - .zip(admin.as_bytes().iter()) - .all(|(&c1, &c2)| c1 == c2) - }) - ); + } else { + return Ok((0..(plaintext.len() - admin.len())).any(|i| { + plaintext + .iter() + .skip(i) + .zip(admin.as_bytes().iter()) + .all(|(&c1, &c2)| c1 == c2) + })); } }; @@ -135,45 +143,47 @@ fn problem_27 () { // problem 28 is just matasano::sha1_mac #[test] -fn problem_29 () { - let key: Vec = ::rand::thread_rng() - .gen_iter() +fn problem_29() { + let key: Vec = rand::thread_rng() + .sample_iter(&rand::distributions::Standard) .take(::rand::thread_rng().gen_range(5, 25)) .collect(); let valid_input = b"comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"; let valid_mac = matasano::sha1_mac(valid_input, &key[..]); - let possibles = matasano::crack_sha1_mac_length_extension(valid_input, valid_mac, b";admin=true"); - assert!( - possibles.iter().all(|&(ref input, _)| { - input.ends_with(b";admin=true") - }) - ); - assert!( - possibles.iter().any(|&(ref input, ref mac)| { - &matasano::sha1_mac(&input[..], &key[..])[..] == &mac[..] - }) + let possibles = matasano::crack_sha1_mac_length_extension( + valid_input, + valid_mac, + b";admin=true", ); + assert!(possibles + .iter() + .all(|&(ref input, _)| input.ends_with(b";admin=true"))); + assert!(possibles.iter().any( + |&(ref input, ref mac)| &matasano::sha1_mac(&input[..], &key[..])[..] + == &mac[..] + )); } #[test] -fn problem_30 () { - let key: Vec = ::rand::thread_rng() - .gen_iter() +fn problem_30() { + let key: Vec = rand::thread_rng() + .sample_iter(&rand::distributions::Standard) .take(::rand::thread_rng().gen_range(5, 25)) .collect(); let valid_input = b"comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"; let valid_mac = matasano::md4_mac(valid_input, &key[..]); - let possibles = matasano::crack_md4_mac_length_extension(valid_input, valid_mac, b";admin=true"); - assert!( - possibles.iter().all(|&(ref input, _)| { - input.ends_with(b";admin=true") - }) - ); - assert!( - possibles.iter().any(|&(ref input, ref mac)| { - &matasano::md4_mac(&input[..], &key[..])[..] == &mac[..] - }) + let possibles = matasano::crack_md4_mac_length_extension( + valid_input, + valid_mac, + b";admin=true", ); + assert!(possibles + .iter() + .all(|&(ref input, _)| input.ends_with(b";admin=true"))); + assert!(possibles.iter().any( + |&(ref input, ref mac)| &matasano::md4_mac(&input[..], &key[..])[..] + == &mac[..] + )); } diff --git a/tests/util.rs b/tests/util.rs index 6921eea..390559e 100644 --- a/tests/util.rs +++ b/tests/util.rs @@ -4,66 +4,66 @@ extern crate rand; extern crate rustc_serialize as serialize; extern crate time; -use std::io::prelude::*; use std::fs::File; +use std::io::prelude::*; -use self::rand::Rng; +use self::rand::{Rng, RngCore}; use self::serialize::base64::FromBase64; use self::serialize::hex::FromHex; -pub fn read_as_hex_lines (filename: &str) -> Vec> { +pub fn read_as_hex_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); - return ::std::io::BufReader::new(fh) + return std::io::BufReader::new(fh) .lines() .map(|line| line.unwrap().from_hex().unwrap()) .collect(); } -pub fn read_as_base64_lines (filename: &str) -> Vec> { +pub fn read_as_base64_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); - return ::std::io::BufReader::new(fh) + return std::io::BufReader::new(fh) .lines() .map(|line| line.unwrap().from_base64().unwrap()) .collect(); } -pub fn read_as_lines (filename: &str) -> Vec> { +pub fn read_as_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); - return ::std::io::BufReader::new(fh) + return std::io::BufReader::new(fh) .lines() .map(|line| line.unwrap().as_bytes().to_vec()) .collect(); } -pub fn read_as_base64 (filename: &str) -> Vec { +pub fn read_as_base64(filename: &str) -> Vec { let fh = File::open(filename).unwrap(); - return ::std::io::BufReader::new(fh) + return std::io::BufReader::new(fh) .lines() .map(|line| line.unwrap().from_base64().unwrap()) .collect::>>() .concat(); } -pub fn read (filename: &str) -> Vec { +pub fn read(filename: &str) -> Vec { let outfh = File::open(filename).unwrap(); return outfh.bytes().map(|c| c.unwrap()).collect(); } -pub fn write (filename: &str, data: &[u8]) { +pub fn write(filename: &str, data: &[u8]) { let mut outfh = File::create(filename).unwrap(); outfh.write(data).unwrap(); } -pub fn random_aes_128_key () -> [u8; 16] { +pub fn random_aes_128_key() -> [u8; 16] { let mut key = [0; 16]; self::rand::thread_rng().fill_bytes(&mut key); return key; } -pub fn coinflip () -> bool { +pub fn coinflip() -> bool { self::rand::thread_rng().gen() } -pub fn now () -> u32 { +pub fn now() -> u32 { return self::time::now().to_timespec().sec as u32; } -- cgit v1.2.3-54-g00ecf