diff --git a/src/crypto.rs b/src/crypto.rs index 39e1190..5f16cdf 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -14,21 +14,21 @@ use x25519_dalek::{EphemeralSecret, PublicKey, SharedSecret}; const AES_NONCE_SIZE: usize = 12; -async fn ephemeral_dh( +pub async fn edh( reader: &mut BufReader>, writer: &mut BufWriter>, buf: &mut Vec, go_first: bool, -) -> Result> { +) -> Result> { let own_sec = EphemeralSecret::new(OsRng); let own_pbk = PublicKey::from(&own_sec); if go_first { - comms::send_bytes(writer, own_pbk.as_bytes()).await?; - comms::recv_bytes(reader, buf).await?; + comms::send_bytes(writer, None, &own_pbk.as_bytes().to_vec()).await?; + comms::recv_bytes(reader, None, buf).await?; } else { - comms::recv_bytes(reader, buf).await?; - comms::send_bytes(writer, own_pbk.as_bytes()).await?; + comms::recv_bytes(reader, None, buf).await?; + comms::send_bytes(writer, None, &own_pbk.as_bytes().to_vec()).await?; } let sliced_buf: [u8; 32] = buf[..32].try_into()?; @@ -37,7 +37,9 @@ async fn ephemeral_dh( Ok(own_sec.diffie_hellman(&recv_pbk)) } -fn aes_cipher(secret: SharedSecret) -> Result, Box> { +pub fn aes_cipher( + secret: SharedSecret, +) -> Result, Box> { Ok(Aes256Gcm::new(secret.as_bytes().into())) } @@ -47,21 +49,20 @@ fn generate_nonce(rng: &mut impl RngCore) -> Nonce { nonce } -fn aes_encrypt( - data: Vec, +pub fn aes_encrypt( + data: &Vec, cipher: &mut AesGcm, - rng: &mut impl RngCore, -) -> Result, Box> { + rng: &mut OsRng, +) -> Result, Box> { let nonce = generate_nonce(rng); let encrypted = cipher.encrypt(&nonce, data.as_ref()).unwrap(); // TODO: handle error types Ok(encrypted) } -fn aes_decrypt( - data: Vec, +pub fn aes_decrypt( + data: &Vec, cipher: &mut AesGcm, - rng: &mut impl RngCore, -) -> Result, Box> { +) -> Result, Box> { let (nonce_bytes, data) = data.split_at(AES_NONCE_SIZE); let decrypted = cipher .decrypt(Nonce::from_slice(nonce_bytes), data.as_ref())