diff options
| author | JosJuice <josjuice@gmail.com> | 2026-10-24 14:16:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-24 14:16:21 +0200 |
| commit | c12121f419c40f99eeb65a9039d9fc95be72e94a (patch) | |
| tree | 4a82bf435d5c914930a0aa500563422dbcdc25a2 /Source/Core | |
| parent | e1d35f666cc35f6ead55d32ea2679df44d04da72 (diff) | |
| parent | e56740eabce91b58119ad7a03f5a42db16a3e077 (diff) | |
Merge pull request #14917 from jordan-woyak/skylanders-common-aes
Skylanders: Replace mbedtls_aes usage with Common::AES.
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp | 40 | ||||
| -rw-r--r-- | Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h | 16 |
2 files changed, 29 insertions, 27 deletions
diff --git a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp index 7db61999bd..e702e3cbbc 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp +++ b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp @@ -8,8 +8,6 @@ #include <span> #include <string> -#include <mbedtls/aes.h> - #include "Common/BitUtils.h" #include "Common/CommonTypes.h" #include "Common/Hash.h" @@ -53,10 +51,10 @@ void SkylanderFigure::PopulateKeys() } SkylanderFigure::SkylanderFigure(const std::string& file_path) + : m_sky_file{File::IOFile(file_path, "w+b")} { - m_sky_file = File::IOFile(file_path, "w+b"); - m_data = {}; } + // Generate a AES key without the block filled in void SkylanderFigure::GenerateIncompleteHashIn(u8* dest) const { @@ -80,6 +78,7 @@ void SkylanderFigure::GenerateIncompleteHashIn(u8* dest) const memcpy(dest, hash_in.data(), 0x56); } + void SkylanderFigure::Encrypt(std::span<const u8, FIGURE_SIZE> input) { std::array<u8, 0x56> hash_in = {}; @@ -109,24 +108,21 @@ void SkylanderFigure::Encrypt(std::span<const u8, FIGURE_SIZE> input) Common::ComputeMD5(hash_in, hash_out.data()); - mbedtls_aes_context aes_context = {}; - - mbedtls_aes_setkey_enc(&aes_context, hash_out.data(), 128); - - mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_ENCRYPT, current_block.data(), - encrypted.data() + (i * BLOCK_SIZE)); + Common::AES::CreateContextEncrypt(hash_out.data()) + ->CryptIvZero(current_block.data(), encrypted.data() + (i * BLOCK_SIZE), BLOCK_SIZE); } memcpy(m_data.data(), encrypted.data(), FIGURE_SIZE); DEBUG_LOG_FMT(IOS_USB, "Encrypted skylander data: \n{}", HexDump(encrypted.data(), FIGURE_SIZE)); } -SkylanderFigure::SkylanderFigure(File::IOFile file) + +SkylanderFigure::SkylanderFigure(File::IOFile file) : m_sky_file{std::move(file)} { - m_sky_file = std::move(file); m_sky_file.Seek(0, File::SeekOrigin::Begin); m_sky_file.ReadBytes(m_data.data(), m_data.size()); } + bool SkylanderFigure::Create(u16 sky_id, u16 sky_var, std::optional<std::array<u8, 4>> requested_nuid) { @@ -141,12 +137,12 @@ bool SkylanderFigure::Create(u16 sky_id, u16 sky_var, // Set the NUID of the figure if (requested_nuid) - std::memcpy(&m_data[0], requested_nuid->data(), 4); + std::memcpy(m_data.data(), requested_nuid->data(), 4); else - Common::Random::Generate(&m_data[0], 4); + Common::Random::Generate(m_data.data(), 4); // The BCC (Block Check Character) - m_data[4] = m_data[0] ^ m_data[1] ^ m_data[2] ^ m_data[3]; + m_data[4] = u8(u32(m_data[0]) ^ m_data[1] ^ m_data[2] ^ m_data[3]); // ATQA m_data[5] = 0x81; @@ -167,6 +163,7 @@ bool SkylanderFigure::Create(u16 sky_id, u16 sky_var, Save(); return true; } + void SkylanderFigure::Save() { m_sky_file.Seek(0, File::SeekOrigin::Begin); @@ -250,6 +247,7 @@ FigureData SkylanderFigure::GetData() const return figure_data; } + void SkylanderFigure::SetData(FigureData* figure_data) { std::array<u8, FIGURE_SIZE> decrypted = {}; @@ -356,6 +354,7 @@ void SkylanderFigure::SetData(FigureData* figure_data) Save(); } + void SkylanderFigure::DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const { std::array<u8, 0x56> hash_in = {}; @@ -396,26 +395,25 @@ void SkylanderFigure::DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const Common::ComputeMD5(hash_in, hash_out.data()); - mbedtls_aes_context aes_context = {}; - - mbedtls_aes_setkey_dec(&aes_context, hash_out.data(), 128); - - mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_DECRYPT, current_block.data(), - decrypted.data() + (i * BLOCK_SIZE)); + Common::AES::CreateContextDecrypt(hash_out.data()) + ->CryptIvZero(current_block.data(), decrypted.data() + (i * BLOCK_SIZE), BLOCK_SIZE); } memcpy(dest->data(), decrypted.data(), FIGURE_SIZE); DEBUG_LOG_FMT(IOS_USB, "Decrypted skylander data: \n{}", HexDump(decrypted.data(), FIGURE_SIZE)); } + void SkylanderFigure::Close() { m_sky_file.Close(); } + void SkylanderFigure::SetBlock(u8 block, const u8* buf) { memcpy(m_data.data() + (block * BLOCK_SIZE), buf, BLOCK_SIZE); } + bool SkylanderFigure::FileIsOpen() const { return m_sky_file.IsOpen(); diff --git a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h index 0e652d66ca..03af2592bd 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h +++ b/Source/Core/Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h @@ -53,23 +53,27 @@ struct FigureData final }; }; -constexpr u32 BLOCK_COUNT = 0x40; -constexpr u32 BLOCK_SIZE = 0x10; -constexpr u32 FIGURE_SIZE = BLOCK_COUNT * BLOCK_SIZE; +constexpr std::size_t BLOCK_COUNT = 0x40; +constexpr std::size_t BLOCK_SIZE = 0x10; +constexpr std::size_t FIGURE_SIZE = BLOCK_COUNT * BLOCK_SIZE; class SkylanderFigure { public: - SkylanderFigure(const std::string& file_path); - SkylanderFigure(File::IOFile file); + explicit SkylanderFigure(const std::string& file_path); + explicit SkylanderFigure(File::IOFile file); + bool Create(u16 sky_id, u16 sky_var, std::optional<std::array<u8, 4>> requested_nuid = std::nullopt); void Save(); void Close(); bool FileIsOpen() const; + void GetBlock(u8 index, u8* dest) const; void SetBlock(u8 block, const u8* buf); + void DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const; + FigureData GetData() const; void SetData(FigureData* data); @@ -80,6 +84,6 @@ private: void Encrypt(std::span<const u8, FIGURE_SIZE>); File::IOFile m_sky_file; - std::array<u8, FIGURE_SIZE> m_data; + std::array<u8, FIGURE_SIZE> m_data{}; }; } // namespace IOS::HLE::USB |
