cryptopals

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

commit d413c86de35859b6f555a1b10c81a6cf5f1b2e00
parent e082523b3c72697a90d47b8a4fd30d92401b78a0
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri,  1 Sep 2017 16:02:07 +1200

Some error handling code.

Diffstat:
MCargo.toml | 10+++++-----
Adata/q12_input.txt | 4++++
Asrc/errors.rs | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 6+++++-
Msrc/s1c01.rs | 14+++++++-------
Msrc/s2c11.rs | 2+-
Asrc/s2c12.rs | 37+++++++++++++++++++++++++++++++++++++
7 files changed, 108 insertions(+), 14 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" authors = ["Jared Tobin <jared@jtobin.ca>"] [dependencies] -base64 = "0.6.0" -clap = "2.26.0" -hex = "0.2.0" -openssl = "0.9.11" -rand = "0.3.16" +base64 = "0.6" +clap = "2.26" +hex = "0.2" +openssl = "0.9" +rand = "0.3" diff --git a/data/q12_input.txt b/data/q12_input.txt @@ -0,0 +1,4 @@ +Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg +aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq +dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg +YnkK diff --git a/src/errors.rs b/src/errors.rs @@ -0,0 +1,49 @@ + +extern crate hex; + +use std::error; +use std::fmt; +use std::process; + +pub enum CryptopalsError { + HexConversionError(hex::FromHexError) +} + +impl fmt::Display for CryptopalsError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + CryptopalsError::HexConversionError(ref err) => + fmt::Display::fmt(err, f) + } + } +} + +impl error::Error for CryptopalsError { + fn description(&self) -> &str { + match *self { + CryptopalsError::HexConversionError(ref err) => err.description() + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + CryptopalsError::HexConversionError(ref err) => Some(err) + } + } +} + +impl fmt::Debug for CryptopalsError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + CryptopalsError::HexConversionError(ref err) => + fmt::Debug::fmt(err, f) + } + } +} + +pub fn handle<A>(input: Result<A, CryptopalsError>) -> A { + input.unwrap_or_else(|err| { + println!("error (cryptopals):\n {}", err); + process::exit(1); + }) +} diff --git a/src/main.rs b/src/main.rs @@ -7,13 +7,17 @@ mod s1c07; mod s2c09; mod s2c10; mod s2c11; +mod s2c12; + +mod errors; fn main() { - println!("s1c01:\n{}\n", s1c01::s1c01()); + println!("s1c01:\n{}\n", errors::handle(s1c01::s1c01())); println!("s1c02:\n{}\n", s1c02::s1c02()); println!("s1c03:\n{}\n", s1c03::s1c03()); println!("s1c07:\n{}\n", s1c07::s1c07()); println!("s2c09:\n{}\n", s2c09::s2c09()); println!("s2c10:\n{}\n", s2c10::s2c10()); println!("s2c11:\n{}\n", s2c11::s2c11()); + println!("s2c12:\n{}\n", s2c12::s2c12()); } diff --git a/src/s1c01.rs b/src/s1c01.rs @@ -2,6 +2,7 @@ extern crate base64; extern crate hex; +use errors::CryptopalsError; use self::hex::{FromHex, FromHexError}; use std::process; @@ -9,15 +10,14 @@ const INPUT: &str = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6\ f7573206d757368726f6f6d"; -pub fn hex_to_b64(input: &str) -> Result<String, FromHexError> { - let raw: Result<Vec<u8>, _> = FromHex::from_hex(&input); +pub fn hex_to_b64(input: &str) -> Result<String, CryptopalsError> { + let raw: Result<Vec<u8>, _> = FromHex::from_hex(&input) + .map_err(|err| CryptopalsError::HexConversionError(err)); + raw.map(|contents| base64::encode(&contents)) } -pub fn s1c01() -> String { - hex_to_b64(&INPUT).unwrap_or_else(|err| { - println!("error (cryptopals): {}", err); - process::exit(1); - }) +pub fn s1c01() -> Result<String, CryptopalsError> { + hex_to_b64(INPUT) } diff --git a/src/s2c11.rs b/src/s2c11.rs @@ -17,7 +17,7 @@ pub fn gen_bytes(size: usize) -> Vec<u8> { let mut rng = rand::thread_rng(); let mut buffer = Vec::with_capacity(size); - for foo in 0..size { + for _ in 0..size { let byte: u8 = rng.gen(); buffer.push(byte); } diff --git a/src/s2c12.rs b/src/s2c12.rs @@ -0,0 +1,37 @@ + +extern crate base64; +extern crate openssl; + +use s1c07::aes_128_ecb_crypt; +use s2c09::pkcs; +use s2c11; +use self::openssl::symm::Mode; + +const BLOCK_SIZE: usize = 16; + +const APPENDER: &str = + "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg + aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq + dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg + YnkK"; + +pub fn mystery_crypter(message: &[u8], key: &[u8]) -> Vec<u8> { + let m_size = message.len() + APPENDER.len(); + let c_size = m_size + BLOCK_SIZE - m_size % BLOCK_SIZE; + + let mut ciphertext = Vec::with_capacity(c_size); + + let appender = base64::decode(APPENDER).unwrap(); + + ciphertext.extend_from_slice(message); + ciphertext.extend_from_slice(&appender); + + ciphertext = pkcs(&ciphertext, c_size); + + aes_128_ecb_crypt(Mode::Encrypt, key, &ciphertext) +} + +pub fn s2c12() -> String { + String::from("foo") +} +