[noise] structs for handshake response

This commit is contained in:
c0repwn3r 2022-12-15 11:34:28 -05:00
parent a9ced39c41
commit d57423b0c8
Signed by: core
GPG Key ID: FDBF740DADDCEECF
1 changed files with 41 additions and 0 deletions

View File

@ -1 +1,42 @@
//! `Noise_IKpsk2` handshake response packet //! `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(),
}
}
}