瀏覽代碼

working encryption!

master
Isabelle L. 5 年之前
父節點
當前提交
db63dc576e
共有 4 個檔案被更改,包括 24 行新增8 行删除
  1. +1
    -0
      Cargo.lock
  2. +5
    -3
      src/client.rs
  3. +1
    -1
      src/lib.rs
  4. +17
    -4
      src/server.rs

+ 1
- 0
Cargo.lock 查看文件

@@ -345,6 +345,7 @@ dependencies = [
"crc32fast", "crc32fast",
"futures", "futures",
"futures-util", "futures-util",
"orion",
"ring", "ring",
"serde", "serde",
"serde_json", "serde_json",


+ 5
- 3
src/client.rs 查看文件

@@ -3,6 +3,7 @@ use crate::config::ClientConfig as Config;
use crate::Result; use crate::Result;
use async_std::net::TcpStream; use async_std::net::TcpStream;
use futures_util::io::AsyncReadExt; use futures_util::io::AsyncReadExt;
use ilmp::encrypt;


/// wraps the client /// wraps the client
pub async fn client(port: u16) -> Result<()> { pub async fn client(port: u16) -> Result<()> {
@@ -13,14 +14,15 @@ pub async fn client(port: u16) -> Result<()> {
let (mut read, mut write) = stream.split(); let (mut read, mut write) = stream.split();


let key = crate::initialize_connection(&mut read, &mut write).await?; let key = crate::initialize_connection(&mut read, &mut write).await?;
println!("{:?}", key);
let encryption = encrypt::SymmetricEncrypt::new(key);
println!("successfully hardened connection");


/*let message = ilmp::Message::new(
let message = ilmp::Message::new(
"Isabelle".to_owned(), "Isabelle".to_owned(),
"oh god oh fuck this shit actually works".to_owned(), "oh god oh fuck this shit actually works".to_owned(),
); );


ilmp::write(&mut stream, message, encrypt::NoEncrypt::new()).await?;*/
ilmp::write(&mut write, message, &encryption).await?;


loop {} loop {}
} }

+ 1
- 1
src/lib.rs 查看文件

@@ -42,7 +42,7 @@ pub async fn initialize_connection(
agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng).expect("ring broke"); agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng).expect("ring broke");
let my_pub_key = my_priv_key.compute_public_key().expect("ring broke"); let my_pub_key = my_priv_key.compute_public_key().expect("ring broke");
let agreement_packet = ilmp::Agreement::new(my_pub_key.as_ref().into()); let agreement_packet = ilmp::Agreement::new(my_pub_key.as_ref().into());
ilmp::write(write, agreement_packet, encrypt::NoEncrypt::new()).await?;
ilmp::write(write, agreement_packet, &encrypt::NoEncrypt::new()).await?;


// receive peer's pub key // receive peer's pub key
let packet = ilmp::read(read).await?.unwrap(); let packet = ilmp::read(read).await?.unwrap();


+ 17
- 4
src/server.rs 查看文件

@@ -6,8 +6,11 @@ use async_std::{
}; };
use futures::io::{ReadHalf, WriteHalf}; use futures::io::{ReadHalf, WriteHalf};
use futures_util::{io::AsyncReadExt, stream::StreamExt}; use futures_util::{io::AsyncReadExt, stream::StreamExt};
use ilmp::encrypt;
use ilmp::encrypt::Encryption;
use ilmp::Sendable; use ilmp::Sendable;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use orion::aead;
use std::{collections::HashMap, sync::Mutex}; use std::{collections::HashMap, sync::Mutex};
use uuid::Uuid; use uuid::Uuid;


@@ -32,20 +35,30 @@ pub async fn server(port: u16) -> Result<()> {


let (mut read, mut write) = stream.split(); let (mut read, mut write) = stream.split();
let stream_id = Uuid::new_v4(); let stream_id = Uuid::new_v4();

let key = crate::initialize_connection(&mut read, &mut write).await?; let key = crate::initialize_connection(&mut read, &mut write).await?;
println!("{:?}", key);
let encryption = encrypt::SymmetricEncrypt::new(key);
println!("successfully hardened connection");


WRITE_STREAMS.lock().expect("could not aqcuire lock").insert(stream_id.clone(), write); WRITE_STREAMS.lock().expect("could not aqcuire lock").insert(stream_id.clone(), write);
task::spawn(handle_stream(read, stream_id));
task::spawn(handle_stream(read, stream_id, encryption));
} }


Ok(()) Ok(())
} }


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

let res = match packet.kind { let res = match packet.kind {
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), ilmp::PacketKind::Message => ilmp::Message::from_packet(packet),
_ => panic!("bad packet"), _ => panic!("bad packet"),


Loading…
取消
儲存