diff --git a/Cargo.lock b/Cargo.lock index 3b19e4b..d2e4d9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,18 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "aes" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8" -dependencies = [ - "cfg-if", - "cipher 0.3.0", - "cpufeatures", - "opaque-debug", -] - [[package]] name = "aes" version = "0.8.3" @@ -36,7 +24,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2" dependencies = [ "cfg-if", - "cipher 0.4.4", + "cipher", "cpufeatures", ] @@ -190,7 +178,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -199,7 +187,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -226,7 +214,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" dependencies = [ - "cipher 0.4.4", + "cipher", ] [[package]] @@ -246,11 +234,11 @@ checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" [[package]] name = "cfb8" -version = "0.7.1" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3a4b6c43bf284e617a659ce5dc149676680530a3a4a9bb6b278d1a9ed5b229d" +checksum = "014c0a0e1ad0dae6a86c082db2f9bd7fe8c2c734227047d0d8b4d4a3a094a1e1" dependencies = [ - "cipher 0.3.0", + "cipher", ] [[package]] @@ -259,15 +247,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cipher" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7" -dependencies = [ - "generic-array", -] - [[package]] name = "cipher" version = "0.4.4" @@ -448,7 +427,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -726,6 +705,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.10" @@ -932,7 +920,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" dependencies = [ "block-padding", - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -1253,12 +1241,6 @@ dependencies = [ "parking_lot_core", ] -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - [[package]] name = "openssl" version = "0.10.57" @@ -1380,7 +1362,7 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e847e2c91a18bfa887dd028ec33f2fe6f25db77db3619024764914affe8b69a6" dependencies = [ - "aes 0.8.3", + "aes", "cbc", "der", "pbkdf2", @@ -1650,7 +1632,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97a22f5af31f73a954c10289c93e8a50cc23d971e80ee446f1f6f7137a088213" dependencies = [ - "cipher 0.4.4", + "cipher", ] [[package]] @@ -1886,7 +1868,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" name = "swarm-bot" version = "0.4.1" dependencies = [ - "aes 0.7.5", + "aes", "anyhow", "assert_matches", "async-trait", @@ -1901,6 +1883,7 @@ dependencies = [ "flate2", "float-ord", "futures", + "generic-array 1.0.0", "hematite-nbt", "hex-literal", "indexmap 2.0.2", @@ -1928,6 +1911,7 @@ dependencies = [ "tokio-tungstenite", "trust-dns-resolver", "tungstenite", + "typenum", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c841ff2..7a05e76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"} @@ -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" diff --git a/src/protocol/io/mod.rs b/src/protocol/io/mod.rs index 397824f..3190285 100644 --- a/src/protocol/io/mod.rs +++ b/src/protocol/io/mod.rs @@ -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; +type AesEncrypt = cfb8::Encryptor; +type AesDecrypt = cfb8::Decryptor; +type Cfb8Block = crypto_common::Block; /// /// /// as per 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::() }; + + 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::() }; + + debug_assert!(prefix.is_empty()); + debug_assert!(suffix.is_empty()); + + self.decryptor.decrypt_blocks_mut(blocks); } }