From 0cc5808bf47bc93ffea52c7c1159dfa3464ab75c Mon Sep 17 00:00:00 2001 From: psucien Date: Wed, 1 Jan 2025 17:18:25 +0100 Subject: [PATCH] fix for trophy decryption --- src/common/config.cpp | 8 ++++---- src/common/config.h | 4 ++-- src/core/crypto/crypto.cpp | 20 +++++--------------- src/core/crypto/crypto.h | 3 ++- src/core/file_format/trp.cpp | 12 +++++++++++- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 4fce7d97f..728e5df06 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -67,7 +67,7 @@ static int cursorHideTimeout = 5; // 5 seconds (default) static bool separateupdatefolder = false; static bool compatibilityData = false; static bool checkCompatibilityOnStartup = false; -static std::string trophyKey = ""; +static std::vector trophyKey; // Gui std::vector settings_install_dirs = {}; @@ -92,11 +92,11 @@ std::string emulator_language = "en"; // Language u32 m_language = 1; // english -std::string getTrophyKey() { +std::vector getTrophyKey() { return trophyKey; } -void setTrophyKey(std::string key) { +void setTrophyKey(std::vector key) { trophyKey = key; } @@ -664,7 +664,7 @@ void load(const std::filesystem::path& path) { if (data.contains("Keys")) { const toml::value& keys = data.at("Keys"); - trophyKey = toml::find_or(keys, "TrophyKey", ""); + trophyKey = toml::find>(keys, "TrophyKey"); } } diff --git a/src/common/config.h b/src/common/config.h index 9d943008b..9e7dff2a9 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -15,8 +15,8 @@ void load(const std::filesystem::path& path); void save(const std::filesystem::path& path); void saveMainWindow(const std::filesystem::path& path); -std::string getTrophyKey(); -void setTrophyKey(std::string key); +std::vector getTrophyKey(); +void setTrophyKey(std::vector key); bool isNeoMode(); bool isFullscreenMode(); diff --git a/src/core/crypto/crypto.cpp b/src/core/crypto/crypto.cpp index 472d284fc..4020edfd8 100644 --- a/src/core/crypto/crypto.cpp +++ b/src/core/crypto/crypto.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include -#include + #include "crypto.h" CryptoPP::RSA::PrivateKey Crypto::key_pkg_derived_key3_keyset_init() { @@ -138,29 +138,19 @@ void Crypto::aesCbcCfb128DecryptEntry(std::span ivkey, } } -static void hexToBytes(const char* hex, unsigned char* dst) { - for (size_t i = 0; hex[i] != 0; i++) { - const unsigned char value = (hex[i] < 0x3A) ? (hex[i] - 0x30) : (hex[i] - 0x37); - dst[i / 2] |= ((i % 2) == 0) ? (value << 4) : (value); - } -} - -void Crypto::decryptEFSM(std::span NPcommID, +void Crypto::decryptEFSM(std::span trophyKey, + std::span NPcommID, std::span efsmIv, std::span ciphertext, std::span decrypted) { - std::vector TrophyIV = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // step 1: Encrypt NPcommID CryptoPP::CBC_Mode::Encryption encrypt; - const char* TrophyKeyget = Config::getTrophyKey().c_str(); - std::vector TrophyKey; - hexToBytes(TrophyKeyget, TrophyKey.data()); - + std::vector trophyIv(16, 0); std::vector trpKey(16); + encrypt.SetKeyWithIV(trophyKey.data(), trophyKey.size(), trophyIv.data()); encrypt.ProcessData(trpKey.data(), NPcommID.data(), 16); - encrypt.SetKeyWithIV(TrophyKey.data(), TrophyKey.size(), TrophyIV.data()); // step 2: decrypt efsm. CryptoPP::CBC_Mode::Decryption decrypt; diff --git a/src/core/crypto/crypto.h b/src/core/crypto/crypto.h index 83249bd7d..b5d8104b5 100644 --- a/src/core/crypto/crypto.h +++ b/src/core/crypto/crypto.h @@ -32,7 +32,8 @@ public: void aesCbcCfb128DecryptEntry(std::span ivkey, std::span ciphertext, std::span decrypted); - void decryptEFSM(std::span, std::span efsmIv, + void decryptEFSM(std::span trophyKey, + std::span NPcommID, std::span efsmIv, std::span ciphertext, std::span decrypted); void PfsGenCryptoKey(std::span ekpfs, std::span seed, diff --git a/src/core/file_format/trp.cpp b/src/core/file_format/trp.cpp index 2ca88c778..187dd8d9d 100644 --- a/src/core/file_format/trp.cpp +++ b/src/core/file_format/trp.cpp @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "common/config.h" #include "common/logging/log.h" #include "common/path_util.h" #include "trp.h" @@ -39,6 +40,15 @@ bool TRP::Extract(const std::filesystem::path& trophyPath, const std::string tit LOG_CRITICAL(Common_Filesystem, "Game sce_sys directory doesn't exist"); return false; } + + const auto user_key = Config::getTrophyKey(); + if (user_key.size() != 16) { + LOG_CRITICAL(Common_Filesystem, "Trophy decryption key is not specified"); + return false; + } + + const std::span key{(CryptoPP::byte*)user_key.data(), 16}; + for (int index = 0; const auto& it : std::filesystem::directory_iterator(gameSysDir)) { if (it.is_regular_file()) { GetNPcommID(trophyPath, index); @@ -97,7 +107,7 @@ bool TRP::Extract(const std::filesystem::path& trophyPath, const std::string tit return false; } file.Read(ESFM); - crypto.decryptEFSM(np_comm_id, esfmIv, ESFM, XML); // decrypt + crypto.decryptEFSM(key, np_comm_id, esfmIv, ESFM, XML); // decrypt removePadding(XML); std::string xml_name = entry.entry_name; size_t pos = xml_name.find("ESFM");