summaryrefslogtreecommitdiff
path: root/src/core/crypto
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-08-16 17:10:01 -0400
committerGravatar Zach Hilman2018-08-23 11:53:30 -0400
commitcde665c56514c1b701c0fe94fc943c7692be7f32 (patch)
tree70ab7a531c27b88c423f08c3fd84313dad2fe081 /src/core/crypto
parentgame_list: Add SD registration loading to game list (diff)
downloadyuzu-cde665c56514c1b701c0fe94fc943c7692be7f32.tar.gz
yuzu-cde665c56514c1b701c0fe94fc943c7692be7f32.tar.xz
yuzu-cde665c56514c1b701c0fe94fc943c7692be7f32.zip
key_manager: Switch to boost flat_map for keys
Should make key gets marginally faster.
Diffstat (limited to 'src/core/crypto')
-rw-r--r--src/core/crypto/key_manager.cpp5
-rw-r--r--src/core/crypto/key_manager.h41
2 files changed, 14 insertions, 32 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index db8b22c85..95158e630 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -125,7 +125,8 @@ bool KeyManager::KeyFileExists(bool title) {
125 FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys"); 125 FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys");
126} 126}
127 127
128const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { 128void KeyManager::DeriveSDSeedLazy() {
129const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
129 {"master_key_00", {S128KeyType::Master, 0, 0}}, 130 {"master_key_00", {S128KeyType::Master, 0, 0}},
130 {"master_key_01", {S128KeyType::Master, 1, 0}}, 131 {"master_key_01", {S128KeyType::Master, 1, 0}},
131 {"master_key_02", {S128KeyType::Master, 2, 0}}, 132 {"master_key_02", {S128KeyType::Master, 2, 0}},
@@ -169,7 +170,7 @@ const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_fi
169 {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, 170 {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}},
170}; 171};
171 172
172const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { 173const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
173 {"header_key", {S256KeyType::Header, 0, 0}}, 174 {"header_key", {S256KeyType::Header, 0, 0}},
174 {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, 175 {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}},
175 {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, 176 {"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 0c62d4421..33b1ad383 100644
--- a/src/core/crypto/key_manager.h
+++ b/src/core/crypto/key_manager.h
@@ -7,10 +7,11 @@
7#include <array> 7#include <array>
8#include <string> 8#include <string>
9#include <type_traits> 9#include <type_traits>
10#include <unordered_map>
11#include <vector> 10#include <vector>
11#include <boost/container/flat_map.hpp>
12#include <fmt/format.h> 12#include <fmt/format.h>
13#include "common/common_types.h" 13#include "common/common_types.h"
14#include "core/loader/loader.h"
14 15
15namespace Core::Crypto { 16namespace Core::Crypto {
16 17
@@ -59,34 +60,14 @@ struct KeyIndex {
59 } 60 }
60}; 61};
61 62
62// The following two (== and hash) are so KeyIndex can be a key in unordered_map 63// boost flat_map requires operator< for O(log(n)) lookups.
63
64template <typename KeyType>
65bool operator==(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) {
66 return std::tie(lhs.type, lhs.field1, lhs.field2) == std::tie(rhs.type, rhs.field1, rhs.field2);
67}
68
69template <typename KeyType> 64template <typename KeyType>
70bool operator!=(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) { 65bool operator<(const KeyIndex<KeyType>& lhs, const KeyIndex<KeyType>& rhs) {
71 return !operator==(lhs, rhs); 66 return (static_cast<size_t>(lhs.type) < static_cast<size_t>(rhs.type)) ||
67 (lhs.type == rhs.type && lhs.field1 < rhs.field1) ||
68 (lhs.type == rhs.type && lhs.field1 == rhs.field1 && lhs.field2 < rhs.field2);
72} 69}
73 70
74} // namespace Core::Crypto
75
76namespace std {
77template <typename KeyType>
78struct hash<Core::Crypto::KeyIndex<KeyType>> {
79 size_t operator()(const Core::Crypto::KeyIndex<KeyType>& k) const {
80 using std::hash;
81
82 return ((hash<u64>()(static_cast<u64>(k.type)) ^ (hash<u64>()(k.field1) << 1)) >> 1) ^
83 (hash<u64>()(k.field2) << 1);
84 }
85};
86} // namespace std
87
88namespace Core::Crypto {
89
90class KeyManager { 71class KeyManager {
91public: 72public:
92 KeyManager(); 73 KeyManager();
@@ -103,15 +84,15 @@ public:
103 static bool KeyFileExists(bool title); 84 static bool KeyFileExists(bool title);
104 85
105private: 86private:
106 std::unordered_map<KeyIndex<S128KeyType>, Key128> s128_keys; 87 boost::container::flat_map<KeyIndex<S128KeyType>, Key128> s128_keys;
107 std::unordered_map<KeyIndex<S256KeyType>, Key256> s256_keys; 88 boost::container::flat_map<KeyIndex<S256KeyType>, Key256> s256_keys;
108 89
109 bool dev_mode; 90 bool dev_mode;
110 void LoadFromFile(const std::string& filename, bool is_title_keys); 91 void LoadFromFile(const std::string& filename, bool is_title_keys);
111 void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2, 92 void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2,
112 const std::string& filename, bool title); 93 const std::string& filename, bool title);
113 94
114 static const std::unordered_map<std::string, KeyIndex<S128KeyType>> s128_file_id; 95 static const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> s128_file_id;
115 static const std::unordered_map<std::string, KeyIndex<S256KeyType>> s256_file_id; 96 static const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> s256_file_id;
116}; 97};
117} // namespace Core::Crypto 98} // namespace Core::Crypto