From bea1e38c674485d5df6a4266f9bb54e46d39d9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 21 May 2018 15:40:29 +0200 Subject: [PATCH] Move ECCSignature to Common::ec and give it a less confusing name --- Source/Core/Common/Crypto/ec.cpp | 8 ++++---- Source/Core/Common/Crypto/ec.h | 7 +++++-- Source/Core/Core/HW/WiiSave.cpp | 2 +- Source/Core/Core/IOS/ES/Identity.cpp | 4 +++- Source/Core/Core/IOS/IOSC.cpp | 10 +++++----- Source/Core/Core/IOS/IOSC.h | 12 +++++------- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Source/Core/Common/Crypto/ec.cpp b/Source/Core/Common/Crypto/ec.cpp index b9a0ca6491..9a57300f89 100644 --- a/Source/Core/Common/Crypto/ec.cpp +++ b/Source/Core/Common/Crypto/ec.cpp @@ -241,7 +241,7 @@ static void silly_random(u8* rndArea, u8 count) } } -std::array Sign(const u8* key, const u8* hash) +Signature Sign(const u8* key, const u8* hash) { u8 e[30]{}; memcpy(e + 10, hash, 20); @@ -272,7 +272,7 @@ std::array Sign(const u8* key, const u8* hash) bn_inv(minv, m, ec_N, sizeof(minv)); bn_mul(s.data.data(), minv, kk, ec_N, 30); - std::array signature; + Signature signature; std::copy(r.data.cbegin(), r.data.cend(), signature.begin()); std::copy(s.data.cbegin(), s.data.cend(), signature.begin() + 30); return signature; @@ -300,10 +300,10 @@ bool VerifySignature(const u8* public_key, const u8* signature, const u8* hash) return (bn_compare(rx.data(), R, 30) == 0); } -std::array PrivToPub(const u8* key) +PublicKey PrivToPub(const u8* key) { const Point data = key * ec_G; - std::array result; + PublicKey result; std::copy_n(data.Data(), result.size(), result.begin()); return result; } diff --git a/Source/Core/Common/Crypto/ec.h b/Source/Core/Common/Crypto/ec.h index 442bbec311..5b21dd96cc 100644 --- a/Source/Core/Common/Crypto/ec.h +++ b/Source/Core/Common/Crypto/ec.h @@ -10,8 +10,11 @@ namespace Common::ec { +using Signature = std::array; +using PublicKey = std::array; + /// Generate a signature using ECDSA. -std::array Sign(const u8* key, const u8* hash); +Signature Sign(const u8* key, const u8* hash); /// Check a signature using ECDSA. /// @@ -24,5 +27,5 @@ bool VerifySignature(const u8* public_key, const u8* signature, const u8* hash); std::array ComputeSharedSecret(const u8* private_key, const u8* public_key); /// Convert a ECC private key (30 bytes) to a public key (60 bytes). -std::array PrivToPub(const u8* key); +PublicKey PrivToPub(const u8* key); } // namespace Common::ec diff --git a/Source/Core/Core/HW/WiiSave.cpp b/Source/Core/Core/HW/WiiSave.cpp index 21fd3150a1..cf376cb501 100644 --- a/Source/Core/Core/HW/WiiSave.cpp +++ b/Source/Core/Core/HW/WiiSave.cpp @@ -455,7 +455,7 @@ void WiiSave::do_sig() // Sign the data. IOS::CertECC ap_cert; - IOS::ECCSignature ap_sig; + Common::ec::Signature ap_sig; m_ios.GetIOSC().Sign(ap_sig.data(), reinterpret_cast(&ap_cert), Titles::SYSTEM_MENU, data.get(), data_size); diff --git a/Source/Core/Core/IOS/ES/Identity.cpp b/Source/Core/Core/IOS/ES/Identity.cpp index f33cd4bccd..6805bd62b1 100644 --- a/Source/Core/Core/IOS/ES/Identity.cpp +++ b/Source/Core/Core/IOS/ES/Identity.cpp @@ -8,12 +8,14 @@ #include +#include "Common/Crypto/ec.h" #include "Common/Logging/Log.h" #include "Common/ScopeGuard.h" #include "Common/StringUtil.h" #include "Core/ConfigManager.h" #include "Core/HW/Memmap.h" #include "Core/IOS/ES/Formats.h" +#include "Core/IOS/IOSC.h" #include "Core/IOS/Uids.h" namespace IOS @@ -197,7 +199,7 @@ IPCCommandResult ES::VerifySign(const IOCtlVRequest& request) { if (!request.HasNumberOfValidVectors(3, 0)) return GetDefaultReply(ES_EINVAL); - if (request.in_vectors[1].size != sizeof(IOS::ECCSignature)) + if (request.in_vectors[1].size != sizeof(Common::ec::Signature)) return GetDefaultReply(ES_EINVAL); std::vector hash(request.in_vectors[0].size); diff --git a/Source/Core/Core/IOS/IOSC.cpp b/Source/Core/Core/IOS/IOSC.cpp index 734d8e391e..362a235cb1 100644 --- a/Source/Core/Core/IOS/IOSC.cpp +++ b/Source/Core/Core/IOS/IOSC.cpp @@ -70,10 +70,10 @@ struct BootMiiKeyDump u32 unk2; // 0x17C std::array eeprom_pad; // 0x180 - u32 ms_id; // 0x200 - u32 ca_id; // 0x204 - u32 ng_key_id; // 0x208 - IOS::ECCSignature ng_sig; // 0x20c + u32 ms_id; // 0x200 + u32 ca_id; // 0x204 + u32 ng_key_id; // 0x208 + Common::ec::Signature ng_sig; // 0x20c struct Counter { u8 boot2version; @@ -108,7 +108,7 @@ constexpr std::array DEFAULT_PRIVATE_KEY = {{ }}; // clang-format off -constexpr ECCSignature DEFAULT_SIGNATURE = {{ +constexpr Common::ec::Signature DEFAULT_SIGNATURE = {{ // R 0x00, 0xD8, 0x81, 0x63, 0xB2, 0x00, 0x6B, 0x0B, 0x54, 0x82, 0x88, 0x63, 0x81, 0x1C, 0x00, 0x71, 0x12, 0xED, 0xB7, 0xFD, 0x21, 0xAB, 0x0E, 0x50, 0x0E, 0x1F, 0xBF, 0x78, 0xAD, 0x37, diff --git a/Source/Core/Core/IOS/IOSC.h b/Source/Core/Core/IOS/IOSC.h index 394de9fdad..8155a5f0a7 100644 --- a/Source/Core/Core/IOS/IOSC.h +++ b/Source/Core/Core/IOS/IOSC.h @@ -13,6 +13,7 @@ #include "Common/CommonTypes.h" #include "Common/Crypto/AES.h" +#include "Common/Crypto/ec.h" class PointerWrap; @@ -37,8 +38,6 @@ enum class PublicKeyType : u32 ECC = 2, }; -using ECCSignature = std::array; - #pragma pack(push, 4) struct SignatureRSA4096 { @@ -61,7 +60,7 @@ static_assert(sizeof(SignatureRSA2048) == 0x180, "Wrong size for SignatureRSA204 struct SignatureECC { SignatureType type; - ECCSignature sig; + Common::ec::Signature sig; u8 fill[0x40]; char issuer[0x40]; }; @@ -76,7 +75,6 @@ struct CertHeader }; using RSA2048PublicKey = std::array; -using ECCPublicKey = std::array; struct CertRSA4096RSA2048 { @@ -103,7 +101,7 @@ struct CertRSA2048ECC { SignatureRSA2048 signature; CertHeader header; - ECCPublicKey public_key; + Common::ec::PublicKey public_key; std::array padding; }; static_assert(sizeof(CertRSA2048ECC) == 0x240, "Wrong size for CertRSA2048ECC"); @@ -113,7 +111,7 @@ struct CertECC { SignatureECC signature; CertHeader header; - ECCPublicKey public_key; + Common::ec::PublicKey public_key; std::array padding; }; static_assert(sizeof(CertECC) == 0x180, "Wrong size for CertECC"); @@ -267,7 +265,7 @@ private: KeyEntries m_key_entries; KeyEntry m_root_key_entry; - ECCSignature m_console_signature{}; + Common::ec::Signature m_console_signature{}; // Retail keyblob are issued by CA00000001. Default to 1 even though IOSC actually defaults to 2. u32 m_ms_id = 2; u32 m_ca_id = 1;