1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
9 #include "allocators.h"
10 #include "serialize.h"
22 * const unsigned int PRIVATE_KEY_SIZE = 279;
23 * const unsigned int PUBLIC_KEY_SIZE = 65;
24 * const unsigned int SIGNATURE_SIZE = 72;
26 * see www.keylength.com
27 * script supports up to 75 for single byte push
31 * secure_allocator is defined in allocators.h
32 * CPrivKey is a serialized private key, with all parameters included (279 bytes)
34 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
36 /** An encapsulated private key. */
40 //! Whether this private key is valid. We check for correctness when modifying the key
41 //! data, so fValid should always correspond to the actual state.
44 //! Whether the public key corresponding to this private key is (to be) compressed.
47 //! The actual byte data
48 unsigned char vch[32];
50 //! Check whether the 32-byte array pointed to be vch is valid keydata.
51 bool static Check(const unsigned char* vch);
54 //! Construct an invalid private key.
55 CKey() : fValid(false), fCompressed(false)
60 //! Copy constructor. This is necessary because of memlocking.
61 CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
64 memcpy(vch, secret.vch, sizeof(vch));
67 //! Destructor (again necessary because of memlocking).
73 friend bool operator==(const CKey& a, const CKey& b)
75 return a.fCompressed == b.fCompressed && a.size() == b.size() &&
76 memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
79 //! Initialize using begin and end iterators to byte data.
81 void Set(const T pbegin, const T pend, bool fCompressedIn)
83 if (pend - pbegin != 32) {
87 if (Check(&pbegin[0])) {
88 memcpy(vch, (unsigned char*)&pbegin[0], 32);
90 fCompressed = fCompressedIn;
96 //! Simple read-only vector-like interface.
97 unsigned int size() const { return (fValid ? 32 : 0); }
98 const unsigned char* begin() const { return vch; }
99 const unsigned char* end() const { return vch + size(); }
101 //! Check whether this private key is valid.
102 bool IsValid() const { return fValid; }
104 //! Check whether the public key corresponding to this private key is (to be) compressed.
105 bool IsCompressed() const { return fCompressed; }
107 //! Initialize from a CPrivKey (serialized OpenSSL private key data).
108 bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
110 //! Generate a new private key using a cryptographic PRNG.
111 void MakeNewKey(bool fCompressed);
114 * Convert the private key to a CPrivKey (serialized OpenSSL private key data).
117 CPrivKey GetPrivKey() const;
120 * Compute the public key from a private key.
123 CPubKey GetPubKey() const;
126 * Create a DER-serialized signature.
127 * The test_case parameter tweaks the deterministic nonce, and is only for
128 * testing. It should be zero for normal use.
130 bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
133 * Create a compact signature (65 bytes), which allows reconstructing the used public key.
134 * The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
135 * The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
136 * 0x1D = second key with even y, 0x1E = second key with odd y,
137 * add 0x04 for compressed keys.
139 bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
141 //! Derive BIP32 child key.
142 bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
145 * Verify thoroughly whether a private key and a public key match.
146 * This is done using a different mechanism than just regenerating it.
148 bool VerifyPubKey(const CPubKey& vchPubKey) const;
150 //! Load private key and check that public key matches.
151 bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
153 //! Check whether an element of a signature (r or s) is valid.
154 static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
158 unsigned char nDepth;
159 unsigned char vchFingerprint[4];
161 unsigned char vchChainCode[32];
164 friend bool operator==(const CExtKey& a, const CExtKey& b)
166 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
167 memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
170 void Encode(unsigned char code[74]) const;
171 void Decode(const unsigned char code[74]);
172 bool Derive(CExtKey& out, unsigned int nChild) const;
173 CExtPubKey Neuter() const;
174 void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
177 /** Check that required EC support is available at runtime */
178 bool ECC_InitSanityCheck(void);
180 #endif // BITCOIN_KEY_H