diff options
| author | 2018-09-29 11:48:51 -0400 | |
|---|---|---|
| committer | 2018-10-07 13:16:23 -0400 | |
| commit | 3ec054643e50f2845fb6a1a924b83bd71a0e2234 (patch) | |
| tree | 313f2bcfba3610aa9bad98c3a7995d475712adc2 /src/core/crypto/key_manager.cpp | |
| parent | qt: Add rederive keyset menu option (diff) | |
| download | yuzu-3ec054643e50f2845fb6a1a924b83bd71a0e2234.tar.gz yuzu-3ec054643e50f2845fb6a1a924b83bd71a0e2234.tar.xz yuzu-3ec054643e50f2845fb6a1a924b83bd71a0e2234.zip | |
partition_data_manager: Rename system files for hekate
x
Diffstat (limited to 'src/core/crypto/key_manager.cpp')
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 292 |
1 files changed, 160 insertions, 132 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 | } |