diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-04-09 00:06:18 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-04-09 02:00:33 -0400 |
commit | f1f522df90e4af23f442067e269463710193148d (patch) | |
tree | b397d986d68623e79f12499da64f76f26cd30f99 /tests/set4.rs | |
parent | 6807601cb64e7b18b832cab2939cbb107e3727bb (diff) | |
download | matasano-f1f522df90e4af23f442067e269463710193148d.tar.gz matasano-f1f522df90e4af23f442067e269463710193148d.zip |
get this compiling again
Diffstat (limited to 'tests/set4.rs')
-rw-r--r-- | tests/set4.rs | 112 |
1 files changed, 61 insertions, 51 deletions
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<u8> { @@ -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<u8> { @@ -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<bool, Vec<u8>> { - 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<u8> = ::rand::thread_rng() - .gen_iter() +fn problem_29() { + let key: Vec<u8> = 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<u8> = ::rand::thread_rng() - .gen_iter() +fn problem_30() { + let key: Vec<u8> = 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[..] + )); } |