diff options
author | Zach Hilman <zachhilman@gmail.com> | 2018-07-30 01:00:09 +0200 |
---|---|---|
committer | Zach Hilman <zachhilman@gmail.com> | 2018-08-01 06:16:54 +0200 |
commit | 03149d3e4a7f8038d9c88cbeb19dee25a39e0042 (patch) | |
tree | cae04a5eefd883d1a665d9502370ec5ff9faa3fd /src/core/crypto | |
parent | Allow key loading from %YUZU_DIR%/keys in addition to ~/.switch (diff) | |
download | yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar.gz yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar.bz2 yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar.lz yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar.xz yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.tar.zst yuzu-03149d3e4a7f8038d9c88cbeb19dee25a39e0042.zip |
Diffstat (limited to 'src/core/crypto')
-rw-r--r-- | src/core/crypto/aes_util.h | 7 | ||||
-rw-r--r-- | src/core/crypto/ctr_encryption_layer.cpp | 7 | ||||
-rw-r--r-- | src/core/crypto/ctr_encryption_layer.h | 1 | ||||
-rw-r--r-- | src/core/crypto/encryption_layer.h | 3 | ||||
-rw-r--r-- | src/core/crypto/key_manager.cpp | 13 | ||||
-rw-r--r-- | src/core/crypto/key_manager.h | 7 |
6 files changed, 23 insertions, 15 deletions
diff --git a/src/core/crypto/aes_util.h b/src/core/crypto/aes_util.h index fa77d5560..5b0b02738 100644 --- a/src/core/crypto/aes_util.h +++ b/src/core/crypto/aes_util.h @@ -4,11 +4,16 @@ #pragma once +#include <memory> +#include <type_traits> +#include <vector> #include "common/assert.h" #include "core/file_sys/vfs.h" namespace Core::Crypto { +struct CipherContext; + enum class Mode { CTR = 11, ECB = 2, @@ -20,8 +25,6 @@ enum class Op { Decrypt, }; -struct CipherContext; - template <typename Key, size_t KeySize = sizeof(Key)> class AESCipher { static_assert(std::is_same_v<Key, std::array<u8, KeySize>>, "Key must be std::array of u8."); diff --git a/src/core/crypto/ctr_encryption_layer.cpp b/src/core/crypto/ctr_encryption_layer.cpp index 5dbc257e5..106db02b3 100644 --- a/src/core/crypto/ctr_encryption_layer.cpp +++ b/src/core/crypto/ctr_encryption_layer.cpp @@ -2,7 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include <string.h> +#include <cstring> #include "common/assert.h" #include "core/crypto/ctr_encryption_layer.h" @@ -33,11 +33,10 @@ size_t CTREncryptionLayer::Read(u8* data, size_t length, size_t offset) const { size_t read = 0x10 - sector_offset; if (length + sector_offset < 0x10) { - memcpy(data, block.data() + sector_offset, std::min<u64>(length, read)); + std::memcpy(data, block.data() + sector_offset, std::min<u64>(length, read)); return read; } - - memcpy(data, block.data() + sector_offset, read); + std::memcpy(data, block.data() + sector_offset, read); return read + Read(data + read, length - read, offset + read); } diff --git a/src/core/crypto/ctr_encryption_layer.h b/src/core/crypto/ctr_encryption_layer.h index 697d7c6a5..11b8683c7 100644 --- a/src/core/crypto/ctr_encryption_layer.h +++ b/src/core/crypto/ctr_encryption_layer.h @@ -4,6 +4,7 @@ #pragma once +#include <vector> #include "core/crypto/aes_util.h" #include "core/crypto/encryption_layer.h" #include "core/crypto/key_manager.h" diff --git a/src/core/crypto/encryption_layer.h b/src/core/crypto/encryption_layer.h index 84f11bf5e..71bca1f23 100644 --- a/src/core/crypto/encryption_layer.h +++ b/src/core/crypto/encryption_layer.h @@ -10,7 +10,8 @@ namespace Core::Crypto { // Basically non-functional class that implements all of the methods that are irrelevant to an // EncryptionLayer. Reduces duplicate code. -struct EncryptionLayer : public FileSys::VfsFile { +class EncryptionLayer : public FileSys::VfsFile { +public: explicit EncryptionLayer(FileSys::VirtualFile base); size_t Read(u8* data, size_t length, size_t offset) const override = 0; diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index dea092b5e..33633de7e 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -2,9 +2,11 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <array> #include <fstream> #include <locale> #include <sstream> +#include <string_view> #include <mbedtls/sha256.h> #include "common/assert.h" #include "common/common_paths.h" @@ -86,17 +88,18 @@ void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) { if (is_title_keys) { auto rights_id_raw = HexStringToArray<16>(out[0]); - u128 rights_id = *reinterpret_cast<std::array<u64, 2>*>(&rights_id_raw); + u128 rights_id{}; + std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size()); Key128 key = HexStringToArray<16>(out[1]); SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); } else { std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower); if (s128_file_id.find(out[0]) != s128_file_id.end()) { - const auto index = s128_file_id[out[0]]; + const auto index = s128_file_id.at(out[0]); Key128 key = HexStringToArray<16>(out[1]); SetKey(index.type, key, index.field1, index.field2); } else if (s256_file_id.find(out[0]) != s256_file_id.end()) { - const auto index = s256_file_id[out[0]]; + const auto index = s256_file_id.at(out[0]); Key256 key = HexStringToArray<32>(out[1]); SetKey(index.type, key, index.field1, index.field2); } @@ -143,7 +146,7 @@ void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) { s256_keys[{id, field1, field2}] = key; } -std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { +const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { {"master_key_00", {S128KeyType::Master, 0, 0}}, {"master_key_01", {S128KeyType::Master, 1, 0}}, {"master_key_02", {S128KeyType::Master, 2, 0}}, @@ -187,7 +190,7 @@ std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, }; -std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { +const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { {"header_key", {S256KeyType::Header, 0, 0}}, {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index a52ea4cb9..28a560a3f 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -5,6 +5,7 @@ #pragma once #include <array> +#include <type_traits> #include <unordered_map> #include <vector> #include <fmt/format.h> @@ -50,7 +51,7 @@ struct KeyIndex { std::string DebugInfo() const { u8 key_size = 16; - if (std::is_same_v<KeyType, S256KeyType>) + if constexpr (std::is_same_v<KeyType, S256KeyType>) key_size = 32; return fmt::format("key_size={:02X}, key={:02X}, field1={:016X}, field2={:016X}", key_size, static_cast<u8>(type), field1, field2); @@ -110,7 +111,7 @@ private: void AttemptLoadKeyFile(std::string_view dir1, std::string_view dir2, std::string_view filename, bool title); - static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; - static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; + const static std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; + const static std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; }; } // namespace Core::Crypto |