Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
update encryption crate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Oct 22, 2023
1 parent 77ffbdb commit beaf72f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
62 changes: 23 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tokio = { version = "1.33", features = [
trust-dns-resolver = "0.23"

# encryption
aes = "0.7.5"
aes = "0.8.3"

# zlib
#flate2 = {version = "1.0"}
Expand Down Expand Up @@ -110,11 +110,13 @@ interfaces = { package = "swarmbot-interfaces", path = "interfaces", version = "
anyhow = "1.0.75"
bincode = "2.0.0-rc.3"
hex-literal = "0.4.1"
cfb8 = "0.7"
cfb8 = "0.8"
tokio-stream = "0.1.14"
once_cell = { version = "1.18.0", features = ["parking_lot"] }
async-trait = "0.1.74"
tungstenite = "0.20.1"
generic-array = "1.0.0"
typenum = "1.17.0"

[dev-dependencies]
assert_matches = "1.5"
Expand Down
35 changes: 27 additions & 8 deletions src/protocol/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,56 @@
use std::io::{Read, Write};

use aes::{cipher::NewCipher, Aes128};
use cfb8::cipher::AsyncStreamCipher;
use aes::{
cipher::{crypto_common, BlockDecryptMut, BlockEncryptMut, KeyIvInit},
Aes128,
};
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};

pub mod reader;
pub mod writer;

type AesEncrypt = cfb8::Cfb8<Aes128>;
type AesEncrypt = cfb8::Encryptor<Aes128>;
type AesDecrypt = cfb8::Decryptor<Aes128>;
type Cfb8Block = crypto_common::Block<AesEncrypt>;

/// <https://github.com/RustCrypto/block-ciphers/issues/28>
/// <https://docs.rs/cfb-mode/0.7.1/cfb_mode/>
/// as per <https://wiki.vg/Protocol_Encryption#Symmetric_Encryption> the key and iv are the same
struct Aes {
cipher: AesEncrypt,
encryptor: AesEncrypt,
decryptor: AesDecrypt,
}

impl Aes {
pub fn new(key: &[u8]) -> Self {
let iv = key;

let cipher = AesEncrypt::new_from_slices(key, iv).unwrap();
let encryptor = AesEncrypt::new_from_slices(key, iv).unwrap();
let decryptor = AesDecrypt::new_from_slices(key, iv).unwrap();

Self { cipher }
Self {
encryptor,
decryptor,
}
}

pub fn encrypt(&mut self, elem: &mut [u8]) {
self.cipher.encrypt(elem);
let (prefix, blocks, suffix) = unsafe { elem.align_to_mut::<Cfb8Block>() };

debug_assert!(prefix.is_empty());
debug_assert!(suffix.is_empty());

self.encryptor.encrypt_blocks_mut(blocks);
}

pub fn decrypt(&mut self, elem: &mut [u8]) {
self.cipher.decrypt(elem);
let (prefix, blocks, suffix) = unsafe { elem.align_to_mut::<Cfb8Block>() };

debug_assert!(prefix.is_empty());
debug_assert!(suffix.is_empty());

self.decryptor.decrypt_blocks_mut(blocks);
}
}

Expand Down

0 comments on commit beaf72f

Please sign in to comment.