Adressed review comments

This commit is contained in:
B3n30 2020-03-15 21:19:41 +01:00
parent ebeea43fb8
commit eaaa76b922
2 changed files with 17 additions and 23 deletions

View File

@ -242,14 +242,11 @@ void LoadNativeFirmKeysOld3DS() {
auto asn1 = RSA::CreateASN1Message(secret_data); auto asn1 = RSA::CreateASN1Message(secret_data);
auto result = rsa.GetSignature(asn1); auto result = rsa.GetSignature(asn1);
if (result.size() < 0x100) { if (result.size() < 0x100) {
std::vector<u8> temp; std::vector<u8> temp(0x100);
temp.resize(0x100 - result.size()); std::copy(result.begin(), result.end(), temp.end() - result.size());
std::fill(temp.begin(), temp.end(), 0);
std::copy(result.begin(), result.end(), std::back_inserter(temp));
result = temp; result = temp;
} else if (result.size() > 0x100) { } else if (result.size() > 0x100) {
std::vector<u8> temp(result.begin(), result.begin() + 0x100); result.resize(0x100);
result = temp;
} }
CryptoPP::SHA256 sha; CryptoPP::SHA256 sha;
@ -262,13 +259,13 @@ void LoadNativeFirmKeysOld3DS() {
key_slots.at(0x25).SetKeyX(key); key_slots.at(0x25).SetKeyX(key);
} }
void LoadSaveModeNativeFirmKeysOld3DS() { void LoadSafeModeNativeFirmKeysOld3DS() {
// Use the save mode native firm instead of the normal mode since there are only 2 version of it // Use the safe mode native firm instead of the normal mode since there are only 2 version of it
// and thus we can use fixed offsets // and thus we can use fixed offsets
constexpr u64 save_mode_native_firm_id = 0x00040138'00000003; constexpr u64 safe_mode_native_firm_id = 0x00040138'00000003;
FileSys::NCCHArchive archive(save_mode_native_firm_id, Service::FS::MediaType::NAND); FileSys::NCCHArchive archive(safe_mode_native_firm_id, Service::FS::MediaType::NAND);
std::array<char, 8> exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0}; std::array<char, 8> exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0};
FileSys::Path file_path = FileSys::MakeNCCHFilePath( FileSys::Path file_path = FileSys::MakeNCCHFilePath(
FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath); FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath);
@ -281,7 +278,7 @@ void LoadSaveModeNativeFirmKeysOld3DS() {
auto firm = std::move(file_result).Unwrap(); auto firm = std::move(file_result).Unwrap();
const std::size_t size = firm->GetSize(); const std::size_t size = firm->GetSize();
if (size != 843776) { if (size != 843776) {
LOG_ERROR(HW_AES, "save mode native firm has wrong size {}", size); LOG_ERROR(HW_AES, "safe mode native firm has wrong size {}", size);
return; return;
} }
std::vector<u8> firm_buffer(size); std::vector<u8> firm_buffer(size);
@ -323,16 +320,16 @@ void LoadNativeFirmKeysNew3DS() {
AESKey secret_key; AESKey secret_key;
secret.ReadArray(secret_key.data(), secret_key.size()); secret.ReadArray(secret_key.data(), secret_key.size());
// Use the save mode native firm instead of the normal mode since there are only 1 version of it // Use the safe mode native firm instead of the normal mode since there are only 1 version of it
// and thus we can use fixed offsets // and thus we can use fixed offsets
constexpr u64 save_mode_native_firm_id = 0x00040138'20000003; constexpr u64 safe_mode_native_firm_id = 0x00040138'20000003;
// TODO(B3N30): Add the 0x25 KeyX that gets initalized by native_firm // TODO(B3N30): Add the 0x25 KeyX that gets initalized by native_firm
// TODO(B3N30): Add the 0x18 - 0x1F KeyX that gets initalized by native_firm. This probably // TODO(B3N30): Add the 0x18 - 0x1F KeyX that gets initalized by native_firm. This probably
// requires the normal native firm with version > 9.6.0-X // requires the normal native firm with version > 9.6.0-X
FileSys::NCCHArchive archive(save_mode_native_firm_id, Service::FS::MediaType::NAND); FileSys::NCCHArchive archive(safe_mode_native_firm_id, Service::FS::MediaType::NAND);
std::array<char, 8> exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0}; std::array<char, 8> exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0};
FileSys::Path file_path = FileSys::MakeNCCHFilePath( FileSys::Path file_path = FileSys::MakeNCCHFilePath(
FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath); FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath);
@ -354,7 +351,7 @@ void LoadNativeFirmKeysNew3DS() {
return a | b << 8 | c << 16 | d << 24; return a | b << 8 | c << 16 | d << 24;
}; };
if (MakeMagic('F', 'I', 'R', 'M') != header.magic) { if (MakeMagic('F', 'I', 'R', 'M') != header.magic) {
LOG_ERROR(HW_AES, "N3DS SAVE MODE Native Firm has wrong header {}", header.magic); LOG_ERROR(HW_AES, "N3DS SAFE MODE Native Firm has wrong header {}", header.magic);
return; return;
} }
@ -505,7 +502,7 @@ void InitKeys() {
HW::RSA::InitSlots(); HW::RSA::InitSlots();
LoadBootromKeys(); LoadBootromKeys();
LoadNativeFirmKeysOld3DS(); LoadNativeFirmKeysOld3DS();
LoadSaveModeNativeFirmKeysOld3DS(); LoadSafeModeNativeFirmKeysOld3DS();
LoadNativeFirmKeysNew3DS(); LoadNativeFirmKeysNew3DS();
LoadPresetKeys(); LoadPresetKeys();
} }

