1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "allocators.h"
11 #include "serialize.h"
18 // const unsigned int PRIVATE_KEY_SIZE = 279;
19 // const unsigned int PUBLIC_KEY_SIZE = 65;
20 // const unsigned int SIGNATURE_SIZE = 72;
22 // see www.keylength.com
23 // script supports up to 75 for single byte push
25 /** A reference to a CKey: the Hash160 of its serialized public key */
26 class CKeyID : public uint160
29 CKeyID() : uint160(0) {}
30 CKeyID(const uint160& in) : uint160(in) {}
33 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
34 class CScriptID : public uint160
37 CScriptID() : uint160(0) {}
38 CScriptID(const uint160& in) : uint160(in) {}
41 /** An encapsulated public key. */
45 // Just store the serialized data.
46 // Its length can very cheaply be computed from the first byte.
47 unsigned char vch[65];
49 // Compute the length of a pubkey with a given first byte.
50 unsigned int static GetLen(unsigned char chHeader)
52 if (chHeader == 2 || chHeader == 3)
54 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
59 // Set this key data to be invalid
66 // Construct an invalid public key.
72 // Initialize a public key using begin/end iterators to byte data.
74 void Set(const T pbegin, const T pend)
76 int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
77 if (len && len == (pend - pbegin))
78 memcpy(vch, (unsigned char*)&pbegin[0], len);
83 // Construct a public key using begin/end iterators to byte data.
85 CPubKey(const T pbegin, const T pend)
90 // Construct a public key from a byte vector.
91 CPubKey(const std::vector<unsigned char>& vch)
93 Set(vch.begin(), vch.end());
96 // Simple read-only vector-like interface to the pubkey data.
97 unsigned int size() const { return GetLen(vch[0]); }
98 const unsigned char* begin() const { return vch; }
99 const unsigned char* end() const { return vch + size(); }
100 const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
102 // Comparator implementation.
103 friend bool operator==(const CPubKey& a, const CPubKey& b)
105 return a.vch[0] == b.vch[0] &&
106 memcmp(a.vch, b.vch, a.size()) == 0;
108 friend bool operator!=(const CPubKey& a, const CPubKey& b)
112 friend bool operator<(const CPubKey& a, const CPubKey& b)
114 return a.vch[0] < b.vch[0] ||
115 (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
118 // Implement serialization, as if this was a byte vector.
119 unsigned int GetSerializeSize(int nType, int nVersion) const
123 template <typename Stream>
124 void Serialize(Stream& s, int nType, int nVersion) const
126 unsigned int len = size();
127 ::WriteCompactSize(s, len);
128 s.write((char*)vch, len);
130 template <typename Stream>
131 void Unserialize(Stream& s, int nType, int nVersion)
133 unsigned int len = ::ReadCompactSize(s);
135 s.read((char*)vch, len);
137 // invalid pubkey, skip available data
145 // Get the KeyID of this public key (hash of its serialization)
148 return CKeyID(Hash160(vch, vch + size()));
151 // Get the 256-bit hash of this public key.
152 uint256 GetHash() const
154 return Hash(vch, vch + size());
157 // Check syntactic correctness.
159 // Note that this is consensus critical as CheckSig() calls it!
165 // fully validate whether this is a valid public key (more expensive than IsValid())
166 bool IsFullyValid() const;
168 // Check whether this is a compressed public key.
169 bool IsCompressed() const
174 // Verify a DER signature (~72 bytes).
175 // If this public key is not fully valid, the return value will be false.
176 bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
178 // Recover a public key from a compact signature.
179 bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
181 // Turn this public key into an uncompressed public key.
184 // Derive BIP32 child pubkey.
185 bool Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
189 // secure_allocator is defined in allocators.h
190 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
191 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
193 /** An encapsulated private key. */
197 // Whether this private key is valid. We check for correctness when modifying the key
198 // data, so fValid should always correspond to the actual state.
201 // Whether the public key corresponding to this private key is (to be) compressed.
204 // The actual byte data
205 unsigned char vch[32];
207 // Check whether the 32-byte array pointed to be vch is valid keydata.
208 bool static Check(const unsigned char* vch);
211 // Construct an invalid private key.
212 CKey() : fValid(false), fCompressed(false)
217 // Copy constructor. This is necessary because of memlocking.
218 CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
221 memcpy(vch, secret.vch, sizeof(vch));
224 // Destructor (again necessary because of memlocking).
230 friend bool operator==(const CKey& a, const CKey& b)
232 return a.fCompressed == b.fCompressed && a.size() == b.size() &&
233 memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
236 // Initialize using begin and end iterators to byte data.
237 template <typename T>
238 void Set(const T pbegin, const T pend, bool fCompressedIn)
240 if (pend - pbegin != 32) {
244 if (Check(&pbegin[0])) {
245 memcpy(vch, (unsigned char*)&pbegin[0], 32);
247 fCompressed = fCompressedIn;
253 // Simple read-only vector-like interface.
254 unsigned int size() const { return (fValid ? 32 : 0); }
255 const unsigned char* begin() const { return vch; }
256 const unsigned char* end() const { return vch + size(); }
258 // Check whether this private key is valid.
259 bool IsValid() const { return fValid; }
261 // Check whether the public key corresponding to this private key is (to be) compressed.
262 bool IsCompressed() const { return fCompressed; }
264 // Initialize from a CPrivKey (serialized OpenSSL private key data).
265 bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
267 // Generate a new private key using a cryptographic PRNG.
268 void MakeNewKey(bool fCompressed);
270 // Convert the private key to a CPrivKey (serialized OpenSSL private key data).
271 // This is expensive.
272 CPrivKey GetPrivKey() const;
274 // Compute the public key from a private key.
275 // This is expensive.
276 CPubKey GetPubKey() const;
278 // Create a DER-serialized signature.
279 bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool lowS = true) const;
281 // Create a compact signature (65 bytes), which allows reconstructing the used public key.
282 // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
283 // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
284 // 0x1D = second key with even y, 0x1E = second key with odd y,
285 // add 0x04 for compressed keys.
286 bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
288 // Derive BIP32 child key.
289 bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
291 // Load private key and check that public key matches.
292 bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
294 // Check whether an element of a signature (r or s) is valid.
295 static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
299 unsigned char nDepth;
300 unsigned char vchFingerprint[4];
302 unsigned char vchChainCode[32];
305 friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
307 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
308 memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.pubkey == b.pubkey;
311 void Encode(unsigned char code[74]) const;
312 void Decode(const unsigned char code[74]);
313 bool Derive(CExtPubKey& out, unsigned int nChild) const;
317 unsigned char nDepth;
318 unsigned char vchFingerprint[4];
320 unsigned char vchChainCode[32];
323 friend bool operator==(const CExtKey& a, const CExtKey& b)
325 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
326 memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
329 void Encode(unsigned char code[74]) const;
330 void Decode(const unsigned char code[74]);
331 bool Derive(CExtKey& out, unsigned int nChild) const;
332 CExtPubKey Neuter() const;
333 void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
336 /** Check that required EC support is available at runtime */
337 bool ECC_InitSanityCheck(void);
339 #endif // BITCOIN_KEY_H