From eaaa76b922636985a624d904cc410042a5ead228 Mon Sep 17 00:00:00 2001 From: B3n30 Date: Sun, 15 Mar 2020 21:19:41 +0100 Subject: [PATCH] Adressed review comments --- src/core/hw/aes/key.cpp | 29 +++++++++++++---------------- src/core/hw/rsa/rsa.cpp | 11 ++++------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/core/hw/aes/key.cpp b/src/core/hw/aes/key.cpp index c61b04a2e..21d63f252 100644 --- a/src/core/hw/aes/key.cpp +++ b/src/core/hw/aes/key.cpp @@ -242,14 +242,11 @@ void LoadNativeFirmKeysOld3DS() { auto asn1 = RSA::CreateASN1Message(secret_data); auto result = rsa.GetSignature(asn1); if (result.size() < 0x100) { - std::vector temp; - temp.resize(0x100 - result.size()); - std::fill(temp.begin(), temp.end(), 0); - std::copy(result.begin(), result.end(), std::back_inserter(temp)); + std::vector temp(0x100); + std::copy(result.begin(), result.end(), temp.end() - result.size()); result = temp; } else if (result.size() > 0x100) { - std::vector temp(result.begin(), result.begin() + 0x100); - result = temp; + result.resize(0x100); } CryptoPP::SHA256 sha; @@ -262,13 +259,13 @@ void LoadNativeFirmKeysOld3DS() { key_slots.at(0x25).SetKeyX(key); } -void LoadSaveModeNativeFirmKeysOld3DS() { - // Use the save mode native firm instead of the normal mode since there are only 2 version of it +void LoadSafeModeNativeFirmKeysOld3DS() { + // 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 - 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 exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0}; FileSys::Path file_path = FileSys::MakeNCCHFilePath( FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath); @@ -281,7 +278,7 @@ void LoadSaveModeNativeFirmKeysOld3DS() { auto firm = std::move(file_result).Unwrap(); const std::size_t size = firm->GetSize(); 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; } std::vector firm_buffer(size); @@ -323,16 +320,16 @@ void LoadNativeFirmKeysNew3DS() { AESKey secret_key; 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 - 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 0x18 - 0x1F KeyX that gets initalized by native_firm. This probably // 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 exefs_filepath = {'.', 'f', 'i', 'r', 'm', 0, 0, 0}; FileSys::Path file_path = FileSys::MakeNCCHFilePath( FileSys::NCCHFileOpenType::NCCHData, 0, FileSys::NCCHFilePathType::ExeFS, exefs_filepath); @@ -354,7 +351,7 @@ void LoadNativeFirmKeysNew3DS() { return a | b << 8 | c << 16 | d << 24; }; 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; } @@ -505,7 +502,7 @@ void InitKeys() { HW::RSA::InitSlots(); LoadBootromKeys(); LoadNativeFirmKeysOld3DS(); - LoadSaveModeNativeFirmKeysOld3DS(); + LoadSafeModeNativeFirmKeysOld3DS(); LoadNativeFirmKeysNew3DS(); LoadPresetKeys(); } diff --git a/src/core/hw/rsa/rsa.cpp b/src/core/hw/rsa/rsa.cpp index c436ad74f..b08df31a0 100644 --- a/src/core/hw/rsa/rsa.cpp +++ b/src/core/hw/rsa/rsa.cpp @@ -10,7 +10,6 @@ #include "common/common_paths.h" #include "common/file_util.h" #include "common/logging/log.h" -#include "common/string_util.h" #include "core/hw/rsa/rsa.h" namespace HW::RSA { @@ -21,7 +20,7 @@ std::vector HexToBytes(const std::string& hex) { for (unsigned int i = 0; i < hex.length(); i += 2) { std::string byteString = hex.substr(i, 2); - u8 byte = (u8)strtol(byteString.c_str(), NULL, 16); + u8 byte = static_cast(std::strtol(byteString.c_str(), nullptr, 16)); bytes.push_back(byte); } return bytes; @@ -48,7 +47,7 @@ void InitSlots() { initialized = true; const std::string filepath = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir) + BOOTROM9; - auto file = FileUtil::IOFile(filepath, "rb"); + FileUtil::IOFile file(filepath, "rb"); if (!file) { return; } @@ -61,14 +60,12 @@ void InitSlots() { constexpr std::size_t RSA_MODULUS_POS = 0xB3E0; file.Seek(RSA_MODULUS_POS, SEEK_SET); - std::vector modulus; - modulus.resize(256); + std::vector modulus(256); file.ReadArray(modulus.data(), modulus.size()); constexpr std::size_t RSA_EXPONENT_POS = 0xB4E0; file.Seek(RSA_EXPONENT_POS, SEEK_SET); - std::vector exponent; - exponent.resize(256); + std::vector exponent(256); file.ReadArray(exponent.data(), exponent.size()); rsa_slots[0] = RsaSlot(exponent, modulus);