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