|
|
|
|
@@ -1,19 +1,18 @@
|
|
|
|
|
use std::io::{Read, Write as _};
|
|
|
|
|
use std::io::Write as _;
|
|
|
|
|
|
|
|
|
|
use aes::cipher::{
|
|
|
|
|
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, KeyIvInit, block_padding::Pkcs7,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use digest::generic_array::GenericArray;
|
|
|
|
|
use flate2::write::ZlibEncoder;
|
|
|
|
|
use flate2::{Compression, read::ZlibDecoder};
|
|
|
|
|
use flate2::Compression;
|
|
|
|
|
use flate2::write::{ZlibDecoder, ZlibEncoder};
|
|
|
|
|
|
|
|
|
|
use crate::error::ApiError;
|
|
|
|
|
use crate::title::{MaiVersion, MaiVersionExt, Sdgb1_40, Sdgb1_50};
|
|
|
|
|
|
|
|
|
|
impl MaiVersionExt for Sdgb1_40 {
|
|
|
|
|
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
let mut decompressed = decompress(data.as_mut());
|
|
|
|
|
fn decode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
let mut decompressed = decompress(data.as_ref());
|
|
|
|
|
if decompressed.is_empty() {
|
|
|
|
|
return Err(ApiError::EmptyResponse);
|
|
|
|
|
}
|
|
|
|
|
@@ -41,14 +40,19 @@ impl MaiVersionExt for Sdgb1_40 {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MaiVersionExt for Sdgb1_50 {
|
|
|
|
|
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
if data.as_mut().is_empty() {
|
|
|
|
|
fn decode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
let mut data = data.as_ref().to_vec();
|
|
|
|
|
if data.is_empty() {
|
|
|
|
|
return Err(ApiError::EmptyResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let decrypted = decrypt(&mut data, Self::AES_KEY, Self::AES_IV)?;
|
|
|
|
|
let decompressed = decompress(decrypted);
|
|
|
|
|
Ok(decompressed)
|
|
|
|
|
if data.len() % 16 != 0 {
|
|
|
|
|
let pad = 16 - (data.len() % 16);
|
|
|
|
|
data.resize(data.len() + pad, pad as _);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let decrypted = decrypt_vec(&data, Self::AES_KEY, Self::AES_IV)?;
|
|
|
|
|
Ok(decompress(decrypted))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn encode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
@@ -72,8 +76,9 @@ fn compress(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
|
|
|
|
|
fn decompress(data: impl AsRef<[u8]>) -> Vec<u8> {
|
|
|
|
|
let mut buf = Vec::with_capacity(data.as_ref().len() * 2);
|
|
|
|
|
let mut decode = ZlibDecoder::new(data.as_ref());
|
|
|
|
|
_ = decode.read_to_end(&mut buf);
|
|
|
|
|
let mut decode = ZlibDecoder::new(&mut buf);
|
|
|
|
|
_ = decode.write_all(data.as_ref());
|
|
|
|
|
_ = decode.finish();
|
|
|
|
|
buf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -104,6 +109,13 @@ fn decrypt<'ct>(
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn decrypt_vec(data: impl AsRef<[u8]>, key: &[u8; 32], iv: &[u8; 16]) -> Result<Vec<u8>, ApiError> {
|
|
|
|
|
let key = GenericArray::from_slice(key);
|
|
|
|
|
let iv = GenericArray::from_slice(iv);
|
|
|
|
|
let decryptor = Aes256CbcDec::new(key, iv);
|
|
|
|
|
Ok(decryptor.decrypt_padded_vec_mut::<Pkcs7>(data.as_ref())?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod _tests {
|
|
|
|
|
|
|
|
|
|
@@ -148,4 +160,69 @@ mod _tests {
|
|
|
|
|
assert_eq!(enc, data);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: user data decryption
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_user_data_dec() -> Result<(), ApiError> {
|
|
|
|
|
let data = [
|
|
|
|
|
60, 33, 100, 111, 99, 116, 121, 112, 101, 32, 104, 116, 109, 108, 62, 60, 104, 116,
|
|
|
|
|
109, 108, 32, 108, 97, 110, 103, 61, 34, 101, 110, 34, 62, 60, 104, 101, 97, 100, 62,
|
|
|
|
|
60, 116, 105, 116, 108, 101, 62, 72, 84, 84, 80, 32, 83, 116, 97, 116, 117, 115, 32,
|
|
|
|
|
53, 48, 48, 32, 226, 128, 147, 32, 73, 110, 116, 101, 114, 110, 97, 108, 32, 83, 101,
|
|
|
|
|
114, 118, 101, 114, 32, 69, 114, 114, 111, 114, 60, 47, 116, 105, 116, 108, 101, 62,
|
|
|
|
|
60, 115, 116, 121, 108, 101, 32, 116, 121, 112, 101, 61, 34, 116, 101, 120, 116, 47,
|
|
|
|
|
99, 115, 115, 34, 62, 104, 49, 32, 123, 102, 111, 110, 116, 45, 102, 97, 109, 105, 108,
|
|
|
|
|
121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105, 97, 108, 44, 115, 97, 110, 115,
|
|
|
|
|
45, 115, 101, 114, 105, 102, 59, 99, 111, 108, 111, 114, 58, 119, 104, 105, 116, 101,
|
|
|
|
|
59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58, 35,
|
|
|
|
|
53, 50, 53, 68, 55, 54, 59, 102, 111, 110, 116, 45, 115, 105, 122, 101, 58, 50, 50,
|
|
|
|
|
112, 120, 59, 125, 32, 104, 50, 32, 123, 102, 111, 110, 116, 45, 102, 97, 109, 105,
|
|
|
|
|
108, 121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105, 97, 108, 44, 115, 97, 110,
|
|
|
|
|
115, 45, 115, 101, 114, 105, 102, 59, 99, 111, 108, 111, 114, 58, 119, 104, 105, 116,
|
|
|
|
|
101, 59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58,
|
|
|
|
|
35, 53, 50, 53, 68, 55, 54, 59, 102, 111, 110, 116, 45, 115, 105, 122, 101, 58, 49, 54,
|
|
|
|
|
112, 120, 59, 125, 32, 104, 51, 32, 123, 102, 111, 110, 116, 45, 102, 97, 109, 105,
|
|
|
|
|
108, 121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105, 97, 108, 44, 115, 97, 110,
|
|
|
|
|
115, 45, 115, 101, 114, 105, 102, 59, 99, 111, 108, 111, 114, 58, 119, 104, 105, 116,
|
|
|
|
|
101, 59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114, 58,
|
|
|
|
|
35, 53, 50, 53, 68, 55, 54, 59, 102, 111, 110, 116, 45, 115, 105, 122, 101, 58, 49, 52,
|
|
|
|
|
112, 120, 59, 125, 32, 98, 111, 100, 121, 32, 123, 102, 111, 110, 116, 45, 102, 97,
|
|
|
|
|
109, 105, 108, 121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105, 97, 108, 44, 115,
|
|
|
|
|
97, 110, 115, 45, 115, 101, 114, 105, 102, 59, 99, 111, 108, 111, 114, 58, 98, 108, 97,
|
|
|
|
|
99, 107, 59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108, 111, 114,
|
|
|
|
|
58, 119, 104, 105, 116, 101, 59, 125, 32, 98, 32, 123, 102, 111, 110, 116, 45, 102, 97,
|
|
|
|
|
109, 105, 108, 121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105, 97, 108, 44, 115,
|
|
|
|
|
97, 110, 115, 45, 115, 101, 114, 105, 102, 59, 99, 111, 108, 111, 114, 58, 119, 104,
|
|
|
|
|
105, 116, 101, 59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108,
|
|
|
|
|
111, 114, 58, 35, 53, 50, 53, 68, 55, 54, 59, 125, 32, 112, 32, 123, 102, 111, 110,
|
|
|
|
|
116, 45, 102, 97, 109, 105, 108, 121, 58, 84, 97, 104, 111, 109, 97, 44, 65, 114, 105,
|
|
|
|
|
97, 108, 44, 115, 97, 110, 115, 45, 115, 101, 114, 105, 102, 59, 98, 97, 99, 107, 103,
|
|
|
|
|
114, 111, 117, 110, 100, 58, 119, 104, 105, 116, 101, 59, 99, 111, 108, 111, 114, 58,
|
|
|
|
|
98, 108, 97, 99, 107, 59, 102, 111, 110, 116, 45, 115, 105, 122, 101, 58, 49, 50, 112,
|
|
|
|
|
120, 59, 125, 32, 97, 32, 123, 99, 111, 108, 111, 114, 58, 98, 108, 97, 99, 107, 59,
|
|
|
|
|
125, 32, 97, 46, 110, 97, 109, 101, 32, 123, 99, 111, 108, 111, 114, 58, 98, 108, 97,
|
|
|
|
|
99, 107, 59, 125, 32, 46, 108, 105, 110, 101, 32, 123, 104, 101, 105, 103, 104, 116,
|
|
|
|
|
58, 49, 112, 120, 59, 98, 97, 99, 107, 103, 114, 111, 117, 110, 100, 45, 99, 111, 108,
|
|
|
|
|
111, 114, 58, 35, 53, 50, 53, 68, 55, 54, 59, 98, 111, 114, 100, 101, 114, 58, 110,
|
|
|
|
|
111, 110, 101, 59, 125, 60, 47, 115, 116, 121, 108, 101, 62, 60, 47, 104, 101, 97, 100,
|
|
|
|
|
62, 60, 98, 111, 100, 121, 62, 60, 104, 49, 62, 72, 84, 84, 80, 32, 83, 116, 97, 116,
|
|
|
|
|
117, 115, 32, 53, 48, 48, 32, 226, 128, 147, 32, 73, 110, 116, 101, 114, 110, 97, 108,
|
|
|
|
|
32, 83, 101, 114, 118, 101, 114, 32, 69, 114, 114, 111, 114, 60, 47, 104, 49, 62, 60,
|
|
|
|
|
104, 114, 32, 99, 108, 97, 115, 115, 61, 34, 108, 105, 110, 101, 34, 32, 47, 62, 60,
|
|
|
|
|
112, 62, 60, 98, 62, 84, 121, 112, 101, 60, 47, 98, 62, 32, 83, 116, 97, 116, 117, 115,
|
|
|
|
|
32, 82, 101, 112, 111, 114, 116, 60, 47, 112, 62, 60, 112, 62, 60, 98, 62, 68, 101,
|
|
|
|
|
115, 99, 114, 105, 112, 116, 105, 111, 110, 60, 47, 98, 62, 32, 84, 104, 101, 32, 115,
|
|
|
|
|
101, 114, 118, 101, 114, 32, 101, 110, 99, 111, 117, 110, 116, 101, 114, 101, 100, 32,
|
|
|
|
|
97, 110, 32, 117, 110, 101, 120, 112, 101, 99, 116, 101, 100, 32, 99, 111, 110, 100,
|
|
|
|
|
105, 116, 105, 111, 110, 32, 116, 104, 97, 116, 32, 112, 114, 101, 118, 101, 110, 116,
|
|
|
|
|
101, 100, 32, 105, 116, 32, 102, 114, 111, 109, 32, 102, 117, 108, 102, 105, 108, 108,
|
|
|
|
|
105, 110, 103, 32, 116, 104, 101, 32, 114, 101, 113, 117, 101, 115, 116, 46, 60, 47,
|
|
|
|
|
112, 62, 60, 104, 114, 32, 99, 108, 97, 115, 115, 61, 34, 108, 105, 110, 101, 34, 32,
|
|
|
|
|
47, 62, 60, 104, 51, 62, 65, 112, 97, 99, 104, 101, 32, 84, 111, 109, 99, 97, 116, 47,
|
|
|
|
|
56, 46, 53, 46, 51, 57, 60, 47, 104, 51, 62, 60, 47, 98, 111, 100, 121, 62, 60, 47,
|
|
|
|
|
104, 116, 109, 108, 62,
|
|
|
|
|
];
|
|
|
|
|
let _ = Sdgb1_50::decode(data)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|