diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-07-14 19:04:59 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-07-14 19:04:59 +0200 |
commit | abf502f73cc5b3c788762b463b57a4e62b63aa08 (patch) | |
tree | a6c167c64f253156f8632511b3c606c9d77e03c4 /src | |
parent | Begin work on creating initiation (diff) | |
download | wireguard-rs-abf502f73cc5b3c788762b463b57a4e62b63aa08.tar.xz wireguard-rs-abf502f73cc5b3c788762b463b57a4e62b63aa08.zip |
Test precomputed values
Sanity check
Diffstat (limited to 'src')
-rw-r--r-- | src/messages.rs | 20 | ||||
-rw-r--r-- | src/noise.rs | 38 |
2 files changed, 44 insertions, 14 deletions
diff --git a/src/messages.rs b/src/messages.rs index 92a5a3e..805de8d 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -29,7 +29,7 @@ impl From<&[u8]> for Initiation { let mut msg : Self; owned.copy_from_slice(b); - // cast to MessageInitiate + // cast to Initiation unsafe { msg = mem::transmute::<[u8; mem::size_of::<Self>()], Self>(owned); }; @@ -61,7 +61,7 @@ impl Into<Vec<u8>> for Initiation { impl fmt::Debug for Initiation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, - "MessageInitiate {{ type = {} }}", + "MessageInitiation {{ type = {} }}", self.f_type ) } @@ -101,7 +101,7 @@ impl Eq for Initiation {} */ #[repr(C)] #[derive(Copy, Clone)] -pub struct MessageResponse { +pub struct Response { f_type : u8, f_reserved : [u8; 3], f_sender : u32, @@ -110,14 +110,14 @@ pub struct MessageResponse { f_empty : [u8; SIZE_TAG], } -impl From<&[u8]> for MessageResponse { +impl From<&[u8]> for Response { fn from(b: &[u8]) -> Self { // create owned copy let mut owned = [0u8; mem::size_of::<Self>()]; let mut msg : Self; owned.copy_from_slice(b); - // cast to MessageInitiate + // cast to MessageResponse unsafe { msg = mem::transmute::<[u8; mem::size_of::<Self>()], Self>(owned); }; @@ -130,7 +130,7 @@ impl From<&[u8]> for MessageResponse { } } -impl Into<Vec<u8>> for MessageResponse { +impl Into<Vec<u8>> for Response { fn into(self) -> Vec<u8> { // correct endianness let mut msg = self; @@ -148,7 +148,7 @@ impl Into<Vec<u8>> for MessageResponse { } } -impl fmt::Debug for MessageResponse { +impl fmt::Debug for Response { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "MessageResponse {{ type = {} }}", @@ -158,7 +158,7 @@ impl fmt::Debug for MessageResponse { } #[cfg(test)] -impl PartialEq for MessageResponse { +impl PartialEq for Response { fn eq(&self, other: &Self) -> bool { self.f_type == other.f_type && self.f_reserved == other.f_reserved && @@ -175,7 +175,7 @@ mod tests { #[test] fn message_response_identity() { - let msg = MessageResponse { + let msg = Response { f_type : TYPE_RESPONSE, f_reserved : [0u8; 3], f_sender : 146252, @@ -195,7 +195,7 @@ mod tests { }; let buf : Vec<u8> = msg.into(); - assert_eq!(msg, MessageResponse::from(&buf[..])); + assert_eq!(msg, Response::from(&buf[..])); } #[test] diff --git a/src/noise.rs b/src/noise.rs index 6d3850c..4d741c0 100644 --- a/src/noise.rs +++ b/src/noise.rs @@ -16,9 +16,6 @@ type HMACBlake2s = Hmac<Blake2s>; /* Internal functions for processing and creating noise messages */ -const IDENTIFIER : &[u8] = b"WireGuard v1 zx2c4 Jason@zx2c4.com"; -const CONSTRUCTION : &[u8] = b"Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"; - const SIZE_CK : usize = 32; const SIZE_HS : usize = 32; @@ -39,6 +36,14 @@ const INITIAL_HS : [u8; SIZE_HS] = [ ]; macro_rules! HASH { + ($input1:expr) => { + { + let mut hsh = <Blake2s as Digest>::new(); + Digest::input(&mut hsh, $input1); + Digest::result(hsh) + } + }; + ($input1:expr, $input2:expr) => { { let mut hsh = <Blake2s as Digest>::new(); @@ -51,7 +56,11 @@ macro_rules! HASH { macro_rules! HMAC { ($key:expr, $input:expr) => { - HMACBlake2s::new($key).hash($input).result() + { + let mut mac = HMACBlake2s::new($key); + mac.hash($input); + mac.result() + } }; ($key:expr, $input1:expr, $input2:expr) => { @@ -80,6 +89,27 @@ macro_rules! KDF2 { } } +#[cfg(test)] +mod tests { + use super::*; + + const IDENTIFIER : &[u8] = b"WireGuard v1 zx2c4 Jason@zx2c4.com"; + const CONSTRUCTION : &[u8] = b"Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"; + + #[test] + fn precomputed_chain_key() { + assert_eq!(INITIAL_CK[..], HASH!(CONSTRUCTION)[..]); + } + + #[test] + fn precomputed_hash() { + assert_eq!( + INITIAL_HS[..], + HASH!(INITIAL_CK, IDENTIFIER)[..] + ); + } +} + pub fn create_initiation(peer : &Peer, id : u32) -> Result<Vec<u8>, ()> { let mut rng = OsRng::new().unwrap(); let mut msg : messages::Initiation = Default::default(); |