1#[cfg(feature = "server")]
21pub mod password;
22pub mod kex;
23
24use why2::
25{
26 Grid,
27 encrypter,
28 decrypter,
29 options as core_options,
30 auth::AuthenticatedData,
31};
32
33use sha2::{ Sha256, Digest };
34
35use crate::options;
36
37const GRID_W: usize = options::GRID_DIMENSIONS.0;
39const GRID_H: usize = options::GRID_DIMENSIONS.1;
40
41pub fn sha256(seed_str: &str) -> [u8; 32] {
44 let mut hasher = Sha256::new();
46 hasher.update(seed_str.as_bytes());
47
48 hasher.finalize().into()
50}
51
52pub fn encrypt_packet(packet_bytes: Vec<u8>, keys: &options::SharedKeys) -> Vec<u8>
53{
54 let mut input_i64 = Vec::with_capacity((packet_bytes.len() + 7) / 8);
56 for chunk in packet_bytes.chunks(8)
57 {
58 let mut buf = [0u8; 8];
59 buf[..chunk.len()].copy_from_slice(chunk);
60 input_i64.push(i64::from_be_bytes(buf));
61 }
62
63 let encrypted_data = encrypter::encrypt::<GRID_W, GRID_H>(&input_i64, Some(&keys.0)).expect("Encrypting packet failed");
65
66 AuthenticatedData::authenticate(encrypted_data, keys.1.as_slice().try_into().unwrap()).into()
68}
69
70pub fn decrypt_packet(mut decoded_packet: Vec<u8>, keys: &options::SharedKeys) -> Option<Vec<u8>>
71{
72 let auth_packet: AuthenticatedData<GRID_W, GRID_H> = decoded_packet.as_slice().try_into().ok()?;
74
75 if !auth_packet.verify(keys.1.as_slice().try_into().ok()?)
77 {
78 return None;
79 }
80
81 let decrypted_packet = decrypter::decrypt(core_options::EncryptedData
83 {
84 output: auth_packet.encrypted_data.output,
85 key: Grid::from_key(&keys.0).ok()?,
86 nonce: auth_packet.encrypted_data.nonce,
87 }).ok()?;
88
89 decoded_packet = Vec::with_capacity(decrypted_packet.output.len() * 8);
91 for val in decrypted_packet.output.to_vec()
92 {
93 decoded_packet.extend_from_slice(&val.to_be_bytes());
94 }
95
96 Some(decoded_packet)
97}