Pārlūkot izejas kodu

small refactor

master
Isabelle L. pirms 5 gadiem
vecāks
revīzija
030ac04ebb
4 mainītis faili ar 13 papildinājumiem un 24 dzēšanām
  1. +2
    -2
      Cargo.toml
  2. +3
    -11
      src/client.rs
  3. +4
    -3
      src/lib.rs
  4. +4
    -8
      src/server.rs

+ 2
- 2
Cargo.toml Parādīt failu

@@ -15,8 +15,8 @@ structopt = "0.3.14"
chrono = "0.4.11"
futures = "0.3.5"
toml = "0.5.6"
ilmp = { git = "https://github.com/izzabelle/ilmp" }
# ilmp = { path = "../ilmp"}
# ilmp = { git = "https://github.com/izzabelle/ilmp" }
ilmp = { path = "../ilmp"}
ring = "0.16.13"
thiserror = "1.0.18"
orion = "0.15.1"


+ 3
- 11
src/client.rs Parādīt failu

@@ -4,12 +4,8 @@ use crate::Result;
use async_std::{io, net::TcpStream, task};
use futures::io::{ReadHalf, WriteHalf};
use futures_util::io::AsyncReadExt;
use ilmp::{
encrypt::{EncryptKind, Encryption, SymmetricEncrypt},
Sendable,
};
use ilmp::{encrypt::SymmetricEncrypt, Sendable};
use lazy_static::lazy_static;
use orion::aead;
use std::sync::Mutex;

lazy_static! {
@@ -47,12 +43,8 @@ pub async fn outgoing(mut write: WriteHalf<TcpStream>, encryption: SymmetricEncr

pub async fn incoming(mut read: ReadHalf<TcpStream>, encryption: SymmetricEncrypt) -> Result<()> {
loop {
let packet = ilmp::read(&mut read).await?;
if let Some(mut packet) = packet {
if packet.encrypt_kind == EncryptKind::Symmetric {
packet.contents = aead::open(encryption.key().unwrap(), &packet.contents)?;
}

let packet = ilmp::read(&mut read, &encryption).await?;
if let Some(packet) = packet {
let res = match packet.kind {
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet),
_ => panic!("bad packet"),


+ 4
- 3
src/lib.rs Parādīt failu

@@ -45,15 +45,16 @@ pub async fn initialize_connection(
ilmp::write(write, agreement_packet, &encrypt::NoEncrypt::new()).await?;

// receive peer's pub key
let packet = ilmp::read(read).await?.unwrap();
let packet = ilmp::read(read, &encrypt::NoEncrypt::new()).await?.unwrap();
let agreement_packet = ilmp::Agreement::from_packet(packet)?;
let peer_pub_key =
agreement::UnparsedPublicKey::new(&agreement::X25519, agreement_packet.public_key);

// generate aead key
agreement::agree_ephemeral(my_priv_key, &peer_pub_key, MsgError::Ring, |key_material| {
let key_material =
digest::digest(&digest::SHA256, key_material.as_ref().into()).as_ref().to_vec();
let key_material = digest::digest(&digest::SHA256, key_material.as_ref().into())
.as_ref()
.to_vec();
Ok(aead::SecretKey::from_slice(&key_material)?)
})
}

+ 4
- 8
src/server.rs Parādīt failu

@@ -11,7 +11,6 @@ use ilmp::encrypt;
use ilmp::encrypt::Encryption;
use ilmp::Sendable;
use lazy_static::lazy_static;
use orion::aead;
use std::collections::HashMap;
use uuid::Uuid;

@@ -58,12 +57,8 @@ async fn handle_stream(
encryption: encrypt::SymmetricEncrypt,
) -> Result<()> {
loop {
let packet = ilmp::read(&mut stream).await?;
if let Some(mut packet) = packet {
if packet.encrypt_kind == encrypt::EncryptKind::Symmetric {
packet.contents = aead::open(encryption.key().unwrap(), &packet.contents)?;
}

let packet = ilmp::read(&mut stream, &encryption).await?;
if let Some(packet) = packet {
let res = match packet.kind {
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet),
_ => panic!("bad packet"),
@@ -80,9 +75,10 @@ async fn handle_stream(
Ok(())
}

async fn relay_packet<T>(packet: T, encryption: &encrypt::SymmetricEncrypt) -> Result<()>
async fn relay_packet<T, E>(packet: T, encryption: &E) -> Result<()>
where
T: Clone + Sendable,
E: Encryption,
{
let mut locked_write_streams = WRITE_STREAMS.lock().await;
let stream = futures::stream::iter(locked_write_streams.iter_mut());


Notiek ielāde…
Atcelt
Saglabāt