add source

This commit is contained in:
xhacker-zzz 2023-01-20 00:18:44 +08:00
parent 57c942a0d2
commit b44ada92b1
5 changed files with 283 additions and 0 deletions

65
CMakeLists.txt Normal file
View File

@ -0,0 +1,65 @@
# CMakeList.txt : CMake project for KgmWasm, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("KgmWasm")
set(CMAKE_CXX_STANDARD 14)
include_directories(
$<TARGET_PROPERTY:INTERFACE_INCLUDE_DIRECTORIES>
)
# Add source to this project's executable.
set(RUNTIME_METHODS_LIST
getValue
writeArrayToMemory
UTF8ToString
)
list(JOIN RUNTIME_METHODS_LIST "," RUNTIME_METHODS)
set(EMSCRIPTEN_FLAGS
"--bind"
"-s NO_DYNAMIC_EXECUTION=1"
"-s MODULARIZE=1"
"-s EXPORT_NAME=KgmCryptoModule"
"-s EXPORTED_RUNTIME_METHODS=${RUNTIME_METHODS}"
)
set(EMSCRIPTEN_LEGACY_FLAGS
${EMSCRIPTEN_FLAGS}
"-s WASM=0"
"--memory-init-file 0"
)
set(EMSCRIPTEN_WASM_BUNDLE_FLAGS
${EMSCRIPTEN_FLAGS}
"-s SINGLE_FILE=1"
)
list(JOIN EMSCRIPTEN_FLAGS " " EMSCRIPTEN_FLAGS_STR)
list(JOIN EMSCRIPTEN_LEGACY_FLAGS " " EMSCRIPTEN_LEGACY_FLAGS_STR)
list(JOIN EMSCRIPTEN_WASM_BUNDLE_FLAGS " " EMSCRIPTEN_WASM_BUNDLE_FLAGS_STR)
# Define projects config
set(WASM_SOURCES
"KgmWasm.cpp"
)
add_executable(KgmWasm ${WASM_SOURCES})
set_target_properties(
KgmWasm
PROPERTIES LINK_FLAGS ${EMSCRIPTEN_FLAGS_STR}
)
add_executable(KgmWasmBundle ${WASM_SOURCES})
set_target_properties(
KgmWasmBundle
PROPERTIES LINK_FLAGS ${EMSCRIPTEN_WASM_BUNDLE_FLAGS_STR}
)
add_executable(KgmLegacy ${WASM_SOURCES})
set_target_properties(
KgmLegacy
PROPERTIES LINK_FLAGS ${EMSCRIPTEN_LEGACY_FLAGS_STR}
)

20
KgmWasm.cpp Normal file
View File

@ -0,0 +1,20 @@
// KgmWasm.cpp : Defines the entry point for the application.
//
#include "KgmWasm.h"
#include "kgm.hpp"
#include <stddef.h>
#include <string.h>
size_t preDec(uintptr_t blob, size_t blobSize, std::string ext)
{
return PreDec((uint8_t*)blob, blobSize, ext == "vpr");
}
void decBlob(uintptr_t blob, size_t blobSize, size_t offset)
{
Decrypt((uint8_t*)blob, blobSize, offset);
return;
}

18
KgmWasm.h Normal file
View File

@ -0,0 +1,18 @@
// KgmWasm.h : Include file for standard system include files,
// or project specific include files.
#pragma once
#include <emscripten/bind.h>
#include <string>
namespace em = emscripten;
size_t preDec(uintptr_t blob, size_t blobSize, std::string ext);
void decBlob(uintptr_t blob, size_t blobSize, size_t offset);
EMSCRIPTEN_BINDINGS(QmcCrypto)
{
em::function("preDec", &preDec, em::allow_raw_pointers());
em::function("decBlob", &decBlob, em::allow_raw_pointers());
}

112
kgm.hpp Normal file
View File

