From 75bac1e5144f372d693e2e276fb0084273a26880 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 1 May 2020 10:12:45 -0400 Subject: [PATCH 1/3] core/hw/rsa: Take std::vector instances by value Allows us to std::move them and allow calling code to eliminate copies from occurring entirely. --- src/core/hw/rsa/rsa.cpp | 2 +- src/core/hw/rsa/rsa.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/hw/rsa/rsa.cpp b/src/core/hw/rsa/rsa.cpp index 8f2c93243..a349cbf37 100644 --- a/src/core/hw/rsa/rsa.cpp +++ b/src/core/hw/rsa/rsa.cpp @@ -74,7 +74,7 @@ void InitSlots() { std::vector exponent(256); file.ReadArray(exponent.data(), exponent.size()); - rsa_slots[0] = RsaSlot(exponent, modulus); + rsa_slots[0] = RsaSlot(std::move(exponent), std::move(modulus)); // TODO(B3N30): Initalize the other slots. But since they aren't used at all, we can skip them // for now } diff --git a/src/core/hw/rsa/rsa.h b/src/core/hw/rsa/rsa.h index 869d98d89..6385ed783 100644 --- a/src/core/hw/rsa/rsa.h +++ b/src/core/hw/rsa/rsa.h @@ -11,9 +11,9 @@ namespace HW::RSA { class RsaSlot { public: - RsaSlot() : init(false) {} - RsaSlot(const std::vector& exponent, const std::vector& modulus) - : init(true), exponent(exponent), modulus(modulus) {} + RsaSlot() = default; + RsaSlot(std::vector exponent, std::vector modulus) + : init(true), exponent(std::move(exponent)), modulus(std::move(modulus)) {} std::vector GetSignature(const std::vector& message); operator bool() const { @@ -22,7 +22,7 @@ public: } private: - bool init; + bool init = false; std::vector exponent; std::vector modulus; }; From c55e81b9466a0af9356797aca1ea1a870bc6de6d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 1 May 2020 10:14:48 -0400 Subject: [PATCH 2/3] core/hw/rsa: Make operator bool explicit Prevents error-prone implicit conversions to bool from occurring. --- src/core/hw/rsa/rsa.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hw/rsa/rsa.h b/src/core/hw/rsa/rsa.h index 6385ed783..51288f41f 100644 --- a/src/core/hw/rsa/rsa.h +++ b/src/core/hw/rsa/rsa.h @@ -16,7 +16,7 @@ public: : init(true), exponent(std::move(exponent)), modulus(std::move(modulus)) {} std::vector GetSignature(const std::vector& message); - operator bool() const { + explicit operator bool() const { // TODO(B3N30): Maybe check if exponent and modulus are vailid return init; } From 3ab070119293e6edbff74162df78eae8fdf6cbb2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 1 May 2020 10:17:01 -0400 Subject: [PATCH 3/3] core/hw/rsa: Make GetSignature() a const member function This doesn't directly modify instance state, so it can be made const. --- src/core/hw/aes/key.cpp | 2 +- src/core/hw/rsa/rsa.cpp | 2 +- src/core/hw/rsa/rsa.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/hw/aes/key.cpp b/src/core/hw/aes/key.cpp index 2148ab04c..e406aed71 100644 --- a/src/core/hw/aes/key.cpp +++ b/src/core/hw/aes/key.cpp @@ -223,7 +223,7 @@ void LoadNativeFirmKeysOld3DS() { return; } - auto rsa = RSA::GetSlot(0); + const auto rsa = RSA::GetSlot(0); if (!rsa) { LOG_ERROR(HW_AES, "RSA slot is missing"); return; diff --git a/src/core/hw/rsa/rsa.cpp b/src/core/hw/rsa/rsa.cpp index a349cbf37..b4179fd69 100644 --- a/src/core/hw/rsa/rsa.cpp +++ b/src/core/hw/rsa/rsa.cpp @@ -31,7 +31,7 @@ std::vector HexToBytes(const std::string& hex) { constexpr std::size_t SlotSize = 4; std::array rsa_slots; -std::vector RsaSlot::GetSignature(const std::vector& message) { +std::vector RsaSlot::GetSignature(const std::vector& message) const { CryptoPP::Integer sig = CryptoPP::ModularExponentiation(CryptoPP::Integer(message.data(), message.size()), CryptoPP::Integer(exponent.data(), exponent.size()), diff --git a/src/core/hw/rsa/rsa.h b/src/core/hw/rsa/rsa.h index 51288f41f..26dc84cb1 100644 --- a/src/core/hw/rsa/rsa.h +++ b/src/core/hw/rsa/rsa.h @@ -14,7 +14,7 @@ public: RsaSlot() = default; RsaSlot(std::vector exponent, std::vector modulus) : init(true), exponent(std::move(exponent)), modulus(std::move(modulus)) {} - std::vector GetSignature(const std::vector& message); + std::vector GetSignature(const std::vector& message) const; explicit operator bool() const { // TODO(B3N30): Maybe check if exponent and modulus are vailid