diff --git a/quicktap/src/noise/handshake/response.rs b/quicktap/src/noise/handshake/response.rs index f323277..7189050 100644 --- a/quicktap/src/noise/handshake/response.rs +++ b/quicktap/src/noise/handshake/response.rs @@ -1 +1,42 @@ //! `Noise_IKpsk2` handshake response packet + +use crate::noise::handshake::{HandshakeState, needs_cookie}; +use crate::qcrypto::hashes::{qcrypto_hash_twice, qcrypto_mac}; +use crate::qcrypto::LABEL_MAC1; + +struct HandshakeResponseRaw { + sender: [u8; 4], + receiver: [u8; 4], + ephemeral: [u8; 32], + empty: [u8; 0 + 16], +} +impl HandshakeResponseRaw { + fn to_bytes(&self, session: &mut HandshakeState) -> [u8; 92] { + let mut output_array = [0u8; 92]; + + output_array[0] = 2u8; + output_array[4..8].copy_from_slice(&self.sender); + output_array[8..12].copy_from_slice(&self.receiver); + output_array[12..44].copy_from_slice(&self.ephemeral); + output_array[44..60].copy_from_slice(&self.empty); + + let mac1: [u8; 16] = qcrypto_mac(&qcrypto_hash_twice(LABEL_MAC1.as_bytes(), session.s_pub_i.as_bytes()), &output_array[..116]); + + output_array[116..132].copy_from_slice(&mac1); + + let mac2 = if needs_cookie(session) { qcrypto_mac(&session.cookies[session.cookies.len() - 1].cookie, &output_array[..132]) } else { [0u8; 16] }; + + output_array[132..148].copy_from_slice(&mac2); + + output_array + } + + fn from_bytes(packet: [u8; 92]) -> Self { + Self { + sender: packet[4..8].try_into().unwrap(), + receiver: packet[8..12].try_into().unwrap(), + ephemeral: packet[12..44].try_into().unwrap(), + empty: packet[44..60].try_into().unwrap(), + } + } +} \ No newline at end of file