feat: compat cipher 0.5

This commit is contained in:
zonyitoo
2026-04-27 00:51:42 +08:00
parent f02e277c05
commit 2609729cb0
2 changed files with 30 additions and 12 deletions
@@ -55,7 +55,7 @@ use std::{
use aes::{
Aes128, Aes256, Block,
cipher::{BlockDecrypt, BlockEncrypt, KeyInit},
cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit},
};
use byte_string::ByteStr;
use bytes::{Buf, BufMut, Bytes, BytesMut};
@@ -315,11 +315,17 @@ impl DecryptedReader {
match self.method {
CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {
let cipher = Aes128::new_from_slice(&identity_sub_key[0..16]).expect("AES-128");
cipher.decrypt_block_b2b(Block::from_slice(eih), &mut user_hash);
cipher.decrypt_block_b2b(
<&Block as TryFrom<&[u8]>>::try_from(eih).expect("EIH key length mismatch"),
&mut user_hash,
);
}
CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {
let cipher = Aes256::new_from_slice(&identity_sub_key[0..32]).expect("AES-256");
cipher.decrypt_block_b2b(Block::from_slice(eih), &mut user_hash);
cipher.decrypt_block_b2b(
<&Block as TryFrom<&[u8]>>::try_from(eih).expect("EIH key length mismatch"),
&mut user_hash,
);
}
_ => unreachable!("{} doesn't support EIH", self.method),
}
@@ -58,7 +58,7 @@ use std::{
use aes::{
Aes128, Aes256, Block,
cipher::{BlockDecrypt, BlockEncrypt, KeyInit},
cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit},
};
use byte_string::ByteStr;
use bytes::{Buf, BufMut, Bytes, BytesMut};
@@ -225,12 +225,14 @@ fn encrypt_message(
match method {
CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {
let cipher = Aes128::new_from_slice(ipsk).expect("AES-128 init");
let block = Block::from_mut_slice(packet_header);
let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)
.expect("Packet header length mismatch");
cipher.encrypt_block(block);
}
CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {
let cipher = Aes256::new_from_slice(ipsk).expect("AES-256 init");
let block = Block::from_mut_slice(packet_header);
let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)
.expect("Packet header length mismatch");
cipher.encrypt_block(block);
}
_ => unreachable!("{} is not an AES-*-GCM cipher", method),
@@ -305,12 +307,14 @@ fn decrypt_message(
match method {
CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {
let cipher = Aes128::new_from_slice(key).expect("AES-128 init");
let block = Block::from_mut_slice(packet_header);
let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)
.expect("Packet header length mismatch");
cipher.decrypt_block(block);
}
CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {
let cipher = Aes256::new_from_slice(key).expect("AES-256 init");
let block = Block::from_mut_slice(packet_header);
let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)
.expect("Packet header length mismatch");
cipher.decrypt_block(block);
}
_ => unreachable!("{} is not an AES-*-GCM cipher", method),
@@ -343,11 +347,15 @@ fn decrypt_message(
match method {
CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {
let cipher = Aes128::new_from_slice(key).expect("AES-128 init");
cipher.decrypt_block(Block::from_mut_slice(eih));
cipher.decrypt_block(
<&mut Block as TryFrom<&mut [u8]>>::try_from(eih).expect("EIH key length mismatch"),
);
}
CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {
let cipher = Aes256::new_from_slice(key).expect("AES-256 init");
cipher.decrypt_block(Block::from_mut_slice(eih))
cipher.decrypt_block(
<&mut Block as TryFrom<&mut [u8]>>::try_from(eih).expect("EIH key length mismatch"),
)
}
_ => unreachable!("{} doesn't support EIH", method),
}
@@ -466,11 +474,15 @@ pub fn encrypt_client_payload_aead_2022(
match method {
CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {
let cipher = Aes128::new_from_slice(ipsk).expect("AES-128 init");
cipher.encrypt_block(Block::from_mut_slice(identity_header));
cipher.encrypt_block(
<&mut Block as TryFrom<&mut [u8]>>::try_from(identity_header).expect("EIH key length mismatch"),
);
}
CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {
let cipher = Aes256::new_from_slice(ipsk).expect("AES-256 init");
cipher.encrypt_block(Block::from_mut_slice(identity_header));
cipher.encrypt_block(
<&mut Block as TryFrom<&mut [u8]>>::try_from(identity_header).expect("EIH key length mismatch"),
);
}
_ => unreachable!("{} doesn't support EIH", method),
}