Browse Source

working encryption!

master
Isabelle L. 5 years ago
parent
commit
841d72a306
3 changed files with 23 additions and 10 deletions
  1. +1
    -0
      Cargo.toml
  2. +7
    -6
      src/encrypt.rs
  3. +15
    -4
      src/lib.rs

+ 1
- 0
Cargo.toml View File

@@ -15,3 +15,4 @@ ring = "0.16.13"
anyhow = "1.0.30" anyhow = "1.0.30"
thiserror = "1.0.17" thiserror = "1.0.17"
crc32fast = "1.2.0" crc32fast = "1.2.0"
orion = "0.15.1"

+ 7
- 6
src/encrypt.rs View File

@@ -1,23 +1,24 @@
use crate::Packet; use crate::Packet;
use orion::aead::SecretKey;


/// trait that allows for me to be lazy /// trait that allows for me to be lazy
pub trait Encryption { pub trait Encryption {
fn kind(&self) -> EncryptKind; fn kind(&self) -> EncryptKind;
fn key(&self) -> Option<Vec<u8>>;
fn key(&self) -> Option<&SecretKey>;
fn encrypt(&self, packet: Packet) -> Packet; fn encrypt(&self, packet: Packet) -> Packet;
fn decrypt(&self, packet: Packet) -> Packet; fn decrypt(&self, packet: Packet) -> Packet;
} }


/// uses ring's aead module /// uses ring's aead module
pub struct SymmetricEncrypt(Vec<u8>);
pub struct SymmetricEncrypt(SecretKey);


impl Encryption for SymmetricEncrypt { impl Encryption for SymmetricEncrypt {
fn kind(&self) -> EncryptKind { fn kind(&self) -> EncryptKind {
EncryptKind::Symmetric EncryptKind::Symmetric
} }


fn key(&self) -> Option<Vec<u8>> {
Some(self.0.clone())
fn key(&self) -> Option<&SecretKey> {
Some(&self.0)
} }


fn encrypt(&self, _packet: Packet) -> Packet { fn encrypt(&self, _packet: Packet) -> Packet {
@@ -30,7 +31,7 @@ impl Encryption for SymmetricEncrypt {
} }


impl SymmetricEncrypt { impl SymmetricEncrypt {
pub fn new(key: Vec<u8>) -> SymmetricEncrypt {
pub fn new(key: SecretKey) -> SymmetricEncrypt {
SymmetricEncrypt(key) SymmetricEncrypt(key)
} }
} }
@@ -44,7 +45,7 @@ impl Encryption for NoEncrypt {
} }


// lol // lol
fn key(&self) -> Option<Vec<u8>> {
fn key(&self) -> Option<&SecretKey> {
None None
} }




+ 15
- 4
src/lib.rs View File

@@ -24,6 +24,7 @@ pub mod encrypt;


use encrypt::{EncryptKind, Encryption}; use encrypt::{EncryptKind, Encryption};
use futures_util::io::{AsyncReadExt, AsyncWriteExt}; use futures_util::io::{AsyncReadExt, AsyncWriteExt};
use orion::aead;
use ring::digest; use ring::digest;
use std::convert::TryInto; use std::convert::TryInto;
use std::marker::Unpin; use std::marker::Unpin;
@@ -43,8 +44,8 @@ pub trait Sendable: Sized {
pub struct Packet { pub struct Packet {
pub kind: PacketKind, pub kind: PacketKind,
pub encrypt_kind: EncryptKind, pub encrypt_kind: EncryptKind,
integrity_hash: Vec<u8>,
contents: Vec<u8>,
pub integrity_hash: Vec<u8>,
pub contents: Vec<u8>,
} }


impl Packet { impl Packet {
@@ -148,6 +149,8 @@ pub enum IlmpError {
SerdeJson(#[from] serde_json::error::Error), SerdeJson(#[from] serde_json::error::Error),
#[error("string parsing error")] #[error("string parsing error")]
StringParse(#[from] std::string::FromUtf8Error), StringParse(#[from] std::string::FromUtf8Error),
#[error("orion error")]
Orion(#[from] orion::errors::UnknownCryptoError),
} }


/// reads a `Packet` from a stream /// reads a `Packet` from a stream
@@ -183,7 +186,7 @@ where
} }


/// writes a `Sendable` packet to a stream /// writes a `Sendable` packet to a stream
pub async fn write<S, P, E>(stream: &mut S, packet: P, encryption: E) -> Result<()>
pub async fn write<S, P, E>(stream: &mut S, packet: P, encryption: &E) -> Result<()>
where where
S: AsyncWriteExt + Unpin, S: AsyncWriteExt + Unpin,
P: Sendable, P: Sendable,
@@ -195,6 +198,14 @@ where
stream.write(&network_packet.0).await?; stream.write(&network_packet.0).await?;
Ok(()) Ok(())
} }
EncryptKind::Symmetric => todo!(),
EncryptKind::Symmetric => {
let mut packet = packet.to_packet(encryption.kind())?;
packet.contents = aead::seal(encryption.key().unwrap(), &packet.contents)?;
packet.integrity_hash =
digest::digest(&digest::SHA256, &packet.contents).as_ref().to_vec();
let network_packet = packet.to_network_packet();
stream.write(&network_packet.0).await?;
Ok(())
}
} }
} }

Loading…
Cancel
Save