Skip to content

Commit

Permalink
🚀 feat(gear): Optimize data type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jan 4, 2024
1 parent c357dcd commit 1fe4e4e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/utils/decryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ring::aead::UnboundKey;
use ring::aead::AES_128_GCM;
use ring::error::Unspecified;

use super::gear::RandNonceSequence;
use super::gear::AbsoluteNonceSequence;

pub fn decrypt_bytes(
cipherbytes: Vec<u8>,
Expand All @@ -15,7 +15,7 @@ pub fn decrypt_bytes(
) -> Result<Vec<u8>, Unspecified> {
// 使用 AES_KEY 加密
let unbound_key = UnboundKey::new(&AES_128_GCM, &aes_key)?;
let nonce_sequence = RandNonceSequence::new(nonce.to_vec());
let nonce_sequence = AbsoluteNonceSequence::new(nonce.to_vec());

let mut opening_key = OpeningKey::new(unbound_key, nonce_sequence);
let mut in_out = [cipherbytes.clone(), tag.to_vec()].concat(); // 复制一份
Expand Down
6 changes: 2 additions & 4 deletions src/utils/encryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ use ring::rand::SystemRandom;

use crate::exceptions::OblivionException;

use super::gear::RandNonceSequence;
use super::gear::AbsoluteNonceSequence;

/// Encrypt plaintext using AES
///
/// `encrypt_messgae`是`encrypt_bytes`
pub fn encrypt_plaintext(
string: String,
aes_key: &[u8],
Expand All @@ -38,7 +36,7 @@ pub fn encrypt_bytes(
let rand = SystemRandom::new();
rand.fill(&mut nonce_bytes).unwrap();

let nonce_sequence = RandNonceSequence::new(nonce_bytes.clone());
let nonce_sequence = AbsoluteNonceSequence::new(nonce_bytes.clone());
let mut sealing_key = SealingKey::new(unbound_key, nonce_sequence);

let associated_data = Aad::empty();
Expand Down
46 changes: 26 additions & 20 deletions src/utils/gear.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Oblivion Abstract Gear
use crate::exceptions::OblivionException;
use ring::{
aead::{Nonce, NonceSequence},
Expand All @@ -8,22 +9,31 @@ use tokio::{
net::TcpStream,
};

pub struct RandNonceSequence {
/// Absolute Nonce Sequence Structure
///
/// This structure is used to pass in pre-generated Nonce directly.
///
/// Warning: this is not a generalized generation scheme and should not be used in production environments,
/// you should make sure that the Nonce you pass in is a sufficiently garbled byte string.
pub struct AbsoluteNonceSequence {
nonce: Vec<u8>,
}

impl NonceSequence for RandNonceSequence {
impl NonceSequence for AbsoluteNonceSequence {
fn advance(&mut self) -> Result<Nonce, Unspecified> {
Nonce::try_assume_unique_for_key(&self.nonce)
}
}

impl RandNonceSequence {
impl AbsoluteNonceSequence {
pub fn new(nonce: Vec<u8>) -> Self {
Self { nonce: nonce }
}
}

/// Socket Abstract Structure
///
/// Used to abstract Oblivion's handling of transmitted data, wrapping all data type conversions.
pub struct Socket {
tcp: TcpStream,
}
Expand All @@ -44,15 +54,13 @@ impl Socket {
Err(_) => return Err(OblivionException::UnexpectedDisconnection),
};

let len_int: i32 = match std::str::from_utf8(&len_bytes) {
Ok(len_int) => len_int,
match std::str::from_utf8(&len_bytes) {
Ok(len_int) => match len_int.parse() {
Ok(len) => Ok(len),
Err(_) => Err(OblivionException::BadBytes),
},
Err(_) => return Err(OblivionException::BadBytes),
}
.parse()
.expect("Failed to receieve length");

let len: usize = len_int.try_into().expect("Failed to generate unsize value");
Ok(len)
}

pub async fn recv_int(&mut self, len: usize) -> Result<i32, OblivionException> {
Expand All @@ -62,23 +70,21 @@ impl Socket {
Err(_) => return Err(OblivionException::UnexpectedDisconnection),
};

let int: i32 = match std::str::from_utf8(&len_bytes) {
Ok(len_int) => len_int,
match std::str::from_utf8(&len_bytes) {
Ok(len_int) => match len_int.parse() {
Ok(len) => Ok(len),
Err(_) => Err(OblivionException::BadBytes),
},
Err(_) => return Err(OblivionException::BadBytes),
}
.parse()
.expect("Failed to receieve length");

Ok(int)
}

pub async fn recv(&mut self, len: usize) -> Result<Vec<u8>, OblivionException> {
let mut recv_bytes: Vec<u8> = vec![0; len];
match self.tcp.read_exact(&mut recv_bytes).await {
Ok(_) => {}
Err(_) => return Err(OblivionException::UnexpectedDisconnection),
};
Ok(recv_bytes)
Ok(_) => Ok(recv_bytes),
Err(_) => Err(OblivionException::UnexpectedDisconnection),
}
}

pub async fn recv_str(&mut self, len: usize) -> Result<String, OblivionException> {
Expand Down
1 change: 1 addition & 0 deletions src/utils/generator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Oblivion Generator
extern crate rand;
extern crate ring;

Expand Down

0 comments on commit 1fe4e4e

Please sign in to comment.