[noise] handshake state init helper

This commit is contained in:
c0repwn3r 2022-12-15 13:30:35 -05:00
parent 3b95577a8d
commit 70630f27fc
Signed by: core
GPG Key ID: FDBF740DADDCEECF
2 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,6 @@
//! `Noise_IKpsk2` handshake, specifically the way WireGuard defines it //! `Noise_IKpsk2` handshake, specifically the way WireGuard defines it
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use rand::rngs::OsRng;
use tai64::Tai64N; use tai64::Tai64N;
use x25519_dalek::{EphemeralSecret, PublicKey, StaticSecret}; use x25519_dalek::{EphemeralSecret, PublicKey, StaticSecret};
use crate::qcrypto::timestamp; 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 /// Represents the internal handshake state. This does not really need to be messed with by outside users
#[allow(missing_docs)] #[allow(missing_docs)]
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub struct HandshakeState { pub struct HandshakeState<'a> {
pub h: [u8; 32], pub h: [u8; 32],
pub ck: [u8; 32], pub ck: [u8; 32],
@ -43,7 +44,7 @@ pub struct HandshakeState {
pub s_pub_r: PublicKey, pub s_pub_r: PublicKey,
pub e_priv_me: StaticSecret, pub e_priv_me: StaticSecret,
pub s_priv_me: StaticSecret, pub s_priv_me: &'a StaticSecret,
pub s_pub_them: PublicKey, pub s_pub_them: PublicKey,
pub i_i: u32, pub i_i: u32,
@ -53,14 +54,34 @@ pub struct HandshakeState {
pub cookies: Vec<Cookie> pub cookies: Vec<Cookie>
} }
impl HandshakeState { impl<'a> HandshakeState<'a> {
/// Determines if the state variables of this `HandshakeState` are the same as another /// Determines if the state variables of this `HandshakeState` are the same as another
#[allow(clippy::suspicious_operation_groupings)] #[allow(clippy::suspicious_operation_groupings)]
pub fn is_eq(&self, other: &HandshakeState) -> bool { 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 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 { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HandshakeState") f.debug_struct("HandshakeState")
.field("h", &self.h) .field("h", &self.h)

View File

@ -1,4 +1,3 @@
//! Contains structs and functions for serializing and deserializing different packets in the Noise_IKpsk2 handshake and data frames //! Contains structs and functions for serializing and deserializing different packets in the Noise_IKpsk2 handshake and data frames
pub mod handshake; pub mod handshake;
pub mod error; pub mod error;