summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-03-25 23:30:59 -0400
committerJesse Luehrs <doy@tozt.net>2015-03-25 23:30:59 -0400
commitfcdc3f0127fe841ecdf808d1941018f4fe7a2b85 (patch)
tree96498ab38434f4ca47a2d2e98374969ed15cdf7a /tests
parentedf552548638c4532f99a18a6afd0738fbd789c0 (diff)
downloadmatasano-fcdc3f0127fe841ecdf808d1941018f4fe7a2b85.tar.gz
matasano-fcdc3f0127fe841ecdf808d1941018f4fe7a2b85.zip
problem 16
Diffstat (limited to 'tests')
-rw-r--r--tests/lib.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/lib.rs b/tests/lib.rs
index 72c808a..cc37ec4 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -302,3 +302,41 @@ fn problem_15 () {
Some(&b""[..])
);
}
+
+#[test]
+fn problem_16 () {
+ let key = random_aes_128_key();
+ let iv = random_aes_128_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]| -> bool {
+ let plaintext = matasano::decrypt_aes_128_cbc(ciphertext, &key[..], &iv[..]);
+ return (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_bitflipping(&encode);
+ assert!(verify(&ciphertext[..]));
+}