Dash Core  0.12.2.1
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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  unsigned char vch[32];
47 
49  bool static Check(const unsigned char* vch);
50 
51 public:
53  CKey() : fValid(false), fCompressed(false)
54  {
55  LockObject(vch);
56  }
57 
59  CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
60  {
61  LockObject(vch);
62  memcpy(vch, secret.vch, sizeof(vch));
63  }
64 
67  {
69  }
70 
71  friend bool operator==(const CKey& a, const CKey& b)
72  {
73  return a.fCompressed == b.fCompressed && a.size() == b.size() &&
74  memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
75  }
76 
78  template <typename T>
79  void Set(const T pbegin, const T pend, bool fCompressedIn)
80  {
81  if (pend - pbegin != 32) {
82  fValid = false;
83  return;
84  }
85  if (Check(&pbegin[0])) {
86  memcpy(vch, (unsigned char*)&pbegin[0], 32);
87  fValid = true;
88  fCompressed = fCompressedIn;
89  } else {
90  fValid = false;
91  }
92  }
93 
95  unsigned int size() const { return (fValid ? 32 : 0); }
96  const unsigned char* begin() const { return vch; }
97  const unsigned char* end() const { return vch + size(); }
98 
100  bool IsValid() const { return fValid; }
101 
103  bool IsCompressed() const { return fCompressed; }
104 
106  bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
107 
109  void MakeNewKey(bool fCompressed);
110 
115  CPrivKey GetPrivKey() const;
116 
121  CPubKey GetPubKey() const;
122 
127  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
128 
136  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
137 
139  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
140 
145  bool VerifyPubKey(const CPubKey& vchPubKey) const;
146 
148  bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
149 
151  static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
152 };
153 
154 struct CExtKey {
155  unsigned char nDepth;
156  unsigned char vchFingerprint[4];
157  unsigned int nChild;
160 
161  friend bool operator==(const CExtKey& a, const CExtKey& b)
162  {
163  return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
164  a.chaincode == b.chaincode && a.key == b.key;
165  }
166 
167  void Encode(unsigned char code[74]) const;
168  void Decode(const unsigned char code[74]);
169  bool Derive(CExtKey& out, unsigned int nChild) const;
170  CExtPubKey Neuter() const;
171  void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
172  template <typename Stream>
173  void Serialize(Stream& s, int nType, int nVersion) const
174  {
175  unsigned int len = 74;
176  ::WriteCompactSize(s, len);
177  unsigned char code[74];
178  Encode(code);
179  s.write((const char *)&code[0], len);
180  }
181  template <typename Stream>
182  void Unserialize(Stream& s, int nType, int nVersion)
183  {
184  unsigned int len = ::ReadCompactSize(s);
185  unsigned char code[74];
186  s.read((char *)&code[0], len);
187  Decode(code);
188  }
189 };
190 
192 void ECC_Start(void);
193 
195 void ECC_Stop(void);
196 
198 bool ECC_InitSanityCheck(void);
199 
200 #endif // BITCOIN_KEY_H
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:255
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:79
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:135
CKey(const CKey &secret)
Copy constructor. This is necessary because of memlocking.
Definition: key.h:59
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:288
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:43
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:247
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:71
bool VerifyPubKey(const CPubKey &vchPubKey) const
Definition: key.cpp:184
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Definition: key.cpp:169
void UnlockObject(const T &t)
Definition: pagelocker.h:171
void Unserialize(Stream &s, int nType, int nVersion)
Definition: key.h:182
CPrivKey GetPrivKey() const
Definition: key.cpp:143
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:225
void Encode(unsigned char code[74]) const
Definition: key.cpp:278
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:213
static bool CheckSignatureElement(const unsigned char *vch, int len, bool half)
Check whether an element of a signature (r or s) is valid.
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:32
CKey key
Definition: key.h:159
CExtPubKey Neuter() const
Definition: key.cpp:268
unsigned char vchFingerprint[4]
Definition: key.h:156
ChainCode chaincode
Definition: key.h:158
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:161
void Decode(const unsigned char code[74])
Definition: key.cpp:289
const unsigned char * begin() const
Definition: key.h:96
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 fValid
Definition: key.h:40
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:103
void ECC_Start(void)
Definition: key.cpp:304
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
Definition: key.h:97
unsigned char nDepth
Definition: key.h:155
CPubKey GetPubKey() const
Definition: key.cpp:156
unsigned char vch[32]
The actual byte data.
Definition: key.h:46
void Serialize(Stream &s, int nType, int nVersion) const
Definition: key.h:173
Definition: pubkey.h:37
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:95
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:100
~CKey()
Destructor (again necessary because of memlocking).
Definition: key.h:66
CKey()
Construct an invalid private key.
Definition: key.h:53
void WriteCompactSize(Stream &os, uint64_t nSize)
Definition: serialize.h:263
Definition: key.h:35
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:198
unsigned int nChild
Definition: key.h:157
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
void LockObject(const T &t)
Definition: pagelocker.h:165
void ECC_Stop(void)
Definition: key.cpp:323
Definition: key.h:154
bool ECC_InitSanityCheck(void)
Definition: key.cpp:297