diff options
author | Jesse Luehrs <doy@tozt.net> | 2015-03-15 19:59:46 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2015-03-15 19:59:54 -0400 |
commit | 171dbac2f26b1561b62058c1b952eaa7d7604a90 (patch) | |
tree | c905869cc71c2e3149640d5aa245f0ec6ffd9475 /src/aes.rs | |
parent | 118cd5f72956e91c256ef066db0054a7bcd14f50 (diff) | |
download | matasano-171dbac2f26b1561b62058c1b952eaa7d7604a90.tar.gz matasano-171dbac2f26b1561b62058c1b952eaa7d7604a90.zip |
problem 10
Diffstat (limited to 'src/aes.rs')
-rw-r--r-- | src/aes.rs | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1,6 +1,8 @@ use openssl; use std::collections::HashSet; +use primitives::fixed_xor; + pub fn decrypt_aes_128_ecb (bytes: &[u8], key: &[u8]) -> Vec<u8> { return openssl::crypto::symm::decrypt( openssl::crypto::symm::Type::AES_128_ECB, @@ -10,6 +12,31 @@ pub fn decrypt_aes_128_ecb (bytes: &[u8], key: &[u8]) -> Vec<u8> { ) } +pub fn decrypt_aes_128_cbc (bytes: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> { + let mut prev = iv.clone(); + let mut plaintext = vec![]; + for block in bytes.chunks(16) { + // XXX not sure what's going on here - decrypt_aes_128_ecb doesn't + // decrypt the last block? + let double_block: Vec<u8> = block + .iter() + .chain(block.iter()).map(|x| *x) + .collect(); + let plaintext_block = fixed_xor( + &decrypt_aes_128_ecb(&double_block[..], key)[..], + prev + ); + for &c in &plaintext_block[..16] { + plaintext.push(c); + } + prev = block.clone(); + } + let padding = plaintext[plaintext.len() - 1]; + let new_len = plaintext.len() - padding as usize; + plaintext.truncate(new_len); + return plaintext; +} + pub fn find_aes_128_ecb_encrypted_string (inputs: &[Vec<u8>]) -> Vec<u8> { let mut max_dups = 0; let mut found = vec![]; |