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.
10 #include "serialize.h"
11 #include "support/allocators/secure.h"
20 * const unsigned int PRIVATE_KEY_SIZE = 279;
21 * const unsigned int PUBLIC_KEY_SIZE = 65;
22 * const unsigned int SIGNATURE_SIZE = 72;
24 * see www.keylength.com
25 * script supports up to 75 for single byte push
29 * secure_allocator is defined in allocators.h
30 * CPrivKey is a serialized private key, with all parameters included (279 bytes)
32 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
34 /** An encapsulated private key. */
38 //! Whether this private key is valid. We check for correctness when modifying the key
39 //! data, so fValid should always correspond to the actual state.
42 //! Whether the public key corresponding to this private key is (to be) compressed.
45 //! The actual byte data
46 unsigned char vch[32];
48 //! Check whether the 32-byte array pointed to be vch is valid keydata.
49 bool static Check(const unsigned char* vch);
52 //! Construct an invalid private key.
53 CKey() : fValid(false), fCompressed(false)
58 //! Copy constructor. This is necessary because of memlocking.
59 CKey(const CKey& secret) : fValid(secret.fValid), fCompressed(secret.fCompressed)
62 memcpy(vch, secret.vch, sizeof(vch));
65 //! Destructor (again necessary because of memlocking).
71 friend bool operator==(const CKey& a, const CKey& b)
73 return a.fCompressed == b.fCompressed && a.size() == b.size() &&
74 memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
77 //! Initialize using begin and end iterators to byte data.
79 void Set(const T pbegin, const T pend, bool fCompressedIn)
81 if (pend - pbegin != 32) {
85 if (Check(&pbegin[0])) {
86 memcpy(vch, (unsigned char*)&pbegin[0], 32);
88 fCompressed = fCompressedIn;
94 //! Simple read-only vector-like interface.
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(); }
99 //! Check whether this private key is valid.
100 bool IsValid() const { return fValid; }
102 //! Check whether the public key corresponding to this private key is (to be) compressed.
103 bool IsCompressed() const { return fCompressed; }
105 //! Initialize from a CPrivKey (serialized OpenSSL private key data).
106 bool SetPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
108 //! Generate a new private key using a cryptographic PRNG.
109 void MakeNewKey(bool fCompressed);
112 * Convert the private key to a CPrivKey (serialized OpenSSL private key data).
115 CPrivKey GetPrivKey() const;
118 * Compute the public key from a private key.
121 CPubKey GetPubKey() const;
124 * Create a DER-serialized signature.
125 * The test_case parameter tweaks the deterministic nonce.
127 bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
130 * Create a compact signature (65 bytes), which allows reconstructing the used public key.
131 * The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
132 * The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
133 * 0x1D = second key with even y, 0x1E = second key with odd y,
134 * add 0x04 for compressed keys.
136 bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
138 //! Derive BIP32 child key.
139 bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
142 * Verify thoroughly whether a private key and a public key match.
143 * This is done using a different mechanism than just regenerating it.
145 bool VerifyPubKey(const CPubKey& vchPubKey) const;
147 //! Load private key and check that public key matches.
148 bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
150 //! Check whether an element of a signature (r or s) is valid.
151 static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
155 unsigned char nDepth;
156 unsigned char vchFingerprint[4];
161 friend bool operator==(const CExtKey& a, const CExtKey& b)
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;
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);
174 /** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
175 void ECC_Start(void);
177 /** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
180 /** Check that required EC support is available at runtime. */
181 bool ECC_InitSanityCheck(void);
183 #endif // BITCOIN_KEY_H