mirror of
https://github.com/izzy2lost/dolphin.git
synced 2026-06-19 01:16:48 -07:00
ec: Improve readability and clarity
- Move all of the ec functions into the Common::ec namespace. - Give the public functions better names and some usage information. - Move all of the "elt" related functions into an "elt" class including all of the arithmetic operations, so that the logic becomes clearer and feels less like assembly. This also makes it much more obvious what the parameters are, instead of only using unsigned char* (which doesn't tell anything about what the pointer is used for or the size). - Similarly, add a new "Point" class and move point functions there. Overload the arithmetic operators to make calculations easier to read
This commit is contained in:
+220
-322
File diff suppressed because it is too large
Load Diff
@@ -8,8 +8,14 @@
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
|
||||
void generate_ecdsa(u8* R, u8* S, const u8* k, const u8* hash);
|
||||
|
||||
void ec_priv_to_pub(const u8* k, u8* Q);
|
||||
namespace Common::ec
|
||||
{
|
||||
/// Generate a signature using ECDSA.
|
||||
std::array<u8, 60> Sign(const u8* key, const u8* hash);
|
||||
|
||||
/// Compute a shared secret from a private key (30 bytes) and public key (60 bytes).
|
||||
std::array<u8, 60> ComputeSharedSecret(const u8* private_key, const u8* public_key);
|
||||
|
||||
/// Convert a ECC private key (30 bytes) to a public key (60 bytes).
|
||||
std::array<u8, 60> PrivToPub(const u8* key);
|
||||
} // namespace Common::ec
|
||||
|
||||
@@ -251,7 +251,7 @@ ReturnCode IOSC::ComputeSharedKey(Handle dest_handle, Handle private_handle, Han
|
||||
|
||||
// Calculate the ECC shared secret.
|
||||
const std::array<u8, 0x3c> shared_secret =
|
||||
ComputeSharedSecret(private_entry->data.data(), public_entry->data.data());
|
||||
Common::ec::ComputeSharedSecret(private_entry->data.data(), public_entry->data.data());
|
||||
|
||||
std::array<u8, 20> sha1;
|
||||
mbedtls_sha1(shared_secret.data(), shared_secret.size() / 2, sha1.data());
|
||||
@@ -425,7 +425,8 @@ static Certificate MakeBlankSigECCert(const char* signer, const char* name, cons
|
||||
std::strncpy(reinterpret_cast<char*>(cert_out.data()) + 0xc4, name, 0x40);
|
||||
const u32 swapped_key_id = Common::swap32(key_id);
|
||||
std::memcpy(cert_out.data() + 0x104, &swapped_key_id, sizeof(swapped_key_id));
|
||||
ec_priv_to_pub(private_key, cert_out.data() + 0x108);
|
||||
const std::array<u8, 60> public_key = Common::ec::PrivToPub(private_key);
|
||||
std::copy(public_key.cbegin(), public_key.cend(), cert_out.begin() + 0x108);
|
||||
return cert_out;
|
||||
}
|
||||
|
||||
@@ -454,11 +455,12 @@ void IOSC::Sign(u8* sig_out, u8* ap_cert_out, u64 title_id, const u8* data, u32
|
||||
std::copy(cert.begin(), cert.end(), ap_cert_out);
|
||||
|
||||
mbedtls_sha1(ap_cert_out + 0x80, 0x100, hash.data());
|
||||
generate_ecdsa(ap_cert_out + 4, ap_cert_out + 34, m_key_entries[HANDLE_CONSOLE_KEY].data.data(),
|
||||
hash.data());
|
||||
auto signature = Common::ec::Sign(m_key_entries[HANDLE_CONSOLE_KEY].data.data(), hash.data());
|
||||
std::copy(signature.cbegin(), signature.cend(), ap_cert_out + 4);
|
||||
|
||||
mbedtls_sha1(data, data_size, hash.data());
|
||||
generate_ecdsa(sig_out, sig_out + 30, ap_priv.data(), hash.data());
|
||||
signature = Common::ec::Sign(ap_priv.data(), hash.data());
|
||||
std::copy(signature.cbegin(), signature.cend(), sig_out);
|
||||
}
|
||||
|
||||
constexpr std::array<u8, 512> ROOT_PUBLIC_KEY = {
|
||||
|
||||
Reference in New Issue
Block a user