peerplays-fc/src/crypto/elliptic_secp256k1.cpp

112 lines
3.3 KiB
C++
Raw Normal View History

#include <fc/crypto/elliptic.hpp>
#include <fc/crypto/base58.hpp>
#include <fc/crypto/openssl.hpp>
#include <fc/fwd_impl.hpp>
#include <fc/exception/exception.hpp>
#include <fc/log/logger.hpp>
#include <assert.h>
2015-03-07 13:48:45 +00:00
#include <secp256k1.h>
namespace fc { namespace ecc {
2014-12-12 00:16:02 +00:00
namespace detail
{
2015-03-10 20:56:20 +00:00
static void init_lib();
2015-03-07 13:48:45 +00:00
2015-03-10 13:47:53 +00:00
typedef public_key_data pub_data_type;
typedef private_key_secret priv_data_type;
2015-03-09 09:30:34 +00:00
2015-03-10 13:47:53 +00:00
#include "_elliptic_impl.cpp"
2015-03-09 09:30:34 +00:00
2015-03-10 13:47:53 +00:00
void private_key_impl::free_key()
{
2015-03-09 09:30:34 +00:00
if( _key != nullptr )
{
2015-03-10 13:47:53 +00:00
delete _key;
_key = nullptr;
2015-03-09 09:30:34 +00:00
}
2015-03-10 13:47:53 +00:00
}
2015-03-09 09:30:34 +00:00
2015-03-10 13:47:53 +00:00
private_key_secret* private_key_impl::dup_key( const private_key_secret* cpy )
{
return new private_key_secret( *cpy );
}
2015-03-09 09:30:34 +00:00
2015-03-10 13:47:53 +00:00
void private_key_impl::copy_key( private_key_secret* to, const private_key_secret* from )
{
*to = *from;
}
}
2015-03-07 13:48:45 +00:00
private_key private_key::regenerate( const fc::sha256& secret )
{
private_key self;
2015-03-09 09:30:34 +00:00
self.my->_key = new private_key_secret(secret);
return self;
}
fc::sha256 private_key::get_secret()const
{
2015-03-11 10:07:14 +00:00
if( !my->_key )
{
return fc::sha256();
}
2015-03-09 09:30:34 +00:00
return *my->_key;
}
2015-03-07 13:48:45 +00:00
private_key::private_key( EC_KEY* k )
{
2015-03-09 09:30:34 +00:00
my->_key = new private_key_secret( get_secret( k ) );
EC_KEY_free(k);
}
public_key private_key::get_public_key()const
{
2015-03-09 09:30:34 +00:00
FC_ASSERT( my->_key != nullptr );
public_key_data pub;
2015-03-07 13:48:45 +00:00
unsigned int pk_len;
2015-03-09 09:30:34 +00:00
FC_ASSERT( secp256k1_ec_pubkey_create( (unsigned char*) pub.begin(), (int*) &pk_len, (unsigned char*) my->_key->data(), 1 ) );
FC_ASSERT( pk_len == pub.size() );
return public_key(pub);
}
fc::sha512 private_key::get_shared_secret( const public_key& other )const
{
2015-03-09 09:30:34 +00:00
FC_ASSERT( my->_key != nullptr );
FC_ASSERT( other.my->_key != nullptr );
public_key_data pub(*other.my->_key);
FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key->data() ) );
2015-03-07 13:48:45 +00:00
// ECDH_compute_key( (unsigned char*)&buf, sizeof(buf), EC_KEY_get0_public_key(other.my->_key), my->_key, ecies_key_derivation );
2015-03-09 09:30:34 +00:00
return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 );
}
2015-03-11 14:54:21 +00:00
static int extended_nonce_function( unsigned char *nonce32, const unsigned char *msg32,
const unsigned char *key32, unsigned int attempt,
const void *data ) {
unsigned int* extra = (unsigned int*) data;
(*extra)++;
return secp256k1_nonce_function_default( nonce32, msg32, key32, *extra, nullptr );
}
2013-06-27 18:18:02 +00:00
compact_signature private_key::sign_compact( const fc::sha256& digest )const
{
2015-03-09 09:30:34 +00:00
FC_ASSERT( my->_key != nullptr );
2015-03-07 13:48:45 +00:00
compact_signature result;
2015-03-09 09:30:34 +00:00
int recid;
2015-03-11 14:54:21 +00:00
unsigned int counter = 0;
2015-03-09 09:30:34 +00:00
do
{
2015-03-11 14:54:21 +00:00
FC_ASSERT( secp256k1_ecdsa_sign_compact( (unsigned char*) digest.data(), (unsigned char*) result.begin() + 1, (unsigned char*) my->_key->data(), extended_nonce_function, &counter, &recid ));
2015-03-09 09:30:34 +00:00
} while( !public_key::is_canonical( result ) );
result.begin()[0] = 27 + 4 + recid;
2015-03-07 13:48:45 +00:00
return result;
}
2015-03-10 20:56:20 +00:00
#include "_elliptic_mixed_secp256k1.cpp"
} }
2015-03-10 10:34:14 +00:00
#include "_elliptic_common.cpp"