changes to module visibility

This commit is contained in:
17ms 2023-03-20 11:10:23 +02:00
parent 7e056f2711
commit 6a3c4e736c

View File

@ -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<ReadHalf<'_>>,
writer: &mut BufWriter<WriteHalf<'_>>,
buf: &mut Vec<u8>,
go_first: bool,
) -> Result<SharedSecret, Box<dyn Error>> {
) -> Result<SharedSecret, Box<dyn Error + Send + Sync>> {
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<AesGcm<Aes256, U12>, Box<dyn Error>> {
pub fn aes_cipher(
secret: SharedSecret,
) -> Result<AesGcm<Aes256, U12>, Box<dyn Error + Sync + Send>> {
Ok(Aes256Gcm::new(secret.as_bytes().into()))
}
@ -47,21 +49,20 @@ fn generate_nonce(rng: &mut impl RngCore) -> Nonce<U12> {
nonce
}
fn aes_encrypt(
data: Vec<u8>,
pub fn aes_encrypt(
data: &Vec<u8>,
cipher: &mut AesGcm<Aes256, U12>,
rng: &mut impl RngCore,
) -> Result<Vec<u8>, Box<dyn Error>> {
rng: &mut OsRng,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
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<u8>,
pub fn aes_decrypt(
data: &Vec<u8>,
cipher: &mut AesGcm<Aes256, U12>,
rng: &mut impl RngCore,
) -> Result<Vec<u8>, Box<dyn Error>> {
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
let (nonce_bytes, data) = data.split_at(AES_NONCE_SIZE);
let decrypted = cipher
.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref())