bug fixes
This commit is contained in:
parent
0fd0574c05
commit
6563c12f88
3 changed files with 185 additions and 91 deletions
|
|
@ -108,6 +108,7 @@ set( fc_sources
|
||||||
src/crypto/sha512.cpp
|
src/crypto/sha512.cpp
|
||||||
src/crypto/dh.cpp
|
src/crypto/dh.cpp
|
||||||
src/crypto/blowfish.cpp
|
src/crypto/blowfish.cpp
|
||||||
|
src/crypto/elliptic.cpp
|
||||||
src/network/tcp_socket.cpp
|
src/network/tcp_socket.cpp
|
||||||
src/network/udp_socket.cpp
|
src/network/udp_socket.cpp
|
||||||
src/network/http/http_connection.cpp
|
src/network/http/http_connection.cpp
|
||||||
|
|
|
||||||
|
|
@ -4,96 +4,109 @@
|
||||||
#include <fc/crypto/sha512.hpp>
|
#include <fc/crypto/sha512.hpp>
|
||||||
#include <fc/fwd.hpp>
|
#include <fc/fwd.hpp>
|
||||||
#include <fc/array.hpp>
|
#include <fc/array.hpp>
|
||||||
#include <vector>
|
|
||||||
#include <fc/io/raw_fwd.hpp>
|
#include <fc/io/raw_fwd.hpp>
|
||||||
|
|
||||||
namespace fc
|
namespace fc {
|
||||||
{
|
|
||||||
namespace ecc
|
namespace ecc {
|
||||||
{
|
namespace detail
|
||||||
|
{
|
||||||
namespace detail
|
class public_key_impl;
|
||||||
{
|
class private_key_impl;
|
||||||
class public_key_impl;
|
}
|
||||||
class private_key_impl;
|
|
||||||
}
|
typedef fc::array<char,33> public_key_data;
|
||||||
|
typedef fc::array<char,72> signature;
|
||||||
typedef fc::array<char,72> signature;
|
typedef fc::array<unsigned char,65> compact_signature;
|
||||||
typedef fc::array<unsigned char,65> compact_signature;
|
|
||||||
|
class public_key
|
||||||
class public_key
|
{
|
||||||
{
|
public:
|
||||||
public:
|
public_key();
|
||||||
public_key();
|
public_key(const public_key& k);
|
||||||
~public_key();
|
~public_key();
|
||||||
bool verify( const fc::sha256& digest, const signature& sig );
|
bool verify( const fc::sha256& digest, const signature& sig );
|
||||||
|
public_key_data serialize()const;
|
||||||
std::vector<char> serialize()const;
|
public_key( const public_key_data& v );
|
||||||
public_key( const std::vector<char>& v );
|
public_key( const compact_signature& c, const fc::sha256& digest );
|
||||||
public_key( const compact_signature& c, const fc::sha256& digest );
|
|
||||||
private:
|
bool valid()const;
|
||||||
friend class private_key;
|
|
||||||
fc::fwd<detail::public_key_impl,8> my;
|
public_key( public_key&& pk );
|
||||||
};
|
public_key& operator=( public_key&& pk );
|
||||||
|
public_key& operator=( const public_key& pk );
|
||||||
|
private:
|
||||||
class private_key
|
friend class private_key;
|
||||||
{
|
fc::fwd<detail::public_key_impl,8> my;
|
||||||
public:
|
};
|
||||||
private_key();
|
|
||||||
private_key( std::vector<char> k );
|
|
||||||
~private_key();
|
class private_key
|
||||||
|
{
|
||||||
static private_key generate();
|
public:
|
||||||
static private_key regenerate( const fc::sha256& secret );
|
private_key();
|
||||||
|
private_key( std::vector<char> k );
|
||||||
fc::sha256 get_secret()const; // get the private key secret
|
private_key( private_key&& pk );
|
||||||
|
private_key( const private_key& pk );
|
||||||
/**
|
~private_key();
|
||||||
* Given a public key, calculatse a 512 bit shared secret between that
|
|
||||||
* key and this private key.
|
private_key& operator=( private_key&& pk );
|
||||||
*/
|
private_key& operator=( const private_key& pk );
|
||||||
fc::sha512 get_shared_secret( const public_key& pub );
|
|
||||||
|
static private_key generate();
|
||||||
signature sign( const fc::sha256& digest );
|
static private_key regenerate( const fc::sha256& secret );
|
||||||
compact_signature sign_compact( const fc::sha256& digest );
|
|
||||||
bool verify( const fc::sha256& digest, const signature& sig );
|
fc::sha256 get_secret()const; // get the private key secret
|
||||||
|
|
||||||
public_key get_public_key()const;
|
/**
|
||||||
private:
|
* Given a public key, calculatse a 512 bit shared secret between that
|
||||||
fc::fwd<detail::private_key_impl,8> my;
|
* key and this private key.
|
||||||
};
|
*/
|
||||||
} // namespace ecc
|
fc::sha512 get_shared_secret( const public_key& pub );
|
||||||
|
|
||||||
|
signature sign( const fc::sha256& digest );
|
||||||
|
compact_signature sign_compact( const fc::sha256& digest );
|
||||||
|
bool verify( const fc::sha256& digest, const signature& sig );
|
||||||
|
|
||||||
|
public_key get_public_key()const;
|
||||||
|
private:
|
||||||
|
fc::fwd<detail::private_key_impl,8> my;
|
||||||
|
};
|
||||||
|
} // namespace ecc
|
||||||
|
void to_variant( const ecc::private_key& var, variant& vo );
|
||||||
|
void from_variant( const variant& var, ecc::private_key& vo );
|
||||||
|
void to_variant( const ecc::public_key& var, variant& vo );
|
||||||
|
void from_variant( const variant& var, ecc::public_key& vo );
|
||||||
|
|
||||||
namespace raw
|
namespace raw
|
||||||
{
|
{
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
inline void pack( Stream& s, const fc::ecc::public_key& v )
|
void unpack( Stream& s, fc::ecc::public_key& pk)
|
||||||
{
|
{
|
||||||
pack( s, v.serialize() );
|
ecc::public_key_data ser;
|
||||||
}
|
fc::raw::unpack(s,ser);
|
||||||
template<typename Stream>
|
pk = fc::ecc::public_key( ser );
|
||||||
inline void unpack( Stream& s, fc::ecc::public_key& v )
|
}
|
||||||
{
|
|
||||||
std::vector<char> d;
|
|
||||||
unpack( s, d );
|
|
||||||
v = fc::ecc::public_key( fc::move(d) );
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
inline void pack( Stream& s, const fc::ecc::private_key& v )
|
void pack( Stream& s, const fc::ecc::public_key& pk)
|
||||||
{
|
{
|
||||||
pack( s, v.get_secret() );
|
fc::raw::pack( s, pk.serialize() );
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
inline void unpack( Stream& s, fc::ecc::private_key& v )
|
void unpack( Stream& s, fc::ecc::private_key& pk)
|
||||||
{
|
{
|
||||||
fc::sha256 secret;
|
fc::sha256 sec;
|
||||||
unpack( s, secret );
|
unpack( s, sec );
|
||||||
v = fc::ecc::private_key::regenerate( secret );
|
pk = ecc::private_key::regenerate(sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
void pack( Stream& s, const fc::ecc::private_key& pk)
|
||||||
|
{
|
||||||
|
fc::raw::pack( s, pk.get_secret() );
|
||||||
|
}
|
||||||
} // namespace raw
|
} // namespace raw
|
||||||
|
|
||||||
} // fc
|
} // namespace fc
|
||||||
|
|
|
||||||
|
|
@ -272,14 +272,14 @@ namespace fc { namespace ecc {
|
||||||
return 1 == ECDSA_verify( 0, (unsigned char*)&digest, sizeof(digest), (unsigned char*)&sig, sizeof(sig), my->_key );
|
return 1 == ECDSA_verify( 0, (unsigned char*)&digest, sizeof(digest), (unsigned char*)&sig, sizeof(sig), my->_key );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<char> public_key::serialize()const
|
public_key_data public_key::serialize()const
|
||||||
{
|
{
|
||||||
EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_COMPRESSED );
|
EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_COMPRESSED );
|
||||||
size_t nbytes = i2o_ECPublicKey( my->_key, nullptr );
|
size_t nbytes = i2o_ECPublicKey( my->_key, nullptr );
|
||||||
std::vector<char> dat(nbytes);
|
assert( nbytes == 33 );
|
||||||
char* front = &dat[0];
|
public_key_data dat;
|
||||||
|
char* front = &dat.data[0];
|
||||||
i2o_ECPublicKey( my->_key, (unsigned char**)&front );
|
i2o_ECPublicKey( my->_key, (unsigned char**)&front );
|
||||||
fprintf( stderr, "public key size: %lu\n", nbytes );
|
|
||||||
return dat;
|
return dat;
|
||||||
/*
|
/*
|
||||||
EC_POINT* pub = EC_KEY_get0_public_key( my->_key );
|
EC_POINT* pub = EC_KEY_get0_public_key( my->_key );
|
||||||
|
|
@ -293,11 +293,11 @@ namespace fc { namespace ecc {
|
||||||
public_key::~public_key()
|
public_key::~public_key()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
public_key::public_key( const std::vector<char>& v )
|
public_key::public_key( const public_key_data& dat )
|
||||||
{
|
{
|
||||||
const char* front = &v[0];
|
const char* front = &dat.data[0];
|
||||||
my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 );
|
my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 );
|
||||||
my->_key = o2i_ECPublicKey( &my->_key, (const unsigned char**)&front, v.size() );
|
my->_key = o2i_ECPublicKey( &my->_key, (const unsigned char**)&front, sizeof(public_key_data) );
|
||||||
if( !my->_key )
|
if( !my->_key )
|
||||||
{
|
{
|
||||||
fprintf( stderr, "decode error occurred??" );
|
fprintf( stderr, "decode error occurred??" );
|
||||||
|
|
@ -426,4 +426,84 @@ namespace fc { namespace ecc {
|
||||||
return csig;
|
return csig;
|
||||||
}
|
}
|
||||||
|
|
||||||
} }
|
private_key& private_key::operator=( private_key&& pk )
|
||||||
|
{
|
||||||
|
if( my->_key )
|
||||||
|
{
|
||||||
|
EC_KEY_free(my->_key);
|
||||||
|
}
|
||||||
|
my->_key = pk.my->_key;
|
||||||
|
pk.my->_key = nullptr;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
public_key::public_key( const public_key& pk )
|
||||||
|
:my(pk.my)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public_key::public_key( public_key&& pk )
|
||||||
|
:my( fc::move( pk.my) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
private_key::private_key( const private_key& pk )
|
||||||
|
:my(pk.my)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
private_key::private_key( private_key&& pk )
|
||||||
|
:my( fc::move( pk.my) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public_key& public_key::operator=( public_key&& pk )
|
||||||
|
{
|
||||||
|
if( my->_key )
|
||||||
|
{
|
||||||
|
EC_KEY_free(my->_key);
|
||||||
|
}
|
||||||
|
my->_key = pk.my->_key;
|
||||||
|
pk.my->_key = nullptr;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
public_key& public_key::operator=( const public_key& pk )
|
||||||
|
{
|
||||||
|
if( my->_key )
|
||||||
|
{
|
||||||
|
EC_KEY_free(my->_key);
|
||||||
|
}
|
||||||
|
my->_key = EC_KEY_dup(pk.my->_key);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
private_key& private_key::operator=( const private_key& pk )
|
||||||
|
{
|
||||||
|
if( my->_key )
|
||||||
|
{
|
||||||
|
EC_KEY_free(my->_key);
|
||||||
|
}
|
||||||
|
my->_key = EC_KEY_dup(pk.my->_key);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
void to_variant( const ecc::private_key& var, variant& vo )
|
||||||
|
{
|
||||||
|
vo = var.get_secret();
|
||||||
|
}
|
||||||
|
void from_variant( const variant& var, ecc::private_key& vo )
|
||||||
|
{
|
||||||
|
fc::sha256 sec;
|
||||||
|
from_variant( var, sec );
|
||||||
|
vo = ecc::private_key::regenerate(sec);
|
||||||
|
}
|
||||||
|
|
||||||
|
void to_variant( const ecc::public_key& var, variant& vo )
|
||||||
|
{
|
||||||
|
vo = var.serialize();
|
||||||
|
}
|
||||||
|
void from_variant( const variant& var, ecc::public_key& vo )
|
||||||
|
{
|
||||||
|
ecc::public_key_data dat;
|
||||||
|
from_variant( var, dat );
|
||||||
|
vo = ecc::public_key(dat);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue