aes/cfb8 works now
This commit is contained in:
parent
9be71312ab
commit
6a58dbf184
|
@ -37,14 +37,23 @@ impl McCipher {
|
|||
) -> Result<Self> {
|
||||
let aes_key: [u8; 16] = decrypt_rsa(private_key, data)?
|
||||
.as_slice()[0..16].try_into().unwrap();
|
||||
let mut key: [u8; 16] =
|
||||
vec![0;16].as_slice()[0..16].try_into().unwrap();
|
||||
key.copy_from_slice(&aes_key);
|
||||
let mut state_en: [u8; 16] =
|
||||
vec![0;16].as_slice()[0..16].try_into().unwrap();
|
||||
state_en.copy_from_slice(&aes_key);
|
||||
let mut state_de: [u8; 16] =
|
||||
vec![0;16].as_slice()[0..16].try_into().unwrap();
|
||||
state_de.copy_from_slice(&aes_key);
|
||||
Ok(Self {
|
||||
key: aes_key.clone(),
|
||||
state_en: aes_key.clone(),
|
||||
state_de: aes_key.clone(),
|
||||
key,
|
||||
state_en,
|
||||
state_de,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn encrypt_aes(&self, data: Vec<u8>) -> Vec<u8> {
|
||||
pub fn encrypt_aes(&mut self, data: Vec<u8>) -> Vec<u8> {
|
||||
let mut out_data = vec![0; data.len()];
|
||||
for i in 0..data.len() {
|
||||
out_data[i] = self.encrypt_block(data[i]);
|
||||
|
@ -52,7 +61,7 @@ impl McCipher {
|
|||
out_data
|
||||
}
|
||||
|
||||
pub fn decrypt_aes(&self, data: Vec<u8>) -> Vec<u8> {
|
||||
pub fn decrypt_aes(&mut self, data: Vec<u8>) -> Vec<u8> {
|
||||
let mut out_data = vec![0; data.len()];
|
||||
for i in 0..data.len() {
|
||||
out_data[i] = self.decrypt_block(data[i]);
|
||||
|
@ -60,28 +69,32 @@ impl McCipher {
|
|||
out_data
|
||||
}
|
||||
|
||||
fn shift_left(mut arr: [u8; 16], new: u8) {
|
||||
for i in 1..arr.len() {
|
||||
arr[i] = arr[i - 1];
|
||||
fn shift_left(arr: [u8; 16], new: u8) -> [u8; 16] {
|
||||
let mut arr = arr;
|
||||
for i in 0..arr.len() - 1 {
|
||||
arr[i] = arr[i + 1];
|
||||
}
|
||||
arr[0] = new;
|
||||
arr[15] = new;
|
||||
arr
|
||||
}
|
||||
|
||||
fn encrypt_block(&self, data: u8) -> u8 {
|
||||
fn encrypt_block(&mut self, data: u8) -> u8 {
|
||||
let cipher = Aes128::new(GenericArray::from_slice(&self.key));
|
||||
let mut block = GenericArray::clone_from_slice(&self.state_en);
|
||||
cipher.encrypt_block(&mut block);
|
||||
let data = data ^ block[15];
|
||||
Self::shift_left(self.state_en, data);
|
||||
let data = data ^ block[0];
|
||||
self.state_en = Self::shift_left(self.state_en, data);
|
||||
assert_ne!(self.state_en, self.key);
|
||||
assert_ne!(self.state_en, self.state_de);
|
||||
data
|
||||
}
|
||||
|
||||
fn decrypt_block(&self, data: u8) -> u8 {
|
||||
fn decrypt_block(&mut self, data: u8) -> u8 {
|
||||
let cipher = Aes128::new(GenericArray::from_slice(&self.key));
|
||||
let mut block = GenericArray::clone_from_slice(&self.state_de);
|
||||
cipher.decrypt_block(&mut block);
|
||||
Self::shift_left(self.state_de, data);
|
||||
let data = data ^ block[15];
|
||||
self.state_de = Self::shift_left(self.state_de, data);
|
||||
let data = data ^ block[0];
|
||||
data
|
||||
}
|
||||
}
|
||||
|
|
|
@ -235,7 +235,7 @@ unsafe impl<'a> Send for ProtocolConnection<'a> {}
|
|||
#[async_trait]
|
||||
impl<'a> ProtocolRead for ProtocolConnection<'a> {
|
||||
async fn read_data(&mut self) -> Result<Vec<u8>> {
|
||||
match &self.aes_cipher {
|
||||
match &mut self.aes_cipher {
|
||||
Some(aes_cipher) => {
|
||||
let length = read_var_int_stream_encrypted(
|
||||
self.stream_read, aes_cipher).await? as usize;
|
||||
|
@ -262,7 +262,7 @@ impl<'a> ProtocolWrite for ProtocolConnection<'a> {
|
|||
async fn write_data(&mut self, data: &mut Vec<u8>) -> Result<()> {
|
||||
let mut out_data = convert_var_int(data.len() as i32);
|
||||
out_data.append(data);
|
||||
match &self.aes_cipher {
|
||||
match &mut self.aes_cipher {
|
||||
Some(aes_cipher) => {
|
||||
self.stream_write.write_all(
|
||||
&aes_cipher.encrypt_aes(out_data)).await?;
|
||||
|
@ -301,7 +301,7 @@ impl<'a> ProtocolWrite for WriteHaftProtocolConnection<'a> {
|
|||
async fn write_data(&mut self, data: &mut Vec<u8>) -> Result<()> {
|
||||
let mut out_data = convert_var_int(data.len() as i32);
|
||||
out_data.append(data);
|
||||
match &self.aes_cipher {
|
||||
match &mut self.aes_cipher {
|
||||
Some(aes_cipher) => {
|
||||
self.stream_write.write_all(
|
||||
&aes_cipher.encrypt_aes(out_data)).await?;
|
||||
|
@ -350,7 +350,7 @@ unsafe impl<'a> Send for ReadHaftProtocolConnection<'a> {}
|
|||
#[async_trait]
|
||||
impl<'a> ProtocolRead for ReadHaftProtocolConnection<'a> {
|
||||
async fn read_data(&mut self) -> Result<Vec<u8>> {
|
||||
match &self.aes_cipher {
|
||||
match &mut self.aes_cipher {
|
||||
Some(aes_cipher) => {
|
||||
let length = read_var_int_stream_encrypted(
|
||||
self.stream_read, aes_cipher).await? as usize;
|
||||
|
@ -412,17 +412,17 @@ async fn read_var_int_stream(stream: &mut OwnedReadHalf) -> Result<i32> {
|
|||
}
|
||||
async fn read_var_int_stream_encrypted(
|
||||
stream: &mut OwnedReadHalf,
|
||||
cipher: &McCipher,
|
||||
cipher: &mut McCipher,
|
||||
) -> Result<i32> {
|
||||
let mut data: Vec<u8> = vec![];
|
||||
|
||||
loop {
|
||||
let encrypted_byte = stream.read_u8().await?;
|
||||
let mut current_byte = cipher.decrypt_aes(vec![encrypted_byte]);
|
||||
let current_byte = cipher.decrypt_aes(vec![encrypted_byte])[0];
|
||||
|
||||
data.append(&mut current_byte);
|
||||
data.append(&mut vec![current_byte]);
|
||||
|
||||
if (current_byte[0] & CONTINUE_BIT) == 0 {
|
||||
if (current_byte & CONTINUE_BIT) == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue