cryptopals

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

commit 4b307a8b848020c4b29634f00ff97ccc8be2ab1f
parent af9473f3e1f4649c88cae9ba4fc775fef84044ec
Author: Jared Tobin <jared@jtobin.ca>
Date:   Sat, 26 Aug 2017 19:20:37 +1200

Misc updates.

Diffstat:
MREADME.md | 3++-
Mdocs/s2.md | 44+++++++++++++++++++++++++++++++++++++++++++-
Mlib/ecb_detector/src/main.rs | 4++--
3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md @@ -3,7 +3,8 @@ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jtobin/cryptopals/blob/master/LICENSE) Matasano's [cryptopals challenges](http://cryptopals.com/), implemented mainly -in [Rust](https://www.rust-lang.org) and [Haskell](https://haskell-lang.org/). +in [Rust](https://www.rust-lang.org) and [Haskell](https://haskell-lang.org/) +(with plenty of help from bash and friends). ## Problems diff --git a/docs/s2.md b/docs/s2.md @@ -2,6 +2,10 @@ #### 2.9 +PKCS #7 padding here just means that to pad a message of length 'l' to 'k' +bytes, one appends 'k - l' bytes -- each of value 'k - l' -- to the message. +So here we get four bytes' worth of padding, each of value 04: + $ echo -n 'YELLOW SUBMARINE' | ./bin/pkcs 20 | xxd 00000000: 5945 4c4c 4f57 2053 5542 4d41 5249 4e45 YELLOW SUBMARINE 00000010: 0404 0404 0a ..... @@ -17,10 +21,48 @@ Using OpenSSL: I'm back and I'm ringin' the bell A rockin' on the mike while the fly girls yell -Here's an answer that I had to write code to get: +Or, 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 +#### 2.11 + +I'm having fun with the shell so I was originally going to string this all +together with bash. One could generate keys/IVs like so (note the use of +LC_CTYPE in order to get 'tr' to work properly on OS X): + + $ AES_KEY=$(LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c16) + $ IV=$(LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c16) + +and flip a coin: + + $ HEAD=$(($RANDOM % 2)) + +and even come up with some random bytes to prepend and append: + + $ NPREPEND=$(jot -r 1 5 10) NAPPEND=$(jot -r 1 5 10) + $ PREPENDER=$(LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c$NPREPEND) + $ APPENDER=$(LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c$NAPPEND) + $ echo 'message' | sed -e "s/^/$PREPENDER/;s/$/$APPENDER/" + ifqfc9FshtmessagekOIxmrYfR + +So you could do something crazy, like: + + $ echo 'message' | sed -e "s/^/$PREPENDER/;s/$/$APPENDER/" | \ + base64 | tr -d '\n' | if [[ $HEAD == 0]]; \ + then ./bin/aes_ecb --encrypt -k $AES_KEY; \ + else ./bin/aes_cbc --encrypt -k $AES_KEY --iv $IV; fi + zcE4rONdRk04w8v4Sm8HYQ== + +and then: + + $ echo "zcE4rONdRk04w8v4Sm8HYQ==" | ./bin/ecb_detector + likely cbc + +which is actually the wrong guess here. + +But, uh, let's not use bash for this. + diff --git a/lib/ecb_detector/src/main.rs b/lib/ecb_detector/src/main.rs @@ -6,10 +6,10 @@ use std::io::{self, Read}; const KEY_SIZE: usize = 16; -fn ecb_detector(encoded: &[u8], size: usize) -> bool { +fn ecb_detector(ciphertext: &[u8], size: usize) -> bool { let mut blocks = HashSet::new(); - for block in encoded.chunks(size) { + for block in ciphertext.chunks(size) { if blocks.contains(block) { return true; }