use serialize::hex::{ ToHex };
use self::PacketType::{ Command, Answer, Error };
use crypto;
use serialize::hex::{ ToHex };
use self::PacketType::{ Command, Answer, Error };
use crypto;
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.
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.
// A macro to return a 'Err(ReadingError::IO(..))' in case of error.
macro_rules! try_read_io(
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(ReadingError::IO(e)) })
// A macro to return a 'Err(ReadingError::IO(..))' in case of error.
macro_rules! try_read_io(
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(ReadingError::IO(e)) })
// A macro to return a 'Err(WritingError::IO(..))' in case of error.
macro_rules! try_write_io(
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(WritingError::IO(e)) })
// A macro to return a 'Err(WritingError::IO(..))' in case of error.
macro_rules! try_write_io(
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(WritingError::IO(e)) })
pub type ReadingResult = Result<Packet, ReadingError>;
pub type WritingResult = Result<(), WritingError>;
pub type ReadingResult = Result<Packet, ReadingError>;
pub type WritingResult = Result<(), WritingError>;
static MAX_PAYLOAD_SIZE: uint = 39;
static FIXED_PACKET_SIZE: uint = 1 + 8 + 10; // Packet type + timestamp + MAC.
static MAX_PAYLOAD_SIZE: uint = 39;
static FIXED_PACKET_SIZE: uint = 1 + 8 + 10; // Packet type + timestamp + MAC.
-/// D...D: Encrypted data (AES-256 CBC mode) of:
-/// |I|C...C|P...P| for command and answer packet:
-/// 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)
+/// D...D: Encrypted data (AES-256 CBC mode) of:
+/// |I|C...C|P...P| for command and answer packet:
+/// 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)
/// MMMMMMMMMM: first 10 bytes (most significant) of the HMAC-SHA256 of:
/// for command and answer packet:
/// |I|C...C| for weak variant
/// |I|C...C|P...P|for fixed variant
/// |0000000000000000| for error packet
/// MMMMMMMMMM: first 10 bytes (most significant) of the HMAC-SHA256 of:
/// for command and answer packet:
/// |I|C...C| for weak variant
/// |I|C...C|P...P|for fixed variant
/// |0000000000000000| for error packet
impl fmt::Show for PacketType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Show for PacketType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
&Command(ref data) => write!(formatter, "Command {{ {} }}", data_to_str(data)),
&Answer(ref data) => write!(formatter, "Answer {{ {} }}", data_to_str(data)),
match self {
&Command(ref data) => write!(formatter, "Command {{ {} }}", data_to_str(data)),
&Answer(ref data) => write!(formatter, "Answer {{ {} }}", data_to_str(data)),
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) };
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());
- 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()) {
return Err(ReadingError::UnconsistentEncryptedSize)
}
let mut data = match crypto::decrypt(encrypted_data.as_slice(), iv_from_timestamp(timestamp).as_slice()) {
if try_read_io!(input.read(&mut mac_read)) != mac_read.len() {
return Err(ReadingError::UnconsistentMACSize)
}
if try_read_io!(input.read(&mut mac_read)) != mac_read.len() {
return Err(ReadingError::UnconsistentMACSize)
}
return Err(ReadingError::Data)
}
match packet_type { 0x0A => Error(ErrorType::Crypt), _ => Error(ErrorType::Auth) }
return Err(ReadingError::Data)
}
match packet_type { 0x0A => Error(ErrorType::Crypt), _ => Error(ErrorType::Auth) }
let mut iv = io::MemWriter::with_capacity(16);
let _ = iv.write_be_u64(0u64);
let _ = iv.write_be_u64(timestamp);
let mut iv = io::MemWriter::with_capacity(16);
let _ = iv.write_be_u64(0u64);
let _ = iv.write_be_u64(timestamp);