chore: bump tc_tea version

This commit is contained in:
鲁树人 2024-09-12 00:51:50 +01:00
parent 3292ad51ea
commit af965ee0cc
3 changed files with 31 additions and 77 deletions

75
Cargo.lock generated
View File

@ -195,6 +195,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.158"
@ -223,15 +229,6 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "ppv-lite86"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
@ -250,36 +247,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "same-file"
version = "1.0.6"
@ -320,12 +287,12 @@ dependencies = [
[[package]]
name = "tc_tea"
version = "0.1.4"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab26285e70ee5cbec8582c76b8124dfe65b564b43dad14b9caca6fd127d66d2"
checksum = "0e0fb162058ad530c29c7e283ae3ca72d5188d8e7275c079f7622b3e053a547b"
dependencies = [
"rand",
"rand_chacha",
"byteorder",
"thiserror",
]
[[package]]
@ -391,6 +358,7 @@ dependencies = [
"anyhow",
"byteorder",
"itertools",
"lazy_static",
"tc_tea",
"thiserror",
"umc_utils",
@ -624,24 +592,3 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -7,6 +7,7 @@ edition = "2021"
anyhow = "1.0.86"
byteorder = "1.5.0"
itertools = "0.13.0"
tc_tea = { version = "0.1.4", default-features = false, features = [] }
lazy_static = "1.5.0"
tc_tea = { version = "0.2.0", default-features = false }
thiserror = "1.0.63"
umc_utils = { path = "../utils" }

View File

@ -1,6 +1,8 @@
use anyhow::Result;
use itertools::Itertools;
use lazy_static::lazy_static;
use std::ops::Mul;
use tc_tea::TcTeaError;
use thiserror::Error;
use umc_utils::base64;
@ -13,14 +15,18 @@ const EKEY_V2_KEY2: [u8; 16] = [
0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25, 0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54,
];
#[derive(Debug, Clone, PartialEq, Error)]
lazy_static! {
static ref EKEY_SIMPLE_KEY: [u8; 8] = make_simple_key::<8>();
}
#[derive(Debug, PartialEq, Error)]
pub enum EKeyDecryptError {
#[error("EKey is too short for decryption")]
EKeyTooShort,
#[error("Error when decrypting ekey v1")]
FailDecryptV1,
#[error("Error when decrypting ekey v2")]
FailDecryptV2,
#[error("Error when decrypting ekey v1: {0}")]
FailDecryptV1(TcTeaError),
#[error("Error when decrypting ekey v2: {0}")]
FailDecryptV2(TcTeaError),
}
fn make_simple_key<const N: usize>() -> [u8; N] {
@ -44,21 +50,21 @@ pub fn decrypt_v1(ekey: &[u8]) -> Result<Box<[u8]>> {
let ekey = base64::decode(ekey)?;
let (header, cipher) = ekey.split_at(8);
let simple_key = make_simple_key::<8>();
let tea_key = simple_key
// tea_key: interleave a byte from each stream
let tea_key = EKEY_SIMPLE_KEY
.iter()
.zip(header)
.flat_map(|(&simple_part, &header_part)| [simple_part, header_part])
.collect::<Vec<_>>();
.flat_map(|(&simple_key_part, &header_part)| [simple_key_part, header_part])
.collect_vec();
let plaintext = tc_tea::decrypt(cipher, tea_key).ok_or(EKeyDecryptError::FailDecryptV1)?;
let plaintext = tc_tea::decrypt(cipher, tea_key).map_err(EKeyDecryptError::FailDecryptV1)?;
Ok([header, &plaintext].concat().into())
}
pub fn decrypt_v2(ekey: &[u8]) -> Result<Box<[u8]>> {
let ekey = base64::decode(ekey)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY1).ok_or(EKeyDecryptError::FailDecryptV2)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY2).ok_or(EKeyDecryptError::FailDecryptV2)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY1).map_err(EKeyDecryptError::FailDecryptV2)?;
let ekey = tc_tea::decrypt(ekey, EKEY_V2_KEY2).map_err(EKeyDecryptError::FailDecryptV2)?;
let ekey = ekey.iter().take_while(|&&b| b != 0).copied().collect_vec();
decrypt_v1(&ekey)