@ -0,0 +1,112 @@
#include <vector>
std::vector<uint8_t> VprHeader = {
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31 };
std::vector<uint8_t> KgmHeader = {
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14 };
std::vector<uint8_t> VprMaskDiff = {
0x25, 0xDF, 0xE8, 0xA6, 0x75, 0x1E, 0x75, 0x0E,
0x2F, 0x80, 0xF3, 0x2D, 0xB8, 0xB6, 0xE3, 0x11, 0x00 };
std::vector<uint8_t> MaskV2;
std::vector<uint8_t> table1 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x21, 0x01, 0x61, 0x01, 0x21, 0x01, 0xe1, 0x01, 0x21, 0x01, 0x61, 0x01, 0x21, 0x01,
0xd2, 0x23, 0x02, 0x02, 0x42, 0x42, 0x02, 0x02, 0xc2, 0xc2, 0x02, 0x02, 0x42, 0x42, 0x02, 0x02,
0xd3, 0xd3, 0x02, 0x03, 0x63, 0x43, 0x63, 0x03, 0xe3, 0xc3, 0xe3, 0x03, 0x63, 0x43, 0x63, 0x03,
0x94, 0xb4, 0x94, 0x65, 0x04, 0x04, 0x04, 0x04, 0x84, 0x84, 0x84, 0x84, 0x04, 0x04, 0x04, 0x04,
0x95, 0x95, 0x95, 0x95, 0x04, 0x05, 0x25, 0x05, 0xe5, 0x85, 0xa5, 0x85, 0xe5, 0x05, 0x25, 0x05,
0xd6, 0xb6, 0x96, 0xb6, 0xd6, 0x27, 0x06, 0x06, 0xc6, 0xc6, 0x86, 0x86, 0xc6, 0xc6, 0x06, 0x06,
0xd7, 0xd7, 0x97, 0x97, 0xd7, 0xd7, 0x06, 0x07, 0xe7, 0xc7, 0xe7, 0x87, 0xe7, 0xc7, 0xe7, 0x07,
0x18, 0x38, 0x18, 0x78, 0x18, 0x38, 0x18, 0xe9, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x08, 0x09, 0x29, 0x09, 0x69, 0x09, 0x29, 0x09,
0xda, 0x3a, 0x1a, 0x3a, 0x5a, 0x3a, 0x1a, 0x3a, 0xda, 0x2b, 0x0a, 0x0a, 0x4a, 0x4a, 0x0a, 0x0a,
0xdb, 0xdb, 0x1b, 0x1b, 0x5b, 0x5b, 0x1b, 0x1b, 0xdb, 0xdb, 0x0a, 0x0b, 0x6b, 0x4b, 0x6b, 0x0b,
0x9c, 0xbc, 0x9c, 0x7c, 0x1c, 0x3c, 0x1c, 0x7c, 0x9c, 0xbc, 0x9c, 0x6d, 0x0c, 0x0c, 0x0c, 0x0c,
0x9d, 0x9d, 0x9d, 0x9d, 0x1d, 0x1d, 0x1d, 0x1d, 0x9d, 0x9d, 0x9d, 0x9d, 0x0c, 0x0d, 0x2d, 0x0d,
0xde, 0xbe, 0x9e, 0xbe, 0xde, 0x3e, 0x1e, 0x3e, 0xde, 0xbe, 0x9e, 0xbe, 0xde, 0x2f, 0x0e, 0x0e,
0xdf, 0xdf, 0x9f, 0x9f, 0xdf, 0xdf, 0x1f, 0x1f, 0xdf, 0xdf, 0x9f, 0x9f, 0xdf, 0xdf, 0x0e, 0x0f,
0x00, 0x20, 0x00, 0x60, 0x00, 0x20, 0x00, 0xe0, 0x00, 0x20, 0x00, 0x60, 0x00, 0x20, 0x00, 0xf1
};
std::vector<uint8_t> table2 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x23, 0x01, 0x67, 0x01, 0x23, 0x01, 0xef, 0x01, 0x23, 0x01, 0x67, 0x01, 0x23, 0x01,
0xdf, 0x21, 0x02, 0x02, 0x46, 0x46, 0x02, 0x02, 0xce, 0xce, 0x02, 0x02, 0x46, 0x46, 0x02, 0x02,
0xde, 0xde, 0x02, 0x03, 0x65, 0x47, 0x65, 0x03, 0xed, 0xcf, 0xed, 0x03, 0x65, 0x47, 0x65, 0x03,
0x9d, 0xbf, 0x9d, 0x63, 0x04, 0x04, 0x04, 0x04, 0x8c, 0x8c, 0x8c, 0x8c, 0x04, 0x04, 0x04, 0x04,
0x9c, 0x9c, 0x9c, 0x9c, 0x04, 0x05, 0x27, 0x05, 0xeb, 0x8d, 0xaf, 0x8d, 0xeb, 0x05, 0x27, 0x05,
0xdb, 0xbd, 0x9f, 0xbd, 0xdb, 0x25, 0x06, 0x06, 0xca, 0xca, 0x8e, 0x8e, 0xca, 0xca, 0x06, 0x06,
0xda, 0xda, 0x9e, 0x9e, 0xda, 0xda, 0x06, 0x07, 0xe9, 0x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8, 0x97, 0x7F, 0x1B, 0x4B, 0x0C, 0xC2, 0xC1, 0x21, 0x4C, 0xCC, 0x58, 0xF5,
0x94, 0x52, 0xA3, 0xF3, 0xD3, 0xE0, 0x68, 0xF4, 0x00, 0x23, 0xF3, 0x5E, 0x0A, 0x7B, 0x93, 0xDD,
0xAB, 0x12, 0xB2, 0x13, 0xE8, 0x84, 0xD7, 0xA7, 0x9F, 0x0F, 0x32, 0x4C, 0x55, 0x1D, 0x04, 0x36,
0x52, 0xDC, 0x03, 0xF3, 0xF9, 0x4E, 0x42, 0xE9, 0x3D, 0x61, 0xEF, 0x7C, 0xB6, 0xB3, 0x93, 0x50,
};
uint8_t getMask(size_t pos) {
size_t offset = pos >> 4;
uint8_t value = 0;
while (offset >= 0x11) {
value ^= table1[offset % 272];
offset >>= 4;
value ^= table2[offset % 272];
offset >>= 4;
}
return MaskV2PreDef[pos % 272] ^ value;
}
std::vector<uint8_t> key(17);
bool isVpr = false;
size_t PreDec(uint8_t* fileData, size_t size, bool iV) {
uint32_t headerLen = *(uint32_t*)(fileData + 0x10);
memcpy(key.data(), (fileData + 0x1C), 0x10);
key[16] = 0;
isVpr = iV;
return headerLen;
}
void Decrypt(uint8_t* fileData, size_t size, size_t offset) {
for (size_t i = 0; i < size; ++i) {
uint8_t med8 = key[(i + offset) % 17] ^ fileData[i];
med8 ^= (med8 & 0xf) << 4;
uint8_t msk8 = getMask(i + offset);
msk8 ^= (msk8 & 0xf) << 4;
fileData[i] = med8 ^ msk8;
if (isVpr) {
fileData[i] ^= VprMaskDiff[(i + offset) % 17];
}
}
}

