diff options
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 292 | ||||
| -rw-r--r-- | src/core/crypto/key_manager.h | 16 | ||||
| -rw-r--r-- | src/core/crypto/partition_data_manager.cpp | 89 | ||||
| -rw-r--r-- | src/core/crypto/partition_data_manager.h | 9 | ||||
| -rw-r--r-- | src/yuzu/main.cpp | 29 | ||||
| -rw-r--r-- | src/yuzu/main.h | 7 |
6 files changed, 247 insertions, 195 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 027643654..a59a7e1f5 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <array> | 6 | #include <array> |
| 7 | #include <bitset> | 7 | #include <bitset> |
| 8 | #include <cctype> | ||
| 8 | #include <fstream> | 9 | #include <fstream> |
| 9 | #include <locale> | 10 | #include <locale> |
| 10 | #include <map> | 11 | #include <map> |
| @@ -23,6 +24,7 @@ | |||
| 23 | #include "common/logging/log.h" | 24 | #include "common/logging/log.h" |
| 24 | #include "core/crypto/aes_util.h" | 25 | #include "core/crypto/aes_util.h" |
| 25 | #include "core/crypto/key_manager.h" | 26 | #include "core/crypto/key_manager.h" |
| 27 | #include "core/crypto/partition_data_manager.h" | ||
| 26 | #include "core/file_sys/content_archive.h" | 28 | #include "core/file_sys/content_archive.h" |
| 27 | #include "core/file_sys/nca_metadata.h" | 29 | #include "core/file_sys/nca_metadata.h" |
| 28 | #include "core/file_sys/partition_filesystem.h" | 30 | #include "core/file_sys/partition_filesystem.h" |
| @@ -37,11 +39,21 @@ constexpr u64 CURRENT_CRYPTO_REVISION = 0x5; | |||
| 37 | 39 | ||
| 38 | using namespace Common; | 40 | using namespace Common; |
| 39 | 41 | ||
| 40 | const static std::array<SHA256Hash, 4> eticket_source_hashes{ | 42 | const std::array<SHA256Hash, 2> eticket_source_hashes{ |
| 41 | "B71DB271DC338DF380AA2C4335EF8873B1AFD408E80B3582D8719FC81C5E511C"_array32, // eticket_rsa_kek_source | 43 | "B71DB271DC338DF380AA2C4335EF8873B1AFD408E80B3582D8719FC81C5E511C"_array32, // eticket_rsa_kek_source |
| 42 | "E8965A187D30E57869F562D04383C996DE487BBA5761363D2D4D32391866A85C"_array32, // eticket_rsa_kekek_source | 44 | "E8965A187D30E57869F562D04383C996DE487BBA5761363D2D4D32391866A85C"_array32, // eticket_rsa_kekek_source |
| 43 | }; | 45 | }; |
| 44 | 46 | ||
| 47 | const std::map<std::pair<S128KeyType, u64>, std::string> KEYS_VARIABLE_LENGTH{ | ||
| 48 | {{S128KeyType::Master, 0}, "master_key_"}, | ||
| 49 | {{S128KeyType::Package1, 0}, "package1_key_"}, | ||
| 50 | {{S128KeyType::Package2, 0}, "package2_key_"}, | ||
| 51 | {{S128KeyType::Titlekek, 0}, "titlekek_"}, | ||
| 52 | {{S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob)}, "keyblob_key_source_"}, | ||
| 53 | {{S128KeyType::Keyblob, 0}, "keyblob_key_"}, | ||
| 54 | {{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"}, | ||
| 55 | }; | ||
| 56 | |||
| 45 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { | 57 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) { |
| 46 | Key128 out{}; | 58 | Key128 out{}; |
| 47 | 59 | ||
| @@ -58,7 +70,7 @@ Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, K | |||
| 58 | return out; | 70 | return out; |
| 59 | } | 71 | } |
| 60 | 72 | ||
| 61 | Key128 DeriveKeyblobKey(Key128 sbk, Key128 tsec, Key128 source) { | 73 | Key128 DeriveKeyblobKey(const Key128& sbk, const Key128& tsec, Key128 source) { |
| 62 | AESCipher<Key128> sbk_cipher(sbk, Mode::ECB); | 74 | AESCipher<Key128> sbk_cipher(sbk, Mode::ECB); |
| 63 | AESCipher<Key128> tsec_cipher(tsec, Mode::ECB); | 75 | AESCipher<Key128> tsec_cipher(tsec, Mode::ECB); |
| 64 | tsec_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt); | 76 | tsec_cipher.Transcode(source.data(), source.size(), source.data(), Op::Decrypt); |
| @@ -66,6 +78,69 @@ Key128 DeriveKeyblobKey(Key128 sbk, Key128 tsec, Key128 source) { | |||
| 66 | return source; | 78 | return source; |
| 67 | } | 79 | } |
| 68 | 80 | ||
| 81 | Key128 DeriveMasterKey(const std::array<u8, 0x90>& keyblob, const Key128& master_source) { | ||
| 82 | Key128 master_root; | ||
| 83 | std::memcpy(master_root.data(), keyblob.data(), sizeof(Key128)); | ||
| 84 | |||
| 85 | AESCipher<Key128> master_cipher(master_root, Mode::ECB); | ||
| 86 | |||
| 87 | Key128 master{}; | ||
| 88 | master_cipher.Transcode(master_source.data(), master_source.size(), master.data(), Op::Decrypt); | ||
| 89 | return master; | ||
| 90 | } | ||
| 91 | |||
| 92 | std::array<u8, 144> DecryptKeyblob(const std::array<u8, 176>& encrypted_keyblob, | ||
| 93 | const Key128& key) { | ||
| 94 | std::array<u8, 0x90> keyblob; | ||
| 95 | AESCipher<Key128> cipher(key, Mode::CTR); | ||
| 96 | cipher.SetIV(std::vector<u8>(encrypted_keyblob.data() + 0x10, encrypted_keyblob.data() + 0x20)); | ||
| 97 | cipher.Transcode(encrypted_keyblob.data() + 0x20, keyblob.size(), keyblob.data(), Op::Decrypt); | ||
| 98 | return keyblob; | ||
| 99 | } | ||
| 100 | |||
| 101 | void KeyManager::DeriveGeneralPurposeKeys(u8 crypto_revision) { | ||
| 102 | const auto kek_generation_source = | ||
| 103 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)); | ||
| 104 | const auto key_generation_source = | ||
| 105 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); | ||
| 106 | |||
| 107 | if (HasKey(S128KeyType::Master, crypto_revision)) { | ||
| 108 | for (auto kak_type : | ||
| 109 | {KeyAreaKeyType::Application, KeyAreaKeyType::Ocean, KeyAreaKeyType::System}) { | ||
| 110 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 111 | static_cast<u64>(kak_type))) { | ||
| 112 | const auto source = | ||
| 113 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 114 | static_cast<u64>(kak_type)); | ||
| 115 | const auto kek = | ||
| 116 | GenerateKeyEncryptionKey(source, GetKey(S128KeyType::Master, crypto_revision), | ||
| 117 | kek_generation_source, key_generation_source); | ||
| 118 | SetKey(S128KeyType::KeyArea, kek, crypto_revision, static_cast<u64>(kak_type)); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | AESCipher<Key128> master_cipher(GetKey(S128KeyType::Master, crypto_revision), Mode::ECB); | ||
| 123 | for (auto key_type : {SourceKeyType::Titlekek, SourceKeyType::Package2}) { | ||
| 124 | if (HasKey(S128KeyType::Source, static_cast<u64>(key_type))) { | ||
| 125 | Key128 key{}; | ||
| 126 | master_cipher.Transcode( | ||
| 127 | GetKey(S128KeyType::Source, static_cast<u64>(key_type)).data(), key.size(), | ||
| 128 | key.data(), Op::Decrypt); | ||
| 129 | SetKey(key_type == SourceKeyType::Titlekek ? S128KeyType::Titlekek | ||
| 130 | : S128KeyType::Package2, | ||
| 131 | key, crypto_revision); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) { | ||
| 138 | AESCipher<Key128> mac_cipher(keyblob_key, Mode::ECB); | ||
| 139 | Key128 mac_key{}; | ||
| 140 | mac_cipher.Transcode(mac_source.data(), mac_key.size(), mac_key.data(), Op::Decrypt); | ||
| 141 | return mac_key; | ||
| 142 | } | ||
| 143 | |||
| 69 | boost::optional<Key128> DeriveSDSeed() { | 144 | boost::optional<Key128> DeriveSDSeed() { |
| 70 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | 145 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + |
| 71 | "/system/save/8000000000000043", | 146 | "/system/save/8000000000000043", |
| @@ -166,10 +241,10 @@ std::vector<TicketRaw> GetTicketblob(const FileUtil::IOFile& ticket_save) { | |||
| 166 | for (std::size_t offset = 0; offset + 0x4 < buffer.size(); ++offset) { | 241 | for (std::size_t offset = 0; offset + 0x4 < buffer.size(); ++offset) { |
| 167 | if (buffer[offset] == 0x4 && buffer[offset + 1] == 0x0 && buffer[offset + 2] == 0x1 && | 242 | if (buffer[offset] == 0x4 && buffer[offset + 1] == 0x0 && buffer[offset + 2] == 0x1 && |
| 168 | buffer[offset + 3] == 0x0) { | 243 | buffer[offset + 3] == 0x0) { |
| 169 | TicketRaw next{}; | 244 | out.emplace_back(); |
| 245 | auto& next = out.back(); | ||
| 170 | std::memcpy(&next, buffer.data() + offset, sizeof(TicketRaw)); | 246 | std::memcpy(&next, buffer.data() + offset, sizeof(TicketRaw)); |
| 171 | offset += next.size(); | 247 | offset += next.size(); |
| 172 | out.push_back(next); | ||
| 173 | } | 248 | } |
| 174 | } | 249 | } |
| 175 | 250 | ||
| @@ -180,8 +255,7 @@ template <size_t size> | |||
| 180 | static std::array<u8, size> operator^(const std::array<u8, size>& lhs, | 255 | static std::array<u8, size> operator^(const std::array<u8, size>& lhs, |
| 181 | const std::array<u8, size>& rhs) { | 256 | const std::array<u8, size>& rhs) { |
| 182 | std::array<u8, size> out{}; | 257 | std::array<u8, size> out{}; |
| 183 | for (size_t i = 0; i < size; ++i) | 258 | std::transform(lhs.begin(), lhs.end(), rhs.begin(), out.begin(), std::bit_xor<>()); |
| 184 | out[i] = lhs[i] ^ rhs[i]; | ||
| 185 | return out; | 259 | return out; |
| 186 | } | 260 | } |
| 187 | 261 | ||
| @@ -193,17 +267,32 @@ static std::array<u8, target_size> MGF1(const std::array<u8, in_size>& seed) { | |||
| 193 | std::vector<u8> out; | 267 | std::vector<u8> out; |
| 194 | size_t i = 0; | 268 | size_t i = 0; |
| 195 | while (out.size() < target_size) { | 269 | while (out.size() < target_size) { |
| 196 | out.resize(out.size() + 0x20, 0); | 270 | out.resize(out.size() + 0x20); |
| 197 | seed_exp[in_size + 3] = i; | 271 | seed_exp[in_size + 3] = i; |
| 198 | mbedtls_sha256(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0); | 272 | mbedtls_sha256(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0); |
| 199 | ++i; | 273 | ++i; |
| 200 | } | 274 | } |
| 201 | 275 | ||
| 202 | std::array<u8, target_size> target{}; | 276 | std::array<u8, target_size> target; |
| 203 | std::memcpy(target.data(), out.data(), target_size); | 277 | std::memcpy(target.data(), out.data(), target_size); |
| 204 | return target; | 278 | return target; |
| 205 | } | 279 | } |
| 206 | 280 | ||
| 281 | template <size_t size> | ||
| 282 | static boost::optional<u64> FindTicketOffset(const std::array<u8, size>& data) { | ||
| 283 | u64 offset = 0; | ||
| 284 | for (size_t i = 0x20; i < data.size() - 0x10; ++i) { | ||
| 285 | if (data[i] == 0x1) { | ||
| 286 | offset = i + 1; | ||
| 287 | break; | ||
| 288 | } else if (data[i] != 0x0) { | ||
| 289 | return boost::none; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | return offset; | ||
| 294 | } | ||
| 295 | |||
| 207 | boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | 296 | boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, |
| 208 | const RSAKeyPair<2048>& key) { | 297 | const RSAKeyPair<2048>& key) { |
| 209 | u32 cert_authority; | 298 | u32 cert_authority; |
| @@ -215,14 +304,17 @@ boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | |||
| 215 | "Attempting to parse ticket with non-standard certificate authority {:08X}.", | 304 | "Attempting to parse ticket with non-standard certificate authority {:08X}.", |
| 216 | cert_authority); | 305 | cert_authority); |
| 217 | 306 | ||
| 218 | Key128 rights_id{}; | 307 | Key128 rights_id; |
| 219 | std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); | 308 | std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); |
| 220 | 309 | ||
| 310 | if (rights_id == Key128{}) | ||
| 311 | return boost::none; | ||
| 312 | |||
| 221 | Key128 key_temp{}; | 313 | Key128 key_temp{}; |
| 222 | 314 | ||
| 223 | if (!std::any_of(ticket.begin() + 0x190, ticket.begin() + 0x280, [](u8 b) { return b != 0; })) { | 315 | if (!std::any_of(ticket.begin() + 0x190, ticket.begin() + 0x280, [](u8 b) { return b != 0; })) { |
| 224 | std::memcpy(key_temp.data(), ticket.data() + 0x180, key_temp.size()); | 316 | std::memcpy(key_temp.data(), ticket.data() + 0x180, key_temp.size()); |
| 225 | return std::pair<Key128, Key128>{rights_id, key_temp}; | 317 | return std::make_pair(rights_id, key_temp); |
| 226 | } | 318 | } |
| 227 | 319 | ||
| 228 | mbedtls_mpi D; // RSA Private Exponent | 320 | mbedtls_mpi D; // RSA Private Exponent |
| @@ -241,13 +333,13 @@ boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | |||
| 241 | 333 | ||
| 242 | mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr); | 334 | mbedtls_mpi_exp_mod(&M, &S, &D, &N, nullptr); |
| 243 | 335 | ||
| 244 | std::array<u8, 0x100> rsa_step{}; | 336 | std::array<u8, 0x100> rsa_step; |
| 245 | mbedtls_mpi_write_binary(&M, rsa_step.data(), rsa_step.size()); | 337 | mbedtls_mpi_write_binary(&M, rsa_step.data(), rsa_step.size()); |
| 246 | 338 | ||
| 247 | u8 m_0 = rsa_step[0]; | 339 | u8 m_0 = rsa_step[0]; |
| 248 | std::array<u8, 0x20> m_1{}; | 340 | std::array<u8, 0x20> m_1; |
| 249 | std::memcpy(m_1.data(), rsa_step.data() + 0x01, m_1.size()); | 341 | std::memcpy(m_1.data(), rsa_step.data() + 0x01, m_1.size()); |
| 250 | std::array<u8, 0xDF> m_2{}; | 342 | std::array<u8, 0xDF> m_2; |
| 251 | std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); | 343 | std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); |
| 252 | 344 | ||
| 253 | if (m_0 != 0) | 345 | if (m_0 != 0) |
| @@ -256,21 +348,14 @@ boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | |||
| 256 | m_1 = m_1 ^ MGF1<0x20>(m_2); | 348 | m_1 = m_1 ^ MGF1<0x20>(m_2); |
| 257 | m_2 = m_2 ^ MGF1<0xDF>(m_1); | 349 | m_2 = m_2 ^ MGF1<0xDF>(m_1); |
| 258 | 350 | ||
| 259 | u64 offset = 0; | 351 | const auto offset = FindTicketOffset(m_2); |
| 260 | for (size_t i = 0x20; i < m_2.size() - 0x10; ++i) { | 352 | if (offset == boost::none) |
| 261 | if (m_2[i] == 0x1) { | 353 | return boost::none; |
| 262 | offset = i + 1; | 354 | ASSERT(offset.get() > 0); |
| 263 | break; | ||
| 264 | } else if (m_2[i] != 0x0) { | ||
| 265 | return boost::none; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | ASSERT(offset > 0); | ||
| 270 | 355 | ||
| 271 | std::memcpy(key_temp.data(), m_2.data() + offset, key_temp.size()); | 356 | std::memcpy(key_temp.data(), m_2.data() + offset.get(), key_temp.size()); |
| 272 | 357 | ||
| 273 | return std::pair<Key128, Key128>{rights_id, key_temp}; | 358 | return std::make_pair(rights_id, key_temp); |
| 274 | } | 359 | } |
| 275 | 360 | ||
| 276 | KeyManager::KeyManager() { | 361 | KeyManager::KeyManager() { |
| @@ -293,10 +378,11 @@ KeyManager::KeyManager() { | |||
| 293 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "console.keys_autogenerated", false); | 378 | AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, "console.keys_autogenerated", false); |
| 294 | } | 379 | } |
| 295 | 380 | ||
| 296 | static bool ValidCryptoRevisionString(const std::string& base, size_t begin, size_t length) { | 381 | static bool ValidCryptoRevisionString(std::string_view base, size_t begin, size_t length) { |
| 297 | if (base.size() < begin + length) | 382 | if (base.size() < begin + length) |
| 298 | return false; | 383 | return false; |
| 299 | return std::all_of(base.begin() + begin, base.begin() + begin + length, ::isdigit); | 384 | return std::all_of(base.begin() + begin, base.begin() + begin + length, |
| 385 | [](u8 c) { return std::isdigit(c); }); | ||
| 300 | } | 386 | } |
| 301 | 387 | ||
| 302 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | 388 | void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { |
| @@ -351,16 +437,7 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | |||
| 351 | const auto index = std::stoul(out[0].substr(18, 2), nullptr, 16); | 437 | const auto index = std::stoul(out[0].substr(18, 2), nullptr, 16); |
| 352 | encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]); | 438 | encrypted_keyblobs[index] = Common::HexStringToArray<0xB0>(out[1]); |
| 353 | } else { | 439 | } else { |
| 354 | for (const auto& kv : std::map<std::pair<S128KeyType, u64>, std::string>{ | 440 | for (const auto& kv : KEYS_VARIABLE_LENGTH) { |
| 355 | {{S128KeyType::Master, 0}, "master_key_"}, | ||
| 356 | {{S128KeyType::Package1, 0}, "package1_key_"}, | ||
| 357 | {{S128KeyType::Package2, 0}, "package2_key_"}, | ||
| 358 | {{S128KeyType::Titlekek, 0}, "titlekek_"}, | ||
| 359 | {{S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob)}, | ||
| 360 | "keyblob_key_source_"}, | ||
| 361 | {{S128KeyType::Keyblob, 0}, "keyblob_key_"}, | ||
| 362 | {{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"}, | ||
| 363 | }) { | ||
| 364 | if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) | 441 | if (!ValidCryptoRevisionString(out[0], kv.second.size(), 2)) |
| 365 | continue; | 442 | continue; |
| 366 | if (out[0].compare(0, kv.second.size(), kv.second) == 0) { | 443 | if (out[0].compare(0, kv.second.size(), kv.second) == 0) { |
| @@ -379,9 +456,9 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) { | |||
| 379 | } | 456 | } |
| 380 | } | 457 | } |
| 381 | 458 | ||
| 382 | const static std::array<const char*, 3> kak_names = { | 459 | static constexpr std::array<const char*, 3> kak_names = { |
| 383 | "key_area_key_application_", "key_area_key_ocean_", "key_area_key_system_"}; | 460 | "key_area_key_application_", "key_area_key_ocean_", "key_area_key_system_"}; |
| 384 | for (size_t j = 0; j < 3; ++j) { | 461 | for (size_t j = 0; j < kak_names.size(); ++j) { |
| 385 | const auto& match = kak_names[j]; | 462 | const auto& match = kak_names[j]; |
| 386 | if (out[0].compare(0, std::strlen(match), match) == 0) { | 463 | if (out[0].compare(0, std::strlen(match), match) == 0) { |
| 387 | const auto index = | 464 | const auto index = |
| @@ -403,7 +480,7 @@ void KeyManager::AttemptLoadKeyFile(const std::string& dir1, const std::string& | |||
| 403 | LoadFromFile(dir2 + DIR_SEP + filename, title); | 480 | LoadFromFile(dir2 + DIR_SEP + filename, title); |
| 404 | } | 481 | } |
| 405 | 482 | ||
| 406 | bool KeyManager::BaseDeriveNecessary() { | 483 | bool KeyManager::BaseDeriveNecessary() const { |
| 407 | const auto check_key_existence = [this](auto key_type, u64 index1 = 0, u64 index2 = 0) { | 484 | const auto check_key_existence = [this](auto key_type, u64 index1 = 0, u64 index2 = 0) { |
| 408 | return !HasKey(key_type, index1, index2); | 485 | return !HasKey(key_type, index1, index2); |
| 409 | }; | 486 | }; |
| @@ -512,9 +589,9 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { | |||
| 512 | 589 | ||
| 513 | // Variable cases | 590 | // Variable cases |
| 514 | if (id == S128KeyType::KeyArea) { | 591 | if (id == S128KeyType::KeyArea) { |
| 515 | const static std::array<const char*, 3> kak_names = {"key_area_key_application_{:02X}", | 592 | static constexpr std::array<const char*, 3> kak_names = {"key_area_key_application_{:02X}", |
| 516 | "key_area_key_ocean_{:02X}", | 593 | "key_area_key_ocean_{:02X}", |
| 517 | "key_area_key_system_{:02X}"}; | 594 | "key_area_key_system_{:02X}"}; |
| 518 | WriteKeyToFile(category, fmt::format(kak_names.at(field2), field1), key); | 595 | WriteKeyToFile(category, fmt::format(kak_names.at(field2), field1), key); |
| 519 | } else if (id == S128KeyType::Master) { | 596 | } else if (id == S128KeyType::Master) { |
| 520 | WriteKeyToFile(category, fmt::format("master_key_{:02X}", field1), key); | 597 | WriteKeyToFile(category, fmt::format("master_key_{:02X}", field1), key); |
| @@ -575,11 +652,11 @@ void KeyManager::DeriveSDSeedLazy() { | |||
| 575 | SetKey(S128KeyType::SDSeed, res.get()); | 652 | SetKey(S128KeyType::SDSeed, res.get()); |
| 576 | } | 653 | } |
| 577 | 654 | ||
| 578 | static Key128 CalculateCMAC(const u8* source, size_t size, Key128 key) { | 655 | static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) { |
| 579 | Key128 out{}; | 656 | Key128 out{}; |
| 580 | 657 | ||
| 581 | mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), key.data(), 0x80, | 658 | mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), key.data(), |
| 582 | source, size, out.data()); | 659 | key.size() * 8, source, size, out.data()); |
| 583 | return out; | 660 | return out; |
| 584 | } | 661 | } |
| 585 | 662 | ||
| @@ -610,12 +687,12 @@ void KeyManager::DeriveBase() { | |||
| 610 | else if (has_bis(3) && !has_bis(2)) | 687 | else if (has_bis(3) && !has_bis(2)) |
| 611 | copy_bis(3, 2); | 688 | copy_bis(3, 2); |
| 612 | 689 | ||
| 613 | std::bitset<32> revisions{}; | 690 | std::bitset<32> revisions(0xFFFFFFFF); |
| 614 | revisions.set(); | 691 | for (size_t i = 0; i < revisions.size(); ++i) { |
| 615 | for (size_t i = 0; i < 32; ++i) { | ||
| 616 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i) || | 692 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Keyblob), i) || |
| 617 | encrypted_keyblobs[i] == std::array<u8, 0xB0>{}) | 693 | encrypted_keyblobs[i] == std::array<u8, 0xB0>{}) { |
| 618 | revisions.reset(i); | 694 | revisions.reset(i); |
| 695 | } | ||
| 619 | } | 696 | } |
| 620 | 697 | ||
| 621 | if (!revisions.any()) | 698 | if (!revisions.any()) |
| @@ -624,12 +701,8 @@ void KeyManager::DeriveBase() { | |||
| 624 | const auto sbk = GetKey(S128KeyType::SecureBoot); | 701 | const auto sbk = GetKey(S128KeyType::SecureBoot); |
| 625 | const auto tsec = GetKey(S128KeyType::TSEC); | 702 | const auto tsec = GetKey(S128KeyType::TSEC); |
| 626 | const auto master_source = GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master)); | 703 | const auto master_source = GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master)); |
| 627 | const auto kek_generation_source = | ||
| 628 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKekGeneration)); | ||
| 629 | const auto key_generation_source = | ||
| 630 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::AESKeyGeneration)); | ||
| 631 | 704 | ||
| 632 | for (size_t i = 0; i < 32; ++i) { | 705 | for (size_t i = 0; i < revisions.size(); ++i) { |
| 633 | if (!revisions[i]) | 706 | if (!revisions[i]) |
| 634 | continue; | 707 | continue; |
| 635 | 708 | ||
| @@ -643,13 +716,8 @@ void KeyManager::DeriveBase() { | |||
| 643 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))) | 716 | if (!HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))) |
| 644 | continue; | 717 | continue; |
| 645 | 718 | ||
| 646 | const auto mac_source = | 719 | const auto mac_key = DeriveKeyblobMACKey( |
| 647 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC)); | 720 | key, GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyblobMAC))); |
| 648 | |||
| 649 | AESCipher<Key128> mac_cipher(key, Mode::ECB); | ||
| 650 | Key128 mac_key{}; | ||
| 651 | mac_cipher.Transcode(mac_source.data(), mac_key.size(), mac_key.data(), Op::Decrypt); | ||
| 652 | |||
| 653 | SetKey(S128KeyType::KeyblobMAC, mac_key, i); | 721 | SetKey(S128KeyType::KeyblobMAC, mac_key, i); |
| 654 | 722 | ||
| 655 | Key128 cmac = CalculateCMAC(encrypted_keyblobs[i].data() + 0x10, 0xA0, mac_key); | 723 | Key128 cmac = CalculateCMAC(encrypted_keyblobs[i].data() + 0x10, 0xA0, mac_key); |
| @@ -657,39 +725,27 @@ void KeyManager::DeriveBase() { | |||
| 657 | continue; | 725 | continue; |
| 658 | 726 | ||
| 659 | // Decrypt keyblob | 727 | // Decrypt keyblob |
| 660 | bool has_keyblob = keyblobs[i] != std::array<u8, 0x90>{}; | 728 | if (keyblobs[i] == std::array<u8, 0x90>{}) { |
| 661 | 729 | keyblobs[i] = DecryptKeyblob(encrypted_keyblobs[i], key); | |
| 662 | AESCipher<Key128> cipher(key, Mode::CTR); | ||
| 663 | cipher.SetIV(std::vector<u8>(encrypted_keyblobs[i].data() + 0x10, | ||
| 664 | encrypted_keyblobs[i].data() + 0x20)); | ||
| 665 | cipher.Transcode(encrypted_keyblobs[i].data() + 0x20, keyblobs[i].size(), | ||
| 666 | keyblobs[i].data(), Op::Decrypt); | ||
| 667 | |||
| 668 | if (!has_keyblob) { | ||
| 669 | WriteKeyToFile<0x90>(KeyCategory::Console, fmt::format("keyblob_{:02X}", i), | 730 | WriteKeyToFile<0x90>(KeyCategory::Console, fmt::format("keyblob_{:02X}", i), |
| 670 | keyblobs[i]); | 731 | keyblobs[i]); |
| 671 | } | 732 | } |
| 672 | 733 | ||
| 673 | Key128 package1{}; | 734 | Key128 package1; |
| 674 | std::memcpy(package1.data(), keyblobs[i].data() + 0x80, sizeof(Key128)); | 735 | std::memcpy(package1.data(), keyblobs[i].data() + 0x80, sizeof(Key128)); |
| 675 | SetKey(S128KeyType::Package1, package1, i); | 736 | SetKey(S128KeyType::Package1, package1, i); |
| 676 | 737 | ||
| 677 | // Derive master key | 738 | // Derive master key |
| 678 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master))) { | 739 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::Master))) { |
| 679 | Key128 master_root{}; | 740 | SetKey(S128KeyType::Master, |
| 680 | std::memcpy(master_root.data(), keyblobs[i].data(), sizeof(Key128)); | 741 | DeriveMasterKey(keyblobs[i], GetKey(S128KeyType::Source, |
| 681 | 742 | static_cast<u64>(SourceKeyType::Master))), | |
| 682 | AESCipher<Key128> master_cipher(master_root, Mode::ECB); | 743 | i); |
| 683 | |||
| 684 | Key128 master{}; | ||
| 685 | master_cipher.Transcode(master_source.data(), master_source.size(), master.data(), | ||
| 686 | Op::Decrypt); | ||
| 687 | SetKey(S128KeyType::Master, master, i); | ||
| 688 | } | 744 | } |
| 689 | } | 745 | } |
| 690 | 746 | ||
| 691 | revisions.set(); | 747 | revisions.set(); |
| 692 | for (size_t i = 0; i < 32; ++i) { | 748 | for (size_t i = 0; i < revisions.size(); ++i) { |
| 693 | if (!HasKey(S128KeyType::Master, i)) | 749 | if (!HasKey(S128KeyType::Master, i)) |
| 694 | revisions.reset(i); | 750 | revisions.reset(i); |
| 695 | } | 751 | } |
| @@ -697,39 +753,12 @@ void KeyManager::DeriveBase() { | |||
| 697 | if (!revisions.any()) | 753 | if (!revisions.any()) |
| 698 | return; | 754 | return; |
| 699 | 755 | ||
| 700 | for (size_t i = 0; i < 32; ++i) { | 756 | for (size_t i = 0; i < revisions.size(); ++i) { |
| 701 | if (!revisions[i]) | 757 | if (!revisions[i]) |
| 702 | continue; | 758 | continue; |
| 703 | 759 | ||
| 704 | // Derive general purpose keys | 760 | // Derive general purpose keys |
| 705 | if (HasKey(S128KeyType::Master, i)) { | 761 | DeriveGeneralPurposeKeys(i); |
| 706 | for (auto kak_type : | ||
| 707 | {KeyAreaKeyType::Application, KeyAreaKeyType::Ocean, KeyAreaKeyType::System}) { | ||
| 708 | if (HasKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 709 | static_cast<u64>(kak_type))) { | ||
| 710 | const auto source = | ||
| 711 | GetKey(S128KeyType::Source, static_cast<u64>(SourceKeyType::KeyAreaKey), | ||
| 712 | static_cast<u64>(kak_type)); | ||
| 713 | const auto kek = | ||
| 714 | GenerateKeyEncryptionKey(source, GetKey(S128KeyType::Master, i), | ||
| 715 | kek_generation_source, key_generation_source); | ||
| 716 | SetKey(S128KeyType::KeyArea, kek, i, static_cast<u64>(kak_type)); | ||
| 717 | } | ||
| 718 | } | ||
| 719 | |||
| 720 | AESCipher<Key128> master_cipher(GetKey(S128KeyType::Master, i), Mode::ECB); | ||
| 721 | for (auto key_type : {SourceKeyType::Titlekek, SourceKeyType::Package2}) { | ||
| 722 | if (HasKey(S128KeyType::Source, static_cast<u64>(key_type))) { | ||
| 723 | Key128 key{}; | ||
| 724 | master_cipher.Transcode( | ||
| 725 | GetKey(S128KeyType::Source, static_cast<u64>(key_type)).data(), key.size(), | ||
| 726 | key.data(), Op::Decrypt); | ||
| 727 | SetKey(key_type == SourceKeyType::Titlekek ? S128KeyType::Titlekek | ||
| 728 | : S128KeyType::Package2, | ||
| 729 | key, i); | ||
| 730 | } | ||
| 731 | } | ||
| 732 | } | ||
| 733 | } | 762 | } |
| 734 | 763 | ||
| 735 | if (HasKey(S128KeyType::Master, 0) && | 764 | if (HasKey(S128KeyType::Master, 0) && |
| @@ -751,7 +780,7 @@ void KeyManager::DeriveBase() { | |||
| 751 | } | 780 | } |
| 752 | } | 781 | } |
| 753 | 782 | ||
| 754 | void KeyManager::DeriveETicket(PartitionDataManager data) { | 783 | void KeyManager::DeriveETicket(PartitionDataManager& data) { |
| 755 | // ETicket keys | 784 | // ETicket keys |
| 756 | const auto es = Service::FileSystem::GetUnionContents()->GetEntry( | 785 | const auto es = Service::FileSystem::GetUnionContents()->GetEntry( |
| 757 | 0x0100000000000033, FileSys::ContentRecordType::Program); | 786 | 0x0100000000000033, FileSys::ContentRecordType::Program); |
| @@ -769,30 +798,30 @@ void KeyManager::DeriveETicket(PartitionDataManager data) { | |||
| 769 | 798 | ||
| 770 | const auto bytes = main->ReadAllBytes(); | 799 | const auto bytes = main->ReadAllBytes(); |
| 771 | 800 | ||
| 772 | using namespace Common; | 801 | const auto eticket_kek = FindKeyFromHex16(bytes, eticket_source_hashes[0]); |
| 773 | const auto eticket_kek = FindKeyFromHex(bytes, eticket_source_hashes[0]); | 802 | const auto eticket_kekek = FindKeyFromHex16(bytes, eticket_source_hashes[1]); |
| 774 | const auto eticket_kekek = FindKeyFromHex(bytes, eticket_source_hashes[1]); | ||
| 775 | 803 | ||
| 776 | const auto seed3 = data.GetRSAKekSeed3(); | 804 | const auto seed3 = data.GetRSAKekSeed3(); |
| 777 | const auto mask0 = data.GetRSAKekMask0(); | 805 | const auto mask0 = data.GetRSAKekMask0(); |
| 778 | 806 | ||
| 779 | if (eticket_kek != Key128{}) | 807 | if (eticket_kek != Key128{}) |
| 780 | SetKey(S128KeyType::Source, eticket_kek, static_cast<size_t>(SourceKeyType::ETicketKek)); | 808 | SetKey(S128KeyType::Source, eticket_kek, static_cast<size_t>(SourceKeyType::ETicketKek)); |
| 781 | if (eticket_kekek != Key128{}) | 809 | if (eticket_kekek != Key128{}) { |
| 782 | SetKey(S128KeyType::Source, eticket_kekek, | 810 | SetKey(S128KeyType::Source, eticket_kekek, |
| 783 | static_cast<size_t>(SourceKeyType::ETicketKekek)); | 811 | static_cast<size_t>(SourceKeyType::ETicketKekek)); |
| 812 | } | ||
| 784 | if (seed3 != Key128{}) | 813 | if (seed3 != Key128{}) |
| 785 | SetKey(S128KeyType::RSAKek, seed3, static_cast<size_t>(RSAKekType::Seed3)); | 814 | SetKey(S128KeyType::RSAKek, seed3, static_cast<size_t>(RSAKekType::Seed3)); |
| 786 | if (mask0 != Key128{}) | 815 | if (mask0 != Key128{}) |
| 787 | SetKey(S128KeyType::RSAKek, mask0, static_cast<size_t>(RSAKekType::Mask0)); | 816 | SetKey(S128KeyType::RSAKek, mask0, static_cast<size_t>(RSAKekType::Mask0)); |
| 788 | |||
| 789 | if (eticket_kek == Key128{} || eticket_kekek == Key128{} || seed3 == Key128{} || | 817 | if (eticket_kek == Key128{} || eticket_kekek == Key128{} || seed3 == Key128{} || |
| 790 | mask0 == Key128{}) | 818 | mask0 == Key128{}) { |
| 791 | return; | 819 | return; |
| 820 | } | ||
| 792 | 821 | ||
| 793 | Key128 rsa_oaep_kek{}; | 822 | Key128 rsa_oaep_kek{}; |
| 794 | for (size_t i = 0; i < rsa_oaep_kek.size(); ++i) | 823 | std::transform(seed3.begin(), seed3.end(), mask0.begin(), rsa_oaep_kek.begin(), |
| 795 | rsa_oaep_kek[i] = seed3[i] ^ mask0[i]; | 824 | std::bit_xor<>()); |
| 796 | 825 | ||
| 797 | if (rsa_oaep_kek == Key128{}) | 826 | if (rsa_oaep_kek == Key128{}) |
| 798 | return; | 827 | return; |
| @@ -818,8 +847,7 @@ void KeyManager::DeriveETicket(PartitionDataManager data) { | |||
| 818 | SetKey(S128KeyType::ETicketRSAKek, eticket_final); | 847 | SetKey(S128KeyType::ETicketRSAKek, eticket_final); |
| 819 | 848 | ||
| 820 | // Titlekeys | 849 | // Titlekeys |
| 821 | data.DecryptProdInfo(GetKey(S128KeyType::BIS), | 850 | data.DecryptProdInfo(GetBISKey(0)); |
| 822 | GetKey(S128KeyType::BIS, 0, static_cast<u64>(BISKeyType::Tweak))); | ||
| 823 | 851 | ||
| 824 | const auto eticket_extended_kek = data.GetETicketExtendedKek(); | 852 | const auto eticket_extended_kek = data.GetETicketExtendedKek(); |
| 825 | 853 | ||
| @@ -851,8 +879,8 @@ void KeyManager::DeriveETicket(PartitionDataManager data) { | |||
| 851 | const auto pair = ParseTicket(raw, rsa_key); | 879 | const auto pair = ParseTicket(raw, rsa_key); |
| 852 | if (pair == boost::none) | 880 | if (pair == boost::none) |
| 853 | continue; | 881 | continue; |
| 854 | auto [rid, key] = pair.value(); | 882 | const auto& [rid, key] = pair.value(); |
| 855 | u128 rights_id{}; | 883 | u128 rights_id; |
| 856 | std::memcpy(rights_id.data(), rid.data(), rid.size()); | 884 | std::memcpy(rights_id.data(), rid.data(), rid.size()); |
| 857 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | 885 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); |
| 858 | } | 886 | } |
| @@ -870,14 +898,14 @@ void KeyManager::SetKeyWrapped(S256KeyType id, Key256 key, u64 field1, u64 field | |||
| 870 | SetKey(id, key, field1, field2); | 898 | SetKey(id, key, field1, field2); |
| 871 | } | 899 | } |
| 872 | 900 | ||
| 873 | void KeyManager::PopulateFromPartitionData(PartitionDataManager data) { | 901 | void KeyManager::PopulateFromPartitionData(PartitionDataManager& data) { |
| 874 | if (!BaseDeriveNecessary()) | 902 | if (!BaseDeriveNecessary()) |
| 875 | return; | 903 | return; |
| 876 | 904 | ||
| 877 | if (!data.HasBoot0()) | 905 | if (!data.HasBoot0()) |
| 878 | return; | 906 | return; |
| 879 | 907 | ||
| 880 | for (size_t i = 0; i < 0x20; ++i) { | 908 | for (size_t i = 0; i < encrypted_keyblobs.size(); ++i) { |
| 881 | if (encrypted_keyblobs[i] != std::array<u8, 0xB0>{}) | 909 | if (encrypted_keyblobs[i] != std::array<u8, 0xB0>{}) |
| 882 | continue; | 910 | continue; |
| 883 | encrypted_keyblobs[i] = data.GetEncryptedKeyblob(i); | 911 | encrypted_keyblobs[i] = data.GetEncryptedKeyblob(i); |
| @@ -907,15 +935,15 @@ void KeyManager::PopulateFromPartitionData(PartitionDataManager data) { | |||
| 907 | DeriveBase(); | 935 | DeriveBase(); |
| 908 | 936 | ||
| 909 | Key128 latest_master{}; | 937 | Key128 latest_master{}; |
| 910 | for (s8 i = 0x1F; i > 0; --i) { | 938 | for (s8 i = 0x1F; i >= 0; --i) { |
| 911 | if (GetKey(S128KeyType::Master, i) != Key128{}) { | 939 | if (GetKey(S128KeyType::Master, static_cast<u8>(i)) != Key128{}) { |
| 912 | latest_master = GetKey(S128KeyType::Master, i); | 940 | latest_master = GetKey(S128KeyType::Master, static_cast<u8>(i)); |
| 913 | break; | 941 | break; |
| 914 | } | 942 | } |
| 915 | } | 943 | } |
| 916 | 944 | ||
| 917 | const auto masters = data.GetTZMasterKeys(latest_master); | 945 | const auto masters = data.GetTZMasterKeys(latest_master); |
| 918 | for (size_t i = 0; i < 0x20; ++i) { | 946 | for (size_t i = 0; i < masters.size(); ++i) { |
| 919 | if (masters[i] != Key128{} && !HasKey(S128KeyType::Master, i)) | 947 | if (masters[i] != Key128{} && !HasKey(S128KeyType::Master, i)) |
| 920 | SetKey(S128KeyType::Master, masters[i], i); | 948 | SetKey(S128KeyType::Master, masters[i], i); |
| 921 | } | 949 | } |
| @@ -926,7 +954,7 @@ void KeyManager::PopulateFromPartitionData(PartitionDataManager data) { | |||
| 926 | return; | 954 | return; |
| 927 | 955 | ||
| 928 | std::array<Key128, 0x20> package2_keys{}; | 956 | std::array<Key128, 0x20> package2_keys{}; |
| 929 | for (size_t i = 0; i < 0x20; ++i) { | 957 | for (size_t i = 0; i < package2_keys.size(); ++i) { |
| 930 | if (HasKey(S128KeyType::Package2, i)) | 958 | if (HasKey(S128KeyType::Package2, i)) |
| 931 | package2_keys[i] = GetKey(S128KeyType::Package2, i); | 959 | package2_keys[i] = GetKey(S128KeyType::Package2, i); |
| 932 | } | 960 | } |
diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index d26aa59b6..a41abbdfc 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h | |||
| @@ -11,8 +11,8 @@ | |||
| 11 | #include <boost/optional.hpp> | 11 | #include <boost/optional.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/crypto/partition_data_manager.h" | ||
| 14 | #include "core/file_sys/vfs_types.h" | 15 | #include "core/file_sys/vfs_types.h" |
| 15 | #include "partition_data_manager.h" | ||
| 16 | 16 | ||
| 17 | namespace FileUtil { | 17 | namespace FileUtil { |
| 18 | class IOFile; | 18 | class IOFile; |
| @@ -154,11 +154,11 @@ public: | |||
| 154 | // 8*43 and the private file to exist. | 154 | // 8*43 and the private file to exist. |
| 155 | void DeriveSDSeedLazy(); | 155 | void DeriveSDSeedLazy(); |
| 156 | 156 | ||
| 157 | bool BaseDeriveNecessary(); | 157 | bool BaseDeriveNecessary() const; |
| 158 | void DeriveBase(); | 158 | void DeriveBase(); |
| 159 | void DeriveETicket(PartitionDataManager data); | 159 | void DeriveETicket(PartitionDataManager& data); |
| 160 | 160 | ||
| 161 | void PopulateFromPartitionData(PartitionDataManager data); | 161 | void PopulateFromPartitionData(PartitionDataManager& data); |
| 162 | 162 | ||
| 163 | private: | 163 | private: |
| 164 | std::map<KeyIndex<S128KeyType>, Key128> s128_keys; | 164 | std::map<KeyIndex<S128KeyType>, Key128> s128_keys; |
| @@ -175,6 +175,8 @@ private: | |||
| 175 | void WriteKeyToFile(KeyCategory category, std::string_view keyname, | 175 | void WriteKeyToFile(KeyCategory category, std::string_view keyname, |
| 176 | const std::array<u8, Size>& key); | 176 | const std::array<u8, Size>& key); |
| 177 | 177 | ||
| 178 | void DeriveGeneralPurposeKeys(u8 crypto_revision); | ||
| 179 | |||
| 178 | void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); | 180 | void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0); |
| 179 | void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); | 181 | void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0); |
| 180 | 182 | ||
| @@ -183,7 +185,11 @@ private: | |||
| 183 | }; | 185 | }; |
| 184 | 186 | ||
| 185 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed); | 187 | Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed); |
| 186 | Key128 DeriveKeyblobKey(Key128 sbk, Key128 tsec, Key128 source); | 188 | Key128 DeriveKeyblobKey(const Key128& sbk, const Key128& tsec, Key128 source); |
| 189 | Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source); | ||
| 190 | Key128 DeriveMasterKey(const std::array<u8, 0x90>& keyblob, const Key128& master_source); | ||
| 191 | std::array<u8, 0x90> DecryptKeyblob(const std::array<u8, 0xB0>& encrypted_keyblob, | ||
| 192 | const Key128& key); | ||
| 187 | 193 | ||
| 188 | boost::optional<Key128> DeriveSDSeed(); | 194 | boost::optional<Key128> DeriveSDSeed(); |
| 189 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys); | 195 | Loader::ResultStatus DeriveSDKeys(std::array<Key256, 2>& sd_keys, KeyManager& keys); |
diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp index 60128b0e1..d1c04e98d 100644 --- a/src/core/crypto/partition_data_manager.cpp +++ b/src/core/crypto/partition_data_manager.cpp | |||
| @@ -7,20 +7,24 @@ | |||
| 7 | // hash the new keyblob source and master key and add the hashes to | 7 | // hash the new keyblob source and master key and add the hashes to |
| 8 | // the arrays below. | 8 | // the arrays below. |
| 9 | 9 | ||
| 10 | #include <algorithm> | ||
| 10 | #include <array> | 11 | #include <array> |
| 12 | #include <cctype> | ||
| 13 | #include <cstring> | ||
| 11 | #include <boost/optional/optional.hpp> | 14 | #include <boost/optional/optional.hpp> |
| 12 | #include <mbedtls/sha256.h> | 15 | #include <mbedtls/sha256.h> |
| 13 | #include "common/assert.h" | 16 | #include "common/assert.h" |
| 17 | #include "common/common_funcs.h" | ||
| 14 | #include "common/common_types.h" | 18 | #include "common/common_types.h" |
| 15 | #include "common/hex_util.h" | 19 | #include "common/hex_util.h" |
| 16 | #include "common/logging/log.h" | 20 | #include "common/logging/log.h" |
| 17 | #include "common/string_util.h" | 21 | #include "common/string_util.h" |
| 22 | #include "core/crypto/ctr_encryption_layer.h" | ||
| 18 | #include "core/crypto/key_manager.h" | 23 | #include "core/crypto/key_manager.h" |
| 19 | #include "core/crypto/partition_data_manager.h" | 24 | #include "core/crypto/partition_data_manager.h" |
| 25 | #include "core/crypto/xts_encryption_layer.h" | ||
| 20 | #include "core/file_sys/vfs.h" | 26 | #include "core/file_sys/vfs.h" |
| 21 | #include "core/file_sys/vfs_offset.h" | 27 | #include "core/file_sys/vfs_offset.h" |
| 22 | #include "ctr_encryption_layer.h" | ||
| 23 | #include "xts_encryption_layer.h" | ||
| 24 | 28 | ||
| 25 | using namespace Common; | 29 | using namespace Common; |
| 26 | 30 | ||
| @@ -72,7 +76,7 @@ struct KIPHeader { | |||
| 72 | }; | 76 | }; |
| 73 | static_assert(sizeof(KIPHeader) == 0x100, "KIPHeader has incorrect size."); | 77 | static_assert(sizeof(KIPHeader) == 0x100, "KIPHeader has incorrect size."); |
| 74 | 78 | ||
| 75 | const static std::array<SHA256Hash, 0x10> source_hashes{ | 79 | const std::array<SHA256Hash, 0x10> source_hashes{ |
| 76 | "B24BD293259DBC7AC5D63F88E60C59792498E6FC5443402C7FFE87EE8B61A3F0"_array32, // keyblob_mac_key_source | 80 | "B24BD293259DBC7AC5D63F88E60C59792498E6FC5443402C7FFE87EE8B61A3F0"_array32, // keyblob_mac_key_source |
| 77 | "7944862A3A5C31C6720595EFD302245ABD1B54CCDCF33000557681E65C5664A4"_array32, // master_key_source | 81 | "7944862A3A5C31C6720595EFD302245ABD1B54CCDCF33000557681E65C5664A4"_array32, // master_key_source |
| 78 | "21E2DF100FC9E094DB51B47B9B1D6E94ED379DB8B547955BEF8FE08D8DD35603"_array32, // package2_key_source | 82 | "21E2DF100FC9E094DB51B47B9B1D6E94ED379DB8B547955BEF8FE08D8DD35603"_array32, // package2_key_source |
| @@ -91,7 +95,7 @@ const static std::array<SHA256Hash, 0x10> source_hashes{ | |||
| 91 | "FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"_array32, // rsa_kek_mask0 | 95 | "FC02B9D37B42D7A1452E71444F1F700311D1132E301A83B16062E72A78175085"_array32, // rsa_kek_mask0 |
| 92 | }; | 96 | }; |
| 93 | 97 | ||
| 94 | const static std::array<SHA256Hash, 0x20> keyblob_source_hashes{ | 98 | const std::array<SHA256Hash, 0x20> keyblob_source_hashes{ |
| 95 | "8A06FE274AC491436791FDB388BCDD3AB9943BD4DEF8094418CDAC150FD73786"_array32, // keyblob_key_source_00 | 99 | "8A06FE274AC491436791FDB388BCDD3AB9943BD4DEF8094418CDAC150FD73786"_array32, // keyblob_key_source_00 |
| 96 | "2D5CAEB2521FEF70B47E17D6D0F11F8CE2C1E442A979AD8035832C4E9FBCCC4B"_array32, // keyblob_key_source_01 | 100 | "2D5CAEB2521FEF70B47E17D6D0F11F8CE2C1E442A979AD8035832C4E9FBCCC4B"_array32, // keyblob_key_source_01 |
| 97 | "61C5005E713BAE780641683AF43E5F5C0E03671117F702F401282847D2FC6064"_array32, // keyblob_key_source_02 | 101 | "61C5005E713BAE780641683AF43E5F5C0E03671117F702F401282847D2FC6064"_array32, // keyblob_key_source_02 |
| @@ -129,7 +133,7 @@ const static std::array<SHA256Hash, 0x20> keyblob_source_hashes{ | |||
| 129 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1F | 133 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // keyblob_key_source_1F |
| 130 | }; | 134 | }; |
| 131 | 135 | ||
| 132 | const static std::array<SHA256Hash, 0x20> master_key_hashes{ | 136 | const std::array<SHA256Hash, 0x20> master_key_hashes{ |
| 133 | "0EE359BE3C864BB0782E1D70A718A0342C551EED28C369754F9C4F691BECF7CA"_array32, // master_key_00 | 137 | "0EE359BE3C864BB0782E1D70A718A0342C551EED28C369754F9C4F691BECF7CA"_array32, // master_key_00 |
| 134 | "4FE707B7E4ABDAF727C894AAF13B1351BFE2AC90D875F73B2E20FA94B9CC661E"_array32, // master_key_01 | 138 | "4FE707B7E4ABDAF727C894AAF13B1351BFE2AC90D875F73B2E20FA94B9CC661E"_array32, // master_key_01 |
| 135 | "79277C0237A2252EC3DFAC1F7C359C2B3D121E9DB15BB9AB4C2B4408D2F3AE09"_array32, // master_key_02 | 139 | "79277C0237A2252EC3DFAC1F7C359C2B3D121E9DB15BB9AB4C2B4408D2F3AE09"_array32, // master_key_02 |
| @@ -167,7 +171,7 @@ const static std::array<SHA256Hash, 0x20> master_key_hashes{ | |||
| 167 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1F | 171 | "0000000000000000000000000000000000000000000000000000000000000000"_array32, // master_key_1F |
| 168 | }; | 172 | }; |
| 169 | 173 | ||
| 170 | std::vector<u8> DecompressBLZ(const std::vector<u8>& in) { | 174 | static std::vector<u8> DecompressBLZ(const std::vector<u8>& in) { |
| 171 | const auto data_size = in.size() - 0xC; | 175 | const auto data_size = in.size() - 0xC; |
| 172 | 176 | ||
| 173 | u32 compressed_size{}; | 177 | u32 compressed_size{}; |
| @@ -226,10 +230,10 @@ std::vector<u8> DecompressBLZ(const std::vector<u8>& in) { | |||
| 226 | return out; | 230 | return out; |
| 227 | } | 231 | } |
| 228 | 232 | ||
| 229 | u8 CalculateMaxKeyblobSourceHash() { | 233 | static u8 CalculateMaxKeyblobSourceHash() { |
| 230 | for (s8 i = 0x1F; i >= 0; --i) { | 234 | for (s8 i = 0x1F; i >= 0; --i) { |
| 231 | if (keyblob_source_hashes[i] != SHA256Hash{}) | 235 | if (keyblob_source_hashes[i] != SHA256Hash{}) |
| 232 | return i + 1; | 236 | return static_cast<u8>(i + 1); |
| 233 | } | 237 | } |
| 234 | 238 | ||
| 235 | return 0; | 239 | return 0; |
| @@ -238,10 +242,12 @@ u8 CalculateMaxKeyblobSourceHash() { | |||
| 238 | const u8 PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH = CalculateMaxKeyblobSourceHash(); | 242 | const u8 PartitionDataManager::MAX_KEYBLOB_SOURCE_HASH = CalculateMaxKeyblobSourceHash(); |
| 239 | 243 | ||
| 240 | template <size_t key_size = 0x10> | 244 | template <size_t key_size = 0x10> |
| 241 | std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary, std::array<u8, 0x20> hash) { | 245 | std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary, |
| 242 | std::array<u8, 0x20> temp{}; | 246 | const std::array<u8, 0x20>& hash) { |
| 243 | if (binary.size() < key_size) | 247 | if (binary.size() < key_size) |
| 244 | return {}; | 248 | return {}; |
| 249 | |||
| 250 | std::array<u8, 0x20> temp{}; | ||
| 245 | for (size_t i = 0; i < binary.size() - key_size; ++i) { | 251 | for (size_t i = 0; i < binary.size() - key_size; ++i) { |
| 246 | mbedtls_sha256(binary.data() + i, key_size, temp.data(), 0); | 252 | mbedtls_sha256(binary.data() + i, key_size, temp.data(), 0); |
| 247 | 253 | ||
| @@ -256,19 +262,24 @@ std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary, std::arra | |||
| 256 | return {}; | 262 | return {}; |
| 257 | } | 263 | } |
| 258 | 264 | ||
| 259 | std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<u8>& binary, Key128 key) { | 265 | std::array<u8, 16> FindKeyFromHex16(const std::vector<u8>& binary, std::array<u8, 32> hash) { |
| 260 | SHA256Hash temp{}; | 266 | return FindKeyFromHex<0x10>(binary, hash); |
| 261 | Key128 dec_temp{}; | 267 | } |
| 268 | |||
| 269 | static std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<u8>& binary, | ||
| 270 | const Key128& key) { | ||
| 262 | if (binary.size() < 0x10) | 271 | if (binary.size() < 0x10) |
| 263 | return {}; | 272 | return {}; |
| 264 | 273 | ||
| 274 | SHA256Hash temp{}; | ||
| 275 | Key128 dec_temp{}; | ||
| 265 | std::array<Key128, 0x20> out{}; | 276 | std::array<Key128, 0x20> out{}; |
| 266 | AESCipher<Key128> cipher(key, Mode::ECB); | 277 | AESCipher<Key128> cipher(key, Mode::ECB); |
| 267 | for (size_t i = 0; i < binary.size() - 0x10; ++i) { | 278 | for (size_t i = 0; i < binary.size() - 0x10; ++i) { |
| 268 | cipher.Transcode(binary.data() + i, dec_temp.size(), dec_temp.data(), Op::Decrypt); | 279 | cipher.Transcode(binary.data() + i, dec_temp.size(), dec_temp.data(), Op::Decrypt); |
| 269 | mbedtls_sha256(dec_temp.data(), dec_temp.size(), temp.data(), 0); | 280 | mbedtls_sha256(dec_temp.data(), dec_temp.size(), temp.data(), 0); |
| 270 | 281 | ||
| 271 | for (size_t k = 0; k < 0x20; ++k) { | 282 | for (size_t k = 0; k < out.size(); ++k) { |
| 272 | if (temp == master_key_hashes[k]) { | 283 | if (temp == master_key_hashes[k]) { |
| 273 | out[k] = dec_temp; | 284 | out[k] = dec_temp; |
| 274 | break; | 285 | break; |
| @@ -282,7 +293,7 @@ std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<u8>& bi | |||
| 282 | FileSys::VirtualFile FindFileInDirWithNames(const FileSys::VirtualDir& dir, | 293 | FileSys::VirtualFile FindFileInDirWithNames(const FileSys::VirtualDir& dir, |
| 283 | const std::string& name) { | 294 | const std::string& name) { |
| 284 | auto upper = name; | 295 | auto upper = name; |
| 285 | std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper); | 296 | std::transform(upper.begin(), upper.end(), upper.begin(), [](u8 c) { return std::toupper(c); }); |
| 286 | for (const auto& fname : {name, name + ".bin", upper, upper + ".BIN"}) { | 297 | for (const auto& fname : {name, name + ".bin", upper, upper + ".BIN"}) { |
| 287 | if (dir->GetFile(fname) != nullptr) | 298 | if (dir->GetFile(fname) != nullptr) |
| 288 | return dir->GetFile(fname); | 299 | return dir->GetFile(fname); |
| @@ -303,14 +314,16 @@ PartitionDataManager::PartitionDataManager(FileSys::VirtualDir sysdata_dir) | |||
| 303 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-5-Repair-Main"), | 314 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-5-Repair-Main"), |
| 304 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-6-Repair-Sub"), | 315 | FindFileInDirWithNames(sysdata_dir, "BCPKG2-6-Repair-Sub"), |
| 305 | }), | 316 | }), |
| 306 | secure_monitor(FindFileInDirWithNames(sysdata_dir, "sm")), | 317 | secure_monitor(FindFileInDirWithNames(sysdata_dir, "secmon")), |
| 307 | package1_decrypted(FindFileInDirWithNames(sysdata_dir, "pkg_decr")), | 318 | package1_decrypted(FindFileInDirWithNames(sysdata_dir, "pkg1_decr")), |
| 308 | secure_monitor_bytes(secure_monitor == nullptr ? std::vector<u8>{} | 319 | secure_monitor_bytes(secure_monitor == nullptr ? std::vector<u8>{} |
| 309 | : secure_monitor->ReadAllBytes()), | 320 | : secure_monitor->ReadAllBytes()), |
| 310 | package1_decrypted_bytes(package1_decrypted == nullptr ? std::vector<u8>{} | 321 | package1_decrypted_bytes(package1_decrypted == nullptr ? std::vector<u8>{} |
| 311 | : package1_decrypted->ReadAllBytes()), | 322 | : package1_decrypted->ReadAllBytes()), |
| 312 | prodinfo(FindFileInDirWithNames(sysdata_dir, "PRODINFO")) {} | 323 | prodinfo(FindFileInDirWithNames(sysdata_dir, "PRODINFO")) {} |
| 313 | 324 | ||
| 325 | PartitionDataManager::~PartitionDataManager() = default; | ||
| 326 | |||
| 314 | bool PartitionDataManager::HasBoot0() const { | 327 | bool PartitionDataManager::HasBoot0() const { |
| 315 | return boot0 != nullptr; | 328 | return boot0 != nullptr; |
| 316 | } | 329 | } |
| @@ -417,6 +430,22 @@ FileSys::VirtualFile PartitionDataManager::GetPackage2Raw(Package2Type type) con | |||
| 417 | return package2.at(static_cast<size_t>(type)); | 430 | return package2.at(static_cast<size_t>(type)); |
| 418 | } | 431 | } |
| 419 | 432 | ||
| 433 | bool AttemptDecrypt(const std::array<u8, 16>& key, Package2Header& header) { | ||
| 434 | |||
| 435 | const std::vector<u8> iv(header.header_ctr.begin(), header.header_ctr.end()); | ||
| 436 | Package2Header temp = header; | ||
| 437 | AESCipher<Key128> cipher(key, Mode::CTR); | ||
| 438 | cipher.SetIV(iv); | ||
| 439 | cipher.Transcode(&temp.header_ctr, sizeof(Package2Header) - 0x100, &temp.header_ctr, | ||
| 440 | Op::Decrypt); | ||
| 441 | if (temp.magic == Common::MakeMagic('P', 'K', '2', '1')) { | ||
| 442 | header = temp; | ||
| 443 | return true; | ||
| 444 | } | ||
| 445 | |||
| 446 | return false; | ||
| 447 | } | ||
| 448 | |||
| 420 | void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> package2_keys, | 449 | void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> package2_keys, |
| 421 | Package2Type type) { | 450 | Package2Type type) { |
| 422 | FileSys::VirtualFile file = std::make_shared<FileSys::OffsetVfsFile>( | 451 | FileSys::VirtualFile file = std::make_shared<FileSys::OffsetVfsFile>( |
| @@ -429,18 +458,9 @@ void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> | |||
| 429 | 458 | ||
| 430 | u8 revision = 0xFF; | 459 | u8 revision = 0xFF; |
| 431 | if (header.magic != Common::MakeMagic('P', 'K', '2', '1')) { | 460 | if (header.magic != Common::MakeMagic('P', 'K', '2', '1')) { |
| 432 | for (size_t i = 0; i < 0x20; ++i) { | 461 | for (size_t i = 0; i < package2_keys.size(); ++i) { |
| 433 | const std::vector<u8> iv(header.header_ctr.begin(), header.header_ctr.end()); | 462 | if (AttemptDecrypt(package2_keys[i], header)) |
| 434 | Package2Header temp = header; | ||
| 435 | AESCipher<Key128> cipher(package2_keys[i], Mode::CTR); | ||
| 436 | cipher.SetIV(iv); | ||
| 437 | cipher.Transcode(&temp.header_ctr, sizeof(Package2Header) - 0x100, &temp.header_ctr, | ||
| 438 | Op::Decrypt); | ||
| 439 | if (temp.magic == Common::MakeMagic('P', 'K', '2', '1')) { | ||
| 440 | header = temp; | ||
| 441 | revision = i; | 463 | revision = i; |
| 442 | break; | ||
| 443 | } | ||
| 444 | } | 464 | } |
| 445 | } | 465 | } |
| 446 | 466 | ||
| @@ -460,7 +480,7 @@ void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> | |||
| 460 | 480 | ||
| 461 | // package2_decrypted[static_cast<size_t>(type)] = s1; | 481 | // package2_decrypted[static_cast<size_t>(type)] = s1; |
| 462 | 482 | ||
| 463 | INIHeader ini{}; | 483 | INIHeader ini; |
| 464 | std::memcpy(&ini, c.data(), sizeof(INIHeader)); | 484 | std::memcpy(&ini, c.data(), sizeof(INIHeader)); |
| 465 | if (ini.magic != Common::MakeMagic('I', 'N', 'I', '1')) | 485 | if (ini.magic != Common::MakeMagic('I', 'N', 'I', '1')) |
| 466 | return; | 486 | return; |
| @@ -468,7 +488,7 @@ void PartitionDataManager::DecryptPackage2(std::array<std::array<u8, 16>, 0x20> | |||
| 468 | std::map<u64, KIPHeader> kips{}; | 488 | std::map<u64, KIPHeader> kips{}; |
| 469 | u64 offset = sizeof(INIHeader); | 489 | u64 offset = sizeof(INIHeader); |
| 470 | for (size_t i = 0; i < ini.process_count; ++i) { | 490 | for (size_t i = 0; i < ini.process_count; ++i) { |
| 471 | KIPHeader kip{}; | 491 | KIPHeader kip; |
| 472 | std::memcpy(&kip, c.data() + offset, sizeof(KIPHeader)); | 492 | std::memcpy(&kip, c.data() + offset, sizeof(KIPHeader)); |
| 473 | if (kip.magic != Common::MakeMagic('K', 'I', 'P', '1')) | 493 | if (kip.magic != Common::MakeMagic('K', 'I', 'P', '1')) |
| 474 | return; | 494 | return; |
| @@ -565,16 +585,11 @@ FileSys::VirtualFile PartitionDataManager::GetProdInfoRaw() const { | |||
| 565 | return prodinfo; | 585 | return prodinfo; |
| 566 | } | 586 | } |
| 567 | 587 | ||
| 568 | void PartitionDataManager::DecryptProdInfo(std::array<u8, 16> bis_crypt, | 588 | void PartitionDataManager::DecryptProdInfo(std::array<u8, 0x20> bis_key) { |
| 569 | std::array<u8, 16> bis_tweak) { | ||
| 570 | if (prodinfo == nullptr) | 589 | if (prodinfo == nullptr) |
| 571 | return; | 590 | return; |
| 572 | 591 | ||
| 573 | Key256 final_key{}; | 592 | prodinfo_decrypted = std::make_shared<XTSEncryptionLayer>(prodinfo, bis_key); |
| 574 | std::memcpy(final_key.data(), bis_crypt.data(), bis_crypt.size()); | ||
| 575 | std::memcpy(final_key.data() + sizeof(Key128), bis_tweak.data(), bis_tweak.size()); | ||
| 576 | |||
| 577 | prodinfo_decrypted = std::make_shared<XTSEncryptionLayer>(prodinfo, final_key); | ||
| 578 | } | 593 | } |
| 579 | 594 | ||
| 580 | std::array<u8, 576> PartitionDataManager::GetETicketExtendedKek() const { | 595 | std::array<u8, 576> PartitionDataManager::GetETicketExtendedKek() const { |
diff --git a/src/core/crypto/partition_data_manager.h b/src/core/crypto/partition_data_manager.h index 85bb2a110..45c7fecfa 100644 --- a/src/core/crypto/partition_data_manager.h +++ b/src/core/crypto/partition_data_manager.h | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | |||
| 6 | #include <vector> | 7 | #include <vector> |
| 7 | #include "common/common_funcs.h" | 8 | #include "common/common_funcs.h" |
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| @@ -22,9 +23,10 @@ enum class Package2Type { | |||
| 22 | 23 | ||
| 23 | class PartitionDataManager { | 24 | class PartitionDataManager { |
| 24 | public: | 25 | public: |
| 25 | const static u8 MAX_KEYBLOB_SOURCE_HASH; | 26 | static const u8 MAX_KEYBLOB_SOURCE_HASH; |
| 26 | 27 | ||
| 27 | explicit PartitionDataManager(FileSys::VirtualDir sysdata_dir); | 28 | explicit PartitionDataManager(FileSys::VirtualDir sysdata_dir); |
| 29 | ~PartitionDataManager(); | ||
| 28 | 30 | ||
| 29 | // BOOT0 | 31 | // BOOT0 |
| 30 | bool HasBoot0() const; | 32 | bool HasBoot0() const; |
| @@ -77,7 +79,7 @@ public: | |||
| 77 | // PRODINFO | 79 | // PRODINFO |
| 78 | bool HasProdInfo() const; | 80 | bool HasProdInfo() const; |
| 79 | FileSys::VirtualFile GetProdInfoRaw() const; | 81 | FileSys::VirtualFile GetProdInfoRaw() const; |
| 80 | void DecryptProdInfo(std::array<u8, 0x10> bis_crypt, std::array<u8, 0x10> bis_tweak); | 82 | void DecryptProdInfo(std::array<u8, 0x20> bis_key); |
| 81 | std::array<u8, 0x240> GetETicketExtendedKek() const; | 83 | std::array<u8, 0x240> GetETicketExtendedKek() const; |
| 82 | 84 | ||
| 83 | private: | 85 | private: |
| @@ -98,7 +100,6 @@ private: | |||
| 98 | std::array<std::vector<u8>, 6> package2_spl; | 100 | std::array<std::vector<u8>, 6> package2_spl; |
| 99 | }; | 101 | }; |
| 100 | 102 | ||
| 101 | template <size_t key_size = 0x10> | 103 | std::array<u8, 0x10> FindKeyFromHex16(const std::vector<u8>& binary, std::array<u8, 0x20> hash); |
| 102 | std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary, std::array<u8, 0x20> hash); | ||
| 103 | 104 | ||
| 104 | } // namespace Core::Crypto | 105 | } // namespace Core::Crypto |
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 076fcff18..fc186dc2d 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp | |||
| @@ -173,7 +173,7 @@ GMainWindow::GMainWindow() | |||
| 173 | show(); | 173 | show(); |
| 174 | 174 | ||
| 175 | // Gen keys if necessary | 175 | // Gen keys if necessary |
| 176 | OnReinitializeKeys(false); | 176 | OnReinitializeKeys(ReinitializeKeyBehavior::NoWarning); |
| 177 | 177 | ||
| 178 | // Necessary to load titles from nand in gamelist. | 178 | // Necessary to load titles from nand in gamelist. |
| 179 | Service::FileSystem::CreateFactories(vfs); | 179 | Service::FileSystem::CreateFactories(vfs); |
| @@ -448,7 +448,7 @@ void GMainWindow::ConnectMenuEvents() { | |||
| 448 | 448 | ||
| 449 | // Help | 449 | // Help |
| 450 | connect(ui.action_Rederive, &QAction::triggered, this, | 450 | connect(ui.action_Rederive, &QAction::triggered, this, |
| 451 | std::bind(&GMainWindow::OnReinitializeKeys, this, true)); | 451 | std::bind(&GMainWindow::OnReinitializeKeys, this, ReinitializeKeyBehavior::Warning)); |
| 452 | connect(ui.action_About, &QAction::triggered, this, &GMainWindow::OnAbout); | 452 | connect(ui.action_About, &QAction::triggered, this, &GMainWindow::OnAbout); |
| 453 | } | 453 | } |
| 454 | 454 | ||
| @@ -1381,8 +1381,8 @@ void GMainWindow::OnCoreError(Core::System::ResultStatus result, std::string det | |||
| 1381 | } | 1381 | } |
| 1382 | } | 1382 | } |
| 1383 | 1383 | ||
| 1384 | void GMainWindow::OnReinitializeKeys(bool callouts) { | 1384 | void GMainWindow::OnReinitializeKeys(ReinitializeKeyBehavior behavior) { |
| 1385 | if (callouts) { | 1385 | if (behavior == ReinitializeKeyBehavior::Warning) { |
| 1386 | const auto res = QMessageBox::information( | 1386 | const auto res = QMessageBox::information( |
| 1387 | this, tr("Confirm Key Rederivation"), | 1387 | this, tr("Confirm Key Rederivation"), |
| 1388 | tr("You are about to force rederive all of your keys. \nIf you do not know what this " | 1388 | tr("You are about to force rederive all of your keys. \nIf you do not know what this " |
| @@ -1408,33 +1408,30 @@ void GMainWindow::OnReinitializeKeys(bool callouts) { | |||
| 1408 | Core::Crypto::PartitionDataManager pdm{vfs->OpenDirectory( | 1408 | Core::Crypto::PartitionDataManager pdm{vfs->OpenDirectory( |
| 1409 | FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir), FileSys::Mode::Read)}; | 1409 | FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir), FileSys::Mode::Read)}; |
| 1410 | 1410 | ||
| 1411 | const auto function = [this, &keys, &pdm]() { | 1411 | const auto function = [this, &keys, &pdm] { |
| 1412 | keys.PopulateFromPartitionData(pdm); | 1412 | keys.PopulateFromPartitionData(pdm); |
| 1413 | Service::FileSystem::CreateFactories(vfs); | 1413 | Service::FileSystem::CreateFactories(vfs); |
| 1414 | keys.DeriveETicket(pdm); | 1414 | keys.DeriveETicket(pdm); |
| 1415 | }; | 1415 | }; |
| 1416 | 1416 | ||
| 1417 | std::vector<std::string> errors; | 1417 | QString errors; |
| 1418 | 1418 | ||
| 1419 | if (!pdm.HasFuses()) | 1419 | if (!pdm.HasFuses()) |
| 1420 | errors.push_back("Missing fuses - Cannot derive SBK"); | 1420 | errors += tr("- Missing fuses - Cannot derive SBK\n"); |
| 1421 | if (!pdm.HasBoot0()) | 1421 | if (!pdm.HasBoot0()) |
| 1422 | errors.push_back("Missing BOOT0 - Cannot derive master keys"); | 1422 | errors += tr("- Missing BOOT0 - Cannot derive master keys\n"); |
| 1423 | if (!pdm.HasPackage2()) | 1423 | if (!pdm.HasPackage2()) |
| 1424 | errors.push_back("Missing BCPKG2-1-Normal-Main - Cannot derive general keys"); | 1424 | errors += tr("- Missing BCPKG2-1-Normal-Main - Cannot derive general keys\n"); |
| 1425 | if (!pdm.HasProdInfo()) | 1425 | if (!pdm.HasProdInfo()) |
| 1426 | errors.push_back("Missing PRODINFO - Cannot derive title keys"); | 1426 | errors += tr("- Missing PRODINFO - Cannot derive title keys\n"); |
| 1427 | 1427 | ||
| 1428 | if (!errors.empty()) { | 1428 | if (!errors.isEmpty()) { |
| 1429 | std::string error_str; | ||
| 1430 | for (const auto& error : errors) | ||
| 1431 | error_str += " - " + error + "\n"; | ||
| 1432 | 1429 | ||
| 1433 | QMessageBox::warning( | 1430 | QMessageBox::warning( |
| 1434 | this, tr("Warning Missing Derivation Components"), | 1431 | this, tr("Warning Missing Derivation Components"), |
| 1435 | tr("The following are missing from your configuration that may hinder key " | 1432 | tr("The following are missing from your configuration that may hinder key " |
| 1436 | "derivation. It will be attempted but may not complete.\n\n") + | 1433 | "derivation. It will be attempted but may not complete.\n\n") + |
| 1437 | QString::fromStdString(error_str)); | 1434 | errors); |
| 1438 | } | 1435 | } |
| 1439 | 1436 | ||
| 1440 | QProgressDialog prog; | 1437 | QProgressDialog prog; |
| @@ -1455,7 +1452,7 @@ void GMainWindow::OnReinitializeKeys(bool callouts) { | |||
| 1455 | 1452 | ||
| 1456 | Service::FileSystem::CreateFactories(vfs); | 1453 | Service::FileSystem::CreateFactories(vfs); |
| 1457 | 1454 | ||
| 1458 | if (callouts) { | 1455 | if (behavior == ReinitializeKeyBehavior::Warning) { |
| 1459 | game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan); | 1456 | game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan); |
| 1460 | } | 1457 | } |
| 1461 | } | 1458 | } |
diff --git a/src/yuzu/main.h b/src/yuzu/main.h index fd2c110ee..3663d6aed 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h | |||
| @@ -41,6 +41,11 @@ enum class EmulatedDirectoryTarget { | |||
| 41 | SDMC, | 41 | SDMC, |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | enum class ReinitializeKeyBehavior { | ||
| 45 | NoWarning, | ||
| 46 | Warning, | ||
| 47 | }; | ||
| 48 | |||
| 44 | namespace DiscordRPC { | 49 | namespace DiscordRPC { |
| 45 | class DiscordInterface; | 50 | class DiscordInterface; |
| 46 | } | 51 | } |
| @@ -167,7 +172,7 @@ private slots: | |||
| 167 | void HideFullscreen(); | 172 | void HideFullscreen(); |
| 168 | void ToggleWindowMode(); | 173 | void ToggleWindowMode(); |
| 169 | void OnCoreError(Core::System::ResultStatus, std::string); | 174 | void OnCoreError(Core::System::ResultStatus, std::string); |
| 170 | void OnReinitializeKeys(bool callouts); | 175 | void OnReinitializeKeys(ReinitializeKeyBehavior behavior); |
| 171 | 176 | ||
| 172 | private: | 177 | private: |
| 173 | void UpdateStatusBar(); | 178 | void UpdateStatusBar(); |