From bd520c8f529513eecc9b4cbe16b9f22b00588fd3 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Fri, 27 Dec 2024 18:13:45 +0200 Subject: [PATCH 1/2] get trophy key from toml file --- src/common/config.cpp | 16 ++++++++++++++++ src/common/config.h | 3 +++ src/core/crypto/crypto.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/common/config.cpp b/src/common/config.cpp index deef0fa88..542f3141c 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -67,6 +67,7 @@ static int cursorHideTimeout = 5; // 5 seconds (default) static bool separateupdatefolder = false; static bool compatibilityData = false; static bool checkCompatibilityOnStartup = false; +std::string trophyKey = ""; // Gui std::vector settings_install_dirs = {}; @@ -91,6 +92,14 @@ std::string emulator_language = "en"; // Language u32 m_language = 1; // english +std::string getTrophyKey() { + return trophyKey; +} + +void setTrophyKey(std::string key) { + trophyKey = key; +} + bool isNeoMode() { return isNeo; } @@ -652,6 +661,11 @@ void load(const std::filesystem::path& path) { m_language = toml::find_or(settings, "consoleLanguage", 1); } + + if (data.contains("Keys")) { + const toml::value& keys = data.at("keys"); + trophyKey = toml::find_or(keys, "TrophyKey", ""); + } } void save(const std::filesystem::path& path) { @@ -711,6 +725,8 @@ void save(const std::filesystem::path& path) { data["Vulkan"]["crashDiagnostic"] = vkCrashDiagnostic; data["Debug"]["DebugDump"] = isDebugDump; data["Debug"]["CollectShader"] = isShaderDebug; + + data["Keys"]["TrophyKey"] = trophyKey; std::vector install_dirs; for (const auto& dirString : settings_install_dirs) { diff --git a/src/common/config.h b/src/common/config.h index 701aadb12..9d943008b 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -15,6 +15,9 @@ 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); + bool isNeoMode(); bool isFullscreenMode(); bool getPlayBGM(); diff --git a/src/core/crypto/crypto.cpp b/src/core/crypto/crypto.cpp index 00f1dea46..357b43622 100644 --- a/src/core/crypto/crypto.cpp +++ b/src/core/crypto/crypto.cpp @@ -3,6 +3,7 @@ #include #include "crypto.h" +#include CryptoPP::RSA::PrivateKey Crypto::key_pkg_derived_key3_keyset_init() { CryptoPP::InvertibleRSAFunction params; @@ -137,6 +138,13 @@ 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, std::span efsmIv, std::span ciphertext, std::span decrypted) { @@ -145,9 +153,15 @@ void Crypto::decryptEFSM(std::span NPcommID, // 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 trpKey(16); encrypt.ProcessData(trpKey.data(), NPcommID.data(), 16); + encrypt.SetKeyWithIV(TrophyKey.data(), TrophyKey.size(), TrophyIV.data()); + // step 2: decrypt efsm. CryptoPP::CBC_Mode::Decryption decrypt; decrypt.SetKeyWithIV(trpKey.data(), trpKey.size(), efsmIv.data()); From a107c4701be8c1589657b36fd37545926af32b03 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Fri, 27 Dec 2024 22:31:51 +0200 Subject: [PATCH 2/2] clang format fix --- src/common/config.cpp | 2 +- src/common/config.h | 1 - src/core/crypto/crypto.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 0faf79b4b..87af0b24c 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; -std::string trophyKey = ""; +static std::string trophyKey = ""; static std::string audioBackend = "cubeb"; // Gui diff --git a/src/common/config.h b/src/common/config.h index a7be2136b..937efafab 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -13,7 +13,6 @@ enum HideCursorState : s16 { Never, Idle, Always }; 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); diff --git a/src/core/crypto/crypto.cpp b/src/core/crypto/crypto.cpp index 357b43622..472d284fc 100644 --- a/src/core/crypto/crypto.cpp +++ b/src/core/crypto/crypto.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include -#include "crypto.h" #include +#include "crypto.h" CryptoPP::RSA::PrivateKey Crypto::key_pkg_derived_key3_keyset_init() { CryptoPP::InvertibleRSAFunction params;