From 5104caf6a647dac47509a8e0540a2eb04c1b1ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 12 Feb 2017 11:50:35 +0100 Subject: [PATCH] Move AES code to Common/Crypto --- Source/Core/Common/CMakeLists.txt | 1 + Source/Core/Common/Common.vcxproj | 2 ++ Source/Core/Common/Common.vcxproj.filters | 6 ++++++ Source/Core/Common/Crypto/AES.cpp | 24 +++++++++++++++++++++++ Source/Core/Common/Crypto/AES.h | 18 +++++++++++++++++ Source/Core/Core/IOS/ES/Formats.cpp | 17 +++------------- Source/Core/Core/IOS/ES/Formats.h | 2 -- Source/Core/DiscIO/NANDContentLoader.cpp | 3 ++- 8 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 Source/Core/Common/Crypto/AES.cpp create mode 100644 Source/Core/Common/Crypto/AES.h diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 5f910a1670..9feeb4ad2e 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -30,6 +30,7 @@ set(SRCS Analytics.cpp x64ABI.cpp x64Emitter.cpp MD5.cpp + Crypto/AES.cpp Crypto/bn.cpp Crypto/ec.cpp Logging/LogManager.cpp) diff --git a/Source/Core/Common/Common.vcxproj b/Source/Core/Common/Common.vcxproj index 831dcad4a7..ac2c2617aa 100644 --- a/Source/Core/Common/Common.vcxproj +++ b/Source/Core/Common/Common.vcxproj @@ -139,6 +139,7 @@ + @@ -188,6 +189,7 @@ + diff --git a/Source/Core/Common/Common.vcxproj.filters b/Source/Core/Common/Common.vcxproj.filters index 3c9b952742..aac1b5a2ec 100644 --- a/Source/Core/Common/Common.vcxproj.filters +++ b/Source/Core/Common/Common.vcxproj.filters @@ -75,6 +75,9 @@ Logging + + Crypto + Crypto @@ -259,6 +262,9 @@ + + Crypto + Crypto diff --git a/Source/Core/Common/Crypto/AES.cpp b/Source/Core/Common/Crypto/AES.cpp new file mode 100644 index 0000000000..e12ba2481c --- /dev/null +++ b/Source/Core/Common/Crypto/AES.cpp @@ -0,0 +1,24 @@ +// Copyright 2017 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include + +#include "Common/Crypto/AES.h" + +namespace Common +{ +namespace AES +{ +std::vector Decrypt(const u8* key, u8* iv, const u8* src, size_t 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; +} +} // namespace AES +} // namespace Common diff --git a/Source/Core/Common/Crypto/AES.h b/Source/Core/Common/Crypto/AES.h new file mode 100644 index 0000000000..54d88727ac --- /dev/null +++ b/Source/Core/Common/Crypto/AES.h @@ -0,0 +1,18 @@ +// Copyright 2017 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" + +namespace Common +{ +namespace AES +{ +std::vector Decrypt(const u8* key, u8* iv, const u8* src, size_t size); +} // namespace AES +} // namespace Common diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index 1e66ba4f51..f90bd765b2 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -10,11 +10,10 @@ #include #include -#include - #include "Common/ChunkFile.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" +#include "Common/Crypto/AES.h" namespace IOS { @@ -22,17 +21,6 @@ namespace ES { constexpr size_t CONTENT_VIEW_SIZE = 0x10; -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; -} - TMDReader::TMDReader(const std::vector& bytes) : m_bytes(bytes) { } @@ -264,7 +252,8 @@ std::vector TicketReader::GetTitleKey() const 0x48, 0xd9, 0xc5, 0x45, 0x73, 0x81, 0xaa, 0xf7}; u8 iv[16] = {}; std::copy_n(&m_bytes[GetOffset() + offsetof(Ticket, title_id)], sizeof(Ticket::title_id), iv); - return AESDecode(common_key, iv, &m_bytes[GetOffset() + offsetof(Ticket, title_key)], 16); + return Common::AES::Decrypt(common_key, iv, &m_bytes[GetOffset() + offsetof(Ticket, title_key)], + 16); } } // namespace ES } // namespace IOS diff --git a/Source/Core/Core/IOS/ES/Formats.h b/Source/Core/Core/IOS/ES/Formats.h index 64fd6c3a46..b6d2985cc2 100644 --- a/Source/Core/Core/IOS/ES/Formats.h +++ b/Source/Core/Core/IOS/ES/Formats.h @@ -101,8 +101,6 @@ struct Ticket static_assert(sizeof(Ticket) == 356, "Ticket has the wrong size"); #pragma pack(pop) -std::vector AESDecode(const u8* key, u8* iv, const u8* src, u32 size); - class TMDReader final { public: diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 0344fe1c46..62e2e174cb 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -17,6 +17,7 @@ #include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" +#include "Common/Crypto/AES.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" @@ -239,7 +240,7 @@ void CNANDContentLoader::InitializeContentEntries(const std::vector& data_ap u32 rounded_size = Common::AlignUp(static_cast(content.size), 0x40); - m_Content[i].m_Data = std::make_unique(IOS::ES::AESDecode( + m_Content[i].m_Data = std::make_unique(Common::AES::Decrypt( title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size)); data_app_offset += rounded_size; }