2010-07-14 17:54:31 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-10-26 09:33:52 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_KEY_H
|
|
|
|
#define BITCOIN_KEY_H
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2012-04-17 20:37:47 +02:00
|
|
|
#include "allocators.h"
|
2012-05-14 19:07:52 +02:00
|
|
|
#include "serialize.h"
|
2011-06-26 16:11:56 +02:00
|
|
|
#include "uint256.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2011-06-26 16:11:56 +02:00
|
|
|
|
2014-11-04 14:34:04 +01:00
|
|
|
class CPubKey;
|
2014-10-28 22:47:18 +01:00
|
|
|
|
2014-11-19 10:53:46 +01:00
|
|
|
struct CExtPubKey;
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
/**
|
|
|
|
* secp256k1:
|
|
|
|
* const unsigned int PRIVATE_KEY_SIZE = 279;
|
|
|
|
* const unsigned int PUBLIC_KEY_SIZE = 65;
|
|
|
|
* const unsigned int SIGNATURE_SIZE = 72;
|
|
|
|
*
|
|
|
|
* see www.keylength.com
|
|
|
|
* script supports up to 75 for single byte push
|
|
|
|
*/
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
/**
|
|
|
|
* secure_allocator is defined in allocators.h
|
|
|
|
* CPrivKey is a serialized private key, with all parameters included (279 bytes)
|
|
|
|
*/
|
2011-05-15 09:11:04 +02:00
|
|
|
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
/** An encapsulated private key. */
|
2014-09-19 19:21:46 +02:00
|
|
|
class CKey
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
private:
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Whether this private key is valid. We check for correctness when modifying the key
|
|
|
|
//! data, so fValid should always correspond to the actual state.
|
2013-05-01 06:52:05 +02:00
|
|
|
bool fValid;
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Whether the public key corresponding to this private key is (to be) compressed.
|
2013-05-01 06:52:05 +02:00
|
|
|
bool fCompressed;
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! The actual byte data
|
2013-05-01 06:52:05 +02:00
|
|
|
unsigned char vch[32];
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Check whether the 32-byte array pointed to be vch is valid keydata.
|
2014-09-19 19:21:46 +02:00
|
|
|
bool static Check(const unsigned char* vch);
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
public:
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Construct an invalid private key.
|
2014-09-19 19:21:46 +02:00
|
|
|
CKey() : fValid(false), fCompressed(false)
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
LockObject(vch);
|
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Copy constructor. This is necessary because of memlocking.
|
2014-09-19 19:21:46 +02:00
|
|
|
CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
LockObject(vch);
|
|
|
|
memcpy(vch, secret.vch, sizeof(vch));
|
|
|
|
}
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Destructor (again necessary because of memlocking).
|
2014-09-19 19:21:46 +02:00
|
|
|
~CKey()
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
UnlockObject(vch);
|
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
friend bool operator==(const CKey& a, const CKey& b)
|
|
|
|
{
|
2013-10-28 11:20:26 +01:00
|
|
|
return a.fCompressed == b.fCompressed && a.size() == b.size() &&
|
|
|
|
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
|
2013-07-15 01:05:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Initialize using begin and end iterators to byte data.
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
void Set(const T pbegin, const T pend, bool fCompressedIn)
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
if (pend - pbegin != 32) {
|
|
|
|
fValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Check(&pbegin[0])) {
|
|
|
|
memcpy(vch, (unsigned char*)&pbegin[0], 32);
|
|
|
|
fValid = true;
|
|
|
|
fCompressed = fCompressedIn;
|
|
|
|
} else {
|
|
|
|
fValid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Simple read-only vector-like interface.
|
2013-05-01 06:52:05 +02:00
|
|
|
unsigned int size() const { return (fValid ? 32 : 0); }
|
2014-09-19 19:21:46 +02:00
|
|
|
const unsigned char* begin() const { return vch; }
|
|
|
|
const unsigned char* end() const { return vch + size(); }
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Check whether this private key is valid.
|
2013-05-01 06:52:05 +02:00
|
|
|
bool IsValid() const { return fValid; }
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Check whether the public key corresponding to this private key is (to be) compressed.
|
2013-05-01 06:52:05 +02:00
|
|
|
bool IsCompressed() const { return fCompressed; }
|
2011-06-25 14:57:32 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Initialize from a CPrivKey (serialized OpenSSL private key data).
|
2014-09-19 19:21:46 +02:00
|
|
|
bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Generate a new private key using a cryptographic PRNG.
|
2012-05-16 18:36:38 +02:00
|
|
|
void MakeNewKey(bool fCompressed);
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
/**
|
|
|
|
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
|
|
|
|
* This is expensive.
|
|
|
|
*/
|
2012-05-16 18:36:38 +02:00
|
|
|
CPrivKey GetPrivKey() const;
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
/**
|
|
|
|
* Compute the public key from a private key.
|
|
|
|
* This is expensive.
|
|
|
|
*/
|
2012-05-14 19:07:52 +02:00
|
|
|
CPubKey GetPubKey() const;
|
2011-06-25 14:57:32 +02:00
|
|
|
|
2014-11-06 15:54:50 +01:00
|
|
|
/**
|
|
|
|
* Create a DER-serialized signature.
|
|
|
|
* The test_case parameter tweaks the deterministic nonce, and is only for
|
|
|
|
* testing. It should be zero for normal use.
|
|
|
|
*/
|
|
|
|
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
/**
|
|
|
|
* Create a compact signature (65 bytes), which allows reconstructing the used public key.
|
|
|
|
* The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
|
|
|
|
* The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
|
|
|
|
* 0x1D = second key with even y, 0x1E = second key with odd y,
|
|
|
|
* add 0x04 for compressed keys.
|
|
|
|
*/
|
2014-09-19 19:21:46 +02:00
|
|
|
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
|
2013-07-15 01:05:25 +02:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Derive BIP32 child key.
|
2013-07-15 01:05:25 +02:00
|
|
|
bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
|
2013-10-28 11:20:26 +01:00
|
|
|
|
2014-11-06 10:17:48 +01:00
|
|
|
/**
|
|
|
|
* Verify thoroughly whether a private key and a public key match.
|
|
|
|
* This is done using a different mechanism than just regenerating it.
|
|
|
|
*/
|
|
|
|
bool VerifyPubKey(const CPubKey& vchPubKey) const;
|
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Load private key and check that public key matches.
|
2014-09-19 19:21:46 +02:00
|
|
|
bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
|
2014-02-07 02:19:48 +01:00
|
|
|
|
2014-10-26 09:33:52 +01:00
|
|
|
//! Check whether an element of a signature (r or s) is valid.
|
2014-09-19 19:21:46 +02:00
|
|
|
static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
|
2013-07-15 01:05:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CExtKey {
|
|
|
|
unsigned char nDepth;
|
|
|
|
unsigned char vchFingerprint[4];
|
|
|
|
unsigned int nChild;
|
|
|
|
unsigned char vchChainCode[32];
|
|
|
|
CKey key;
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
friend bool operator==(const CExtKey& a, const CExtKey& b)
|
|
|
|
{
|
2013-07-15 01:05:25 +02:00
|
|
|
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
|
|
|
|
memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Encode(unsigned char code[74]) const;
|
|
|
|
void Decode(const unsigned char code[74]);
|
2014-09-19 19:21:46 +02:00
|
|
|
bool Derive(CExtKey& out, unsigned int nChild) const;
|
2013-07-15 01:05:25 +02:00
|
|
|
CExtPubKey Neuter() const;
|
2014-09-19 19:21:46 +02:00
|
|
|
void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2014-06-03 01:21:03 +02:00
|
|
|
/** Check that required EC support is available at runtime */
|
|
|
|
bool ECC_InitSanityCheck(void);
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_KEY_H
|