diff options
author | Jesse Luehrs <doy@tozt.net> | 2015-03-17 07:46:55 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2015-03-17 08:21:12 -0400 |
commit | 07b711c7736dcf0a7f25ec7a59ae4bffa2c3df9d (patch) | |
tree | 34a86585c2b7f4af8a143101dbd46082a26ae0a2 /src/aes.rs | |
parent | 8da56e330ebf3141249e2f48e7ffcb7e2d65dbc0 (diff) | |
download | matasano-07b711c7736dcf0a7f25ec7a59ae4bffa2c3df9d.tar.gz matasano-07b711c7736dcf0a7f25ec7a59ae4bffa2c3df9d.zip |
problem 11
Diffstat (limited to 'src/aes.rs')
-rw-r--r-- | src/aes.rs | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -1,8 +1,16 @@ -use openssl; +use std; use std::collections::HashSet; +use openssl; + use primitives::{fixed_xor, pad_pkcs7, unpad_pkcs7}; +#[derive(PartialEq,Eq,Debug)] +pub enum BlockCipherMode { + ECB, + CBC, +} + pub fn decrypt_aes_128_ecb (bytes: &[u8], key: &[u8]) -> Vec<u8> { return openssl::crypto::symm::decrypt( openssl::crypto::symm::Type::AES_128_ECB, @@ -65,6 +73,22 @@ pub fn find_aes_128_ecb_encrypted_string (inputs: &[Vec<u8>]) -> Vec<u8> { return found; } +pub fn detect_ecb_cbc<F> (f: F) -> BlockCipherMode where F: Fn(&[u8]) -> Vec<u8> { + let plaintext: Vec<u8> = (0..16) + .cycle() + .take(32) + .flat_map(|n| std::iter::repeat(n).take(17)) + .collect(); + let ciphertext = f(&plaintext[..]); + + if count_duplicate_blocks(&ciphertext[..]) >= 16 { + return BlockCipherMode::ECB; + } + else { + return BlockCipherMode::CBC; + } +} + fn count_duplicate_blocks (input: &[u8]) -> usize { let mut set = HashSet::new(); let mut dups = 0; |