diff options
author | Jesse Luehrs <doy@tozt.net> | 2015-03-28 04:28:46 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2015-03-28 04:28:46 -0400 |
commit | 75b5c3f2f36d527216920d6d437bc1eef3237cd0 (patch) | |
tree | 5cc168e5c35b3344e8d28f339a53f718b1d8f9d0 /src | |
parent | 301a4e5cdf5d206edc63f2bb632d1e0f0d4cc7f6 (diff) | |
download | matasano-75b5c3f2f36d527216920d6d437bc1eef3237cd0.tar.gz matasano-75b5c3f2f36d527216920d6d437bc1eef3237cd0.zip |
problem 18
Diffstat (limited to 'src')
-rw-r--r-- | src/aes.rs | 30 | ||||
-rw-r--r-- | src/lib.rs | 1 |
2 files changed, 31 insertions, 0 deletions
@@ -1,4 +1,5 @@ use openssl; +use std; use primitives::{fixed_xor, pad_pkcs7, unpad_pkcs7}; @@ -53,6 +54,35 @@ pub fn encrypt_aes_128_cbc (bytes: &[u8], key: &[u8], iv: &[u8]) -> Vec<u8> { return ciphertext; } +pub fn aes_128_ctr (bytes: &[u8], key: &[u8], nonce: u64) -> Vec<u8> { + let nonce_array: [u8; 8] = unsafe { + std::mem::transmute(nonce.to_le()) + }; + let mut counter = 0u64; + let mut ret = vec![]; + for block in bytes.chunks(16) { + let counter_array: [u8; 8] = unsafe { + std::mem::transmute(counter.to_le()) + }; + let keystream = encrypt_aes_128_ecb( + &pad_pkcs7( + &nonce_array + .iter() + .chain(counter_array.iter()) + .map(|x| *x) + .collect::<Vec<u8>>()[..], + 16 + )[..], + key + ); + for c in fixed_xor(block, &keystream[..]) { + ret.push(c); + } + counter += 1; + } + return ret; +} + #[test] fn test_encrypt_decrypt () { let plaintext = b"Summertime and the wind is blowing outside in lower \ @@ -12,6 +12,7 @@ pub use aes::decrypt_aes_128_ecb; pub use aes::decrypt_aes_128_cbc; pub use aes::encrypt_aes_128_ecb; pub use aes::encrypt_aes_128_cbc; +pub use aes::aes_128_ctr; pub use base64::to_base64; pub use http::parse_query_string; pub use http::create_query_string; |