summaryrefslogtreecommitdiffstats
path: root/src/core/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/crypto')
-rw-r--r--src/core/crypto/key_manager.cpp44
-rw-r--r--src/core/crypto/partition_data_manager.cpp11
2 files changed, 30 insertions, 25 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index 222fc95ba..87e6a1fd3 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -22,6 +22,7 @@
#include "common/file_util.h"
#include "common/hex_util.h"
#include "common/logging/log.h"
+#include "common/string_util.h"
#include "core/core.h"
#include "core/crypto/aes_util.h"
#include "core/crypto/key_manager.h"
@@ -378,8 +379,9 @@ std::vector<Ticket> GetTicketblob(const FileUtil::IOFile& ticket_save) {
template <size_t size>
static std::array<u8, size> operator^(const std::array<u8, size>& lhs,
const std::array<u8, size>& rhs) {
- std::array<u8, size> out{};
- std::transform(lhs.begin(), lhs.end(), rhs.begin(), out.begin(), std::bit_xor<>());
+ std::array<u8, size> out;
+ std::transform(lhs.begin(), lhs.end(), rhs.begin(), out.begin(),
+ [](u8 lhs, u8 rhs) { return u8(lhs ^ rhs); });
return out;
}
@@ -396,7 +398,7 @@ static std::array<u8, target_size> MGF1(const std::array<u8, in_size>& seed) {
while (out.size() < target_size) {
out.resize(out.size() + 0x20);
seed_exp[in_size + 3] = static_cast<u8>(i);
- mbedtls_sha256(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0);
+ mbedtls_sha256_ret(seed_exp.data(), seed_exp.size(), out.data() + out.size() - 0x20, 0);
++i;
}
@@ -538,7 +540,7 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) {
Key128 key = Common::HexStringToArray<16>(out[1]);
s128_keys[{S128KeyType::Titlekey, rights_id[1], rights_id[0]}] = key;
} else {
- std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower);
+ out[0] = Common::ToLower(out[0]);
if (s128_file_id.find(out[0]) != s128_file_id.end()) {
const auto index = s128_file_id.at(out[0]);
Key128 key = Common::HexStringToArray<16>(out[1]);
@@ -668,23 +670,27 @@ void KeyManager::WriteKeyToFile(KeyCategory category, std::string_view keyname,
const std::array<u8, Size>& key) {
const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir);
std::string filename = "title.keys_autogenerated";
- if (category == KeyCategory::Standard)
+ if (category == KeyCategory::Standard) {
filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated";
- else if (category == KeyCategory::Console)
+ } else if (category == KeyCategory::Console) {
filename = "console.keys_autogenerated";
- const auto add_info_text = !FileUtil::Exists(yuzu_keys_dir + DIR_SEP + filename);
- FileUtil::CreateFullPath(yuzu_keys_dir + DIR_SEP + filename);
- std::ofstream file(yuzu_keys_dir + DIR_SEP + filename, std::ios::app);
- if (!file.is_open())
+ }
+
+ const auto path = yuzu_keys_dir + DIR_SEP + filename;
+ const auto add_info_text = !FileUtil::Exists(path);
+ FileUtil::CreateFullPath(path);
+ FileUtil::IOFile file{path, "a"};
+ if (!file.IsOpen()) {
return;
+ }
if (add_info_text) {
- file
- << "# This file is autogenerated by Yuzu\n"
- << "# It serves to store keys that were automatically generated from the normal keys\n"
- << "# If you are experiencing issues involving keys, it may help to delete this file\n";
+ file.WriteString(
+ "# This file is autogenerated by Yuzu\n"
+ "# It serves to store keys that were automatically generated from the normal keys\n"
+ "# If you are experiencing issues involving keys, it may help to delete this file\n");
}
- file << fmt::format("\n{} = {}", keyname, Common::HexToString(key));
+ file.WriteString(fmt::format("\n{} = {}", keyname, Common::HexToString(key)));
AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title);
}
@@ -944,12 +950,10 @@ void KeyManager::DeriveETicket(PartitionDataManager& data) {
return;
}
- Key128 rsa_oaep_kek{};
- std::transform(seed3.begin(), seed3.end(), mask0.begin(), rsa_oaep_kek.begin(),
- std::bit_xor<>());
-
- if (rsa_oaep_kek == Key128{})
+ const Key128 rsa_oaep_kek = seed3 ^ mask0;
+ if (rsa_oaep_kek == Key128{}) {
return;
+ }
SetKey(S128KeyType::Source, rsa_oaep_kek,
static_cast<u64>(SourceKeyType::RSAOaepKekGeneration));
diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp
index 594cd82c5..d64302f2e 100644
--- a/src/core/crypto/partition_data_manager.cpp
+++ b/src/core/crypto/partition_data_manager.cpp
@@ -161,7 +161,7 @@ std::array<u8, key_size> FindKeyFromHex(const std::vector<u8>& binary,
std::array<u8, 0x20> temp{};
for (size_t i = 0; i < binary.size() - key_size; ++i) {
- mbedtls_sha256(binary.data() + i, key_size, temp.data(), 0);
+ mbedtls_sha256_ret(binary.data() + i, key_size, temp.data(), 0);
if (temp != hash)
continue;
@@ -189,7 +189,7 @@ static std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<
AESCipher<Key128> cipher(key, Mode::ECB);
for (size_t i = 0; i < binary.size() - 0x10; ++i) {
cipher.Transcode(binary.data() + i, dec_temp.size(), dec_temp.data(), Op::Decrypt);
- mbedtls_sha256(dec_temp.data(), dec_temp.size(), temp.data(), 0);
+ mbedtls_sha256_ret(dec_temp.data(), dec_temp.size(), temp.data(), 0);
for (size_t k = 0; k < out.size(); ++k) {
if (temp == master_key_hashes[k]) {
@@ -204,11 +204,12 @@ static std::array<Key128, 0x20> FindEncryptedMasterKeyFromHex(const std::vector<
FileSys::VirtualFile FindFileInDirWithNames(const FileSys::VirtualDir& dir,
const std::string& name) {
- auto upper = name;
- std::transform(upper.begin(), upper.end(), upper.begin(), [](u8 c) { return std::toupper(c); });
+ const auto upper = Common::ToUpper(name);
+
for (const auto& fname : {name, name + ".bin", upper, upper + ".BIN"}) {
- if (dir->GetFile(fname) != nullptr)
+ if (dir->GetFile(fname) != nullptr) {
return dir->GetFile(fname);
+ }
}
return nullptr;