[Crypto] Slightly more robust error handling

This commit is contained in:
wheremyfoodat 2023-06-27 01:12:17 +03:00
parent 0494ca0064
commit 3cf8427670
5 changed files with 41 additions and 34 deletions

View file

@ -9,13 +9,12 @@ namespace Crypto {
std::ifstream file(path, std::ios::in);
if (file.fail()) {
Helpers::warn("keys: Couldn't read key file: %s", path.c_str());
Helpers::warn("Keys: Couldn't read key file: %s", path.c_str());
return;
}
while (!file.eof()) {
std::string line;
std::getline(file, line);
// Skip obvious invalid lines
@ -24,9 +23,8 @@ namespace Crypto {
}
const auto parts = Helpers::split(line, '=');
if (parts.size() != 2) {
Helpers::warn("keys: Failed to parse %s", line.c_str());
Helpers::warn("Keys: Failed to parse %s", line.c_str());
continue;
}
@ -37,16 +35,15 @@ namespace Crypto {
char keyType;
bool is_generator = name == "generator";
if (!is_generator && std::sscanf(name.c_str(), "slot0x%zXKey%c", &slotId, &keyType) != 2) {
Helpers::warn("keys: Ignoring unknown key %s", name.c_str());
Helpers::warn("Keys: Ignoring unknown key %s", name.c_str());
continue;
}
auto key = createKeyFromHex(rawKeyHex);
if (!key.has_value()) {
Helpers::warn("keys: Failed to parse raw key %s", rawKeyHex.c_str());
Helpers::warn("Keys: Failed to parse raw key %s", rawKeyHex.c_str());
continue;
}
@ -56,7 +53,7 @@ namespace Crypto {
}
if (slotId >= AesKeySlotCount) {
Helpers::warn("keys: Invalid key slot id %u", slotId);
Helpers::warn("Keys: Invalid key slot id %u", slotId);
continue;
}
@ -71,7 +68,7 @@ namespace Crypto {
setNormalKey(slotId, key.value());
break;
default:
Helpers::warn("keys: Invalid key type %c", keyType);
Helpers::warn("Keys: Invalid key type %c", keyType);
break;
}
}
@ -80,5 +77,7 @@ namespace Crypto {
for (std::size_t i = 0; i < AesKeySlotCount; i++) {
updateNormalKey(i);
}
keysLoaded = true;
}
};

View file

@ -54,6 +54,14 @@ bool NCCH::loadFromHeader(Crypto::AESEngine &aesEngine, IOFile& file, const FSIn
romFS.hashRegionSize = u64(*(u32*)&header[0x1B8]) * mediaUnit;
if (encrypted) {
if (!aesEngine.haveKeys()) {
Helpers::panic(
"Loaded an encrypted ROM but AES keys don't seem to have been provided correctly! Navigate to the emulator's\n"
"app data folder and make sure you have a sysdata directory with a file called aes_keys.txt which contains your keys!"
);
return false;
}
Crypto::AESKey primaryKeyY;
Crypto::AESKey secondaryKeyY;
std::memcpy(primaryKeyY.data(), header, primaryKeyY.size());