From 334ddf754eac2e92e35e82eff6572b6a73a8a061 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Mon, 2 Jan 2017 05:32:08 +0100 Subject: [PATCH] DiscIO: Export GetKeyFromTicket This function has more uses than just in DiscIO (e.g. WFS). --- Source/Core/DiscIO/NANDContentLoader.cpp | 42 ++++++++++++------------ Source/Core/DiscIO/NANDContentLoader.h | 4 +-- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 6089c735b3..f87e0e31c0 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -60,6 +60,17 @@ std::vector SignedTicketToTicket(const std::vector& signed_ticket) std::copy(signed_ticket.begin() + entry_offset, signed_ticket.end(), ticket.begin()); return ticket; } + +std::vector AESDecode(const u8* key, u8* iv, const u8* src, u32 size) +{ + mbedtls_aes_context aes_ctx; + std::vector buffer(size); + + mbedtls_aes_setkey_dec(&aes_ctx, key, 128); + mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, size, iv, src, buffer.data()); + + return buffer; +} } CNANDContentData::~CNANDContentData() = default; @@ -323,27 +334,6 @@ void CNANDContentLoader::InitializeContentEntries(const std::vector& tmd, } } -std::vector CNANDContentLoader::AESDecode(const u8* key, u8* iv, const u8* src, u32 size) -{ - mbedtls_aes_context aes_ctx; - std::vector buffer(size); - - mbedtls_aes_setkey_dec(&aes_ctx, key, 128); - mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, size, iv, src, buffer.data()); - - return buffer; -} - -std::vector CNANDContentLoader::GetKeyFromTicket(const std::vector& ticket) -{ - const u8 common_key[16] = {0xeb, 0xe4, 0x2a, 0x22, 0x5e, 0x85, 0x93, 0xe4, - 0x48, 0xd9, 0xc5, 0x45, 0x73, 0x81, 0xaa, 0xf7}; - u8 iv[16] = {}; - - std::copy(&ticket[0x01DC], &ticket[0x01DC + 8], iv); - return AESDecode(common_key, iv, &ticket[0x01BF], 16); -} - DiscIO::Region CNANDContentLoader::GetRegion() const { if (!IsValid()) @@ -606,4 +596,14 @@ std::vector FindTicket(u64 title_id) return SignedTicketToTicket(signed_ticket); } +std::vector GetKeyFromTicket(const std::vector& signed_ticket) +{ + const u8 common_key[16] = {0xeb, 0xe4, 0x2a, 0x22, 0x5e, 0x85, 0x93, 0xe4, + 0x48, 0xd9, 0xc5, 0x45, 0x73, 0x81, 0xaa, 0xf7}; + u8 iv[16] = {}; + + std::copy(&signed_ticket[0x01DC], &signed_ticket[0x01DC + 8], iv); + return AESDecode(common_key, iv, &signed_ticket[0x01BF], 16); +} + } // namespace end diff --git a/Source/Core/DiscIO/NANDContentLoader.h b/Source/Core/DiscIO/NANDContentLoader.h index ebf42d919a..193bee596f 100644 --- a/Source/Core/DiscIO/NANDContentLoader.h +++ b/Source/Core/DiscIO/NANDContentLoader.h @@ -25,6 +25,7 @@ enum class Region; bool AddTicket(const std::vector& signed_ticket); std::vector FindSignedTicket(u64 title_id); std::vector FindTicket(u64 title_id); +std::vector GetKeyFromTicket(const std::vector& ticket); class CNANDContentData { @@ -112,9 +113,6 @@ private: const std::vector& decrypted_title_key, const std::vector& data_app); - static std::vector AESDecode(const u8* key, u8* iv, const u8* src, u32 size); - static std::vector GetKeyFromTicket(const std::vector& ticket); - bool m_Valid; bool m_IsWAD; std::string m_Path;