From: Ummon Date: Tue, 4 Nov 2014 12:15:17 +0000 (+0100) Subject: Begining of the machine oracle (work in progress..) X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=c9318a07ce0ec00f999ff17943b83048d536ecd1;p=crypto_lab1.git Begining of the machine oracle (work in progress..) --- diff --git a/src/crypto.rs b/src/crypto.rs index 35c7b48..d9fe182 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -5,9 +5,11 @@ use openssl::crypto::hash::SHA256; use openssl::crypto::hmac::HMAC; use openssl::crypto::symm; +// These aren't the keys you're looking for. static KEY_A: &'static [u8] = [125, 31, 131, 118, 143, 180, 252, 53, 211, 217, 79, 240, 128, 91, 252, 87, 104, 236, 145, 198, 163, 203, 161, 12, 53, 56, 218, 40, 221, 95, 171, 140]; static KEY_C: &'static [u8] = [75, 226, 88, 31, 223, 216, 182, 216, 178, 58, 59, 193, 245, 80, 254, 128, 125, 246, 246, 224, 194, 190, 123, 123, 10, 131, 217, 183, 112, 157, 166, 102]; +/// Only returns the first ten bytes. pub fn compute_mac(data: &[u8]) -> [u8, ..10] { let mut hmac = HMAC(SHA256, KEY_A); hmac.update(data); @@ -16,11 +18,12 @@ pub fn compute_mac(data: &[u8]) -> [u8, ..10] { result } +/// Encrypt may fail if the provided data size isn't a multiple of 16. pub fn encrypt(plaindata: &[u8], iv: &[u8]) -> Option> { let c = symm::Crypter::new(symm::AES_256_CBC); c.init(symm::Encrypt, KEY_C, iv.to_vec()); c.pad(false); // Padding disabled! - let mut r = c.update(plaindata); + let r = c.update(plaindata); let rest = c.finalize(); if rest.is_empty() { Some(r) @@ -29,11 +32,12 @@ pub fn encrypt(plaindata: &[u8], iv: &[u8]) -> Option> { } } +/// Decrypt may fail if the provided data size isn't a multiple of 16. pub fn decrypt(cypherdata: &[u8], iv: &[u8]) -> Option> { let c = symm::Crypter::new(symm::AES_256_CBC); c.init(symm::Decrypt, KEY_C, iv.to_vec()); c.pad(false); // Padding disabled! - let mut r = c.update(cypherdata); + let r = c.update(cypherdata); let rest = c.finalize(); if rest.is_empty() { Some(r) diff --git a/src/end_point.rs b/src/end_point.rs index 71f217e..2d0a66c 100644 --- a/src/end_point.rs +++ b/src/end_point.rs @@ -15,7 +15,7 @@ pub struct Client { end_point: EndPoint, } -struct EndPoint { +pub struct EndPoint { socket: TcpStream, current_timestamp: u64 } @@ -33,7 +33,7 @@ impl Server { for stream in acceptor.incoming() { match stream { Ok(stream) => spawn(proc() { - Server::handle_client(EndPoint { socket: stream, current_timestamp: 0 }); + Server::handle_client(EndPoint::new(stream)); }), _ => return } @@ -246,6 +246,10 @@ impl Client { } impl EndPoint { + pub fn new(socket: TcpStream) -> EndPoint { + EndPoint { socket: socket, current_timestamp: 0 } + } + fn close(&mut self) -> IoResult<()> { try!(self.socket.close_read()); try!(self.socket.close_write()); diff --git a/src/main.rs b/src/main.rs index 1fae4c1..64aef28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,19 @@ use end_point::{ Client, Server }; mod crypto; mod packet; mod end_point; +mod oracle_machine; const PORT: u16 = 4221; fn print_usage() { - println!("{} | | ...", os::args()[0]); + println!( + r"{} genkey | tests | oracle-weak | oracle-fixed + genkey: Generate a 256 bits key + tests: launch some tests between a client and a weak server + oracle-weak: launch a padding oracle attack against a weak server + oracle-fixed: launch a padding oracle attack against a fixed server", + os::args()[0] + ); } fn main() { @@ -35,7 +43,9 @@ fn main() { Ok(mut server) => { println!("Server started"); - Client::start_tests("::1", PORT); + if args.len() > 1 && args[1].as_slice() == "tests" { + Client::start_tests("::1", PORT); + } println!("Press any key to quit"); io::stdin().read_line().ok().expect("Failed to read line"); diff --git a/src/oracle_machine.rs b/src/oracle_machine.rs new file mode 100644 index 0000000..68aef94 --- /dev/null +++ b/src/oracle_machine.rs @@ -0,0 +1,19 @@ +use std::io; +use std::io::{ TcpStream }; +use end_point::EndPoint; + +/// Try to decypher a cyphered data block by using an oracle on the provided address and port. +/// May prints some message on the stdout. +pub fn decypher(address: &str, port: u16, cypherblock: [u8, ..16]) -> Option> { + let end_point = EndPoint::new( + match TcpStream::connect(address, port) { + Ok(s) => s, + _ => { + println!("Unable to connect to the oracle on {}:{}", address, port); + return None + } + } + ); + + None +}