diff --git a/quicktap/src/noise/handshake/mod.rs b/quicktap/src/noise/handshake/mod.rs index 958deee..823430b 100644 --- a/quicktap/src/noise/handshake/mod.rs +++ b/quicktap/src/noise/handshake/mod.rs @@ -1,5 +1,6 @@ //! `Noise_IKpsk2` handshake, specifically the way WireGuard defines it use std::fmt::{Debug, Formatter}; +use rand::rngs::OsRng; use tai64::Tai64N; use x25519_dalek::{EphemeralSecret, PublicKey, StaticSecret}; use crate::qcrypto::timestamp; @@ -32,7 +33,7 @@ pub struct Cookie { /// Represents the internal handshake state. This does not really need to be messed with by outside users #[allow(missing_docs)] #[allow(clippy::module_name_repetitions)] -pub struct HandshakeState { +pub struct HandshakeState<'a> { pub h: [u8; 32], pub ck: [u8; 32], @@ -43,7 +44,7 @@ pub struct HandshakeState { pub s_pub_r: PublicKey, pub e_priv_me: StaticSecret, - pub s_priv_me: StaticSecret, + pub s_priv_me: &'a StaticSecret, pub s_pub_them: PublicKey, pub i_i: u32, @@ -53,14 +54,34 @@ pub struct HandshakeState { pub cookies: Vec } -impl HandshakeState { +impl<'a> HandshakeState<'a> { /// Determines if the state variables of this `HandshakeState` are the same as another #[allow(clippy::suspicious_operation_groupings)] pub fn is_eq(&self, other: &HandshakeState) -> bool { self.h == other.h && self.ck == other.ck && self.e_pub_i == other.e_pub_i && self.s_pub_i == other.s_pub_i && self.s_pub_r == other.s_pub_r && self.i_i == other.i_i && self.i_r == other.i_r } + + /// Create a new handshake state representing a brand-new handshake. + /// This function initializes the important values with their appropriate initialization vectors, and zeroes out all other values. + pub fn new(private_key: &StaticSecret, other_pubkey: PublicKey, pre_shared_key: Option<[u8; 32]>) -> Self { + Self { + h: [0u8; 32], + ck: [0u8; 32], + e_pub_i: PublicKey::from([0u8; 32]), + e_pub_r: PublicKey::from([0u8; 32]), + s_pub_i: PublicKey::from([0u8; 32]), + s_pub_r: PublicKey::from([0u8; 32]), + e_priv_me: StaticSecret::new(OsRng), + s_priv_me: private_key, + s_pub_them: other_pubkey, + i_i: 0, + i_r: 0, + q: pre_shared_key.unwrap_or([0u8; 32]), + cookies: vec![], + } + } } -impl Debug for HandshakeState { +impl<'a> Debug for HandshakeState<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("HandshakeState") .field("h", &self.h) diff --git a/quicktap/src/noise/mod.rs b/quicktap/src/noise/mod.rs index ecac1d8..e9e3167 100644 --- a/quicktap/src/noise/mod.rs +++ b/quicktap/src/noise/mod.rs @@ -1,4 +1,3 @@ //! Contains structs and functions for serializing and deserializing different packets in the Noise_IKpsk2 handshake and data frames - pub mod handshake; pub mod error; \ No newline at end of file