diff options
| author | 2018-10-30 05:03:25 +0100 | |
|---|---|---|
| committer | 2018-10-30 00:03:25 -0400 | |
| commit | 7a5eda59146306dedaf3e6f07f97a8c6898543dd (patch) | |
| tree | 78e07b43fb0113f95e1c8e9426d3b394b9524d4e /src/core/crypto/key_manager.cpp | |
| parent | Merge pull request #1621 from lioncash/ipc (diff) | |
| download | yuzu-7a5eda59146306dedaf3e6f07f97a8c6898543dd.tar.gz yuzu-7a5eda59146306dedaf3e6f07f97a8c6898543dd.tar.xz yuzu-7a5eda59146306dedaf3e6f07f97a8c6898543dd.zip | |
global: Use std::optional instead of boost::optional (#1578)
* get rid of boost::optional
* Remove optional references
* Use std::reference_wrapper for optional references
* Fix clang format
* Fix clang format part 2
* Adressed feedback
* Fix clang format and MacOS build
Diffstat (limited to 'src/core/crypto/key_manager.cpp')
| -rw-r--r-- | src/core/crypto/key_manager.cpp | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 89ae79eb3..904afa039 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp | |||
| @@ -141,28 +141,28 @@ Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) | |||
| 141 | return mac_key; | 141 | return mac_key; |
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | boost::optional<Key128> DeriveSDSeed() { | 144 | std::optional<Key128> DeriveSDSeed() { |
| 145 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + | 145 | const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + |
| 146 | "/system/save/8000000000000043", | 146 | "/system/save/8000000000000043", |
| 147 | "rb+"); | 147 | "rb+"); |
| 148 | if (!save_43.IsOpen()) | 148 | if (!save_43.IsOpen()) |
| 149 | return boost::none; | 149 | return {}; |
| 150 | 150 | ||
| 151 | const FileUtil::IOFile sd_private( | 151 | const FileUtil::IOFile sd_private( |
| 152 | FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + "/Nintendo/Contents/private", "rb+"); | 152 | FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + "/Nintendo/Contents/private", "rb+"); |
| 153 | if (!sd_private.IsOpen()) | 153 | if (!sd_private.IsOpen()) |
| 154 | return boost::none; | 154 | return {}; |
| 155 | 155 | ||
| 156 | std::array<u8, 0x10> private_seed{}; | 156 | std::array<u8, 0x10> private_seed{}; |
| 157 | if (sd_private.ReadBytes(private_seed.data(), private_seed.size()) != private_seed.size()) { | 157 | if (sd_private.ReadBytes(private_seed.data(), private_seed.size()) != private_seed.size()) { |
| 158 | return boost::none; | 158 | return {}; |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | std::array<u8, 0x10> buffer{}; | 161 | std::array<u8, 0x10> buffer{}; |
| 162 | std::size_t offset = 0; | 162 | std::size_t offset = 0; |
| 163 | for (; offset + 0x10 < save_43.GetSize(); ++offset) { | 163 | for (; offset + 0x10 < save_43.GetSize(); ++offset) { |
| 164 | if (!save_43.Seek(offset, SEEK_SET)) { | 164 | if (!save_43.Seek(offset, SEEK_SET)) { |
| 165 | return boost::none; | 165 | return {}; |
| 166 | } | 166 | } |
| 167 | 167 | ||
| 168 | save_43.ReadBytes(buffer.data(), buffer.size()); | 168 | save_43.ReadBytes(buffer.data(), buffer.size()); |
| @@ -172,12 +172,12 @@ boost::optional<Key128> DeriveSDSeed() { | |||
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | if (!save_43.Seek(offset + 0x10, SEEK_SET)) { | 174 | if (!save_43.Seek(offset + 0x10, SEEK_SET)) { |
| 175 | return boost::none; | 175 | return {}; |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | Key128 seed{}; | 178 | Key128 seed{}; |
| 179 | if (save_43.ReadBytes(seed.data(), seed.size()) != seed.size()) { | 179 | if (save_43.ReadBytes(seed.data(), seed.size()) != seed.size()) { |
| 180 | return boost::none; | 180 | return {}; |
| 181 | } | 181 | } |
| 182 | return seed; | 182 | return seed; |
| 183 | } | 183 | } |
| @@ -291,26 +291,26 @@ static std::array<u8, target_size> MGF1(const std::array<u8, in_size>& seed) { | |||
| 291 | } | 291 | } |
| 292 | 292 | ||
| 293 | template <size_t size> | 293 | template <size_t size> |
| 294 | static boost::optional<u64> FindTicketOffset(const std::array<u8, size>& data) { | 294 | static std::optional<u64> FindTicketOffset(const std::array<u8, size>& data) { |
| 295 | u64 offset = 0; | 295 | u64 offset = 0; |
| 296 | for (size_t i = 0x20; i < data.size() - 0x10; ++i) { | 296 | for (size_t i = 0x20; i < data.size() - 0x10; ++i) { |
| 297 | if (data[i] == 0x1) { | 297 | if (data[i] == 0x1) { |
| 298 | offset = i + 1; | 298 | offset = i + 1; |
| 299 | break; | 299 | break; |
| 300 | } else if (data[i] != 0x0) { | 300 | } else if (data[i] != 0x0) { |
| 301 | return boost::none; | 301 | return {}; |
| 302 | } | 302 | } |
| 303 | } | 303 | } |
| 304 | 304 | ||
| 305 | return offset; | 305 | return offset; |
| 306 | } | 306 | } |
| 307 | 307 | ||
| 308 | boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | 308 | std::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, |
| 309 | const RSAKeyPair<2048>& key) { | 309 | const RSAKeyPair<2048>& key) { |
| 310 | u32 cert_authority; | 310 | u32 cert_authority; |
| 311 | std::memcpy(&cert_authority, ticket.data() + 0x140, sizeof(cert_authority)); | 311 | std::memcpy(&cert_authority, ticket.data() + 0x140, sizeof(cert_authority)); |
| 312 | if (cert_authority == 0) | 312 | if (cert_authority == 0) |
| 313 | return boost::none; | 313 | return {}; |
| 314 | if (cert_authority != Common::MakeMagic('R', 'o', 'o', 't')) { | 314 | if (cert_authority != Common::MakeMagic('R', 'o', 'o', 't')) { |
| 315 | LOG_INFO(Crypto, | 315 | LOG_INFO(Crypto, |
| 316 | "Attempting to parse ticket with non-standard certificate authority {:08X}.", | 316 | "Attempting to parse ticket with non-standard certificate authority {:08X}.", |
| @@ -321,7 +321,7 @@ boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | |||
| 321 | std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); | 321 | std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); |
| 322 | 322 | ||
| 323 | if (rights_id == Key128{}) | 323 | if (rights_id == Key128{}) |
| 324 | return boost::none; | 324 | return {}; |
| 325 | 325 | ||
| 326 | Key128 key_temp{}; | 326 | Key128 key_temp{}; |
| 327 | 327 | ||
| @@ -356,17 +356,17 @@ boost::optional<std::pair<Key128, Key128>> ParseTicket(const TicketRaw& ticket, | |||
| 356 | std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); | 356 | std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); |
| 357 | 357 | ||
| 358 | if (m_0 != 0) | 358 | if (m_0 != 0) |
| 359 | return boost::none; | 359 | return {}; |
| 360 | 360 | ||
| 361 | m_1 = m_1 ^ MGF1<0x20>(m_2); | 361 | m_1 = m_1 ^ MGF1<0x20>(m_2); |
| 362 | m_2 = m_2 ^ MGF1<0xDF>(m_1); | 362 | m_2 = m_2 ^ MGF1<0xDF>(m_1); |
| 363 | 363 | ||
| 364 | const auto offset = FindTicketOffset(m_2); | 364 | const auto offset = FindTicketOffset(m_2); |
| 365 | if (offset == boost::none) | 365 | if (!offset) |
| 366 | return boost::none; | 366 | return {}; |
| 367 | ASSERT(offset.get() > 0); | 367 | ASSERT(*offset > 0); |
| 368 | 368 | ||
| 369 | std::memcpy(key_temp.data(), m_2.data() + offset.get(), key_temp.size()); | 369 | std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size()); |
| 370 | 370 | ||
| 371 | return std::make_pair(rights_id, key_temp); | 371 | return std::make_pair(rights_id, key_temp); |
| 372 | } | 372 | } |
| @@ -661,8 +661,8 @@ void KeyManager::DeriveSDSeedLazy() { | |||
| 661 | return; | 661 | return; |
| 662 | 662 | ||
| 663 | const auto res = DeriveSDSeed(); | 663 | const auto res = DeriveSDSeed(); |
| 664 | if (res != boost::none) | 664 | if (res) |
| 665 | SetKey(S128KeyType::SDSeed, res.get()); | 665 | SetKey(S128KeyType::SDSeed, *res); |
| 666 | } | 666 | } |
| 667 | 667 | ||
| 668 | static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) { | 668 | static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) { |
| @@ -889,9 +889,9 @@ void KeyManager::DeriveETicket(PartitionDataManager& data) { | |||
| 889 | 889 | ||
| 890 | for (const auto& raw : res) { | 890 | for (const auto& raw : res) { |
| 891 | const auto pair = ParseTicket(raw, rsa_key); | 891 | const auto pair = ParseTicket(raw, rsa_key); |
| 892 | if (pair == boost::none) | 892 | if (!pair) |
| 893 | continue; | 893 | continue; |
| 894 | const auto& [rid, key] = pair.value(); | 894 | const auto& [rid, key] = *pair; |
| 895 | u128 rights_id; | 895 | u128 rights_id; |
| 896 | std::memcpy(rights_id.data(), rid.data(), rid.size()); | 896 | std::memcpy(rights_id.data(), rid.data(), rid.size()); |
| 897 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | 897 | SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); |