12namespace fc {
namespace ecc {
37 if (pk._key ==
nullptr)
40 }
else if (
_key ==
nullptr ) {
41 _key = EC_KEY_dup( pk._key );
43 EC_KEY_copy(
_key, pk._key );
61 void free_key() BOOST_NOEXCEPT
82 my = std::move(pk.my);
91 static void * ecies_key_derivation(
const void *input,
size_t ilen,
void *output,
size_t *olen)
93 if (*olen < SHA512_DIGEST_LENGTH) {
96 *olen = SHA512_DIGEST_LENGTH;
97 return (
void*)SHA512((
const unsigned char*)input, ilen, (
unsigned char*)output);
100 int static inline EC_KEY_regenerate_key(EC_KEY *eckey,
const BIGNUM *priv_key)
104 EC_POINT *pub_key = NULL;
106 if (!eckey)
return 0;
108 const EC_GROUP *group = EC_KEY_get0_group(eckey);
110 if ((ctx = BN_CTX_new()) == NULL)
113 pub_key = EC_POINT_new(group);
118 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
121 EC_KEY_set_private_key(eckey,priv_key);
122 EC_KEY_set_public_key(eckey,pub_key);
128 if (pub_key) EC_POINT_free(pub_key);
129 if (ctx != NULL) BN_CTX_free(ctx);
137 self.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 );
141 BN_bin2bn( (
const unsigned char*)&secret, 32,
bn );
143 if( !EC_KEY_regenerate_key(
self.my->_key,
bn) )
152 return get_secret( my->_key );
163 pub.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 );
164 EC_KEY_set_public_key(
pub.my->_key, EC_KEY_get0_public_key( my->_key ) );
174 ECDH_compute_key( (
unsigned char*)&
buf,
sizeof(
buf), EC_KEY_get0_public_key(
other.my->_key), my->_key, ecies_key_derivation );
182 auto my_pub_key = get_public_key().serialize();
187 ecdsa_sig sig = ECDSA_do_sign((
unsigned char*)&
digest,
sizeof(
digest), my->_key);
195 int nBitsR = BN_num_bits(sig->r);
196 int nBitsS = BN_num_bits(sig->s);
197 if (nBitsR <= 256 && nBitsS <= 256)
200 EC_KEY* key = EC_KEY_new_by_curve_name( NID_secp256k1 );
202 EC_KEY_set_conv_form( key, POINT_CONVERSION_COMPRESSED );
203 for (
int i=0; i<4; i++)
207 unsigned char* buffer = (
unsigned char*) key_data.
begin();
208 i2o_ECPublicKey( key, &buffer );
209 if ( key_data == my_pub_key )
222 unsigned char* result =
nullptr;
223 auto bytes = i2d_ECDSA_SIG( sig, &result );
224 auto lenR = result[3];
225 auto lenS = result[5+lenR];
227 if( lenR != 32 ) { free(result);
continue; }
228 if( lenS != 32 ) { free(result);
continue; }
233 memcpy( &csig.
data[33], &result[6+lenR], lenS );
238 csig.
data[0] = nRecId+27+4;
private_key_impl() BOOST_NOEXCEPT
private_key_impl & operator=(private_key_impl &&pk) BOOST_NOEXCEPT
~private_key_impl() BOOST_NOEXCEPT
private_key_impl & operator=(const private_key_impl &pk) BOOST_NOEXCEPT
private_key_impl(private_key_impl &&cpy) BOOST_NOEXCEPT
private_key_impl(const private_key_impl &cpy) BOOST_NOEXCEPT
static int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
public_key get_public_key() const
private_key_secret get_secret() const
private_key & operator=(private_key &&pk)
fc::sha512 get_shared_secret(const public_key &pub) const
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
static private_key regenerate(const fc::sha256 &secret)
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
fc::sha256 digest(const T &value)
std::vector< char > bytes
@ self
the connection is to itself
memcpy((char *) pInfo->slotDescription, s, l)