mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-10 06:41:30 +11:00
update aes and cfb8 to 0.8 (#340)
- update aes and cfb8 and make it compile - update some usages to fix lints - fix it so that it actually works - small refactor - fix lint ## Description This updates the aes and cfb8 dependencies to 0.8 fixes #42
This commit is contained in:
parent
ad1f5d1b4c
commit
67dced53e0
|
@ -11,7 +11,7 @@ documentation = "https://docs.rs/valence/"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
aes = "0.7.5"
|
aes = "0.8.2"
|
||||||
anyhow = "1.0.70"
|
anyhow = "1.0.70"
|
||||||
approx = "0.5.1"
|
approx = "0.5.1"
|
||||||
arrayvec = "0.7.2"
|
arrayvec = "0.7.2"
|
||||||
|
@ -26,7 +26,7 @@ bitfield-struct = "0.3.1"
|
||||||
byteorder = "1.4.3"
|
byteorder = "1.4.3"
|
||||||
bytes = "1.2.1"
|
bytes = "1.2.1"
|
||||||
cesu8 = "1.1.0"
|
cesu8 = "1.1.0"
|
||||||
cfb8 = "0.7.1"
|
cfb8 = "0.8.1"
|
||||||
clap = { version = "4.0.30", features = ["derive"] }
|
clap = { version = "4.0.30", features = ["derive"] }
|
||||||
criterion = "0.4.0"
|
criterion = "0.4.0"
|
||||||
directories = "5.0.0"
|
directories = "5.0.0"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
use aes::cipher::{AsyncStreamCipher, NewCipher};
|
use aes::cipher::generic_array::GenericArray;
|
||||||
|
#[cfg(feature = "encryption")]
|
||||||
|
use aes::cipher::{BlockDecryptMut, BlockSizeUser, KeyIvInit};
|
||||||
use anyhow::{bail, ensure};
|
use anyhow::{bail, ensure};
|
||||||
use bytes::{Buf, BytesMut};
|
use bytes::{Buf, BytesMut};
|
||||||
|
|
||||||
|
@ -9,7 +11,7 @@ use crate::packet::{Packet, MAX_PACKET_SIZE};
|
||||||
/// The AES block cipher with a 128 bit key, using the CFB-8 mode of
|
/// The AES block cipher with a 128 bit key, using the CFB-8 mode of
|
||||||
/// operation.
|
/// operation.
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
type Cipher = cfb8::Cfb8<aes::Aes128>;
|
type Cipher = cfb8::Decryptor<aes::Aes128>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PacketDecoder {
|
pub struct PacketDecoder {
|
||||||
|
@ -128,20 +130,29 @@ impl PacketDecoder {
|
||||||
pub fn enable_encryption(&mut self, key: &[u8; 16]) {
|
pub fn enable_encryption(&mut self, key: &[u8; 16]) {
|
||||||
assert!(self.cipher.is_none(), "encryption is already enabled");
|
assert!(self.cipher.is_none(), "encryption is already enabled");
|
||||||
|
|
||||||
let mut cipher = Cipher::new(key.into(), key.into());
|
let mut cipher = Cipher::new_from_slices(key, key).expect("invalid key");
|
||||||
|
|
||||||
// Don't forget to decrypt the data we already have.
|
// Don't forget to decrypt the data we already have.
|
||||||
cipher.decrypt(&mut self.buf);
|
Self::decrypt_bytes(&mut cipher, &mut self.buf);
|
||||||
|
|
||||||
self.cipher = Some(cipher);
|
self.cipher = Some(cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Decrypts the provided byte slice in place using the cipher, without consuming the cipher.
|
||||||
|
#[cfg(feature = "encryption")]
|
||||||
|
fn decrypt_bytes(cipher: &mut Cipher, bytes: &mut [u8]) {
|
||||||
|
for chunk in bytes.chunks_mut(Cipher::block_size()) {
|
||||||
|
let gen_arr = GenericArray::from_mut_slice(chunk);
|
||||||
|
cipher.decrypt_block_mut(gen_arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn queue_bytes(&mut self, mut bytes: BytesMut) {
|
pub fn queue_bytes(&mut self, mut bytes: BytesMut) {
|
||||||
#![allow(unused_mut)]
|
#![allow(unused_mut)]
|
||||||
|
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
if let Some(cipher) = &mut self.cipher {
|
if let Some(cipher) = &mut self.cipher {
|
||||||
cipher.decrypt(&mut bytes);
|
Self::decrypt_bytes(cipher, &mut bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.buf.unsplit(bytes);
|
self.buf.unsplit(bytes);
|
||||||
|
@ -155,7 +166,8 @@ impl PacketDecoder {
|
||||||
|
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
if let Some(cipher) = &mut self.cipher {
|
if let Some(cipher) = &mut self.cipher {
|
||||||
cipher.decrypt(&mut self.buf[len..]);
|
let slice = &mut self.buf[len..];
|
||||||
|
Self::decrypt_bytes(cipher, slice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
#[cfg(feature = "encryption")]
|
||||||
|
use aes::cipher::generic_array::GenericArray;
|
||||||
|
#[cfg(feature = "encryption")]
|
||||||
|
use aes::cipher::{BlockEncryptMut, BlockSizeUser, KeyIvInit};
|
||||||
use anyhow::ensure;
|
use anyhow::ensure;
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
@ -10,7 +14,7 @@ use crate::packet::{Encode, Packet, MAX_PACKET_SIZE};
|
||||||
/// The AES block cipher with a 128 bit key, using the CFB-8 mode of
|
/// The AES block cipher with a 128 bit key, using the CFB-8 mode of
|
||||||
/// operation.
|
/// operation.
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
type Cipher = cfb8::Cfb8<aes::Aes128>;
|
type Cipher = cfb8::Encryptor<aes::Aes128>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PacketEncoder {
|
pub struct PacketEncoder {
|
||||||
|
@ -145,9 +149,10 @@ impl PacketEncoder {
|
||||||
pub fn take(&mut self) -> BytesMut {
|
pub fn take(&mut self) -> BytesMut {
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
if let Some(cipher) = &mut self.cipher {
|
if let Some(cipher) = &mut self.cipher {
|
||||||
use aes::cipher::AsyncStreamCipher;
|
for chunk in self.buf.chunks_mut(Cipher::block_size()) {
|
||||||
|
let gen_arr = GenericArray::from_mut_slice(chunk);
|
||||||
cipher.encrypt(&mut self.buf);
|
cipher.encrypt_block_mut(gen_arr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.buf.split()
|
self.buf.split()
|
||||||
|
@ -168,10 +173,8 @@ impl PacketEncoder {
|
||||||
/// [taken]: Self::take
|
/// [taken]: Self::take
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
pub fn enable_encryption(&mut self, key: &[u8; 16]) {
|
pub fn enable_encryption(&mut self, key: &[u8; 16]) {
|
||||||
use aes::cipher::NewCipher;
|
|
||||||
|
|
||||||
assert!(self.cipher.is_none(), "encryption is already enabled");
|
assert!(self.cipher.is_none(), "encryption is already enabled");
|
||||||
self.cipher = Some(NewCipher::new(key.into(), key.into()));
|
self.cipher = Some(Cipher::new_from_slices(key, key).expect("invalid key"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue