summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-04-18 02:19:02 -0400
committerJesse Luehrs <doy@tozt.net>2015-04-18 02:19:02 -0400
commitb4210c80d347e3d3d7aafe17349e7bf54448994f (patch)
tree3170b1e688d3e7d3411ea9b35ee368a522fa9d7e /tests
parenta16eee14a18d4c345d6485593d3543e342c86af3 (diff)
downloadmatasano-b4210c80d347e3d3d7aafe17349e7bf54448994f.tar.gz
matasano-b4210c80d347e3d3d7aafe17349e7bf54448994f.zip
problem 27
Diffstat (limited to 'tests')
-rw-r--r--tests/set4.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/set4.rs b/tests/set4.rs
index 35c3edf..013ef38 100644
--- a/tests/set4.rs
+++ b/tests/set4.rs
@@ -86,3 +86,49 @@ fn problem_26 () {
let ciphertext = matasano::crack_ctr_bitflipping(&encode);
assert!(verify(&ciphertext[..]));
}
+
+#[test]
+fn problem_27 () {
+ let key = util::random_aes_128_key();
+ let iv = key;
+ let prefix = "comment1=cooking%20MCs;userdata=";
+ let suffix = ";comment2=%20like%20a%20pound%20of%20bacon";
+ let admin = ";admin=true;";
+
+ let escape = |input: &str| {
+ input.replace("%", "%25").replace(";", "%3B").replace("=", "%3D")
+ };
+
+ let encode = |input: &str| -> Vec<u8> {
+ let plaintext: Vec<u8> = prefix
+ .as_bytes()
+ .iter()
+ .chain(escape(input).as_bytes().iter())
+ .chain(suffix.as_bytes().iter())
+ .map(|x| *x)
+ .collect();
+ 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();
+ if plaintext.iter().any(|&c| c < 32 || c > 126) {
+ return Err(plaintext);
+ }
+ else {
+ println!("{}", ::std::str::from_utf8(&plaintext[..]).unwrap());
+ return Ok(
+ (0..(plaintext.len() - admin.len())).any(|i| {
+ plaintext
+ .iter()
+ .skip(i)
+ .zip(admin.as_bytes().iter())
+ .all(|(&c1, &c2)| c1 == c2)
+ })
+ );
+ }
+ };
+
+ let ciphertext = matasano::crack_cbc_iv_key(&encode, &verify);
+ assert!(verify(&ciphertext[..]).unwrap());
+}