summaryrefslogtreecommitdiff
path: root/src/core/crypto/key_manager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/crypto/key_manager.cpp')
-rw-r--r--src/core/crypto/key_manager.cpp114
1 files changed, 112 insertions, 2 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index 95158e630..e4b33f750 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -12,11 +12,105 @@
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
18namespace Core::Crypto { 19namespace Core::Crypto {
19 20
21Key128 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
37boost::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
71Loader::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 AESCipher<Key128> cipher(sd_kek, Mode::ECB);
104 for (size_t i = 0; i < 2; ++i) {
105 for (size_t j = 0; j < 0x20; ++j)
106 sd_key_sources[i][j] ^= sd_seed[j & 0xF];
107 cipher.Transcode(sd_key_sources[i].data(), sd_key_sources[i].size(), sd_keys[i].data(),
108 Op::Decrypt);
109 }
110
111 return Loader::ResultStatus::Success;
112}
113
20KeyManager::KeyManager() { 114KeyManager::KeyManager() {
21 // Initialize keys 115 // Initialize keys
22 const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath(); 116 const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath();
@@ -24,12 +118,15 @@ KeyManager::KeyManager() {
24 if (Settings::values.use_dev_keys) { 118 if (Settings::values.use_dev_keys) {
25 dev_mode = true; 119 dev_mode = true;
26 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false); 120 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false);
121 AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "dev.keys_autogenerated", false);
27 } else { 122 } else {
28 dev_mode = false; 123 dev_mode = false;
29 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false); 124 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false);
125 AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "prod.keys_autogenerated", false);
30 } 126 }
31 127
32 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true); 128 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true);
129 AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "title.keys_autogenerated", false);
33} 130}
34 131
35void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { 132void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) {
@@ -126,6 +223,13 @@ bool KeyManager::KeyFileExists(bool title) {
126} 223}
127 224
128void KeyManager::DeriveSDSeedLazy() { 225void KeyManager::DeriveSDSeedLazy() {
226 if (!HasKey(S128KeyType::SDSeed)) {
227 const auto res = DeriveSDSeed();
228 if (res != boost::none)
229 SetKey(S128KeyType::SDSeed, res.get());
230 }
231}
232
129const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = { 233const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
130 {"master_key_00", {S128KeyType::Master, 0, 0}}, 234 {"master_key_00", {S128KeyType::Master, 0, 0}},
131 {"master_key_01", {S128KeyType::Master, 1, 0}}, 235 {"master_key_01", {S128KeyType::Master, 1, 0}},
@@ -168,11 +272,17 @@ const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> KeyManager:
168 {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}}, 272 {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}},
169 {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}}, 273 {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}},
170 {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}}, 274 {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}},
275 {"sd_card_kek_source", {S128KeyType::Source, static_cast<u64>(SourceKeyType::SDKEK), 0}},
276 {"aes_kek_generation_source",
277 {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKEKGeneration), 0}},
278 {"aes_key_generation_source",
279 {S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration), 0}},
280 {"sd_seed", {S128KeyType::SDSeed, 0, 0}},
171}; 281};
172 282
173const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = { 283const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
174 {"header_key", {S256KeyType::Header, 0, 0}}, 284 {"header_key", {S256KeyType::Header, 0, 0}},
175 {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}}, 285 {"sd_card_save_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::Save), 0}},
176 {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}}, 286 {"sd_card_nca_key_source", {S256KeyType::SDKeySource, static_cast<u64>(SDKeyType::NCA), 0}},
177}; 287};
178} // namespace Core::Crypto 288} // namespace Core::Crypto