68
kgm_wasm.ts Normal file
View File

@ -0,0 +1,68 @@
import { KgmCrypto } from '@xhacker/kgmwasm/KgmWasmBundle';
import KgmCryptoModule from '@xhacker/kgmwasm/KgmWasmBundle';
import { MergeUint8Array } from '@/utils/MergeUint8Array';
// 每次处理 2M 的数据
const DECRYPTION_BUF_SIZE = 2 *1024 * 1024;
export interface KGMDecryptionResult {
success: boolean;
data: Uint8Array;
error: string;
}
/**
* KGM
*
* Uint8Array
* @param {ArrayBuffer} kgmBlob Blob
*/
export async function DecryptKgmWasm(kgmBlob: ArrayBuffer, ext: string): Promise<KGMDecryptionResult> {
const result: KGMDecryptionResult = { success: false, data: new Uint8Array(), error: '' };
// 初始化模组
let KgmCryptoObj: KgmCrypto;
try {
KgmCryptoObj = await KgmCryptoModule();
} catch (err: any) {
result.error = err?.message || 'wasm 加载失败';
return result;
}
if (!KgmCryptoObj) {
result.error = 'wasm 加载失败';
return result;
}
// 申请内存块,并文件末端数据到 WASM 的内存堆
let kgmBuf = new Uint8Array(kgmBlob);
const pQmcBuf = KgmCryptoObj._malloc(DECRYPTION_BUF_SIZE);
KgmCryptoObj.writeArrayToMemory(kgmBuf.slice(0, DECRYPTION_BUF_SIZE), pQmcBuf);
// 进行解密初始化
const headerSize = KgmCryptoObj.preDec(pQmcBuf, DECRYPTION_BUF_SIZE, ext);
console.log(headerSize);
kgmBuf = kgmBuf.slice(headerSize);
const decryptedParts = [];
let offset = 0;
let bytesToDecrypt = kgmBuf.length;
while (bytesToDecrypt > 0) {
const blockSize = Math.min(bytesToDecrypt, DECRYPTION_BUF_SIZE);
// 解密一些片段
const blockData = new Uint8Array(kgmBuf.slice(offset, offset + blockSize));
KgmCryptoObj.writeArrayToMemory(blockData, pQmcBuf);
KgmCryptoObj.decBlob(pQmcBuf, blockSize, offset);
decryptedParts.push(KgmCryptoObj.HEAPU8.slice(pQmcBuf, pQmcBuf + blockSize));
offset += blockSize;
bytesToDecrypt -= blockSize;
}
KgmCryptoObj._free(pQmcBuf);
result.data = MergeUint8Array(decryptedParts);
result.success = true;
return result;
}