diff options
author | bunnei <bunneidev@gmail.com> | 2018-08-05 03:17:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-05 03:17:10 +0200 |
commit | cd96c04339f0b457154ae17810408a9c39959962 (patch) | |
tree | 46691894e3dab41efe835b9c7924668aecc079ef /src/core | |
parent | Merge pull request #923 from lioncash/pragma (diff) | |
parent | aes_util: Add static assertion to Transcode() and XTSTranscode() to ensure well-defined behavior (diff) | |
download | yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.gz yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.bz2 yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.lz yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.xz yuzu-cd96c04339f0b457154ae17810408a9c39959962.tar.zst yuzu-cd96c04339f0b457154ae17810408a9c39959962.zip |
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/crypto/aes_util.cpp | 35 | ||||
-rw-r--r-- | src/core/crypto/aes_util.h | 12 | ||||
-rw-r--r-- | src/core/crypto/encryption_layer.h | 1 | ||||
-rw-r--r-- | src/core/crypto/key_manager.cpp | 15 | ||||
-rw-r--r-- | src/core/crypto/key_manager.h | 7 |
5 files changed, 35 insertions, 35 deletions
diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 4690af5f8..a9876c83e 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp @@ -3,10 +3,22 @@ // Refer to the license.txt file included. #include <mbedtls/cipher.h> +#include "common/assert.h" +#include "common/logging/log.h" #include "core/crypto/aes_util.h" #include "core/crypto/key_manager.h" namespace Core::Crypto { +namespace { +std::vector<u8> CalculateNintendoTweak(size_t sector_id) { + std::vector<u8> out(0x10); + for (size_t i = 0xF; i <= 0xF; --i) { + out[i] = sector_id & 0xFF; + sector_id >>= 8; + } + return out; +} +} // Anonymous namespace static_assert(static_cast<size_t>(Mode::CTR) == static_cast<size_t>(MBEDTLS_CIPHER_AES_128_CTR), "CTR has incorrect value."); @@ -56,27 +68,28 @@ void AESCipher<Key, KeySize>::SetIV(std::vector<u8> iv) { } template <typename Key, size_t KeySize> -void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) { - size_t written = 0; - - const auto context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; +void AESCipher<Key, KeySize>::Transcode(const u8* src, size_t size, u8* dest, Op op) const { + auto* const context = op == Op::Encrypt ? &ctx->encryption_context : &ctx->decryption_context; mbedtls_cipher_reset(context); + size_t written = 0; if (mbedtls_cipher_get_cipher_mode(context) == MBEDTLS_MODE_XTS) { mbedtls_cipher_update(context, src, size, dest, &written); - if (written != size) + if (written != size) { LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", size, written); + } } else { const auto block_size = mbedtls_cipher_get_block_size(context); for (size_t offset = 0; offset < size; offset += block_size) { auto length = std::min<size_t>(block_size, size - offset); mbedtls_cipher_update(context, src + offset, length, dest + offset, &written); - if (written != length) + if (written != length) { LOG_WARNING(Crypto, "Not all data was decrypted requested={:016X}, actual={:016X}.", length, written); + } } } @@ -97,16 +110,6 @@ void AESCipher<Key, KeySize>::XTSTranscode(const u8* src, size_t size, u8* dest, } } -template <typename Key, size_t KeySize> -std::vector<u8> AESCipher<Key, KeySize>::CalculateNintendoTweak(size_t sector_id) { - std::vector<u8> out(0x10); - for (size_t i = 0xF; i <= 0xF; --i) { - out[i] = sector_id & 0xFF; - sector_id >>= 8; - } - return out; -} - template class AESCipher<Key128>; template class AESCipher<Key256>; } // namespace Core::Crypto
\ No newline at end of file diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index 5b0b02738..8ce9d6612 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -7,7 +7,7 @@ #include <memory> #include <type_traits> #include <vector> -#include "common/assert.h" +#include "common/common_types.h" #include "core/file_sys/vfs.h" namespace Core::Crypto { @@ -38,15 +38,19 @@ public: void SetIV(std::vector<u8> iv); template <typename Source, typename Dest> - void Transcode(const Source* src, size_t size, Dest* dest, Op op) { + void Transcode(const Source* src, size_t size, Dest* dest, Op op) const { + static_assert(std::is_trivially_copyable_v<Source> && std::is_trivially_copyable_v<Dest>, + "Transcode source and destination types must be trivially copyable."); Transcode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), op); } - void Transcode(const u8* src, size_t size, u8* dest, Op op); + void Transcode(const u8* src, size_t size, u8* dest, Op op) const; template <typename Source, typename Dest> void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, size_t sector_size, Op op) { + static_assert(std::is_trivially_copyable_v<Source> && std::is_trivially_copyable_v<Dest>, + "XTSTranscode source and destination types must be trivially copyable."); XTSTranscode(reinterpret_cast<const u8*>(src), size, reinterpret_cast<u8*>(dest), sector_id, sector_size, op); } @@ -56,7 +60,5 @@ public: private: std::unique_ptr<CipherContext> ctx; - - static std::vector<u8> CalculateNintendoTweak(size_t sector_id); }; } // namespace Core::Crypto diff --git a/src/core/crypto/encryption_layer.h b/src/core/crypto/encryption_layer.h index 71bca1f23..7f05af9b4 100644 --- a/src/core/crypto/encryption_layer.h +++ b/src/core/crypto/encryption_layer.h @@ -4,6 +4,7 @@ #pragma once +#include "common/common_types.h" #include "core/file_sys/vfs.h" namespace Core::Crypto { diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 678ac5752..fc45e7ab5 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -2,19 +2,16 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <algorithm> #include <array> #include <fstream> #include <locale> #include <sstream> #include <string_view> -#include <mbedtls/sha256.h> -#include "common/assert.h" #include "common/common_paths.h" #include "common/file_util.h" -#include "common/logging/log.h" #include "core/crypto/key_manager.h" #include "core/settings.h" -#include "key_manager.h" namespace Core::Crypto { @@ -66,8 +63,7 @@ KeyManager::KeyManager() { AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); } -void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) { - const auto filename = std::string(filename_); +void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { std::ifstream file(filename); if (!file.is_open()) return; @@ -107,11 +103,8 @@ void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) { } } -void KeyManager::AttemptLoadKeyFile(std::string_view dir1_, std::string_view dir2_, - std::string_view filename_, bool title) { - const std::string dir1(dir1_); - const std::string dir2(dir2_); - const std::string filename(filename_); +void KeyManager::AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, + const std::string& filename, bool title) { if (FileUtil::Exists(dir1 + DIR_SEP + filename)) LoadFromFile(dir1 + DIR_SEP + filename, title); else if (FileUtil::Exists(dir2 + DIR_SEP + filename)) diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index 03152a12c..c4c53cefc 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -5,6 +5,7 @@ #pragma once #include <array> +#include <string> #include <type_traits> #include <unordered_map> #include <vector> @@ -109,9 +110,9 @@ private: std::unordered_map<KeyIndex<S256KeyType>, Key256> s256_keys; bool dev_mode; - void LoadFromFile(std::string_view filename, bool is_title_keys); - void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, - bool title); + void LoadFromFile(const std::string& filename, bool is_title_keys); + void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, + const std::string& filename, bool title); static const std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; static const std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; |