X-Git-Url: http://git.euphorik.ch/?p=crypto_lab1.git;a=blobdiff_plain;f=src%2Fpacket.rs;h=73eecea9ad1cb7948f7bd83948296fdd00ee92dc;hp=ece32c49f62cced2bb1548371c35db24af9592f7;hb=c9318a07ce0ec00f999ff17943b83048d536ecd1;hpb=786dd3ba778ac05917b00e42e8d82929db9fa529 diff --git a/src/packet.rs b/src/packet.rs index ece32c4..73eecea 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -27,8 +27,8 @@ macro_rules! try_read_io( // There are all the errors that may occur when encrypting, authenticating and writing a packet. #[deriving(Show)] pub enum WritingError { - WriteIOError(io::IoError) - // TODO... + WriteIOError(io::IoError), + EncryptError, } // A macro to return a 'IOWritingError' in case of error. @@ -64,7 +64,7 @@ pub enum PacketType { /// Serialized packet format : |LL|P|TTTTTTTT|D...D|MMMMMMMMMM| /// Where: -/// LL: Size on the following data +/// LL: Size of the following data /// P: Packet type: /// 0x00: Command /// OxFF: Answer @@ -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 +/// |I|C...C| for command and answer packet +/// |0000000000000000| for error packet #[deriving(Show)] pub struct Packet { pub t: PacketType, @@ -115,12 +115,12 @@ impl Packet { } pub fn write(&self, output: &mut io::Writer) -> WritingResult { - self.write_with_padding_fun(output, |_: uint, padding_length: uint| -> u8 { + self.write_with_padding_fun(output, |_, padding_length: uint| -> u8 { padding_length as u8 }) } - /// 'padd_fun' is function to fill the padding. The first argument is the index of the current byte, starting at 0. + /// 'padd_fun' is function defining the padding. The first argument is the index of the current byte, starting at 0. /// The second argument is the padding length. pub fn write_with_padding_fun(&self, output: &mut io::Writer, padd_fun: |uint, uint| -> u8) -> WritingResult { fn packet_data(p: &PacketData) -> Vec { @@ -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 @@ -143,7 +143,7 @@ impl Packet { // Padding. match self.t { Command(_) | Answer(_) => { - let padding_size = if data.len() % 16 == 0 { 16 } else { data.len() % 16 } ; + let padding_size = if data.len() % 16 == 0 { 16 } else { 16 - data.len() % 16 } ; data.reserve_additional(padding_size); for i in range(0, padding_size) { data.push(padd_fun(i, padding_size)); @@ -153,7 +153,10 @@ impl Packet { } // Encrypt. - let encrypted_data = crypto::encrypt(data.as_slice(), iv_from_timestamp(self.timestamp).as_slice()); + let encrypted_data = match crypto::encrypt(data.as_slice(), iv_from_timestamp(self.timestamp).as_slice()) { + Some(d) => d, + _ => return Err(EncryptError) + }; // Write packet length. try_write_io!(output.write_be_u16((encrypted_data.len() + FIXED_PACKET_SIZE) as u16)); @@ -200,7 +203,10 @@ impl Packet { if try_read_io!(input.read(encrypted_data.as_mut_slice_())) != encrypted_data.len() { return Err(UnconsistentEncryptedSizeError) } - let mut data = crypto::decrypt(encrypted_data.as_slice(), iv_from_timestamp(timestamp).as_slice()); + let mut data = match crypto::decrypt(encrypted_data.as_slice(), iv_from_timestamp(timestamp).as_slice()) { + Some(d) => d, + _ => return Err(UnconsistentEncryptedSizeError) + }; // Control the size and the content of the padding then remove it. if packet_type == 0x00 || packet_type == 0xFF { @@ -242,7 +248,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) }