diff --git a/CMakeLists.txt b/CMakeLists.txt index d486214..1b3e8b7 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 ") @@ -54,7 +54,10 @@ 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 ) add_library( ${LIB_NAME} STATIC ${LIB_SOURCE} ) diff --git a/include/libeosio/ec.hpp b/include/libeosio/ec.hpp index 07249ca..ff7c57b 100644 --- a/include/libeosio/ec.hpp +++ b/include/libeosio/ec.hpp @@ -101,6 +101,28 @@ 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); + +/** + * 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/src/openssl/ec.cpp b/src/openssl/ec.cpp index 583c940..c001c82 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "internal.h" namespace libeosio { @@ -74,43 +75,9 @@ int ec_generate_privkey(ec_privkey_t *priv) { return 0; } -// Calcualte a public key from a EC_KEY object. -int calculate_pubkey(EC_KEY *ec_key, ec_pubkey_t *pub) { - 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) { - 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. - 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); - } - - EC_POINT_free(point); - return rc; -} - int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub) { - const BIGNUM* n; + int rc = -1; const EC_GROUP *group; EC_POINT *point; @@ -119,7 +86,21 @@ int ec_get_publickey(const ec_privkey_t *priv, ec_pubkey_t* pub) { 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) { 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 diff --git a/src/openssl/helpers.c b/src/openssl/helpers.c new file mode 100644 index 0000000..24ba631 --- /dev/null +++ b/src/openssl/helpers.c @@ -0,0 +1,101 @@ +/** + * 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 + +// 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 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 diff --git a/src/openssl/internal.h b/src/openssl/internal.h new file mode 100644 index 0000000..9a5d6c1 --- /dev/null +++ b/src/openssl/internal.h @@ -0,0 +1,65 @@ +/** + * 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 +#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)) + +#ifdef __cplusplus +extern "C" { +#endif + +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); + +/** + * 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 + +#endif /* LIBEOSIO_OPENSSL_INTERNAL_H */ \ No newline at end of file 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; +} + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 73b1523..c1a8429 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,9 @@ set(TEST_SRC # ec ec/generate.cpp ec/pubkey.cpp + ec/ecdsa_sign.cpp + ec/ecdsa_recover.cpp + ec/ecdsa_verify.cpp # Base58 base58/encode.cpp diff --git a/tests/ec/ecdsa_recover.cpp b/tests/ec/ecdsa_recover.cpp new file mode 100644 index 0000000..d42a8e9 --- /dev/null +++ b/tests/ec/ecdsa_recover.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +TEST_CASE("ec::ecdsa_recover") { + + 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(); +} + diff --git a/tests/ec/ecdsa_sign.cpp b/tests/ec/ecdsa_sign.cpp new file mode 100644 index 0000000..99c19fd --- /dev/null +++ b/tests/ec/ecdsa_sign.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +TEST_CASE("ec::ecdsa_sign") { + + struct testcase { + const char *name; + libeosio::ec_privkey_t key; + libeosio::ec_pubkey_t pub; + libeosio::sha256_t dgst; + }; + + 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 }, + // 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, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d + }, + }, + }, + { + "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, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 + }, + }, + }, + { + "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, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec + }, + }, + }, + { + "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, + 0x71, 0x25, 0x3b, 0xf5, 0x39, 0x32, 0x55, 0x4e, + 0x01, 0x28, 0x5d, 0xf4, 0x02, 0xe8, 0xa2, 0x92, + 0x04, 0xf2, 0x95, 0xbc, 0xfa, 0xed, 0x8f, 0xaa + }, + }, + }, + { + "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, + 0x26, 0x66, 0xba, 0x57, 0xeb, 0x4a, 0xfa, 0x20, + 0xff, 0x40, 0x7b, 0xc7, 0x3c, 0x35, 0xb8, 0xaa, + 0x72, 0x3e, 0x94, 0xcc, 0x05, 0xe1, 0x51, 0xc7 + }, + }, + }, + { + "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, + 0xe8, 0x60, 0x7e, 0x86, 0x73, 0x69, 0x05, 0x90, + 0x97, 0xa2, 0x57, 0xee, 0x12, 0x4b, 0x80, 0x13, + 0x04, 0xfa, 0x7d, 0x70, 0xe7, 0xdc, 0x86, 0xb2 + }, + }, + }, + }; + + 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 ); + + // 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(); +} + diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp new file mode 100644 index 0000000..7eb3e59 --- /dev/null +++ b/tests/ec/ecdsa_verify.cpp @@ -0,0 +1,207 @@ +#include +#include +#include + +TEST_CASE("ec::ecdsa_verify") { + + 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 (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, + 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 #5", + { + 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(); +} +