diff options
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; |