diff options
| author | 2018-08-24 23:47:46 -0400 | |
|---|---|---|
| committer | 2018-08-24 23:47:46 -0400 | |
| commit | 6426b0f5514d6a7c5cc369368947eceb380bfc85 (patch) | |
| tree | b7acdc39a4344570a6f2c098c30ad20114bf84db /src/core/crypto/key_manager.cpp | |
| parent | Merge pull request #1065 from DarkLordZach/window-title (diff) | |
| parent | file_sys/crypto: Fix missing/unnecessary includes (diff) | |
| download | yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.tar.gz yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.tar.xz yuzu-6426b0f5514d6a7c5cc369368947eceb380bfc85.zip | |
Merge pull request #1094 from DarkLordZach/nax0
file_sys: Add support for NAX archives
Diffstat (limited to 'src/core/crypto/key_manager.cpp')
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 172 |
1 files changed, 165 insertions, 7 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index db8b22c85..0b6c07de8 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp | |||
| @@ -12,11 +12,112 @@ | |||
| 12 | #include "common/file_util.h" | 12 | #include "common/file_util.h" |
| 13 | #include "common/hex_util.h" | 13 | #include "common/hex_util.h" |
| 14 | #include "common/logging/log.h" | 14 | #include "common/logging/log.h" |
| 15 | #include "core/crypto/aes_util.h" | ||
| 15 | #include "core/crypto/key_manager.h" | 16 | #include "core/crypto/key_manager.h" |
| 16 | #include "core/settings.h" | 17 | #include "core/settings.h" |
| 17 | 18 | ||
| 18 | namespace Core::Crypto { | 19 | namespace Core::Crypto { |
| 19 | 20 | ||
| 21 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { | ||
| 22 | Key128 out{}; | ||
| 23 | |||
| 24 | AESCipher<Key128> cipher1(master, Mode::ECB); | ||
| 25 | cipher1.Transcode(kek_seed.data(), kek_seed.size(), out.data(), Op::Decrypt); | ||
| 26 | AESCipher<Key128> cipher2(out, Mode::ECB); | ||
| 27 | cipher2.Transcode(source.data(), source.size(), out.data(), Op::Decrypt); | ||
| 28 | |||
| 29 | if (key_seed != Key128{}) { | ||
| 30 | AESCipher<Key128> cipher3(out, Mode::ECB); | ||
| 31 | cipher3.Transcode(key_seed.data(), key_seed.size(), out.data(), Op::Decrypt); | ||
| 32 | } | ||
| 33 | |||
| 34 | return out; | ||
| 35 | } | ||
| 36 | |||
| 37 | boost::optional<Key128> DeriveSDSeed() { | ||
| 38 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | ||
| 39 | "/system/save/8000000000000043", | ||
| 40 | "rb+"); | ||
| 41 | if (!save_43.IsOpen()) | ||
| 42 | return boost::none; | ||
| 43 | const FileUtil::IOFile sd_private( | ||
| 44 | FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + "/Nintendo/Contents/private", "rb+"); | ||
| 45 | if (!sd_private.IsOpen()) | ||
| 46 | return boost::none; | ||
| 47 | |||
| 48 | sd_private.Seek(0, SEEK_SET); | ||
| 49 | std::array<u8, 0x10> private_seed{}; | ||
| 50 | if (sd_private.ReadBytes(private_seed.data(), private_seed.size()) != 0x10) | ||
| 51 | return boost::none; | ||
| 52 | |||
| 53 | std::array<u8, 0x10> buffer{}; | ||
| 54 | size_t offset = 0; | ||
| 55 | for (; offset + 0x10 < save_43.GetSize(); ++offset) { | ||
| 56 | save_43.Seek(offset, SEEK_SET); | ||
| 57 | save_43.ReadBytes(buffer.data(), buffer.size()); | ||
| 58 | if (buffer == private_seed) | ||
| 59 | break; | ||
| 60 | } | ||
| 61 | |||
| 62 | if (offset + 0x10 >= save_43.GetSize()) | ||
| 63 | return boost::none; | ||
| 64 | |||
| 65 | Key128 seed{}; | ||
| 66 | save_43.Seek(offset + 0x10, SEEK_SET); | ||
| 67 | save_43.ReadBytes(seed.data(), seed.size()); | ||
| 68 | return seed; | ||
| 69 | } | ||
| 70 | |||
| 71 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, const KeyManager& keys) { | ||
| 72 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK))) | ||
| 73 | return Loader::ResultStatus::ErrorMissingSDKEKSource; | ||
| 74 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration))) | ||
| 75 | return Loader::ResultStatus::ErrorMissingAESKEKGenerationSource; | ||
| 76 | if (!keys.HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration))) | ||
| 77 | return Loader::ResultStatus::ErrorMissingAESKeyGenerationSource; | ||
| 78 | |||
| 79 | const auto sd_kek_source = | ||
| 80 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK)); | ||
| 81 | const auto aes_kek_gen = | ||
| 82 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration)); | ||
| 83 | const auto aes_key_gen = | ||
| 84 | keys.GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); | ||
| 85 | const auto master_00 = keys.GetKey(S128KeyType::Master); | ||
| 86 | const auto sd_kek = | ||
| 87 | GenerateKeyEncryptionKey(sd_kek_source, master_00, aes_kek_gen, aes_key_gen); | ||
| 88 | |||
| 89 | if (!keys.HasKey(S128KeyType::SDSeed)) | ||
| 90 | return Loader::ResultStatus::ErrorMissingSDSeed; | ||
| 91 | const auto sd_seed = keys.GetKey(S128KeyType::SDSeed); | ||
| 92 | |||
| 93 | if (!keys.HasKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save))) | ||
| 94 | return Loader::ResultStatus::ErrorMissingSDSaveKeySource; | ||
| 95 | if (!keys.HasKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA))) | ||
| 96 | return Loader::ResultStatus::ErrorMissingSDNCAKeySource; | ||
| 97 | |||
| 98 | std::array<Key256, 2> sd_key_sources{ | ||
| 99 | keys.GetKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save)), | ||
| 100 | keys.GetKey(S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA)), | ||
| 101 | }; | ||
| 102 | |||
| 103 | // Combine sources and seed | ||
| 104 | for (auto& source : sd_key_sources) { | ||
| 105 | for (size_t i = 0; i < source.size(); ++i) | ||
| 106 | source[i] ^= sd_seed[i & 0xF]; | ||
| 107 | } | ||
| 108 | |||
| 109 | AESCipher<Key128> cipher(sd_kek, Mode::ECB); | ||
| 110 | // The transform manipulates sd_keys as part of the Transcode, so the return/output is | ||
| 111 | // unnecessary. This does not alter sd_keys_sources. | ||
| 112 | std::transform(sd_key_sources.begin(), sd_key_sources.end(), sd_keys.begin(), | ||
| 113 | sd_key_sources.begin(), [&cipher](const Key256& source, Key256& out) { | ||
| 114 | cipher.Transcode(source.data(), source.size(), out.data(), Op::Decrypt); | ||
| 115 | return source; ///< Return unaltered source to satisfy output requirement. | ||
| 116 | }); | ||
| 117 | |||
| 118 | return Loader::ResultStatus::Success; | ||
| 119 | } | ||
| 120 | |||
| 20 | KeyManager::KeyManager() { | 121 | KeyManager::KeyManager() { |
| 21 | // Initialize keys | 122 | // Initialize keys |
| 22 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); | 123 | const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); |
| @@ -24,12 +125,15 @@ KeyManager::KeyManager() { | |||
| 24 | if (Settings::values.use_dev_keys) { | 125 | if (Settings::values.use_dev_keys) { |
| 25 | dev_mode = true; | 126 | dev_mode = true; |
| 26 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false); | 127 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false); |
| 128 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "dev.keys_autogenerated", false); | ||
| 27 | } else { | 129 | } else { |
| 28 | dev_mode = false; | 130 | dev_mode = false; |
| 29 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false); | 131 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false); |
| 132 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "prod.keys_autogenerated", false); | ||
| 30 | } | 133 | } |
| 31 | 134 | ||
| 32 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); | 135 | AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); |
| 136 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "title.keys_autogenerated", true); | ||
| 33 | } | 137 | } |
| 34 | 138 | ||
| 35 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | 139 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { |
| @@ -56,17 +160,17 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | |||
| 56 | u128 rights_id{}; | 160 | u128 rights_id{}; |
| 57 | std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size()); | 161 | std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size()); |
| 58 | Key128 key = Common::HexStringToArray<16>(out[1]); | 162 | Key128 key = Common::HexStringToArray<16>(out[1]); |
| 59 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | 163 | s128_keys[{S128KeyType::Titlekey, rights_id[1], rights_id[0]}] = key; |
| 60 | } else { | 164 | } else { |
| 61 | std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower); | 165 | std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower); |
| 62 | if (s128_file_id.find(out[0]) != s128_file_id.end()) { | 166 | if (s128_file_id.find(out[0]) != s128_file_id.end()) { |
| 63 | const auto index = s128_file_id.at(out[0]); | 167 | const auto index = s128_file_id.at(out[0]); |
| 64 | Key128 key = Common::HexStringToArray<16>(out[1]); | 168 | Key128 key = Common::HexStringToArray<16>(out[1]); |
| 65 | SetKey(index.type, key, index.field1, index.field2); | 169 | s128_keys[{index.type, index.field1, index.field2}] = key; |
| 66 | } else if (s256_file_id.find(out[0]) != s256_file_id.end()) { | 170 | } else if (s256_file_id.find(out[0]) != s256_file_id.end()) { |
| 67 | const auto index = s256_file_id.at(out[0]); | 171 | const auto index = s256_file_id.at(out[0]); |
| 68 | Key256 key = Common::HexStringToArray<32>(out[1]); | 172 | Key256 key = Common::HexStringToArray<32>(out[1]); |
| 69 | SetKey(index.type, key, index.field1, index.field2); | 173 | s256_keys[{index.type, index.field1, index.field2}] = key; |
| 70 | } | 174 | } |
| 71 | } | 175 | } |
| 72 | } | 176 | } |
| @@ -100,11 +204,50 @@ Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const { | |||
| 100 | return s256_keys.at({id, field1, field2}); | 204 | return s256_keys.at({id, field1, field2}); |
| 101 | } | 205 | } |
| 102 | 206 | ||
| 207 | template <size_t Size> | ||
| 208 | void KeyManager::WriteKeyToFile(bool title_key, std::string_view keyname, | ||
| 209 | const std::array<u8, Size>& key) { | ||
| 210 | const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir); | ||
| 211 | std::string filename = "title.keys_autogenerated"; | ||
| 212 | if (!title_key) | ||
| 213 | filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated"; | ||
| 214 | const auto add_info_text = !FileUtil::Exists(yuzu_keys_dir + DIR_SEP + filename); | ||
| 215 | FileUtil::CreateFullPath(yuzu_keys_dir + DIR_SEP + filename); | ||
| 216 | std::ofstream file(yuzu_keys_dir + DIR_SEP + filename, std::ios::app); | ||
| 217 | if (!file.is_open()) | ||
| 218 | return; | ||
| 219 | if (add_info_text) { | ||
| 220 | file | ||
| 221 | << "# This file is autogenerated by Yuzu\n" | ||
| 222 | << "# It serves to store keys that were automatically generated from the normal keys\n" | ||
| 223 | << "# If you are experiencing issues involving keys, it may help to delete this file\n"; | ||
| 224 | } | ||
| 225 | |||
| 226 | file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key)); | ||
| 227 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, title_key); | ||
| 228 | } | ||
| 229 | |||
| 103 | void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | 230 | void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { |
| 231 | const auto iter = std::find_if( | ||
| 232 | s128_file_id.begin(), s128_file_id.end(), | ||
| 233 | [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S128KeyType>> elem) { | ||
| 234 | return std::tie(elem.second.type, elem.second.field1, elem.second.field2) == | ||
| 235 | std::tie(id, field1, field2); | ||
| 236 | }); | ||
| 237 | if (iter != s128_file_id.end()) | ||
| 238 | WriteKeyToFile(id == S128KeyType::Titlekey, iter->first, key); | ||
| 104 | s128_keys[{id, field1, field2}] = key; | 239 | s128_keys[{id, field1, field2}] = key; |
| 105 | } | 240 | } |
| 106 | 241 | ||
| 107 | void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) { | 242 | void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) { |
| 243 | const auto iter = std::find_if( | ||
| 244 | s256_file_id.begin(), s256_file_id.end(), | ||
| 245 | [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S256KeyType>> elem) { | ||
| 246 | return std::tie(elem.second.type, elem.second.field1, elem.second.field2) == | ||
| 247 | std::tie(id, field1, field2); | ||
| 248 | }); | ||
| 249 | if (iter != s256_file_id.end()) | ||
| 250 | WriteKeyToFile(false, iter->first, key); | ||
| 108 | s256_keys[{id, field1, field2}] = key; | 251 | s256_keys[{id, field1, field2}] = key; |
| 109 | } | 252 | } |
| 110 | 253 | ||
| @@ -125,7 +268,16 @@ bool KeyManager::KeyFileExists(bool title) { | |||
| 125 | FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys"); | 268 | FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys"); |
| 126 | } | 269 | } |
| 127 | 270 | ||
| 128 | const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { | 271 | void KeyManager::DeriveSDSeedLazy() { |
| 272 | if (HasKey(S128KeyType::SDSeed)) | ||
| 273 | return; | ||
| 274 | |||
| 275 | const auto res = DeriveSDSeed(); | ||
| 276 | if (res != boost::none) | ||
| 277 | SetKey(S128KeyType::SDSeed, res.get()); | ||
| 278 | } | ||
| 279 | |||
| 280 | const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { | ||
| 129 | {"master_key_00", {S128KeyType::Master, 0, 0}}, | 281 | {"master_key_00", {S128KeyType::Master, 0, 0}}, |
| 130 | {"master_key_01", {S128KeyType::Master, 1, 0}}, | 282 | {"master_key_01", {S128KeyType::Master, 1, 0}}, |
| 131 | {"master_key_02", {S128KeyType::Master, 2, 0}}, | 283 | {"master_key_02", {S128KeyType::Master, 2, 0}}, |
| @@ -167,11 +319,17 @@ const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_fi | |||
| 167 | {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}}, | 319 | {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}}, |
| 168 | {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}}, | 320 | {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}}, |
| 169 | {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, | 321 | {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, |
| 322 | {"sd_card_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK), 0}}, | ||
| 323 | {"aes_kek_generation_source", | ||
| 324 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration), 0}}, | ||
| 325 | {"aes_key_generation_source", | ||
| 326 | {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration), 0}}, | ||
| 327 | {"sd_seed", {S128KeyType::SDSeed, 0, 0}}, | ||
| 170 | }; | 328 | }; |
| 171 | 329 | ||
| 172 | const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { | 330 | const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { |
| 173 | {"header_key", {S256KeyType::Header, 0, 0}}, | 331 | {"header_key", {S256KeyType::Header, 0, 0}}, |
| 174 | {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, | 332 | {"sd_card_save_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save), 0}}, |
| 175 | {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, | 333 | {"sd_card_nca_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA), 0}}, |
| 176 | }; | 334 | }; |
| 177 | } // namespace Core::Crypto | 335 | } // namespace Core::Crypto |