From bc6a7b7d7583172ebfb0b7aea186b5e018ca1f92 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Tue, 26 May 2020 10:06:28 -0500 Subject: [PATCH] key generation agreement working :) --- src/{asymmetric_key.rs => agreement.rs} | 21 +++--- src/encrypt.rs | 85 +++++++++++++++++++++ src/lib.rs | 98 ++----------------------- 3 files changed, 105 insertions(+), 99 deletions(-) rename src/{asymmetric_key.rs => agreement.rs} (54%) create mode 100644 src/encrypt.rs diff --git a/src/asymmetric_key.rs b/src/agreement.rs similarity index 54% rename from src/asymmetric_key.rs rename to src/agreement.rs index b2adf52..817f2c2 100644 --- a/src/asymmetric_key.rs +++ b/src/agreement.rs @@ -1,30 +1,33 @@ use crate::{Packet, PacketKind, Result}; use chrono::prelude::*; use serde::{Deserialize, Serialize}; +use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct AsymmetricKey { +pub struct Agreement { pub timestamp: i64, + pub message_id: u128, pub public_key: Vec, } -impl AsymmetricKey { - pub fn new(public_key: Vec) -> AsymmetricKey { +impl Agreement { + pub fn new(public_key: Vec) -> Agreement { let timestamp = Utc::now().timestamp(); - AsymmetricKey { public_key, timestamp } + let message_id = Uuid::new_v4().as_u128(); + Agreement { timestamp, message_id, public_key } } } -impl crate::Sendable for AsymmetricKey { +impl crate::Sendable for Agreement { fn to_packet(&self, encrypt_kind: crate::EncryptKind) -> Result { let contents: Vec = serde_json::to_string(&self)?.into_bytes(); - let kind = PacketKind::AsymmetricKey; + let kind = PacketKind::Agreement; Ok(Packet::new(kind, contents, encrypt_kind)) } - fn from_packet(packet: Packet) -> Result { + fn from_packet(packet: Packet) -> Result { let contents = &String::from_utf8(packet.contents)?; - let asymmetric_key: AsymmetricKey = serde_json::from_str(contents)?; - Ok(asymmetric_key) + let agreement: Agreement = serde_json::from_str(contents)?; + Ok(agreement) } } diff --git a/src/encrypt.rs b/src/encrypt.rs new file mode 100644 index 0000000..4b6288c --- /dev/null +++ b/src/encrypt.rs @@ -0,0 +1,85 @@ +use crate::Packet; + +/// trait that allows for me to be lazy +pub trait Encryption { + fn kind(&self) -> EncryptKind; + fn key(&self) -> Option>; + fn encrypt(&self, packet: Packet) -> Packet; + fn decrypt(&self, packet: Packet) -> Packet; +} + +/// uses ring's aead module +pub struct SymmetricEncrypt(Vec); + +impl Encryption for SymmetricEncrypt { + fn kind(&self) -> EncryptKind { + EncryptKind::Symmetric + } + + fn key(&self) -> Option> { + Some(self.0.clone()) + } + + fn encrypt(&self, _packet: Packet) -> Packet { + todo!() + } + + fn decrypt(&self, _packet: Packet) -> Packet { + todo!() + } +} + +impl SymmetricEncrypt { + pub fn new(key: Vec) -> SymmetricEncrypt { + SymmetricEncrypt(key) + } +} + +/// literally not encryption whatsoever +pub struct NoEncrypt; + +impl Encryption for NoEncrypt { + fn kind(&self) -> EncryptKind { + EncryptKind::None + } + + // lol + fn key(&self) -> Option> { + None + } + + // lol + fn encrypt(&self, packet: Packet) -> Packet { + packet + } + + // lol + fn decrypt(&self, packet: Packet) -> Packet { + packet + } +} + +impl NoEncrypt { + pub fn new() -> NoEncrypt { + NoEncrypt + } +} + +/// encryption kind +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum EncryptKind { + None = 0x00, + Symmetric = 0xff, +} + +impl EncryptKind { + /// returns `EncryptKind` from u8 if returned value is valid + pub fn from_u8(kind: u8) -> Option { + match kind { + 0x00 => Some(EncryptKind::None), + 0xff => Some(EncryptKind::Symmetric), + _ => None, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 14fb39f..dcb4ede 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,11 +17,14 @@ mod message; pub use message::Message; -mod asymmetric_key; -pub use asymmetric_key::AsymmetricKey; +mod agreement; +pub use agreement::Agreement; +/// encryption types and functions +pub mod encrypt; +use encrypt::{EncryptKind, Encryption}; use futures_util::io::{AsyncReadExt, AsyncWriteExt}; -use ring::{rand, signature::{self, KeyPair},digest}; +use ring::digest; use std::convert::TryInto; use std::marker::Unpin; use thiserror::Error; @@ -117,7 +120,7 @@ impl Packet { #[repr(u8)] pub enum PacketKind { Message = 0x00, - AsymmetricKey = 0xff, + Agreement = 0xff, } impl PacketKind { @@ -125,87 +128,7 @@ impl PacketKind { pub fn from_u8(kind: u8) -> Option { match kind { 0x00 => Some(PacketKind::Message), - 0xff => Some(PacketKind::AsymmetricKey), - _ => None, - } - } -} - -pub trait Encryption { - fn kind(&self) -> EncryptKind; - fn key(&self) -> Option>; -} - -pub struct AsymmetricEncrypt(Vec); - -impl Encryption for AsymmetricEncrypt { - fn kind(&self) -> EncryptKind { - EncryptKind::Asymmetric - } - - fn key(&self) -> Option> { - Some(self.0.clone()) - } -} - -impl AsymmetricEncrypt { - pub fn new(key: Vec) -> AsymmetricEncrypt { - AsymmetricEncrypt(key) - } -} - -pub struct SymmetricEncrypt(Vec); - -impl Encryption for SymmetricEncrypt { - fn kind(&self) -> EncryptKind { - EncryptKind::Symmetric - } - - fn key(&self) -> Option> { - Some(self.0.clone()) - } -} - -impl SymmetricEncrypt { - pub fn new(key: Vec) -> SymmetricEncrypt { - SymmetricEncrypt(key) - } -} - -pub struct NoEncrypt; - -impl Encryption for NoEncrypt { - fn kind(&self) -> EncryptKind { - EncryptKind::None - } - - fn key(&self) -> Option> { - None - } -} - -impl NoEncrypt { - pub fn new() -> NoEncrypt { - NoEncrypt - } -} - -/// encryption kind -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[repr(u8)] -pub enum EncryptKind { - None = 0x00, - Asymmetric = 0x80, - Symmetric = 0xff, -} - -impl EncryptKind { - /// returns `EncryptKind` from u8 if returned value is valid - pub fn from_u8(kind: u8) -> Option { - match kind { - 0x00 => Some(EncryptKind::None), - 0x80 => Some(EncryptKind::Asymmetric), - 0xff => Some(EncryptKind::Symmetric), + 0xff => Some(PacketKind::Agreement), _ => None, } } @@ -272,11 +195,6 @@ where stream.write(&network_packet.0).await?; Ok(()) } - EncryptKind::Asymmetric => { - let - let mut packet = packet; - packet.contents - }, EncryptKind::Symmetric => todo!(), } }