2013-06-05 19:19:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
#include <fc/crypto/bigint.hpp>
|
2015-03-07 13:48:45 +00:00
|
|
|
#include <fc/crypto/openssl.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/crypto/sha256.hpp>
|
|
|
|
|
#include <fc/crypto/sha512.hpp>
|
|
|
|
|
#include <fc/fwd.hpp>
|
|
|
|
|
#include <fc/array.hpp>
|
2013-06-06 23:20:51 +00:00
|
|
|
#include <fc/io/raw_fwd.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2014-10-02 16:55:20 +00:00
|
|
|
namespace fc {
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-06-07 00:37:04 +00:00
|
|
|
namespace ecc {
|
2014-10-02 16:55:20 +00:00
|
|
|
namespace detail
|
|
|
|
|
{
|
|
|
|
|
class public_key_impl;
|
|
|
|
|
class private_key_impl;
|
2013-06-06 23:20:51 +00:00
|
|
|
}
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-06-07 00:37:04 +00:00
|
|
|
typedef fc::array<char,33> public_key_data;
|
2014-05-26 03:12:33 +00:00
|
|
|
typedef fc::sha256 private_key_secret;
|
2013-12-10 03:25:59 +00:00
|
|
|
typedef fc::array<char,65> public_key_point_data; ///< the full non-compressed version of the ECC point
|
2013-06-07 00:37:04 +00:00
|
|
|
typedef fc::array<char,72> signature;
|
|
|
|
|
typedef fc::array<unsigned char,65> compact_signature;
|
|
|
|
|
|
2013-07-02 21:09:15 +00:00
|
|
|
/**
|
|
|
|
|
* @class public_key
|
|
|
|
|
* @brief contains only the public point of an elliptic curve key.
|
|
|
|
|
*/
|
2013-06-07 00:37:04 +00:00
|
|
|
class public_key
|
2013-06-06 23:20:51 +00:00
|
|
|
{
|
2013-06-07 00:37:04 +00:00
|
|
|
public:
|
|
|
|
|
public_key();
|
|
|
|
|
public_key(const public_key& k);
|
|
|
|
|
~public_key();
|
2015-03-06 13:28:25 +00:00
|
|
|
// bool verify( const fc::sha256& digest, const signature& sig );
|
2013-06-07 00:37:04 +00:00
|
|
|
public_key_data serialize()const;
|
2013-12-10 03:25:59 +00:00
|
|
|
public_key_point_data serialize_ecc_point()const;
|
2013-08-15 23:36:54 +00:00
|
|
|
|
|
|
|
|
operator public_key_data()const { return serialize(); }
|
|
|
|
|
|
2013-12-10 03:25:59 +00:00
|
|
|
|
2013-06-07 00:37:04 +00:00
|
|
|
public_key( const public_key_data& v );
|
2014-02-28 04:35:24 +00:00
|
|
|
public_key( const public_key_point_data& v );
|
2014-12-12 00:16:02 +00:00
|
|
|
public_key( const compact_signature& c, const fc::sha256& digest, bool check_canonical = true );
|
2013-06-07 00:37:04 +00:00
|
|
|
|
|
|
|
|
bool valid()const;
|
2015-03-09 09:30:34 +00:00
|
|
|
/** Computes new pubkey = generator * offset + old pubkey ?! */
|
|
|
|
|
// public_key mult( const fc::sha256& offset )const;
|
|
|
|
|
/** Computes new pubkey = regenerate(offset).pubkey + old pubkey
|
|
|
|
|
* = offset * G + 1 * old pubkey ?! */
|
2013-08-10 06:33:15 +00:00
|
|
|
public_key add( const fc::sha256& offset )const;
|
2013-06-07 00:37:04 +00:00
|
|
|
|
|
|
|
|
public_key( public_key&& pk );
|
|
|
|
|
public_key& operator=( public_key&& pk );
|
|
|
|
|
public_key& operator=( const public_key& pk );
|
2013-08-12 18:42:54 +00:00
|
|
|
|
|
|
|
|
inline friend bool operator==( const public_key& a, const public_key& b )
|
|
|
|
|
{
|
|
|
|
|
return a.serialize() == b.serialize();
|
|
|
|
|
}
|
2013-09-14 03:13:11 +00:00
|
|
|
inline friend bool operator!=( const public_key& a, const public_key& b )
|
|
|
|
|
{
|
|
|
|
|
return a.serialize() != b.serialize();
|
|
|
|
|
}
|
2014-03-03 10:30:23 +00:00
|
|
|
|
|
|
|
|
/// Allows to convert current public key object into base58 number.
|
|
|
|
|
std::string to_base58() const;
|
2015-03-07 13:48:45 +00:00
|
|
|
static std::string to_base58( const public_key_data &key );
|
2014-03-27 23:53:40 +00:00
|
|
|
static public_key from_base58( const std::string& b58 );
|
2014-03-03 10:30:23 +00:00
|
|
|
|
2013-06-07 00:37:04 +00:00
|
|
|
private:
|
|
|
|
|
friend class private_key;
|
2015-03-07 13:48:45 +00:00
|
|
|
static public_key from_key_data( const public_key_data& v );
|
2015-03-09 09:30:34 +00:00
|
|
|
static bool is_canonical( const compact_signature& c );
|
2013-06-07 00:37:04 +00:00
|
|
|
fc::fwd<detail::public_key_impl,8> my;
|
|
|
|
|
};
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-07-02 21:09:15 +00:00
|
|
|
/**
|
|
|
|
|
* @class private_key
|
|
|
|
|
* @brief an elliptic curve private key.
|
|
|
|
|
*/
|
2014-10-02 16:55:20 +00:00
|
|
|
class private_key
|
2013-06-06 23:20:51 +00:00
|
|
|
{
|
2013-06-07 00:37:04 +00:00
|
|
|
public:
|
|
|
|
|
private_key();
|
|
|
|
|
private_key( private_key&& pk );
|
|
|
|
|
private_key( const private_key& pk );
|
|
|
|
|
~private_key();
|
|
|
|
|
|
|
|
|
|
private_key& operator=( private_key&& pk );
|
|
|
|
|
private_key& operator=( const private_key& pk );
|
|
|
|
|
|
|
|
|
|
static private_key generate();
|
|
|
|
|
static private_key regenerate( const fc::sha256& secret );
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2013-07-02 21:09:15 +00:00
|
|
|
/**
|
|
|
|
|
* This method of generation enables creating a new private key in a deterministic manner relative to
|
2014-10-02 16:55:20 +00:00
|
|
|
* an initial seed. A public_key created from the seed can be multiplied by the offset to calculate
|
2013-07-02 21:09:15 +00:00
|
|
|
* the new public key without having to know the private key.
|
|
|
|
|
*/
|
|
|
|
|
static private_key generate_from_seed( const fc::sha256& seed, const fc::sha256& offset = fc::sha256() );
|
|
|
|
|
|
2014-05-26 03:12:33 +00:00
|
|
|
private_key_secret get_secret()const; // get the private key secret
|
|
|
|
|
|
|
|
|
|
operator private_key_secret ()const { return get_secret(); }
|
2013-06-07 00:37:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a public key, calculatse a 512 bit shared secret between that
|
2014-10-02 16:55:20 +00:00
|
|
|
* key and this private key.
|
2013-06-07 00:37:04 +00:00
|
|
|
*/
|
2013-07-11 05:26:54 +00:00
|
|
|
fc::sha512 get_shared_secret( const public_key& pub )const;
|
2013-06-07 00:37:04 +00:00
|
|
|
|
2015-03-06 13:28:25 +00:00
|
|
|
// signature sign( const fc::sha256& digest )const;
|
2013-06-27 18:18:02 +00:00
|
|
|
compact_signature sign_compact( const fc::sha256& digest )const;
|
2015-03-06 13:28:25 +00:00
|
|
|
// bool verify( const fc::sha256& digest, const signature& sig );
|
2013-06-07 00:37:04 +00:00
|
|
|
|
|
|
|
|
public_key get_public_key()const;
|
2014-02-17 02:28:55 +00:00
|
|
|
|
|
|
|
|
inline friend bool operator==( const private_key& a, const private_key& b )
|
|
|
|
|
{
|
|
|
|
|
return a.get_secret() == b.get_secret();
|
|
|
|
|
}
|
2014-10-02 16:55:20 +00:00
|
|
|
inline friend bool operator!=( const private_key& a, const private_key& b )
|
|
|
|
|
{
|
|
|
|
|
return a.get_secret() != b.get_secret();
|
|
|
|
|
}
|
|
|
|
|
inline friend bool operator<( const private_key& a, const private_key& b )
|
|
|
|
|
{
|
|
|
|
|
return a.get_secret() < b.get_secret();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-07 00:37:04 +00:00
|
|
|
private:
|
2015-03-07 13:48:45 +00:00
|
|
|
private_key( EC_KEY* k );
|
|
|
|
|
static fc::sha256 get_secret( const EC_KEY * const k );
|
2013-06-07 00:37:04 +00:00
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
template<typename Stream>
|
|
|
|
|
void unpack( Stream& s, fc::ecc::public_key& pk)
|
|
|
|
|
{
|
|
|
|
|
ecc::public_key_data ser;
|
|
|
|
|
fc::raw::unpack(s,ser);
|
|
|
|
|
pk = fc::ecc::public_key( ser );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
|
void pack( Stream& s, const fc::ecc::public_key& pk)
|
|
|
|
|
{
|
|
|
|
|
fc::raw::pack( s, pk.serialize() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
|
void unpack( Stream& s, fc::ecc::private_key& pk)
|
|
|
|
|
{
|
|
|
|
|
fc::sha256 sec;
|
|
|
|
|
unpack( s, sec );
|
|
|
|
|
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() );
|
|
|
|
|
}
|
2013-08-02 18:32:59 +00:00
|
|
|
|
2013-06-06 23:20:51 +00:00
|
|
|
} // namespace raw
|
2013-06-05 19:19:00 +00:00
|
|
|
|
2014-10-02 16:55:20 +00:00
|
|
|
} // namespace fc
|
2014-05-17 19:35:44 +00:00
|
|
|
#include <fc/reflect/reflect.hpp>
|
|
|
|
|
|
|
|
|
|
FC_REFLECT_TYPENAME( fc::ecc::private_key )
|
|
|
|
|
FC_REFLECT_TYPENAME( fc::ecc::public_key )
|