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"
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
}
}
pub fn generate_key(size_byte: uint) -> IoResult<Vec<u8>> {
- 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
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<u64> = Some(500); // [ms].
// 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 => {
// 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 => {
use end_point::{ Client, Server };
+mod utils;
mod crypto;
mod packet;
mod end_point;
/// 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<Vec<u8>> {
+pub fn decipher(address: &str, port: u16, original_xor_operand: &[u8; 16], cipher_block: &[u8; 16], variant: packet::Variant) -> Option<Vec<u8>> {
let mut end_point = EndPoint::new(
match TcpStream::connect((address, port)) {
Ok(s) => s,
);
// 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.
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.
);
// 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,
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<u8> // 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),
/// |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 {
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::<u8>(),
payload: payload
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();
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()) {
};
// 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)
}
_ => {
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) }
--- /dev/null
+
+pub fn from_elem<T : Copy>(nb_elem: uint, t: T) -> Vec<T> {
+ let mut v = Vec::with_capacity(nb_elem);
+ for _ in range(0u, nb_elem) {
+ v.push(t);
+ }
+ v
+}
\ No newline at end of file