View File

@ -10,7 +10,6 @@
#include "common/common_paths.h" #include "common/common_paths.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/string_util.h"
#include "core/hw/rsa/rsa.h" #include "core/hw/rsa/rsa.h"
namespace HW::RSA { namespace HW::RSA {
@ -21,7 +20,7 @@ std::vector<u8> HexToBytes(const std::string& hex) {
for (unsigned int i = 0; i < hex.length(); i += 2) { for (unsigned int i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2); std::string byteString = hex.substr(i, 2);
u8 byte = (u8)strtol(byteString.c_str(), NULL, 16); u8 byte = static_cast<u8>(std::strtol(byteString.c_str(), nullptr, 16));
bytes.push_back(byte); bytes.push_back(byte);
} }
return bytes; return bytes;
@ -48,7 +47,7 @@ void InitSlots() {
initialized = true; initialized = true;
const std::string filepath = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir) + BOOTROM9; const std::string filepath = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir) + BOOTROM9;
auto file = FileUtil::IOFile(filepath, "rb"); FileUtil::IOFile file(filepath, "rb");
if (!file) { if (!file) {
return; return;
} }
@ -61,14 +60,12 @@ void InitSlots() {
constexpr std::size_t RSA_MODULUS_POS = 0xB3E0; constexpr std::size_t RSA_MODULUS_POS = 0xB3E0;
file.Seek(RSA_MODULUS_POS, SEEK_SET); file.Seek(RSA_MODULUS_POS, SEEK_SET);
std::vector<u8> modulus; std::vector<u8> modulus(256);
modulus.resize(256);
file.ReadArray(modulus.data(), modulus.size()); file.ReadArray(modulus.data(), modulus.size());
constexpr std::size_t RSA_EXPONENT_POS = 0xB4E0; constexpr std::size_t RSA_EXPONENT_POS = 0xB4E0;
file.Seek(RSA_EXPONENT_POS, SEEK_SET); file.Seek(RSA_EXPONENT_POS, SEEK_SET);
std::vector<u8> exponent; std::vector<u8> exponent(256);
exponent.resize(256);
file.ReadArray(exponent.data(), exponent.size()); file.ReadArray(exponent.data(), exponent.size());
rsa_slots[0] = RsaSlot(exponent, modulus); rsa_slots[0] = RsaSlot(exponent, modulus);