From: Ummon Date: Mon, 3 Nov 2014 09:30:25 +0000 (+0100) Subject: Data used for packet error has been modified. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=911f8410c05d3d16922587a59266096f3c6899e1;p=crypto_lab1.git Data used for packet error has been modified. Some clean-up. --- diff --git a/src/crypto.rs b/src/crypto.rs index 60c205b..39e8696 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -12,7 +12,7 @@ pub fn compute_mac(data: &[u8]) -> [u8, ..10] { let mut hmac = HMAC(SHA256, KEY_A); hmac.update(data); let mut result = [0u8, ..10]; - copy_memory(&mut result, hmac.finalize().slice(0, 9)); + copy_memory(&mut result, hmac.finalize().slice(0, 10)); result } diff --git a/src/end_point.rs b/src/end_point.rs index 6ed1fad..b44afc9 100644 --- a/src/end_point.rs +++ b/src/end_point.rs @@ -4,6 +4,7 @@ use std::io::net::tcp::{ TcpAcceptor, TcpListener }; use packet; use packet::{ Packet, Command, Answer, Error, ReadingResult, PacketType }; +// Default timeout when waiting data on a stream (for instance: 'TcpStream::read'). static DEFAULT_TIMEOUT: Option = Some(500); // [ms]. pub struct Server { @@ -105,6 +106,7 @@ impl Client { } /// Send some valid and not-valid packets to the server and check the result. + /// For each test a new client is created. pub fn start_tests(address: &str, port: u16) { let execute = |f: &mut |&mut Client| -> bool| -> bool { match Client::new(address, port) { @@ -125,8 +127,9 @@ impl Client { } let mut tests = vec!( + // 1) |client: &mut Client| -> bool { - println!("Send a valid packet..."); + println!("Sending a valid packet..."); match client.end_point.send_with_result(Command(Packet::random_packet_data([42]))) { Ok(Ok(Packet { t: Answer(..), .. })) => true, other => { @@ -135,9 +138,9 @@ impl Client { } } }, - + // 2) |client: &mut Client| -> bool { - println!("Send a packet with an unknown type..."); + println!("Sending a packet with an unknown type..."); client.end_point.current_timestamp += 1; let mut raw_packet = raw_packet(client.end_point.current_timestamp); raw_packet[2] = 0xEE; // Alter the type. @@ -149,9 +152,9 @@ impl Client { } } }, - + // 3) |client: &mut Client| -> bool { - println!("Send a packet with an old timestamp..."); + println!("Sending a packet with an old timestamp..."); client.end_point.current_timestamp += 1; let raw_packet = raw_packet(0); match client.end_point.send_raw_with_result(raw_packet.as_slice()) { @@ -162,9 +165,9 @@ impl Client { } } }, - + // 4) |client: &mut Client| -> bool { - println!("Send a packet with altered crypted data..."); + println!("Sending a packet with altered crypted data (do not alter the padding)..."); client.end_point.current_timestamp += 1; let mut raw_packet = raw_packet(client.end_point.current_timestamp); raw_packet[11] = 0xDE; @@ -179,9 +182,9 @@ impl Client { } } }, - + // 5) |client: &mut Client| -> bool { - println!("Send a packet with too small data..."); + println!("Sending a packet with too small data..."); let command = Command(Packet::new_packet_data(0, Vec::from_elem(6, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. @@ -191,9 +194,9 @@ impl Client { } } }, - + // 6) |client: &mut Client| -> bool { - println!("Send a packet with too large data..."); + println!("Sending a packet with too large data..."); let command = Command(Packet::new_packet_data(0, Vec::from_elem(40, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. @@ -203,9 +206,9 @@ impl Client { } } }, - + // 7) |client: &mut Client| -> bool { - println!("Send a packet with wrong padding..."); + println!("Sending a packet with wrong padding (all 0)..."); client.end_point.current_timestamp += 1; let mut m = MemWriter::new(); let raw_packet = match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: client.end_point.current_timestamp }).write_with_padding_fun(&mut m, |_, _| -> u8 { 0 }) { diff --git a/src/main.rs b/src/main.rs index e9e911a..1fae4c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,13 +29,17 @@ fn main() { Err(e) => println!("Unable to generate a key. Error: {}", e) } } else { - println!("Starting server..., Press any key to quit"); + println!("Starting server..."); match Server::new("::1", PORT) { Ok(mut server) => { println!("Server started"); + Client::start_tests("::1", PORT); + + println!("Press any key to quit"); io::stdin().read_line().ok().expect("Failed to read line"); + server.close().ok().expect("Failed to close the server"); }, Err(e) => diff --git a/src/packet.rs b/src/packet.rs index ece32c4..632d298 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -76,10 +76,10 @@ pub enum PacketType { /// I: Command ID /// C: Command payload (from 7 to 39 bytes) /// P: Padding from 1 to 16, |I|C...C|P...P| size must be a multiple of 16 -/// |"0000000000000000"| for error packet (16 bytes length) +/// |0000000000000000| for error packet (16 bytes length) /// MMMMMMMMMM: first 10 bytes (most significant) of the HMAC-SHA256 of: /// |I|C...C| for command ans answer packet -/// |"0000000000"| for error packet +/// |0000000000000000| for error packet #[deriving(Show)] pub struct Packet { pub t: PacketType, @@ -134,7 +134,7 @@ impl Packet { let mut data = match self.t { Command(ref p) | Answer(ref p) => packet_data(p), - Error(_) => Vec::from_elem(16, '0' as u8) // Padding as data: 16 * '0'. + Error(_) => Vec::from_elem(16, 0) // Padding as data: 16 * 0. }; // Compute the MAC @@ -152,9 +152,13 @@ impl Packet { _ => () } + println!("data not crypted: {}", data); + // Encrypt. let encrypted_data = crypto::encrypt(data.as_slice(), iv_from_timestamp(self.timestamp).as_slice()); + println!("data crypted: {}", encrypted_data); + // Write packet length. try_write_io!(output.write_be_u16((encrypted_data.len() + FIXED_PACKET_SIZE) as u16)); @@ -242,7 +246,7 @@ impl Packet { _ => { if data.len() != 16 { return Err(UnconsistentDataSizeError) - } else if data != Vec::from_elem(16, '0' as u8) { + } else if data != Vec::from_elem(16, 0) { return Err(DataError) } match packet_type { 0x0A => Error(CryptError), _ => Error(AuthError) }