Dogecoin Core  1.14.2
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_KEY_H
7 #define BITCOIN_KEY_H
8 
9 #include "pubkey.h"
10 #include "serialize.h"
12 #include "uint256.h"
13 
14 #include <stdexcept>
15 #include <vector>
16 
17 
32 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
33 
35 class CKey
36 {
37 private:
40  bool fValid;
41 
44 
46  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
47 
49  bool static Check(const unsigned char* vch);
50 
51 public:
53  CKey() : fValid(false), fCompressed(false)
54  {
55  // Important: vch must be 32 bytes in length to not break serialization
56  keydata.resize(32);
57  }
58 
61  {
62  }
63 
64  friend bool operator==(const CKey& a, const CKey& b)
65  {
66  return a.fCompressed == b.fCompressed &&
67  a.size() == b.size() &&
68  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
69  }
70 
72  template <typename T>
73  void Set(const T pbegin, const T pend, bool fCompressedIn)
74  {
75  if (size_t(pend - pbegin) != keydata.size()) {
76  fValid = false;
77  } else if (Check(&pbegin[0])) {
78  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
79  fValid = true;
80  fCompressed = fCompressedIn;
81  } else {
82  fValid = false;
83  }
84  }
85 
87  unsigned int size() const { return (fValid ? keydata.size() : 0); }
88  const unsigned char* begin() const { return keydata.data(); }
89  const unsigned char* end() const { return keydata.data() + size(); }
90 
92  bool IsValid() const { return fValid; }
93 
95  bool IsCompressed() const { return fCompressed; }
96 
98  bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
99 
101  void MakeNewKey(bool fCompressed);
102 
107  CPrivKey GetPrivKey() const;
108 
113  CPubKey GetPubKey() const;
114 
119  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
120 
128  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
129 
131  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
132 
137  bool VerifyPubKey(const CPubKey& vchPubKey) const;
138 
140  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
141 };
142 
143 struct CExtKey {
144  unsigned char nDepth;
145  unsigned char vchFingerprint[4];
146  unsigned int nChild;
149 
150  friend bool operator==(const CExtKey& a, const CExtKey& b)
151  {
152  return a.nDepth == b.nDepth &&
153  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
154  a.nChild == b.nChild &&
155  a.chaincode == b.chaincode &&
156  a.key == b.key;
157  }
158 
159  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
160  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
161  bool Derive(CExtKey& out, unsigned int nChild) const;
162  CExtPubKey Neuter() const;
163  void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
164  template <typename Stream>
165  void Serialize(Stream& s) const
166  {
167  unsigned int len = BIP32_EXTKEY_SIZE;
168  ::WriteCompactSize(s, len);
169  unsigned char code[BIP32_EXTKEY_SIZE];
170  Encode(code);
171  s.write((const char *)&code[0], len);
172  }
173  template <typename Stream>
174  void Unserialize(Stream& s)
175  {
176  unsigned int len = ::ReadCompactSize(s);
177  unsigned char code[BIP32_EXTKEY_SIZE];
178  s.read((char *)&code[0], len);
179  Decode(code);
180  }
181 };
182 
184 void ECC_Start(void);
185 
187 void ECC_Stop(void);
188 
190 bool ECC_InitSanityCheck(void);
191 
192 #endif // BITCOIN_KEY_H
An encapsulated private key.
Definition: key.h:36
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:212
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:64
const unsigned char * end() const
Definition: key.h:89
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:87
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:92
~CKey()
Destructor (again necessary because of memlocking).
Definition: key.h:60
bool fValid
Whether this private key is valid.
Definition: key.h:40
CKey()
Construct an invalid private key.
Definition: key.h:53
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:142
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:95
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:43
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:155
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:134
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:168
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
const unsigned char * begin() const
Definition: key.h:88
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:46
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:183
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to be vch is valid keydata.
Definition: key.cpp:122
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:224
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:197
An encapsulated public key.
Definition: pubkey.h:40
256-bit opaque blob.
Definition: uint256.h:123
void * memcpy(void *a, const void *b, size_t c)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: key.h:32
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:292
void ECC_Start(void)
Initialize the elliptic curve support.
Definition: key.cpp:299
void ECC_Stop(void)
Deinitialize the elliptic curve support.
Definition: key.cpp:316
const unsigned int BIP32_EXTKEY_SIZE
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: pubkey.h:26
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:245
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:942
Definition: key.h:143
unsigned char vchFingerprint[4]
Definition: key.h:145
CExtPubKey Neuter() const
Definition: key.cpp:263
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:244
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:252
void Serialize(Stream &s) const
Definition: key.h:165
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:284
CKey key
Definition: key.h:148
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:273
unsigned char nDepth
Definition: key.h:144
ChainCode chaincode
Definition: key.h:147
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:150
unsigned int nChild
Definition: key.h:146
void Unserialize(Stream &s)
Definition: key.h:174