mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-05 14:37:08 +00:00
PKG Installer fixed, u128 improved
This commit is contained in:
parent
12f36cf31f
commit
4e62ec7458
12 changed files with 304 additions and 184 deletions
|
|
@ -6,13 +6,6 @@
|
|||
#include "sha1.h"
|
||||
#include "key_vault.h"
|
||||
#include "unpkg.h"
|
||||
#include "restore_new.h"
|
||||
#pragma warning(push)
|
||||
#pragma message("TODO: remove wx dependency: <wx/progdlg.h>")
|
||||
#pragma warning(disable : 4996)
|
||||
#include <wx/progdlg.h>
|
||||
#pragma warning(pop)
|
||||
#include "define_new_memleakdetect.h"
|
||||
|
||||
static bool CheckHeader(const fs::file& pkg_f, PKGHeader& header)
|
||||
{
|
||||
|
|
@ -66,7 +59,7 @@ static bool CheckHeader(const fs::file& pkg_f, PKGHeader& header)
|
|||
}
|
||||
|
||||
// PKG Decryption
|
||||
bool Unpack(const fs::file& pkg_f, std::string dir)
|
||||
bool UnpackPKG(const fs::file& pkg_f, const std::string& dir, volatile f64& progress)
|
||||
{
|
||||
// Save current file offset (probably zero)
|
||||
const u64 start_offset = pkg_f.seek(0, fsm::cur);
|
||||
|
|
@ -85,75 +78,78 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
return false;
|
||||
}
|
||||
|
||||
aes_context c;
|
||||
|
||||
u8 iv[HASH_LEN];
|
||||
be_t<u64>& hi = (be_t<u64>&)iv[0];
|
||||
be_t<u64>& lo = (be_t<u64>&)iv[8];
|
||||
|
||||
// Allocate buffers with BUF_SIZE size or more if required
|
||||
const u64 buffer_size = std::max<u64>(BUF_SIZE, sizeof(PKGEntry) * header.file_count);
|
||||
|
||||
const std::unique_ptr<u8[]> buf(new u8[buffer_size]), ctr(new u8[buffer_size]);
|
||||
|
||||
// Debug key
|
||||
u8 key[0x40] = {};
|
||||
memcpy(key + 0x00, &header.qa_digest[0], 8); // &data[0x60]
|
||||
memcpy(key + 0x08, &header.qa_digest[0], 8); // &data[0x60]
|
||||
memcpy(key + 0x10, &header.qa_digest[8], 8); // &data[0x68]
|
||||
memcpy(key + 0x18, &header.qa_digest[8], 8); // &data[0x68]
|
||||
// Allocate buffer with BUF_SIZE size or more if required
|
||||
const std::unique_ptr<u128[]> buf(new u128[std::max<u64>(BUF_SIZE, sizeof(PKGEntry) * header.file_count) / sizeof(u128)]);
|
||||
|
||||
// Define decryption subfunction (`psp` arg selects the key for specific block)
|
||||
auto decrypt = [&](u64 offset, u64 size, bool psp)
|
||||
auto decrypt = [&](u64 offset, u64 size, bool psp) -> u64
|
||||
{
|
||||
// Initialize buffer
|
||||
std::memset(buf.get(), 0, size);
|
||||
|
||||
// Read the data
|
||||
pkg_f.seek(start_offset + header.data_offset + offset);
|
||||
size = pkg_f.read(buf.get(), size);
|
||||
const u64 bits = (size + HASH_LEN - 1) / HASH_LEN;
|
||||
|
||||
// Read the data and set available size
|
||||
const u64 read = pkg_f.read(buf.get(), size);
|
||||
|
||||
// Get block count
|
||||
const u64 blocks = (read + 15) / 16;
|
||||
|
||||
if (header.pkg_type == PKG_RELEASE_TYPE_DEBUG)
|
||||
{
|
||||
for (u64 j = 0; j < bits; j++)
|
||||
// Debug key
|
||||
be_t<u64> input[8] =
|
||||
{
|
||||
u8 hash[0x14];
|
||||
sha1(key, 0x40, hash);
|
||||
*(u64*)&buf[j * HASH_LEN + 0] ^= *(u64*)&hash[0];
|
||||
*(u64*)&buf[j * HASH_LEN + 8] ^= *(u64*)&hash[8];
|
||||
*(be_t<u64>*)&key[0x38] += 1;
|
||||
header.qa_digest[0],
|
||||
header.qa_digest[0],
|
||||
header.qa_digest[1],
|
||||
header.qa_digest[1],
|
||||
};
|
||||
|
||||
for (u64 i = 0; i < blocks; i++)
|
||||
{
|
||||
// Initialize "debug key" for current position
|
||||
input[7] = offset / 16 + i;
|
||||
|
||||
union
|
||||
{
|
||||
u8 _key[0x14];
|
||||
u128 key;
|
||||
};
|
||||
|
||||
sha1(reinterpret_cast<u8*>(input), sizeof(input), _key);
|
||||
|
||||
buf[i] ^= key;
|
||||
}
|
||||
}
|
||||
|
||||
if (header.pkg_type == PKG_RELEASE_TYPE_RELEASE)
|
||||
{
|
||||
aes_context ctx;
|
||||
|
||||
// Set decryption key
|
||||
aes_setkey_enc(&c, psp ? PKG_AES_KEY2 : PKG_AES_KEY, 128);
|
||||
aes_setkey_enc(&ctx, psp ? PKG_AES_KEY2 : PKG_AES_KEY, 128);
|
||||
|
||||
// Initialize `iv` for the specific position
|
||||
memcpy(iv, header.klicensee, sizeof(iv));
|
||||
if (lo + offset / HASH_LEN < lo) hi++;
|
||||
lo += offset / HASH_LEN;
|
||||
// Initialize "release key" for start position
|
||||
be_t<u128> input = header.klicensee.value() + offset / 16;
|
||||
|
||||
for (u64 j = 0; j < bits; j++)
|
||||
// Increment "release key" for every block
|
||||
for (u64 i = 0; i < blocks; i++, input++)
|
||||
{
|
||||
aes_crypt_ecb(&c, AES_ENCRYPT, iv, ctr.get() + j * HASH_LEN);
|
||||
|
||||
if (!++lo)
|
||||
union
|
||||
{
|
||||
hi++;
|
||||
}
|
||||
}
|
||||
u8 _key[16];
|
||||
u128 key;
|
||||
};
|
||||
|
||||
for (u64 j = 0; j < size; j++)
|
||||
{
|
||||
buf[j] ^= ctr[j];
|
||||
aes_crypt_ecb(&ctx, AES_ENCRYPT, reinterpret_cast<u8*>(&input), _key);
|
||||
|
||||
buf[i] ^= key;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the amount of data written in buf
|
||||
return read;
|
||||
};
|
||||
|
||||
wxProgressDialog pdlg("PKG Decrypter / Installer", "Please wait, decrypting...", header.file_count, 0, wxPD_AUTO_HIDE | wxPD_APP_MODAL);
|
||||
LOG_SUCCESS(LOADER, "PKG: Installing in %s (%d entries)...", dir, header.file_count);
|
||||
|
||||
decrypt(0, header.file_count * sizeof(PKGEntry), header.pkg_platform == PKG_PLATFORM_TYPE_PSP);
|
||||
|
||||
|
|
@ -161,12 +157,16 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
|
||||
std::memcpy(entries.data(), buf.get(), entries.size() * sizeof(PKGEntry));
|
||||
|
||||
for (s32 i = 0; i < entries.size(); i++)
|
||||
for (const auto& entry : entries)
|
||||
{
|
||||
const PKGEntry& entry = entries[i];
|
||||
|
||||
const bool is_psp = (entry.type & PKG_FILE_ENTRY_PSP) != 0;
|
||||
|
||||
if (entry.name_size > 256)
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG: Name size is too big (0x%x)", entry.name_size);
|
||||
continue;
|
||||
}
|
||||
|
||||
decrypt(entry.name_offset, entry.name_size, is_psp);
|
||||
|
||||
const std::string name(reinterpret_cast<char*>(buf.get()), entry.name_size);
|
||||
|
|
@ -181,10 +181,7 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
{
|
||||
const std::string path = dir + name;
|
||||
|
||||
if (fs::is_file(path))
|
||||
{
|
||||
LOG_WARNING(LOADER, "PKG Loader: '%s' is overwritten", path);
|
||||
}
|
||||
const bool did_overwrite = fs::is_file(path);
|
||||
|
||||
if (fs::file out{ path, fom::write | fom::create | fom::trunc })
|
||||
{
|
||||
|
|
@ -192,15 +189,33 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
{
|
||||
const u64 block_size = std::min<u64>(BUF_SIZE, entry.file_size - pos);
|
||||
|
||||
decrypt(entry.file_offset + pos, block_size, is_psp);
|
||||
if (decrypt(entry.file_offset + pos, block_size, is_psp) != block_size)
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG: Failed to extract file %s", path);
|
||||
break;
|
||||
}
|
||||
|
||||
out.write(buf.get(), block_size);
|
||||
if (out.write(buf.get(), block_size) != block_size)
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG: Failed to write file %s", path);
|
||||
break;
|
||||
}
|
||||
|
||||
progress += (block_size + 0.0) / header.data_size;
|
||||
}
|
||||
|
||||
if (did_overwrite)
|
||||
{
|
||||
LOG_SUCCESS(LOADER, "PKG: %s file overwritten", name);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_SUCCESS(LOADER, "PKG: %s file created", name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG Loader: Could not create file '%s'", path);
|
||||
return false;
|
||||
LOG_ERROR(LOADER, "PKG: Could not create file %s", path);
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -210,10 +225,17 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
{
|
||||
const std::string path = dir + name;
|
||||
|
||||
if (!fs::is_dir(path) && !fs::create_dir(path))
|
||||
if (fs::create_dir(path))
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG Loader: Could not create directory: %s", path);
|
||||
return false;
|
||||
LOG_SUCCESS(LOADER, "PKG: %s directory created", name);
|
||||
}
|
||||
else if (fs::is_dir(path))
|
||||
{
|
||||
LOG_SUCCESS(LOADER, "PKG: %s directory already exists", name);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG: Could not create directory %s", path);
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -221,12 +243,9 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
|
|||
|
||||
default:
|
||||
{
|
||||
LOG_ERROR(LOADER, "PKG Loader: unknown PKG file entry: 0x%x", entry.type);
|
||||
return false;
|
||||
LOG_ERROR(LOADER, "PKG: Unknown PKG entry type (0x%x) %s", entry.type, name);
|
||||
}
|
||||
}
|
||||
|
||||
pdlg.Update(i + 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue