From 640f67a9340b27aef4d097928cae8ca729959c4d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 21 Mar 2023 12:31:22 +0100 Subject: [PATCH 01/16] include/libeosio/ec.hpp: Adding ecdsa_sign and ecdsa_verify() --- include/libeosio/ec.hpp | 16 ++++ tests/CMakeLists.txt | 2 + tests/ec/ecdsa_sign.cpp | 143 ++++++++++++++++++++++++++++++++++++ tests/ec/ecdsa_verify.cpp | 149 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 310 insertions(+) create mode 100644 tests/ec/ecdsa_sign.cpp create mode 100644 tests/ec/ecdsa_verify.cpp diff --git a/include/libeosio/ec.hpp b/include/libeosio/ec.hpp index 07249ca..bea3a30 100644 --- a/include/libeosio/ec.hpp +++ b/include/libeosio/ec.hpp @@ -101,6 +101,22 @@ int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub); */ int ec_generate_key(struct ec_keypair *pair); + +/** + * Sign + */ + +/** + * Create a ECDSA signature, returns -1 if an error occured or zero on success. + */ +int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& sig); + +/** + * Verify an ECDSA signature, + * returns zero if the signature is correct. -1 if the signature is incorrect or an error occured. + */ +int ecdsa_verify(const sha256_t* digest, const ec_signature_t& sig, const ec_pubkey_t& key); + /** * Shutdown the ec library. */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 73b1523..d04ae9b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,8 @@ set(TEST_SRC # ec ec/generate.cpp ec/pubkey.cpp + ec/ecdsa_sign.cpp + ec/ecdsa_verify.cpp # Base58 base58/encode.cpp diff --git a/tests/ec/ecdsa_sign.cpp b/tests/ec/ecdsa_sign.cpp new file mode 100644 index 0000000..e6b6902 --- /dev/null +++ b/tests/ec/ecdsa_sign.cpp @@ -0,0 +1,143 @@ +#include +#include +#include + +TEST_CASE("ec::ecdsa_sign") { + + struct testcase { + const char *name; + libeosio::ec_privkey_t key; + libeosio::sha256_t dgst; + libeosio::ec_signature_t expected; + }; + + std::vector tests = { + { + "valid #1", + // Private Key: 5Ke4YqL2TCtiUTTA1CVMXSrrEHuK9HzbUSWX791yC2UaX2dWRDw + { 0xf0, 0x2d, 0x00, 0x72, 0x8a, 0x7a, 0x93, 0x86, 0xaf, 0xbe, 0x19, 0xab, 0x79, 0x8c, 0xa1, 0x61, 0xab, 0x96, 0x74, 0x7f, 0xe5, 0x97, 0x19, 0x07, 0xb1, 0xc8, 0x65, 0x63, 0xc8, 0x11, 0xe6, 0x74 }, + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // SIG_K1_KdgBih1poWj8DYZXwLxMdjaHMzYhuAVp7XshR9ZjrZSubZwsgSpiyUKXu44NmCtKgRFswmqKaioWLTuGZrXwYPsSNCSyyr + { + 0x20, 0x44, 0x3f, 0x72, 0x22, 0xfd, 0x7a, 0x1f, 0x56, 0x2d, 0xef, 0x01, 0x55, 0x40, 0xcf, 0x50, 0x6f, 0x5f, 0xdd, 0xfe, 0x71, 0xd7, 0x18, 0xc9, 0xa8, 0xc8, 0xbe, 0x00, 0x96, 0xf8, 0x7c, 0xc7, + 0x1f, 0x2d, 0xd0, 0xd1, 0xfc, 0x4a, 0x22, 0x6a, 0x25, 0xc4, 0x7c, 0x99, 0xf9, 0xd8, 0x30, 0xfa, 0x8b, 0x5c, 0x33, 0x36, 0x61, 0xd7, 0xcf, 0x6d, 0x04, 0x97, 0x61, 0x76, 0x47, 0x65, 0x30, 0x7b, + 0x66 + }, + }, + { + "valid #2", + // Private Key: 5J1VLF3TtdM3FiaUZNjeUXTUmVmRRULSdxrGf3XwQkRAZLcN64b + { 0x19, 0x6f, 0xd7, 0xa5, 0x3f, 0xab, 0x59, 0x8e, 0xa5, 0xef, 0xec, 0x79, 0xdd, 0xbc, 0x49, 0xae, 0xef, 0xff, 0x41, 0x3c, 0x5f, 0xfe, 0x50, 0x3a, 0x66, 0xbc, 0xff, 0xf1, 0x32, 0x1b, 0x8c, 0x2f }, + { + data: { + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 + }, + }, + // SIG_K1_K4XXx6oSYBzcwzscMstvSxruxdkTCinyN9dnRo4DuBkCCpQbCJQcJmbE7aAmNueBYCccHyyDK5JDfMpvewRF2rGUFtSE2y + { + 0x1f, 0x46, 0xde, 0x7a, 0x7e, 0x87, 0xa7, 0xb0, 0x42, 0xce, 0xdc, 0x57, 0xc9, 0x0d, 0x64, 0x4c, 0xc7, 0x4d, 0xe6, 0x19, 0x5d, 0x34, 0x4e, 0xba, 0xfb, 0xdf, 0x26, 0x79, 0xa1, 0xc6, 0x99, 0x98, + 0xa7, 0x1f, 0x65, 0xcd, 0xab, 0x2d, 0x19, 0x75, 0x27, 0xdc, 0xb2, 0xc5, 0x46, 0x87, 0x5d, 0xbe, 0xc5, 0x8d, 0xb2, 0xb8, 0x7f, 0x15, 0x47, 0xd7, 0xc7, 0x94, 0x0a, 0xd5, 0x52, 0xd9, 0xe3, 0x93, + 0xd7 + }, + }, + { + "valid #3", + // Private Key: 5Hz2iVzC9jDcBUGo2EUSPdcvL1s8jxJ6pY95gQX59mHoHx6zi6W + { 0x16, 0x1f, 0xd0, 0x41, 0x21, 0x50, 0x04, 0x13, 0x90, 0x61, 0xb3, 0xbd, 0xc8, 0x83, 0x09, 0xf0, 0x80, 0x3c, 0x90, 0x45, 0x8e, 0x9a, 0x8c, 0xb2, 0xfb, 0x6a, 0x3c, 0x27, 0x51, 0xcf, 0xd9, 0xca }, + { + data: { + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec + }, + }, + // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh + { + 0x1f, 0x4a, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, + 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, + 0xfc + }, + }, + { + "valid #4", + // Private Key: 5KbH8qYyg6f93gHuAdbDmwCVMoarXHqEn5CdbW2VtHTyMsLShwM + { 0xe9, 0xdb, 0x4d, 0xdc, 0x8e, 0x08, 0xd2, 0x04, 0x75, 0xc2, 0xb6, 0xf7, 0x14, 0x9e, 0xa2, 0x16, 0xd7, 0x2b, 0x34, 0x3b, 0xba, 0xb4, 0x7d, 0xe6, 0xd3, 0x96, 0xc1, 0x63, 0x82, 0xdf, 0x2d, 0x13 }, + { + data: { + 0xbc, 0x83, 0xbe, 0xe1, 0x73, 0x82, 0xfb, 0x02, + 0x71, 0x25, 0x3b, 0xf5, 0x39, 0x32, 0x55, 0x4e, + 0x01, 0x28, 0x5d, 0xf4, 0x02, 0xe8, 0xa2, 0x92, + 0x04, 0xf2, 0x95, 0xbc, 0xfa, 0xed, 0x8f, 0xaa + }, + }, + // SIG_K1_K56oByqkwGeRhn8TfiWdspSzmNy99McLeQ7R9UFJzKgQWjAXVV4hAf1ophqs4RGtBzBTaWA1oQyaWUfPr84VcbzMqLc5H5 + { + 0x1f, 0x4b, 0x40, 0xac, 0x12, 0x8c, 0xb1, 0x78, 0xe0, 0xe3, 0x09, 0xe5, 0x74, 0x3e, 0xe5, 0xd2, 0xf4, 0x46, 0xaa, 0x14, 0x68, 0x39, 0x97, 0xc4, 0x4f, 0x92, 0x03, 0x3a, 0x72, 0x4d, 0x68, 0xe5, + 0x03, 0x71, 0xa7, 0xd9, 0x41, 0x61, 0xec, 0x5a, 0xe4, 0xb5, 0x3a, 0xa9, 0xc7, 0xd4, 0x93, 0x44, 0x6e, 0xb1, 0xe1, 0x66, 0x13, 0xc9, 0x2d, 0x74, 0x82, 0xc9, 0x42, 0x55, 0xef, 0x21, 0x1d, 0x22, + 0xe3 + }, + }, + { + "valid #5", + // Private Key: 5KUb7Y5UqoVggesYJzuNcEmjGtQMpfMiVaEDr9E5K1vNgZqvHok + { 0xda, 0xaa, 0x85, 0x66, 0xf5, 0xc9, 0x4c, 0xfa, 0x1f, 0x72, 0x30, 0x8e, 0xb8, 0xed, 0x8c, 0xd7, 0x2b, 0x08, 0x80, 0xc8, 0x2b, 0x9d, 0xe9, 0x0a, 0x81, 0xec, 0x8f, 0x3e, 0xe7, 0x75, 0xf0, 0x3c }, + { + data: { + 0xfc, 0xb2, 0x0d, 0xa1, 0x22, 0x6c, 0xcc, 0x59, + 0x26, 0x66, 0xba, 0x57, 0xeb, 0x4a, 0xfa, 0x20, + 0xff, 0x40, 0x7b, 0xc7, 0x3c, 0x35, 0xb8, 0xaa, + 0x72, 0x3e, 0x94, 0xcc, 0x05, 0xe1, 0x51, 0xc7 + }, + }, + // SIG_K1_KcU7REFtK9uNhrstdi2WrvF4mBsAKQjvUsSbL4zDWywz3FTNbNMQq1g8bKNEkmD22AH2dDEAPBkLqDYERk38gJygG3wJCx + { + 0x20, 0x3b, 0x03, 0x56, 0x1d, 0x99, 0xca, 0xcd, 0xb4, 0xfe, 0x8a, 0x55, 0x0a, 0x88, 0xe1, 0x84, 0x2b, 0x73, 0xd4, 0x7e, 0x8a, 0x5b, 0x07, 0xe4, 0xe4, 0x38, 0x70, 0x7f, 0x5d, 0xcc, 0x6a, 0xe2, + 0x5b, 0x1a, 0x37, 0xa2, 0x5b, 0x04, 0xaa, 0x55, 0xa6, 0x43, 0x75, 0x1b, 0x86, 0x20, 0x8a, 0xd3, 0xf8, 0xb0, 0xcd, 0x92, 0x30, 0x2b, 0x3b, 0xf9, 0xdc, 0x52, 0x39, 0xf8, 0xdd, 0xe7, 0xfe, 0xff, + 0x35 + }, + }, + { + "valid #6", + // Private Key: 5K4ErsLWnMv53tfFhtp5JF9CYKwmr89Pt2BEpoak1bfdgvHK2fj + { 0xa3, 0x60, 0xd7, 0x80, 0xdc, 0xe8, 0x3c, 0x5d, 0xc8, 0x61, 0xa1, 0x24, 0x6f, 0x65, 0x9e, 0x16, 0x79, 0x40, 0x10, 0x27, 0xfc, 0x9a, 0x40, 0x3f, 0x2b, 0x40, 0x7a, 0xfd, 0x1e, 0x42, 0xde, 0x84 }, + { + data: { + 0xd2, 0xfa, 0xa6, 0x97, 0x12, 0xd7, 0x04, 0x05, + 0xe8, 0x60, 0x7e, 0x86, 0x73, 0x69, 0x05, 0x90, + 0x97, 0xa2, 0x57, 0xee, 0x12, 0x4b, 0x80, 0x13, + 0x04, 0xfa, 0x7d, 0x70, 0xe7, 0xdc, 0x86, 0xb2 + }, + }, + // SIG_K1_KmHSJGQmFPVWqs48hF2YTWm51wS6CU4jobLPjKTyrCeGvwmt1YHLGgkPnENEgujvpJLYRGbffwtfqzPdDtSUXeBDAATKAH + { + 0x20, 0x69, 0x9f, 0x82, 0x48, 0xd6, 0x57, 0x42, 0x1f, 0xd6, 0x76, 0x15, 0x4c, 0xb1, 0x9c, 0xd7, 0x04, 0x1d, 0xb7, 0xed, 0x1b, 0xff, 0x26, 0xbd, 0x2e, 0xc3, 0x65, 0xbf, 0xcb, 0xfb, 0x9b, 0xae, + 0xe5, 0x77, 0x69, 0x23, 0xa8, 0xda, 0x84, 0x1c, 0x2f, 0x03, 0x95, 0x48, 0x33, 0x95, 0x85, 0xdf, 0x43, 0x69, 0xd4, 0x17, 0x37, 0x24, 0x86, 0xdf, 0x81, 0x08, 0xab, 0x81, 0xc4, 0x2c, 0xde, 0x61, + 0x4a + }, + }, + }; + + libeosio::ec_init(); + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libeosio::ec_signature_t result; + + CHECK( libeosio::ecdsa_sign(it->key, &it->dgst, result) == 0 ); + CHECK( result == it->expected ); + } + } + libeosio::ec_shutdown(); +} + diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp new file mode 100644 index 0000000..e50556f --- /dev/null +++ b/tests/ec/ecdsa_verify.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include + +TEST_CASE("ec::ecdsa_sign") { + + struct testcase { + const char *name; + libeosio::sha256_t dgst; + libeosio::ec_pubkey_t pubkey; + libeosio::ec_signature_t sig; + int expected; + }; + + std::vector tests = { + { + "valid #1", + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, + // SIG_K1_KdgBih1poWj8DYZXwLxMdjaHMzYhuAVp7XshR9ZjrZSubZwsgSpiyUKXu44NmCtKgRFswmqKaioWLTuGZrXwYPsSNCSyyr + { + 0x20, 0x44, 0x3f, 0x72, 0x22, 0xfd, 0x7a, 0x1f, 0x56, 0x2d, 0xef, 0x01, 0x55, 0x40, 0xcf, 0x50, 0x6f, 0x5f, 0xdd, 0xfe, 0x71, 0xd7, 0x18, 0xc9, 0xa8, 0xc8, 0xbe, 0x00, 0x96, 0xf8, 0x7c, 0xc7, + 0x1f, 0x2d, 0xd0, 0xd1, 0xfc, 0x4a, 0x22, 0x6a, 0x25, 0xc4, 0x7c, 0x99, 0xf9, 0xd8, 0x30, 0xfa, 0x8b, 0x5c, 0x33, 0x36, 0x61, 0xd7, 0xcf, 0x6d, 0x04, 0x97, 0x61, 0x76, 0x47, 0x65, 0x30, 0x7b, + 0x66 + }, + 0 + }, + { + "valid #2", + { + data: { + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 + }, + }, + // Public Key: EOS6tVtKhTpM6yU7kkiRz1AecDJPcBQo2w4x4oytJbJi5PMV2Rcw2 + { 0x03, 0x07, 0x69, 0xbb, 0xa5, 0x2c, 0xd2, 0xe1, 0x3b, 0x3e, 0x0a, 0x40, 0xb3, 0xa2, 0x44, 0xad, 0x71, 0x6e, 0x32, 0x64, 0x9c, 0x3a, 0x64, 0x27, 0x4f, 0x31, 0x86, 0x8a, 0x4c, 0x69, 0x58, 0x86, 0x49 }, + // SIG_K1_K4XXx6oSYBzcwzscMstvSxruxdkTCinyN9dnRo4DuBkCCpQbCJQcJmbE7aAmNueBYCccHyyDK5JDfMpvewRF2rGUFtSE2y + { + 0x1f, 0x46, 0xde, 0x7a, 0x7e, 0x87, 0xa7, 0xb0, 0x42, 0xce, 0xdc, 0x57, 0xc9, 0x0d, 0x64, 0x4c, 0xc7, 0x4d, 0xe6, 0x19, 0x5d, 0x34, 0x4e, 0xba, 0xfb, 0xdf, 0x26, 0x79, 0xa1, 0xc6, 0x99, 0x98, + 0xa7, 0x1f, 0x65, 0xcd, 0xab, 0x2d, 0x19, 0x75, 0x27, 0xdc, 0xb2, 0xc5, 0x46, 0x87, 0x5d, 0xbe, 0xc5, 0x8d, 0xb2, 0xb8, 0x7f, 0x15, 0x47, 0xd7, 0xc7, 0x94, 0x0a, 0xd5, 0x52, 0xd9, 0xe3, 0x93, + 0xd7 + }, + 0 + }, + { + "valid #3", + { + data: { + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec + }, + }, + // Public Key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i + { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, + // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh + { + 0x1f, 0x4a, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, + 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, + 0xfc + }, + 0 + }, + { + "not valid #1", + { + data: { + 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, + 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, + 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, + 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb + }, + }, + // Public Key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i + { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, + // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh + { + 0x1f, 0x4a, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, + 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, + 0xfc + }, + -1 + }, + { + "not valid #2", + { + data: { + 0xa7, 0xf7, 0x89, 0x36, 0xea, 0xb7, 0x95, 0xa7, + 0x71, 0xaa, 0x73, 0xb5, 0xf6, 0xb8, 0xa0, 0x40, + 0xe5, 0x4f, 0xb3, 0x87, 0xff, 0xd9, 0xb6, 0x4e, + 0x30, 0x4c, 0xa3, 0x78, 0xab, 0x68, 0x86, 0x24 + }, + }, + // EOS5AxTzvLZ7mRPvo1Ju9nCdB31PruYHE9uar8pF6D3CvZQGWcHq8 + { 0x02, 0x25, 0x64, 0x31, 0x9d, 0x41, 0x46, 0x82, 0xeb, 0x60, 0xed, 0x17, 0xe9, 0x8a, 0xd1, 0x21, 0x60, 0xc4, 0x65, 0xe7, 0x7e, 0x73, 0x2e, 0x45, 0xf0, 0x78, 0x8b, 0x7f, 0x43, 0x30, 0x71, 0xbc, 0x34 }, + // SIG_K1_K4QgBwbwAP879cjV66LjPmEBoVA3FXesYp6KxRbbshVeQRSJmgreFkBYx3eBTHNdJx2dxZatx9sRJvwh1JY2F2U6APmHKE + { + 0x1f, 0x45, 0xf7, 0x1c, 0x26, 0x39, 0x8a, 0x32, 0x5c, 0xfe, 0xb4, 0xc3, 0x46, 0x51, 0x22, 0x88, 0xef, 0x3b, 0xad, 0xb2, 0x5f, 0x29, 0x23, 0xab, 0x26, 0xbd, 0x3a, 0x1f, 0xbf, 0x24, 0x12, 0x36, + 0x3f, 0x73, 0xfe, 0x26, 0xfc, 0x3b, 0xd0, 0xca, 0xf1, 0xdd, 0x9d, 0x80, 0x68, 0xb9, 0x66, 0x1d, 0xc4, 0xf9, 0x6f, 0x07, 0x9e, 0xe9, 0xf2, 0x4d, 0x4e, 0xff, 0xc1, 0xd1, 0xe1, 0xf1, 0xbc, 0x6f, + 0x72 + }, + -1 + }, + { + "not valid #3", + { + data: { + 0x48, 0xd7, 0xd3, 0x83,0x9c, 0xa2, 0x82, 0xde, + 0xb6, 0x9a, 0xb8, 0x34,0x36, 0xb0, 0x9f, 0x19, + 0xbb, 0xdf, 0x2b, 0xb5,0x39, 0x42, 0x92, 0x32, + 0x33, 0x34, 0x84, 0xdd,0xba, 0xbd, 0x95, 0x43 + }, + }, + // EOS5vhJWLeBjQAiTZxWdnFkttUDWANurEka69La2nu8fp2gSi5eQk + { 0x02, 0x88, 0xb4, 0x83, 0x3a, 0x86, 0x18, 0xd3, 0xb2, 0x2b, 0xbe, 0xe6, 0x59, 0x3d, 0xf2, 0x41, 0xf6, 0xed, 0x86, 0x40, 0xe6, 0x19, 0xc8, 0x45, 0x03, 0x78, 0x57, 0xde, 0xcb, 0x2a, 0xd7, 0xc2, 0xf0 }, + // SIG_K1_K2XJBWCnx98kr8N3fe25BFDpsc8M4nERkzYSWpVAMoXBv9Nw83xDTTSErstUnh25scMGDHn9TsAqNirNruWRM3jzdKGe7m + { + 0x1f, 0x37, 0x8c, 0xf2, 0xbe, 0xb1, 0x18, 0xb7, 0x80, 0x60, 0xd5, 0x60, 0x9f, 0xee, 0x7b, 0x36, 0xcc, 0x42, 0x9c, 0x12, 0xb8, 0xd1, 0x5f, 0x62, 0xad, 0x88, 0x4f, 0x6c, 0x65, 0xa1, 0x70, 0x03, + 0x30, 0x1b, 0x65, 0x80, 0xc9, 0xe9, 0xba, 0xfe, 0xcf, 0xcc, 0xdb, 0x2d, 0xf0, 0x37, 0x1b, 0x00, 0x39, 0xb5, 0x16, 0x7c, 0xde, 0xec, 0x4b, 0xec, 0xc1, 0xf4, 0xf2, 0x6f, 0x1b, 0xfa, 0x80, 0x9d, + 0x81 + }, + -1 + }, + }; + + libeosio::ec_init(); + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + CHECK( libeosio::ecdsa_verify(&it->dgst, it->sig, it->pubkey) == it->expected ); + } + } + + libeosio::ec_shutdown(); +} + From 7ef801c2a83a43dedecc704f6f29ebb7f525a470 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 21 Mar 2023 13:19:23 +0100 Subject: [PATCH 02/16] include/libeosio/ec.hpp: Adding ecdsa_recover() --- include/libeosio/ec.hpp | 6 ++ tests/CMakeLists.txt | 1 + tests/ec/ecdsa_recover.cpp | 111 +++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 tests/ec/ecdsa_recover.cpp diff --git a/include/libeosio/ec.hpp b/include/libeosio/ec.hpp index bea3a30..ff7c57b 100644 --- a/include/libeosio/ec.hpp +++ b/include/libeosio/ec.hpp @@ -117,6 +117,12 @@ int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& */ int ecdsa_verify(const sha256_t* digest, const ec_signature_t& sig, const ec_pubkey_t& key); +/** + * Recover the public key from the signature. + * returns zero if the public key could be extracted. -1 if an error occured. + */ +int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t& key); + /** * Shutdown the ec library. */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d04ae9b..c1a8429 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,6 +6,7 @@ set(TEST_SRC ec/generate.cpp ec/pubkey.cpp ec/ecdsa_sign.cpp + ec/ecdsa_recover.cpp ec/ecdsa_verify.cpp # Base58 diff --git a/tests/ec/ecdsa_recover.cpp b/tests/ec/ecdsa_recover.cpp new file mode 100644 index 0000000..6d42215 --- /dev/null +++ b/tests/ec/ecdsa_recover.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +TEST_CASE("ec::ecdsa_sign") { + + struct testcase { + const char *name; + libeosio::sha256_t dgst; + libeosio::ec_signature_t sig; + libeosio::ec_pubkey_t expected; + int expectedRet; + }; + + std::vector tests = { + { + "valid #1", + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // SIG_K1_KdgBih1poWj8DYZXwLxMdjaHMzYhuAVp7XshR9ZjrZSubZwsgSpiyUKXu44NmCtKgRFswmqKaioWLTuGZrXwYPsSNCSyyr + { + 0x20, 0x44, 0x3f, 0x72, 0x22, 0xfd, 0x7a, 0x1f, 0x56, 0x2d, 0xef, 0x01, 0x55, 0x40, 0xcf, 0x50, 0x6f, 0x5f, 0xdd, 0xfe, 0x71, 0xd7, 0x18, 0xc9, 0xa8, 0xc8, 0xbe, 0x00, 0x96, 0xf8, 0x7c, 0xc7, + 0x1f, 0x2d, 0xd0, 0xd1, 0xfc, 0x4a, 0x22, 0x6a, 0x25, 0xc4, 0x7c, 0x99, 0xf9, 0xd8, 0x30, 0xfa, 0x8b, 0x5c, 0x33, 0x36, 0x61, 0xd7, 0xcf, 0x6d, 0x04, 0x97, 0x61, 0x76, 0x47, 0x65, 0x30, 0x7b, + 0x66 + }, + // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, + 0 + }, + { + "valid #2", + { + data: { + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 + }, + }, + // SIG_K1_K4XXx6oSYBzcwzscMstvSxruxdkTCinyN9dnRo4DuBkCCpQbCJQcJmbE7aAmNueBYCccHyyDK5JDfMpvewRF2rGUFtSE2y + { + 0x1f, 0x46, 0xde, 0x7a, 0x7e, 0x87, 0xa7, 0xb0, 0x42, 0xce, 0xdc, 0x57, 0xc9, 0x0d, 0x64, 0x4c, 0xc7, 0x4d, 0xe6, 0x19, 0x5d, 0x34, 0x4e, 0xba, 0xfb, 0xdf, 0x26, 0x79, 0xa1, 0xc6, 0x99, 0x98, + 0xa7, 0x1f, 0x65, 0xcd, 0xab, 0x2d, 0x19, 0x75, 0x27, 0xdc, 0xb2, 0xc5, 0x46, 0x87, 0x5d, 0xbe, 0xc5, 0x8d, 0xb2, 0xb8, 0x7f, 0x15, 0x47, 0xd7, 0xc7, 0x94, 0x0a, 0xd5, 0x52, 0xd9, 0xe3, 0x93, + 0xd7 + }, + // Public Key: EOS6tVtKhTpM6yU7kkiRz1AecDJPcBQo2w4x4oytJbJi5PMV2Rcw2 + { 0x03, 0x07, 0x69, 0xbb, 0xa5, 0x2c, 0xd2, 0xe1, 0x3b, 0x3e, 0x0a, 0x40, 0xb3, 0xa2, 0x44, 0xad, 0x71, 0x6e, 0x32, 0x64, 0x9c, 0x3a, 0x64, 0x27, 0x4f, 0x31, 0x86, 0x8a, 0x4c, 0x69, 0x58, 0x86, 0x49 }, + 0 + }, + { + "valid #3", + { + data: { + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec + }, + }, + // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh + { + 0x1f, 0x4a, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, + 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, + 0xfc + }, + // Public Key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i + { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, + 0 + }, + { + "not valid #1 (non valid signature)", + { + data: { + 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, + 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, + 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, + 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb + }, + }, + { + 0x1f, 0x4b, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, + 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, + 0xfe + }, + { }, + -1 + }, + }; + + libeosio::ec_init(); + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libeosio::ec_pubkey_t result; + + CHECK( libeosio::ecdsa_recover(&it->dgst, it->sig, result) == it->expectedRet ); + + if (it->expectedRet == 0) { + CHECK( result == it->expected ); + } + } + } + libeosio::ec_shutdown(); +} + From 15eaa97cec28408d874ffcd7d96db207550087f6 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 21 Mar 2023 15:34:26 +0100 Subject: [PATCH 03/16] tests/ec: fix test names. --- tests/ec/ecdsa_recover.cpp | 2 +- tests/ec/ecdsa_verify.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ec/ecdsa_recover.cpp b/tests/ec/ecdsa_recover.cpp index 6d42215..d42a8e9 100644 --- a/tests/ec/ecdsa_recover.cpp +++ b/tests/ec/ecdsa_recover.cpp @@ -2,7 +2,7 @@ #include #include -TEST_CASE("ec::ecdsa_sign") { +TEST_CASE("ec::ecdsa_recover") { struct testcase { const char *name; diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp index e50556f..8d03c1d 100644 --- a/tests/ec/ecdsa_verify.cpp +++ b/tests/ec/ecdsa_verify.cpp @@ -3,7 +3,7 @@ #include #include -TEST_CASE("ec::ecdsa_sign") { +TEST_CASE("ec::ecdsa_verify") { struct testcase { const char *name; From a3efb5a5f2c6cd648620e73afd2462ce92ce4adc Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 21 Mar 2023 15:43:39 +0100 Subject: [PATCH 04/16] src/openssl/ec.cpp: Remove unused variables. --- src/openssl/ec.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/openssl/ec.cpp b/src/openssl/ec.cpp index 583c940..8fc3ee0 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -110,10 +110,6 @@ int calculate_pubkey(EC_KEY *ec_key, ec_pubkey_t *pub) { int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub) { - const BIGNUM* n; - const EC_GROUP *group; - EC_POINT *point; - // Load private key if (EC_KEY_oct2priv(k, priv->data(), EC_PRIVKEY_SIZE) == 0) { return -1; From 95740d47b32b3c455ea5cf729ed2cabcd2f0b270 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 13:42:53 +0100 Subject: [PATCH 05/16] tests/ec/ecdsa_verify.cpp: Adding more tests --- tests/ec/ecdsa_verify.cpp | 64 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp index 8d03c1d..7eb3e59 100644 --- a/tests/ec/ecdsa_verify.cpp +++ b/tests/ec/ecdsa_verify.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -35,7 +34,66 @@ TEST_CASE("ec::ecdsa_verify") { 0 }, { - "valid #2", + "valid #2 (generated by openssl)", + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, + // SIG_K1_KWRk7botFjDfjNrfk63ud9qEwZbuJ7un8vpEKNzHJ6aSj6eMSVNXmbiXUZV4fsP7PE1kVjYEqtayghNTh1w7ea5ajq4Nrn + { + 0x20, 0x0c, 0xd6, 0xe7, 0xfe, 0x3f, 0xfa, 0x01, 0x93, 0xd2, 0x3b, 0xaa, 0xdd, 0xae, 0x5d, 0x91, 0x76, 0x3a, 0x41, 0xb0, 0x14, 0xdc, 0x29, 0xfc, 0x1b, 0x25, 0x77, 0x40, 0x4a, 0x0c, 0x59, 0xc6, + 0x77, 0x49, 0x4d, 0xf5, 0x07, 0xc9, 0xfd, 0xf5, 0x11, 0xb5, 0x09, 0xe0, 0xf1, 0x72, 0x4c, 0x08, 0x75, 0xda, 0x47, 0x78, 0x65, 0x12, 0x16, 0x39, 0x1f, 0x6c, 0x9b, 0x22, 0x6c, 0xfa, 0xdd, 0x08, + 0xb9 + }, + 0 + }, + { + "valid #3 (generated by eos-go)", + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, + // SIG_K1_KbRFLvuUdpSwpEYuDQ7EmB3ByGF9i6HdpXS8rxkLeLaKdxFoELh1RVewVyZg2x9ZwstSrJfaLuEgDa8R9RQwkntJFAvQc2 + { + 0x20, 0x32, 0xfd, 0xf9, 0x6d, 0x2d, 0xa5, 0xda, 0xf1, 0x4d, 0x75, 0x2e, 0xcf, 0x91, 0x06, 0x9d, 0xbb, 0x6a, 0x24, 0x79, 0xd1, 0x70, 0x8d, 0xc7, 0xa2, 0xc2, 0xc0, 0xb4, 0xf6, 0xb7, 0x2b, 0x06, + 0xbe, 0x37, 0xc7, 0xbb, 0x3b, 0xe6, 0x47, 0x4c, 0xff, 0x6d, 0x90, 0x02, 0xd4, 0x73, 0x0d, 0x5d, 0xb3, 0x76, 0xc6, 0x52, 0xae, 0xca, 0x90, 0x31, 0xf3, 0xf1, 0x76, 0x5a, 0xbb, 0xad, 0xa5, 0xbc, + 0x5f + }, + 0 + }, + { + "valid #4 (generated by eosjs)", + { + data: { + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, + // SIG_K1_JzpX9nSsAiQkNLLZ1vSfmDrFQd7228zFhVopCH7S1TnbQ844mDbJeXBF1qifFAcWKBL2mxX7oqj9tgBEPwtu8KY8cWLMim + { + 0x1f, 0x2a, 0x88, 0x81, 0x3c, 0xce, 0x11, 0xbf, 0x5a, 0x59, 0x77, 0x8c, 0x32, 0x5b, 0x3b, 0xe8, 0x78, 0x8f, 0x7f, 0x7d, 0x18, 0x1a, 0x6d, 0xb3, 0x7b, 0x36, 0x18, 0x3f, 0xc2, 0xba, 0xb5, 0xab, 0x61, 0x69, 0x89, 0x9c, 0xb9, 0x44, 0x2c, 0x19, 0x2a, 0xa3, 0xb8, 0x3d, 0x2d, 0x09, 0xd0, 0xc4, 0x1a, 0x67, 0xc7, 0xa6, 0x5d, 0xf5, 0x10, 0xb4, 0x75, 0x05, 0x69, 0x4e, 0x4e, 0xc0, 0x5f, 0x34, 0x9e + }, + 0 + }, + + { + "valid #4", { data: { 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, @@ -55,7 +113,7 @@ TEST_CASE("ec::ecdsa_verify") { 0 }, { - "valid #3", + "valid #5", { data: { 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, From fe873c1df80b0337a3c473fc3fb1e98eddb18361 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 13:46:28 +0100 Subject: [PATCH 06/16] tests/ec/ecdsa_sign.cpp: need to use ecdsa_verify() to check signatures. different implementations produce different signatures so we can't use hardcoded values. --- tests/ec/ecdsa_sign.cpp | 60 +++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/tests/ec/ecdsa_sign.cpp b/tests/ec/ecdsa_sign.cpp index e6b6902..99c19fd 100644 --- a/tests/ec/ecdsa_sign.cpp +++ b/tests/ec/ecdsa_sign.cpp @@ -7,8 +7,8 @@ TEST_CASE("ec::ecdsa_sign") { struct testcase { const char *name; libeosio::ec_privkey_t key; + libeosio::ec_pubkey_t pub; libeosio::sha256_t dgst; - libeosio::ec_signature_t expected; }; std::vector tests = { @@ -16,6 +16,8 @@ TEST_CASE("ec::ecdsa_sign") { "valid #1", // Private Key: 5Ke4YqL2TCtiUTTA1CVMXSrrEHuK9HzbUSWX791yC2UaX2dWRDw { 0xf0, 0x2d, 0x00, 0x72, 0x8a, 0x7a, 0x93, 0x86, 0xaf, 0xbe, 0x19, 0xab, 0x79, 0x8c, 0xa1, 0x61, 0xab, 0x96, 0x74, 0x7f, 0xe5, 0x97, 0x19, 0x07, 0xb1, 0xc8, 0x65, 0x63, 0xc8, 0x11, 0xe6, 0x74 }, + // Public key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD + { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, { data: { 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, @@ -24,17 +26,13 @@ TEST_CASE("ec::ecdsa_sign") { 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, }, - // SIG_K1_KdgBih1poWj8DYZXwLxMdjaHMzYhuAVp7XshR9ZjrZSubZwsgSpiyUKXu44NmCtKgRFswmqKaioWLTuGZrXwYPsSNCSyyr - { - 0x20, 0x44, 0x3f, 0x72, 0x22, 0xfd, 0x7a, 0x1f, 0x56, 0x2d, 0xef, 0x01, 0x55, 0x40, 0xcf, 0x50, 0x6f, 0x5f, 0xdd, 0xfe, 0x71, 0xd7, 0x18, 0xc9, 0xa8, 0xc8, 0xbe, 0x00, 0x96, 0xf8, 0x7c, 0xc7, - 0x1f, 0x2d, 0xd0, 0xd1, 0xfc, 0x4a, 0x22, 0x6a, 0x25, 0xc4, 0x7c, 0x99, 0xf9, 0xd8, 0x30, 0xfa, 0x8b, 0x5c, 0x33, 0x36, 0x61, 0xd7, 0xcf, 0x6d, 0x04, 0x97, 0x61, 0x76, 0x47, 0x65, 0x30, 0x7b, - 0x66 - }, }, { "valid #2", // Private Key: 5J1VLF3TtdM3FiaUZNjeUXTUmVmRRULSdxrGf3XwQkRAZLcN64b { 0x19, 0x6f, 0xd7, 0xa5, 0x3f, 0xab, 0x59, 0x8e, 0xa5, 0xef, 0xec, 0x79, 0xdd, 0xbc, 0x49, 0xae, 0xef, 0xff, 0x41, 0x3c, 0x5f, 0xfe, 0x50, 0x3a, 0x66, 0xbc, 0xff, 0xf1, 0x32, 0x1b, 0x8c, 0x2f }, + // Public key: EOS6tVtKhTpM6yU7kkiRz1AecDJPcBQo2w4x4oytJbJi5PMV2Rcw2 + { 0x03, 0x07, 0x69, 0xbb, 0xa5, 0x2c, 0xd2, 0xe1, 0x3b, 0x3e, 0x0a, 0x40, 0xb3, 0xa2, 0x44, 0xad, 0x71, 0x6e, 0x32, 0x64, 0x9c, 0x3a, 0x64, 0x27, 0x4f, 0x31, 0x86, 0x8a, 0x4c, 0x69, 0x58, 0x86, 0x49 }, { data: { 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, @@ -43,17 +41,13 @@ TEST_CASE("ec::ecdsa_sign") { 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 }, }, - // SIG_K1_K4XXx6oSYBzcwzscMstvSxruxdkTCinyN9dnRo4DuBkCCpQbCJQcJmbE7aAmNueBYCccHyyDK5JDfMpvewRF2rGUFtSE2y - { - 0x1f, 0x46, 0xde, 0x7a, 0x7e, 0x87, 0xa7, 0xb0, 0x42, 0xce, 0xdc, 0x57, 0xc9, 0x0d, 0x64, 0x4c, 0xc7, 0x4d, 0xe6, 0x19, 0x5d, 0x34, 0x4e, 0xba, 0xfb, 0xdf, 0x26, 0x79, 0xa1, 0xc6, 0x99, 0x98, - 0xa7, 0x1f, 0x65, 0xcd, 0xab, 0x2d, 0x19, 0x75, 0x27, 0xdc, 0xb2, 0xc5, 0x46, 0x87, 0x5d, 0xbe, 0xc5, 0x8d, 0xb2, 0xb8, 0x7f, 0x15, 0x47, 0xd7, 0xc7, 0x94, 0x0a, 0xd5, 0x52, 0xd9, 0xe3, 0x93, - 0xd7 - }, }, { "valid #3", // Private Key: 5Hz2iVzC9jDcBUGo2EUSPdcvL1s8jxJ6pY95gQX59mHoHx6zi6W { 0x16, 0x1f, 0xd0, 0x41, 0x21, 0x50, 0x04, 0x13, 0x90, 0x61, 0xb3, 0xbd, 0xc8, 0x83, 0x09, 0xf0, 0x80, 0x3c, 0x90, 0x45, 0x8e, 0x9a, 0x8c, 0xb2, 0xfb, 0x6a, 0x3c, 0x27, 0x51, 0xcf, 0xd9, 0xca }, + // Public key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i + { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, { data: { 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, @@ -62,17 +56,13 @@ TEST_CASE("ec::ecdsa_sign") { 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec }, }, - // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh - { - 0x1f, 0x4a, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, - 0x48, 0x41, 0x74, 0xb0, 0xe3, 0xb1, 0x4b, 0x06, 0x2c, 0x53, 0x93, 0xbc, 0x35, 0xea, 0xac, 0xd7, 0x9e, 0x07, 0xa7, 0xa1, 0x2e, 0xac, 0xa0, 0x81, 0x45, 0xdb, 0xd4, 0x53, 0x68, 0xda, 0xaa, 0xc6, - 0xfc - }, }, { "valid #4", // Private Key: 5KbH8qYyg6f93gHuAdbDmwCVMoarXHqEn5CdbW2VtHTyMsLShwM { 0xe9, 0xdb, 0x4d, 0xdc, 0x8e, 0x08, 0xd2, 0x04, 0x75, 0xc2, 0xb6, 0xf7, 0x14, 0x9e, 0xa2, 0x16, 0xd7, 0x2b, 0x34, 0x3b, 0xba, 0xb4, 0x7d, 0xe6, 0xd3, 0x96, 0xc1, 0x63, 0x82, 0xdf, 0x2d, 0x13 }, + // Public key: EOS6E12fqQqWLYJS32ffB6LaQYxyDXUQSPfMTMnj6tc5bgntZKcBy + { 0x02, 0xaf, 0xff, 0xeb, 0xef, 0x47, 0x70, 0x58, 0x2f, 0x9b, 0x66, 0x6c, 0xe0, 0xea, 0x84, 0x32, 0x41, 0xa0, 0x94, 0x36, 0x30, 0x9b, 0xfc, 0xdb, 0x9a, 0x58, 0xdd, 0x0e, 0xe6, 0x3e, 0xd4, 0x5a, 0xcd }, { data: { 0xbc, 0x83, 0xbe, 0xe1, 0x73, 0x82, 0xfb, 0x02, @@ -81,17 +71,13 @@ TEST_CASE("ec::ecdsa_sign") { 0x04, 0xf2, 0x95, 0xbc, 0xfa, 0xed, 0x8f, 0xaa }, }, - // SIG_K1_K56oByqkwGeRhn8TfiWdspSzmNy99McLeQ7R9UFJzKgQWjAXVV4hAf1ophqs4RGtBzBTaWA1oQyaWUfPr84VcbzMqLc5H5 - { - 0x1f, 0x4b, 0x40, 0xac, 0x12, 0x8c, 0xb1, 0x78, 0xe0, 0xe3, 0x09, 0xe5, 0x74, 0x3e, 0xe5, 0xd2, 0xf4, 0x46, 0xaa, 0x14, 0x68, 0x39, 0x97, 0xc4, 0x4f, 0x92, 0x03, 0x3a, 0x72, 0x4d, 0x68, 0xe5, - 0x03, 0x71, 0xa7, 0xd9, 0x41, 0x61, 0xec, 0x5a, 0xe4, 0xb5, 0x3a, 0xa9, 0xc7, 0xd4, 0x93, 0x44, 0x6e, 0xb1, 0xe1, 0x66, 0x13, 0xc9, 0x2d, 0x74, 0x82, 0xc9, 0x42, 0x55, 0xef, 0x21, 0x1d, 0x22, - 0xe3 - }, }, { "valid #5", // Private Key: 5KUb7Y5UqoVggesYJzuNcEmjGtQMpfMiVaEDr9E5K1vNgZqvHok { 0xda, 0xaa, 0x85, 0x66, 0xf5, 0xc9, 0x4c, 0xfa, 0x1f, 0x72, 0x30, 0x8e, 0xb8, 0xed, 0x8c, 0xd7, 0x2b, 0x08, 0x80, 0xc8, 0x2b, 0x9d, 0xe9, 0x0a, 0x81, 0xec, 0x8f, 0x3e, 0xe7, 0x75, 0xf0, 0x3c }, + // Public key: EOS6uqJC6F7eEMq7SHREhTzRoAT7uumrmHDDYC595CJKeBgPvPojd + { 0x03, 0x0a, 0x71, 0x8e, 0x48, 0x1a, 0x7a, 0x55, 0x84, 0xb9, 0xaf, 0x24, 0xca, 0x8f, 0x85, 0xcd, 0x0f, 0x55, 0x5c, 0xcf, 0xb3, 0x7b, 0x39, 0x5b, 0xa5, 0xfc, 0xb9, 0xaf, 0x26, 0xc7, 0xc7, 0x88, 0x7b }, { data: { 0xfc, 0xb2, 0x0d, 0xa1, 0x22, 0x6c, 0xcc, 0x59, @@ -100,17 +86,13 @@ TEST_CASE("ec::ecdsa_sign") { 0x72, 0x3e, 0x94, 0xcc, 0x05, 0xe1, 0x51, 0xc7 }, }, - // SIG_K1_KcU7REFtK9uNhrstdi2WrvF4mBsAKQjvUsSbL4zDWywz3FTNbNMQq1g8bKNEkmD22AH2dDEAPBkLqDYERk38gJygG3wJCx - { - 0x20, 0x3b, 0x03, 0x56, 0x1d, 0x99, 0xca, 0xcd, 0xb4, 0xfe, 0x8a, 0x55, 0x0a, 0x88, 0xe1, 0x84, 0x2b, 0x73, 0xd4, 0x7e, 0x8a, 0x5b, 0x07, 0xe4, 0xe4, 0x38, 0x70, 0x7f, 0x5d, 0xcc, 0x6a, 0xe2, - 0x5b, 0x1a, 0x37, 0xa2, 0x5b, 0x04, 0xaa, 0x55, 0xa6, 0x43, 0x75, 0x1b, 0x86, 0x20, 0x8a, 0xd3, 0xf8, 0xb0, 0xcd, 0x92, 0x30, 0x2b, 0x3b, 0xf9, 0xdc, 0x52, 0x39, 0xf8, 0xdd, 0xe7, 0xfe, 0xff, - 0x35 - }, }, { "valid #6", // Private Key: 5K4ErsLWnMv53tfFhtp5JF9CYKwmr89Pt2BEpoak1bfdgvHK2fj { 0xa3, 0x60, 0xd7, 0x80, 0xdc, 0xe8, 0x3c, 0x5d, 0xc8, 0x61, 0xa1, 0x24, 0x6f, 0x65, 0x9e, 0x16, 0x79, 0x40, 0x10, 0x27, 0xfc, 0x9a, 0x40, 0x3f, 0x2b, 0x40, 0x7a, 0xfd, 0x1e, 0x42, 0xde, 0x84 }, + // Public key: EOS5YiBwqnFXqeb5hCmwV9bLHp6Jg5hVnRjzYVApf2DXyRGr7B7kZ + { 0x02, 0x56, 0xc9, 0x41, 0x90, 0x44, 0x8a, 0xcc, 0x89, 0x91, 0x79, 0xaf, 0x4e, 0x3a, 0x72, 0xa7, 0x24, 0x86, 0x7f, 0xd8, 0x03, 0x07, 0x04, 0x30, 0xd3, 0xf3, 0x6b, 0x20, 0x94, 0x85, 0x78, 0xfc, 0x38 }, { data: { 0xd2, 0xfa, 0xa6, 0x97, 0x12, 0xd7, 0x04, 0x05, @@ -119,12 +101,6 @@ TEST_CASE("ec::ecdsa_sign") { 0x04, 0xfa, 0x7d, 0x70, 0xe7, 0xdc, 0x86, 0xb2 }, }, - // SIG_K1_KmHSJGQmFPVWqs48hF2YTWm51wS6CU4jobLPjKTyrCeGvwmt1YHLGgkPnENEgujvpJLYRGbffwtfqzPdDtSUXeBDAATKAH - { - 0x20, 0x69, 0x9f, 0x82, 0x48, 0xd6, 0x57, 0x42, 0x1f, 0xd6, 0x76, 0x15, 0x4c, 0xb1, 0x9c, 0xd7, 0x04, 0x1d, 0xb7, 0xed, 0x1b, 0xff, 0x26, 0xbd, 0x2e, 0xc3, 0x65, 0xbf, 0xcb, 0xfb, 0x9b, 0xae, - 0xe5, 0x77, 0x69, 0x23, 0xa8, 0xda, 0x84, 0x1c, 0x2f, 0x03, 0x95, 0x48, 0x33, 0x95, 0x85, 0xdf, 0x43, 0x69, 0xd4, 0x17, 0x37, 0x24, 0x86, 0xdf, 0x81, 0x08, 0xab, 0x81, 0xc4, 0x2c, 0xde, 0x61, - 0x4a - }, }, }; @@ -135,7 +111,15 @@ TEST_CASE("ec::ecdsa_sign") { libeosio::ec_signature_t result; CHECK( libeosio::ecdsa_sign(it->key, &it->dgst, result) == 0 ); - CHECK( result == it->expected ); + + // Need to use verify here as different implemententations produces different signatures. + // (i have tested eosjs, eos-go and ofc libeosio) + // However, the signatures are correct and can be validated by all implementations. + // + // Now, how do we know that ecdsa_verify is correct? + // well, in escdsa_verify.cpp there are tests that checks hardcoded signatures generated by different implementations and should be fine. + + CHECK( libeosio::ecdsa_verify(&it->dgst, result, it->pub) == 0); } } libeosio::ec_shutdown(); From 7ead6f6bde5df44fe9fb9c5e0a8ad1170e19ce98 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 13:59:14 +0100 Subject: [PATCH 07/16] src/openssl/ec.cpp: simplify EC_POINT_point2oct() with a macro. --- src/openssl/ec.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/openssl/ec.cpp b/src/openssl/ec.cpp index 8fc3ee0..58668f1 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -28,6 +28,9 @@ namespace libeosio { +#define EC_POINT_encode(group, point, buf, len, ctx) \ + EC_POINT_point2oct((group), (point), POINT_CONVERSION_COMPRESSED, (buf), (len), (ctx)) + BN_CTX *ctx = NULL; EC_KEY *k = NULL; @@ -100,8 +103,7 @@ int calculate_pubkey(EC_KEY *ec_key, ec_pubkey_t *pub) { rc = EC_POINT_mul(group, point, pk, NULL, NULL, ctx); if (rc != 0) { // Encode public key - rc = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, - pub->data(), EC_PUBKEY_SIZE, ctx); + rc = EC_POINT_encode(group, point, pub->data(), EC_PUBKEY_SIZE, ctx); } EC_POINT_free(point); From b1da6552f7b488b2090381a7aad3bde6a9195d75 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:03:03 +0100 Subject: [PATCH 08/16] src/openssl/ec.cpp: make calculate_pubkey() output an EC_POINT instead of encoding to binary, also pass group as a parameter. --- src/openssl/ec.cpp | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/openssl/ec.cpp b/src/openssl/ec.cpp index 58668f1..f58053b 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -78,16 +78,8 @@ int ec_generate_privkey(ec_privkey_t *priv) { } // Calcualte a public key from a EC_KEY object. -int calculate_pubkey(EC_KEY *ec_key, ec_pubkey_t *pub) { +int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point) { const BIGNUM* pk; - const EC_GROUP *group; - EC_POINT *point; - int rc; - - // Get the curve (group) number first. - if ((group = EC_KEY_get0_group(ec_key)) == NULL) { - return 0; - } // Then get the private key number if ((pk = EC_KEY_get0_private_key(ec_key)) == NULL) { @@ -95,29 +87,40 @@ int calculate_pubkey(EC_KEY *ec_key, ec_pubkey_t *pub) { } // Create a new point. - if ((point = EC_POINT_new(group)) == NULL) { + if ((*point = EC_POINT_new(group)) == NULL) { return 0; } // Multiply curve (group) and private key to get the public key. - rc = EC_POINT_mul(group, point, pk, NULL, NULL, ctx); - if (rc != 0) { - // Encode public key - rc = EC_POINT_encode(group, point, pub->data(), EC_PUBKEY_SIZE, ctx); - } - - EC_POINT_free(point); - return rc; + return EC_POINT_mul(group, *point, pk, NULL, NULL, NULL); } int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub) { + int rc = -1; + const EC_GROUP *group; + EC_POINT *point; + // Load private key if (EC_KEY_oct2priv(k, priv->data(), EC_PRIVKEY_SIZE) == 0) { return -1; } - return calculate_pubkey(k, pub) == 0 ? -1 : 0; + if ((group = EC_KEY_get0_group(k)) == NULL) { + return -1; + } + + if (calculate_pubkey(group, k, &point) == 0) { + return -1; + } + + // Encode public key + if (EC_POINT_encode(group, point, pub->data(), EC_PUBKEY_SIZE, ctx) != 0) { + rc = 0; + } + + EC_POINT_free(point); + return rc; } int ec_generate_key(struct ec_keypair *pair) { From fdaab853ee46cabb5fc694d7fed553bc5cff8f7e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 14 Mar 2023 18:46:31 +0100 Subject: [PATCH 09/16] CMakeLists.txt: add "C" to project languages. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d486214..5de4642 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ project(libeosio VERSION 0.1.5 DESCRIPTION "C++ library for EOSIO" HOMEPAGE_URL "https://github.com/eosswedenorg/libeosio" - LANGUAGES CXX + LANGUAGES C CXX ) set( PROJECT_MAINTAINER "Henrik Hautakoski ") From 88dacd22f802ca037b09219bade17b94d492eec5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:19:36 +0100 Subject: [PATCH 10/16] src/openssl/ec.cpp: move helper functions/macros to internal.h and helpers.c files. --- CMakeLists.txt | 1 + src/openssl/ec.cpp | 22 +-------------------- src/openssl/helpers.c | 44 ++++++++++++++++++++++++++++++++++++++++++ src/openssl/internal.h | 42 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/openssl/helpers.c create mode 100644 src/openssl/internal.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5de4642..d3bfdd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ include(OpenSSL) set (LIB_SOURCE ${LIB_SOURCE} src/openssl/ec.cpp src/openssl/hash.cpp + src/openssl/helpers.c ) add_library( ${LIB_NAME} STATIC ${LIB_SOURCE} ) diff --git a/src/openssl/ec.cpp b/src/openssl/ec.cpp index f58053b..c001c82 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -25,12 +25,10 @@ #include #include #include +#include "internal.h" namespace libeosio { -#define EC_POINT_encode(group, point, buf, len, ctx) \ - EC_POINT_point2oct((group), (point), POINT_CONVERSION_COMPRESSED, (buf), (len), (ctx)) - BN_CTX *ctx = NULL; EC_KEY *k = NULL; @@ -77,24 +75,6 @@ int ec_generate_privkey(ec_privkey_t *priv) { return 0; } -// Calcualte a public key from a EC_KEY object. -int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point) { - const BIGNUM* pk; - - // Then get the private key number - if ((pk = EC_KEY_get0_private_key(ec_key)) == NULL) { - return 0; - } - - // Create a new point. - if ((*point = EC_POINT_new(group)) == NULL) { - return 0; - } - - // Multiply curve (group) and private key to get the public key. - return EC_POINT_mul(group, *point, pk, NULL, NULL, NULL); -} - int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub) { int rc = -1; diff --git a/src/openssl/helpers.c b/src/openssl/helpers.c new file mode 100644 index 0000000..e812749 --- /dev/null +++ b/src/openssl/helpers.c @@ -0,0 +1,44 @@ +/** + * MIT License + * + * Copyright (c) 2019-2021 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include + +// Calcualte a public key from a EC_KEY object. +int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point) { + const BIGNUM* pk; + + // Then get the private key number + if ((pk = EC_KEY_get0_private_key(ec_key)) == NULL) { + return 0; + } + + // Create a new point. + if ((*point = EC_POINT_new(group)) == NULL) { + return 0; + } + + // Multiply curve (group) and private key to get the public key. + return EC_POINT_mul(group, *point, pk, NULL, NULL, NULL); +} \ No newline at end of file diff --git a/src/openssl/internal.h b/src/openssl/internal.h new file mode 100644 index 0000000..b4c2a63 --- /dev/null +++ b/src/openssl/internal.h @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include + +#ifndef LIBEOSIO_OPENSSL_INTERNAL_H +#define LIBEOSIO_OPENSSL_INTERNAL_H + +#define EC_POINT_encode(group, point, buf, len, ctx) \ + EC_POINT_point2oct((group), (point), POINT_CONVERSION_COMPRESSED, (buf), (len), (ctx)) + +#ifdef __cplusplus +extern "C" { +#endif + +int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point); + +#ifdef __cplusplus +} +#endif + +#endif /* LIBEOSIO_OPENSSL_INTERNAL_H */ \ No newline at end of file From e4150af3555e6d7bd7f3500af46ccf863f4d9ab9 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:20:31 +0100 Subject: [PATCH 11/16] Adding src/openssl/recovery.c --- CMakeLists.txt | 1 + src/openssl/recovery.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/openssl/recovery.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d3bfdd8..58138fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ set (LIB_SOURCE ${LIB_SOURCE} src/openssl/ec.cpp src/openssl/hash.cpp src/openssl/helpers.c + src/openssl/recovery.c ) add_library( ${LIB_NAME} STATIC ${LIB_SOURCE} ) diff --git a/src/openssl/recovery.c b/src/openssl/recovery.c new file mode 100644 index 0000000..02ae975 --- /dev/null +++ b/src/openssl/recovery.c @@ -0,0 +1,79 @@ +// Copyright (c) 2009-2013 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. +// +// Taken from https://github.com/bitcoin/bitcoin/blob/9b1200c23bbced3a78b58067c1f6414103653795/src/key.cpp#L56 +#include +#include + +int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, const BIGNUM* r, const BIGNUM* s, const unsigned char *msg, int msglen, int recid, int check) +{ + if (!eckey) return 0; + + int ret = 0; + BN_CTX *ctx = NULL; + + BIGNUM *x = NULL; + BIGNUM *e = NULL; + BIGNUM *order = NULL; + BIGNUM *sor = NULL; + BIGNUM *eor = NULL; + BIGNUM *field = NULL; + EC_POINT *R = NULL; + EC_POINT *O = NULL; + EC_POINT *Q = NULL; + BIGNUM *rr = NULL; + BIGNUM *zero = NULL; + int n = 0; + int i = recid / 2; + + const EC_GROUP *group = EC_KEY_get0_group(eckey); + if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; } + BN_CTX_start(ctx); + order = BN_CTX_get(ctx); + if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; } + x = BN_CTX_get(ctx); + if (!BN_copy(x, order)) { ret=-1; goto err; } + if (!BN_mul_word(x, i)) { ret=-1; goto err; } + if (!BN_add(x, x, r)) { ret=-1; goto err; } + field = BN_CTX_get(ctx); + if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; } + if (BN_cmp(x, field) >= 0) { ret=0; goto err; } + if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } + if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; } + if (check) + { + if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } + if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; } + if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; } + } + if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } + n = EC_GROUP_get_degree(group); + e = BN_CTX_get(ctx); + if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; } + if (8*msglen > n) BN_rshift(e, e, 8-(n & 7)); + zero = BN_CTX_get(ctx); + BN_zero(zero); + if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; } + rr = BN_CTX_get(ctx); + if (!BN_mod_inverse(rr, r, order, ctx)) { ret=-1; goto err; } + sor = BN_CTX_get(ctx); + if (!BN_mod_mul(sor, s, rr, order, ctx)) { ret=-1; goto err; } + eor = BN_CTX_get(ctx); + if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; } + if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; } + if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; } + + ret = 1; + +err: + if (ctx) { + BN_CTX_end(ctx); + BN_CTX_free(ctx); + } + if (R != NULL) EC_POINT_free(R); + if (O != NULL) EC_POINT_free(O); + if (Q != NULL) EC_POINT_free(Q); + return ret; +} + From d695d1c9adb8fd142519436d84f528cbc9ca6aae Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:21:17 +0100 Subject: [PATCH 12/16] src/openssl/internal.h: Adding ECDSA_SIG_recover_key_GFp function definition. --- src/openssl/internal.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/openssl/internal.h b/src/openssl/internal.h index b4c2a63..8da3f89 100644 --- a/src/openssl/internal.h +++ b/src/openssl/internal.h @@ -35,6 +35,8 @@ extern "C" { int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point); +int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, const BIGNUM* r, const BIGNUM* s, const unsigned char *msg, int msglen, int recid, int check); + #ifdef __cplusplus } #endif From 073955ab50a5755aad844b6f5220c15f1cfe2168 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:33:48 +0100 Subject: [PATCH 13/16] src/openssl/internal.h: Adding EC_KEY_new_secp256k1 macro --- src/openssl/internal.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/openssl/internal.h b/src/openssl/internal.h index 8da3f89..87bd979 100644 --- a/src/openssl/internal.h +++ b/src/openssl/internal.h @@ -22,10 +22,13 @@ * SOFTWARE. */ #include +#include #ifndef LIBEOSIO_OPENSSL_INTERNAL_H #define LIBEOSIO_OPENSSL_INTERNAL_H +#define EC_KEY_new_secp256k1() (EC_KEY_new_by_curve_name( NID_secp256k1 )) + #define EC_POINT_encode(group, point, buf, len, ctx) \ EC_POINT_point2oct((group), (point), POINT_CONVERSION_COMPRESSED, (buf), (len), (ctx)) From 4aa94ebee917d7d20dda9c95892eea8e5316ac0b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:34:47 +0100 Subject: [PATCH 14/16] src/openssl/helpers.c: Adding signature serialize/unserialize functions. --- src/openssl/helpers.c | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/openssl/helpers.c b/src/openssl/helpers.c index e812749..24ba631 100644 --- a/src/openssl/helpers.c +++ b/src/openssl/helpers.c @@ -24,6 +24,7 @@ #include #include +#include // Calcualte a public key from a EC_KEY object. int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **point) { @@ -41,4 +42,60 @@ int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **poi // Multiply curve (group) and private key to get the public key. return EC_POINT_mul(group, *point, pk, NULL, NULL, NULL); +} + +int ECDSA_SIG_unserialize_rs(const unsigned char *sig, BIGNUM **r, BIGNUM **s, int *recid) { + + *recid = sig[0] - 27 - 4; + + if ((*r = BN_bin2bn(sig + 1, 32, NULL)) == NULL) { + return 0; + } + + if ((*s = BN_bin2bn(sig + 33, 32, NULL)) == NULL) { + BN_free(*r); + return 0; + } + return 1; +} + +int ECDSA_SIG_unserialize(const unsigned char *sig, ECDSA_SIG *ecdsa_sig, int *recid) { + + BIGNUM *r, *s; + + if (ECDSA_SIG_unserialize_rs(sig, &r, &s, recid) == 0) { + return 0; + } + + if (ECDSA_SIG_set0(ecdsa_sig, r, s) == 0) { + BN_free(r); + BN_free(s); + return 0; + } + + // r,s pointers are owned by ECDSA_SIG from this point. + // So we should not free them. + return 1; +} + +int ECDSA_SIG_serialize(const ECDSA_SIG *ecdsa_sig, int recid, unsigned char* sig) { + + unsigned char* der = NULL; + int bytes, ret = -1; + unsigned char lR, lS; + + bytes = i2d_ECDSA_SIG( ecdsa_sig, &der ); + lR = der[3]; + lS = der[5+lR]; + + if (lR != 32 || lS != 32) goto err; + + memcpy(sig + 1, &der[4], 32); + memcpy(sig + 33, &der[6+32], 32); + sig[0] = recid + 27 + 4; + + ret = 0; +err: + free(der); + return ret; } \ No newline at end of file From f0ebda6da59c68388b2e5faeb0d8c837539d51bc Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:35:22 +0100 Subject: [PATCH 15/16] src/openssl/internal.h: Adding signature serialize/unserialize function definitions. --- src/openssl/internal.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/openssl/internal.h b/src/openssl/internal.h index 87bd979..9a5d6c1 100644 --- a/src/openssl/internal.h +++ b/src/openssl/internal.h @@ -40,6 +40,24 @@ int calculate_pubkey(const EC_GROUP *group, const EC_KEY *ec_key, EC_POINT **poi int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, const BIGNUM* r, const BIGNUM* s, const unsigned char *msg, int msglen, int recid, int check); +/** + * Signature serialization function. + * sig must be a pointer to a serialized signature and be atleast 65 (32s + 32 + 1) bytes long. + * + * returns -1 if there was an error. zero otherwise. + */ +int ECDSA_SIG_serialize(const ECDSA_SIG *ecdsa_sig, int recid, unsigned char* sig); + +/** + * Signature unserialization functions. + * sig must be a pointer to a serialized signature and be atleast 65 (32s + 32 + 1) bytes long. + * + * returns -1 if there was an error. zero otherwise. + */ +int ECDSA_SIG_unserialize(const unsigned char *sig, ECDSA_SIG *ecdsa_sig, int *recid); + +int ECDSA_SIG_unserialize_rs(const unsigned char *sig, BIGNUM **r, BIGNUM **s, int *recid); + #ifdef __cplusplus } #endif From d853ed6f62def7004944bd27852acf9d98062b73 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 22 Mar 2023 14:36:11 +0100 Subject: [PATCH 16/16] Adding src/openssl/ecdsa.cpp --- CMakeLists.txt | 1 + src/openssl/ecdsa.cpp | 185 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 src/openssl/ecdsa.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 58138fa..1b3e8b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ set( LIB_SOURCE include(OpenSSL) set (LIB_SOURCE ${LIB_SOURCE} src/openssl/ec.cpp + src/openssl/ecdsa.cpp src/openssl/hash.cpp src/openssl/helpers.c src/openssl/recovery.c diff --git a/src/openssl/ecdsa.cpp b/src/openssl/ecdsa.cpp new file mode 100644 index 0000000..a897166 --- /dev/null +++ b/src/openssl/ecdsa.cpp @@ -0,0 +1,185 @@ +/** + * MIT License + * + * Copyright (c) 2019-2021 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include "internal.h" + +namespace libeosio { + +extern BN_CTX *ctx; + +int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& sig) { + + int rc = -1; + EC_POINT *pub; + const EC_GROUP *group; + ECDSA_SIG *ecdsa_sig; + EC_KEY *ec_key; + + if ((ec_key = EC_KEY_new_secp256k1()) == NULL) { + return -1; + } + + if (EC_KEY_oct2priv(ec_key, key.data(), key.size()) < 0) { + goto err1; + } + + group = EC_KEY_get0_group(ec_key); + if (group == NULL) { + goto err1; + } + + if (calculate_pubkey(group, ec_key, &pub) == 0) { + goto err2; + } + + while (1) { + int recid = -1; + const BIGNUM *r, *s; + EC_KEY* tmpk; + + ecdsa_sig = ECDSA_do_sign(digest->data, 32, ec_key); + if (ecdsa_sig == NULL) { + goto err2; + } + + // Get R and S numbers. + r = ECDSA_SIG_get0_r(ecdsa_sig); + s = ECDSA_SIG_get0_s(ecdsa_sig); + + tmpk = EC_KEY_new_by_curve_name( NID_secp256k1 ); + for (int i = 0; i < 4; i++) { + if (ECDSA_SIG_recover_key_GFp(tmpk, r, s, digest->data, sizeof(digest->data), i, 1) == 1) { + const EC_POINT *p = EC_KEY_get0_public_key(tmpk); + + // Compare public keys + if (EC_POINT_cmp(group, pub, p, ctx) == 0) { + recid = i; + break; + } + } + } + + EC_KEY_free( tmpk ); + + // Could not find recovery id. + if (recid == -1) { + goto err2; + } + + if (ECDSA_SIG_serialize(ecdsa_sig, recid, sig.data()) == 0) { + goto out; + } + } + +out: rc = 0; +err2: + EC_POINT_free(pub); +err1: + EC_KEY_free(ec_key); + return rc; +} + +int ecdsa_verify(const sha256_t* digest, const ec_signature_t& sig, const ec_pubkey_t& pub) { + + int recid, ret = -1; + EC_POINT *point; + const EC_GROUP *group; + ECDSA_SIG* ecdsa_sig; + EC_KEY *ec_key; + + ec_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); + if (ec_key == NULL) { + return -1; + } + + if ((ecdsa_sig = ECDSA_SIG_new()) == NULL) { + goto err1; + } + + if (ECDSA_SIG_unserialize(sig.data(), ecdsa_sig, &recid) == 0) { + goto err2; + } + + if ((group = EC_KEY_get0_group(ec_key)) == NULL) { + goto err2; + } + + if ((point = EC_POINT_new(group)) == NULL) { + goto err2; + } + + if (EC_POINT_oct2point(group, point, pub.data(), EC_PUBKEY_SIZE, ctx) == 0) { + goto err3; + } + + if (EC_KEY_set_public_key(ec_key, point) == 0) { + goto err3; + } + + if (ECDSA_do_verify(digest->data, 32, ecdsa_sig, ec_key) == 1) { + ret = 0; + } + +err3: EC_POINT_free(point); +err2: ECDSA_SIG_free(ecdsa_sig); +err1: EC_KEY_free(ec_key); + return ret; +} + +int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t& key) { + + int recid; + int ret = -1; + BIGNUM *r, *s; + EC_KEY *ec_key; + + // Initialize ec variables. + if ((ec_key = EC_KEY_new_secp256k1()) == NULL) goto err1; + + // Unserialize signature into r,s,recid components. + ECDSA_SIG_unserialize_rs(sig.data(), &r, &s, &recid); + + // Recover public key. + if (ECDSA_SIG_recover_key_GFp(ec_key, r, s, digest->data, 32, recid, 1) == 1) { + + // Encode point to binary compressed format. + const EC_POINT *p = EC_KEY_get0_public_key(ec_key); + const EC_GROUP *g = EC_KEY_get0_group(ec_key); + if (EC_POINT_encode(g, p, key.data(), EC_PUBKEY_SIZE, ctx) == 0) { + goto err4; + } + + ret = 0; + } + +err4: BN_free(s); +err3: BN_free(r); +err2: EC_KEY_free(ec_key); +err1: return ret; +} + +} // namespace libeosio \ No newline at end of file