From d7f0bb987b21e93a5798403d294f7905151682f7 Mon Sep 17 00:00:00 2001 From: Ummon Date: Tue, 6 Jan 2015 00:57:49 +0100 Subject: [PATCH] Changes according the latest Rust nightly. --- lab1_rust/Cargo.lock | 24 ++++++++++++------------ lab1_rust/src/crypto.rs | 9 +++++---- lab1_rust/src/end_point.rs | 5 +++-- lab1_rust/src/main.rs | 1 + lab1_rust/src/oracle_machine.rs | 8 ++++---- lab1_rust/src/packet.rs | 31 ++++++++++++++++--------------- lab1_rust/src/utils.rs | 8 ++++++++ 7 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 lab1_rust/src/utils.rs diff --git a/lab1_rust/Cargo.lock b/lab1_rust/Cargo.lock index 00d919b..f84a5b3 100644 --- a/lab1_rust/Cargo.lock +++ b/lab1_rust/Cargo.lock @@ -2,41 +2,41 @@ name = "lab1_rust" version = "0.0.2" dependencies = [ - "openssl 0.2.7 (git+https://github.com/sfackler/rust-openssl.git)", + "openssl 0.2.12 (git+https://github.com/sfackler/rust-openssl.git)", ] [[package]] name = "libressl-pnacl-sys" -version = "2.1.0" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "pnacl-build-helper 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnacl-build-helper 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "openssl" -version = "0.2.7" -source = "git+https://github.com/sfackler/rust-openssl.git#12d31ade9dca8006b23e36d7631e9113f6234fc0" +version = "0.2.12" +source = "git+https://github.com/sfackler/rust-openssl.git#3ab2e07618fe5c1e239a64ce8e267bc1ca95aaf7" dependencies = [ - "openssl-sys 0.2.7 (git+https://github.com/sfackler/rust-openssl.git)", + "openssl-sys 0.2.12 (git+https://github.com/sfackler/rust-openssl.git)", ] [[package]] name = "openssl-sys" -version = "0.2.7" -source = "git+https://github.com/sfackler/rust-openssl.git#12d31ade9dca8006b23e36d7631e9113f6234fc0" +version = "0.2.12" +source = "git+https://github.com/sfackler/rust-openssl.git#3ab2e07618fe5c1e239a64ce8e267bc1ca95aaf7" dependencies = [ - "libressl-pnacl-sys 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libressl-pnacl-sys 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "pkg-config" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "pnacl-build-helper" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/lab1_rust/src/crypto.rs b/lab1_rust/src/crypto.rs index ad7e6fa..67d0b38 100644 --- a/lab1_rust/src/crypto.rs +++ b/lab1_rust/src/crypto.rs @@ -4,16 +4,17 @@ use std::slice::bytes::copy_memory; use openssl::crypto::hash::HashType::SHA256; use openssl::crypto::hmac::HMAC; use openssl::crypto::symm; +use utils::from_elem; // These aren't the keys you're looking for. const 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]; const 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 from HMAC-SHA256. -pub fn compute_mac(data: &[u8]) -> [u8, ..10] { +pub fn compute_mac(data: &[u8]) -> [u8; 10] { let mut hmac = HMAC(SHA256, KEY_A); hmac.update(data); - let mut result = [0u8, ..10]; + let mut result = [0u8; 10]; copy_memory(&mut result, hmac.finalize().slice(0, 10)); result } @@ -47,8 +48,8 @@ pub fn decrypt(cipherdata: &[u8], iv: &[u8]) -> Option> { } pub fn generate_key(size_byte: uint) -> IoResult> { - let mut bytes = Vec::from_elem(size_byte, 0u8); + let mut bytes = from_elem(size_byte, 0u8); let mut generator = try!(OsRng::new()); // Uses '/dev/urandom' on Unix-like systems. - generator.fill_bytes(bytes.as_mut_slice_()); + generator.fill_bytes(bytes.as_mut_slice()); Ok(bytes) } \ No newline at end of file diff --git a/lab1_rust/src/end_point.rs b/lab1_rust/src/end_point.rs index d2cedcb..83967da 100644 --- a/lab1_rust/src/end_point.rs +++ b/lab1_rust/src/end_point.rs @@ -5,6 +5,7 @@ use std::thread::Thread; use packet; use packet::{ Packet, ReadingResult, PacketType }; use packet::PacketType::{ Command, Answer, Error }; +use utils::from_elem; // Default timeout when waiting data on a stream (for instance: 'TcpStream::read'). static DEFAULT_TIMEOUT: Option = Some(500); // [ms]. @@ -195,7 +196,7 @@ impl Client { // 5) |client: &mut Client| -> bool { println!("Sending a packet with too small data..."); - let command = Command(Packet::new_packet_data(0, Vec::from_elem(6, 0x00))); + let command = Command(Packet::new_packet_data(0, from_elem(6, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::ReadingError::IO( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { @@ -207,7 +208,7 @@ impl Client { // 6) |client: &mut Client| -> bool { println!("Sending a packet with too large data..."); - let command = Command(Packet::new_packet_data(0, Vec::from_elem(40, 0x00))); + let command = Command(Packet::new_packet_data(0, from_elem(40, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::ReadingError::IO( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { diff --git a/lab1_rust/src/main.rs b/lab1_rust/src/main.rs index 80551f8..2e15908 100644 --- a/lab1_rust/src/main.rs +++ b/lab1_rust/src/main.rs @@ -8,6 +8,7 @@ use std::os; use end_point::{ Client, Server }; +mod utils; mod crypto; mod packet; mod end_point; diff --git a/lab1_rust/src/oracle_machine.rs b/lab1_rust/src/oracle_machine.rs index 018e2a5..71cee8a 100644 --- a/lab1_rust/src/oracle_machine.rs +++ b/lab1_rust/src/oracle_machine.rs @@ -9,7 +9,7 @@ use end_point::EndPoint; /// Tries to decipher a ciphered data block by using the previous XOR operand and an oracle on the provided address and port. /// May print some messages on stdout. -pub fn decipher(address: &str, port: u16, original_xor_operand: &[u8, ..16], cipher_block: &[u8, ..16], variant: packet::Variant) -> Option> { +pub fn decipher(address: &str, port: u16, original_xor_operand: &[u8; 16], cipher_block: &[u8; 16], variant: packet::Variant) -> Option> { let mut end_point = EndPoint::new( match TcpStream::connect((address, port)) { Ok(s) => s, @@ -22,12 +22,12 @@ pub fn decipher(address: &str, port: u16, original_xor_operand: &[u8, ..16], cip ); // Sees 'packet::Packet' documentation for a complete description about the binary packet structure. - let mut final_packet = [0u8, ..2 + 1 + 8 + 32 + 10]; + let mut final_packet = [0u8; 2 + 1 + 8 + 32 + 10]; final_packet[1] = (final_packet.len() as u8) - 2; // Data length. copy_memory(final_packet.slice_mut(2 + 1 + 8 + 16, 2 + 1 + 8 + 32), cipher_block); - let mut decipher_block = [0u8, ..16]; // The result. - let mut x_prime_block = [0u8, ..16]; // The cipher block ('cipher_block') after AES and before XOR. + let mut decipher_block = [0u8; 16]; // The result. + let mut x_prime_block = [0u8; 16]; // The cipher block ('cipher_block') after AES and before XOR. let mut current_timestamp = 0u64; let mut first_byte = 0u8; // Used to save the first byte for the first iteration. diff --git a/lab1_rust/src/packet.rs b/lab1_rust/src/packet.rs index ec63d9e..c9f111a 100644 --- a/lab1_rust/src/packet.rs +++ b/lab1_rust/src/packet.rs @@ -5,15 +5,16 @@ use std::rand::distributions::IndependentSample; use serialize::hex::{ ToHex }; use self::PacketType::{ Command, Answer, Error }; use crypto; +use utils::from_elem; -#[deriving(Show, Copy)] +#[derive(Show, Copy)] pub enum Variant { Weak, // The MAC is computed on data without padding. Fixed // The MAC is computed on data and padding. } // There are all the errors that may occur when reading an encrypted and authenticated packet. -#[deriving(Show)] +#[derive(Show)] pub enum ReadingError { IO(io::IoError), UnknownPacketType, // If the first byte is unknown. @@ -32,7 +33,7 @@ macro_rules! try_read_io( ); // There are all the errors that may occur when encrypting, authenticating and writing a packet. -#[deriving(Show)] +#[derive(Show)] pub enum WritingError { IO(io::IoError), Encrypt, @@ -50,19 +51,19 @@ static MIN_PAYLOAD_SIZE: uint = 7; static MAX_PAYLOAD_SIZE: uint = 39; static FIXED_PACKET_SIZE: uint = 1 + 8 + 10; // Packet type + timestamp + MAC. -#[deriving(Show, Clone)] +#[derive(Show, Clone)] pub struct PacketData { id: u8, payload: Vec // The size can vary from 'MIN_PAYLOAD_SIZE' to 'MAX_PAYLOAD_SIZE' bytes. } -#[deriving(Show, Clone)] +#[derive(Show, Clone)] pub enum ErrorType { Crypt, Auth } -#[deriving(Clone)] +#[derive(Clone)] pub enum PacketType { Command(PacketData), Answer(PacketData), @@ -89,11 +90,11 @@ pub enum PacketType { /// |I|C...C| for weak variant /// |I|C...C|P...P|for fixed variant /// |0000000000000000| for error packet -#[deriving(Show)] +#[derive(Show)] pub struct Packet { pub t: PacketType, pub timestamp: u64 -} +} impl fmt::Show for PacketType { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -111,8 +112,8 @@ impl fmt::Show for PacketType { impl Packet { pub fn random_packet_data(seed: &[uint]) -> PacketData { let mut rng = if seed.is_empty() { StdRng::new().unwrap() } else { SeedableRng::from_seed(seed) }; - let mut payload = Vec::from_elem(distributions::Range::new(MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE + 1).ind_sample(&mut rng), 0u8); - rng.fill_bytes(payload.as_mut_slice_()); + let mut payload = from_elem(distributions::Range::new(MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE + 1).ind_sample(&mut rng), 0u8); + rng.fill_bytes(payload.as_mut_slice()); PacketData { id: rng.gen::(), payload: payload @@ -143,7 +144,7 @@ impl Packet { let mut data = match self.t { Command(ref p) | Answer(ref p) => packet_data(p), - Error(_) => Vec::from_elem(16, 0) // Padding as data: 16 * 0. + Error(_) => from_elem(16, 0) // Padding as data: 16 * 0. }; let data_size = data.len(); @@ -210,8 +211,8 @@ impl Packet { let timestamp = try_read_io!(input.read_be_u64()); - let mut encrypted_data = Vec::from_elem(data_size as uint - FIXED_PACKET_SIZE, 0u8); - if try_read_io!(input.read(encrypted_data.as_mut_slice_())) != encrypted_data.len() { + let mut encrypted_data = from_elem(data_size as uint - FIXED_PACKET_SIZE, 0u8); + if try_read_io!(input.read(encrypted_data.as_mut_slice())) != encrypted_data.len() { return Err(ReadingError::UnconsistentEncryptedSize) } let mut data = match crypto::decrypt(encrypted_data.as_slice(), iv_from_timestamp(timestamp).as_slice()) { @@ -220,7 +221,7 @@ impl Packet { }; // Reads the MAC. - let mut mac_read = [0u8, ..10]; + let mut mac_read = [0u8; 10]; if try_read_io!(input.read(&mut mac_read)) != mac_read.len() { return Err(ReadingError::UnconsistentMACSize) } @@ -258,7 +259,7 @@ impl Packet { _ => { if data.len() != 16 { return Err(ReadingError::UnconsistentDataSize) - } else if data != Vec::from_elem(16, 0) { + } else if data != from_elem(16, 0) { return Err(ReadingError::Data) } match packet_type { 0x0A => Error(ErrorType::Crypt), _ => Error(ErrorType::Auth) } diff --git a/lab1_rust/src/utils.rs b/lab1_rust/src/utils.rs new file mode 100644 index 0000000..b6be23a --- /dev/null +++ b/lab1_rust/src/utils.rs @@ -0,0 +1,8 @@ + +pub fn from_elem(nb_elem: uint, t: T) -> Vec { + let mut v = Vec::with_capacity(nb_elem); + for _ in range(0u, nb_elem) { + v.push(t); + } + v +} \ No newline at end of file -- 2.45.2