cryptopals

Matasano's cryptopals challenges (cryptopals.com).
Log | Files | Refs | README | LICENSE

commit f6818cd7287714e0dc1cb85a43fd0226bb30d3cd
parent 0bac88e3bb33c388147e3446c4b6a186e3fef9dc
Author: Jared Tobin <jared@jtobin.ca>
Date:   Mon, 29 May 2017 19:14:56 +1200

2.10.

Diffstat:
Mdocs/s2.md | 7+++++++
Mlib/aes_cbc/src/main.rs | 11++++++++---
2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/docs/s2.md b/docs/s2.md @@ -17,3 +17,10 @@ The problem description says NO OPENSSL, but heck them rules AMIRITE? +Ok here's an answer that I had to write code to get: + + $ cat data/s2/q10_input.txt | tr -d '\n' | \ + ./bin/aes_cbc --key "YELLOW SUBMARINE" | head -2 + I'm back and I'm ringin' the bell + A rockin' on the mike while the fly girls yell + diff --git a/lib/aes_cbc/src/main.rs b/lib/aes_cbc/src/main.rs @@ -25,10 +25,11 @@ fn new_crypter_unpadded( key: &[u8], iv: Option<&[u8]> ) -> Crypter { + assert!(key.len() == 16); let mut crypter = match Crypter::new(cipher, mode, key, iv) { Ok(val) => val, - Err(err) => panic!("{}", err) + Err(err) => panic!("{} - key len {}", err, key.len()) }; crypter.pad(false); @@ -37,6 +38,8 @@ fn new_crypter_unpadded( } fn ecb_128_crypt(mode: Mode, key: &[u8], text: &[u8]) -> Vec<u8> { + assert!(text.len() % 16 == 0); + let cipher = Cipher::aes_128_ecb(); let iv = None; @@ -60,6 +63,8 @@ fn ecb_128_decrypt(key: &[u8], text: &[u8]) -> Vec<u8> { } fn cbc_128_encrypt(key: &[u8], text: &[u8], iv: Vec<u8>) -> Vec<u8> { + assert!(iv.len() == 16); + let mut iv = iv; let mut ciphertext = Vec::with_capacity(text.len()); @@ -140,8 +145,8 @@ fn main() { }; let output = match mode { - Mode::Decrypt => cbc_128_decrypt(&decoded[..], key, iv), - Mode::Encrypt => cbc_128_encrypt(&decoded[..], key, iv), + Mode::Decrypt => cbc_128_decrypt(key, &decoded[..], iv), + Mode::Encrypt => cbc_128_encrypt(key, &decoded[..], iv), }; match mode {