Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text asset decryption #48

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github: nicks96432
ko_fi: nicks96432
patreon: nicks96432
custom:
- "https://www.paypal.me/nicks96432"
86 changes: 11 additions & 75 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions extract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ version = "0.1.0"
# For decoding ACB audio
acb = { path = "../acb" }

# For text asset decryption
aes = "0.8.3"
cbc = "0.1.2"

# For reading numbers from binary data
byteorder = "1.5.0"

Expand Down
4 changes: 2 additions & 2 deletions extract/src/class/asset_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rabex::read_ext::ReadUrexExt;
use crate::utils::ReadAlignedExt;
use crate::version::*;

pub fn _construct_p_ptr<R, E>(
pub(super) fn _construct_p_ptr<R, E>(
reader: &mut R,
serialized_file: &SerializedFile,
) -> Result<PPtr, Box<dyn Error>>
Expand Down Expand Up @@ -94,7 +94,7 @@ where
Ok(asset_info)
}

pub fn _construct_asset_bundle<E>(
pub(super) fn _construct_asset_bundle<E>(
data: &[u8],
serialized_file: &SerializedFile,
) -> Result<AssetBundle, Box<dyn Error>>
Expand Down
2 changes: 1 addition & 1 deletion extract/src/class/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::version::*;
use super::asset_bundle::_construct_p_ptr;
use super::mesh::{construct_sub_mesh, construct_vertex_data};

pub fn _construct_sprite<E>(
pub(super) fn _construct_sprite<E>(
data: &[u8],
serialized_file: &SerializedFile,
) -> Result<Sprite, Box<dyn Error>>
Expand Down
67 changes: 61 additions & 6 deletions extract/src/class/text_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ use std::path::Path;
use std::slice::from_raw_parts;
use std::str::FromStr;

use byteorder::BigEndian;
use byteorder::ByteOrder;
use byteorder::LittleEndian;
use aes::cipher::block_padding::Pkcs7;
use aes::cipher::inout::InOutBufReserved;
use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use aes::Aes192;
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use cbc::{Decryptor, Encryptor};
use rabex::files::SerializedFile;
use rabex::objects::classes::TextAsset;
use rabex::read_ext::ReadUrexExt;

use crate::utils::ffmpeg;
use crate::utils::ReadAlignedExt;
use crate::utils::{ffmpeg, ReadAlignedExt};
use crate::version::*;
use crate::ExtractorArgs;

pub fn _construct_text_asset<E>(
pub(super) fn _construct_text_asset<E>(
data: &[u8],
serialized_file: &SerializedFile,
) -> Result<TextAsset, Box<dyn Error>>
Expand Down Expand Up @@ -85,3 +87,56 @@ where
output_path,
)
}

pub const MLTD_TEXT_PBKDF2_HMAC_SHA1_KEY: &[u8; 8] = b"Millicon";
pub const MLTD_TEXT_PBKDF2_HMAC_SHA1_SALT: &[u8; 9] = b"DAISUL___";
pub const MLTD_TEXT_PBKDF2_HMAC_SHA1_ROUNDS: u32 = 1000;

#[rustfmt::skip]
/// The AES-192-CBC key used to decrypt the text asset.
///
/// It is derived from [`MLTD_TEXT_PBKDF2_HMAC_SHA1_KEY`] and
/// [`MLTD_TEXT_PBKDF2_HMAC_SHA1_SALT`] using PBKDF2-HMAC-SHA1, where
/// the first 24 bytes of the derived key are used as the actual key.
pub const MLTD_TEXT_DECRYPT_KEY: &[u8; 24] = &[
0xad, 0x3f, 0x0f, 0x89, 0xee, 0x51, 0xc5, 0x37,
0x73, 0x1f, 0x17, 0x96, 0xf7, 0x5c, 0x71, 0x84,
0x01, 0x61, 0x75, 0x6d, 0xa0, 0xd4, 0x86, 0xc9,
];

#[rustfmt::skip]
/// The AES-192-CBC initialization vector used to decrypt the text asset.
///
/// It is derived from [`MLTD_TEXT_PBKDF2_HMAC_SHA1_KEY`] and
/// [`MLTD_TEXT_PBKDF2_HMAC_SHA1_SALT`] using PBKDF2-HMAC-SHA1, where
/// the last 16 bytes of the derived key are used as the actual IV.
pub const MLTD_TEXT_DECRYPT_IV: &[u8; 16] = &[
0x4e, 0x40, 0xb3, 0x8a, 0xeb, 0xf1, 0xa8, 0x53,
0x12, 0x2c, 0x5f, 0xad, 0xcc, 0xa3, 0x68, 0x5d,
];

pub type MltdTextEncryptor = Encryptor<Aes192>;
pub type MltdTextDecryptor = Decryptor<Aes192>;

pub fn encrypt_text(text: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
let encryptor =
MltdTextEncryptor::new(MLTD_TEXT_DECRYPT_KEY.into(), MLTD_TEXT_DECRYPT_IV.into());
let mut buf = text.to_owned();

let buf = InOutBufReserved::from_mut_slice(&mut buf, text.len()).map_err(|e| e.to_string())?;
let buf = encryptor.encrypt_padded_inout_mut::<Pkcs7>(buf).map_err(|e| e.to_string())?;

Ok(buf.to_owned())
}

pub fn decrypt_text(cipher: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
let decryptor =
MltdTextDecryptor::new(MLTD_TEXT_DECRYPT_KEY.into(), MLTD_TEXT_DECRYPT_IV.into());
let mut buf = cipher.to_owned();

let buf = decryptor
.decrypt_padded_inout_mut::<Pkcs7>(buf.as_mut_slice().into())
.map_err(|e| e.to_string())?;

Ok(buf.to_owned())
}
2 changes: 1 addition & 1 deletion extract/src/class/texture_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::environment::Environment;
use crate::utils::{ffmpeg, solve_puzzle, ReadAlignedExt};
use crate::{version::*, ExtractorArgs};

fn _construct_texture_2d<E>(
pub(super) fn _construct_texture_2d<E>(
data: &[u8],
serialized_file: &SerializedFile,
) -> Result<Texture2D, Box<dyn Error>>
Expand Down
4 changes: 2 additions & 2 deletions extract/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{Read, Seek, SeekFrom};
use byteorder::{BigEndian, ReadBytesExt};
use rabex::read_ext::ReadUrexExt;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Environment {
/// resource data that loaded from bundles
resources: HashMap<String, Vec<u8>>,
Expand All @@ -16,7 +16,7 @@ pub struct Environment {

impl Environment {
pub fn new() -> Self {
Self { resources: HashMap::new(), objects: HashMap::new() }
Self::default()
}

pub fn register_cab(&mut self, path: &str, buf: Vec<u8>) {
Expand Down
Loading