From c1f76abfafc3723b3b216d7985ec18d895699afc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 4 Aug 2018 16:35:35 -0400 Subject: key_manager: Use regular std::string instead of std::string_view The benefit of std::string_view comes from the idea of avoiding copies (essentially acting as a non-owning view), however if we're just going to copy into a local variable immediately, there's not much benefit gained here. --- src/core/crypto/key_manager.cpp | 10 +++------- src/core/crypto/key_manager.h | 7 ++++--- 2 files changed, 7 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 678ac5752..45e7e8545 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -66,8 +66,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 +106,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 +#include #include #include #include @@ -109,9 +110,9 @@ private: std::unordered_map, 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> s128_file_id; static const std::unordered_map> s256_file_id; -- cgit v1.2.3 From 8da651ac4d25fbb48f77b8331b61f3cfa26e8f2c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 4 Aug 2018 16:41:17 -0400 Subject: core/crypto: Remove unnecessary includes --- src/core/crypto/aes_util.cpp | 2 ++ src/core/crypto/aes_util.h | 2 +- src/core/crypto/encryption_layer.h | 1 + src/core/crypto/key_manager.cpp | 5 +---- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 4690af5f8..3d939da15 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp @@ -3,6 +3,8 @@ // Refer to the license.txt file included. #include +#include "common/assert.h" +#include "common/logging/log.h" #include "core/crypto/aes_util.h" #include "core/crypto/key_manager.h" diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index 5b0b02738..81fa0d73f 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -7,7 +7,7 @@ #include #include #include -#include "common/assert.h" +#include "common/common_types.h" #include "core/file_sys/vfs.h" 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 45e7e8545..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 #include #include #include #include #include -#include -#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 { -- cgit v1.2.3 From b25468b4980d2e57c5e94808130196542e568282 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 4 Aug 2018 16:49:39 -0400 Subject: aes_util: Make Transcode() a const member function This doesn't modify member state, so it can be made const. --- src/core/crypto/aes_util.cpp | 13 +++++++------ src/core/crypto/aes_util.h | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'src/core') diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index 3d939da15..e2dc4acb3 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp @@ -58,27 +58,28 @@ void AESCipher::SetIV(std::vector iv) { } template -void AESCipher::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::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(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); + } } } diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index 81fa0d73f..bda41b144 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -38,11 +38,11 @@ public: void SetIV(std::vector iv); template - 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 { Transcode(reinterpret_cast(src), size, reinterpret_cast(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 void XTSTranscode(const Source* src, size_t size, Dest* dest, size_t sector_id, -- cgit v1.2.3 From 64c8212ae1168e86693d05875f4284330e52f26d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 4 Aug 2018 16:52:19 -0400 Subject: aes_util: Make CalculateNintendoTweak() an internally linked function This function doesn't directly depend on class state, so it can be hidden entirely from the interface in the cpp file. --- src/core/crypto/aes_util.cpp | 20 ++++++++++---------- src/core/crypto/aes_util.h | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'src/core') diff --git a/src/core/crypto/aes_util.cpp b/src/core/crypto/aes_util.cpp index e2dc4acb3..a9876c83e 100644 --- a/src/core/crypto/aes_util.cpp +++ b/src/core/crypto/aes_util.cpp @@ -9,6 +9,16 @@ #include "core/crypto/key_manager.h" namespace Core::Crypto { +namespace { +std::vector CalculateNintendoTweak(size_t sector_id) { + std::vector 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(Mode::CTR) == static_cast(MBEDTLS_CIPHER_AES_128_CTR), "CTR has incorrect value."); @@ -100,16 +110,6 @@ void AESCipher::XTSTranscode(const u8* src, size_t size, u8* dest, } } -template -std::vector AESCipher::CalculateNintendoTweak(size_t sector_id) { - std::vector out(0x10); - for (size_t i = 0xF; i <= 0xF; --i) { - out[i] = sector_id & 0xFF; - sector_id >>= 8; - } - return out; -} - template class AESCipher; template class AESCipher; } // 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 bda41b144..ce4947a04 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -56,7 +56,5 @@ public: private: std::unique_ptr ctx; - - static std::vector CalculateNintendoTweak(size_t sector_id); }; } // namespace Core::Crypto -- cgit v1.2.3 From 0d04ee97dc0096f8b417fcbc2f3380fc580a136d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 4 Aug 2018 16:56:26 -0400 Subject: aes_util: Add static assertion to Transcode() and XTSTranscode() to ensure well-defined behavior These functions should only be given trivially-copyable types. --- src/core/crypto/aes_util.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/core') diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index ce4947a04..8ce9d6612 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -39,6 +39,8 @@ public: template void Transcode(const Source* src, size_t size, Dest* dest, Op op) const { + static_assert(std::is_trivially_copyable_v && std::is_trivially_copyable_v, + "Transcode source and destination types must be trivially copyable."); Transcode(reinterpret_cast(src), size, reinterpret_cast(dest), op); } @@ -47,6 +49,8 @@ public: template 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 && std::is_trivially_copyable_v, + "XTSTranscode source and destination types must be trivially copyable."); XTSTranscode(reinterpret_cast(src), size, reinterpret_cast(dest), sector_id, sector_size, op); } -- cgit v1.2.3