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 "support/allocators/secure.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.
129 bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
132 * Create a compact signature (65 bytes), which allows reconstructing the used public key.
133 * The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
134 * The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
135 * 0x1D = second key with even y, 0x1E = second key with odd y,
136 * add 0x04 for compressed keys.
138 bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
140 //! Derive BIP32 child key.
141 bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
144 * Verify thoroughly whether a private key and a public key match.
145 * This is done using a different mechanism than just regenerating it.
147 bool VerifyPubKey(const CPubKey& vchPubKey) const;
149 //! Load private key and check that public key matches.
150 bool Load(CPrivKey& privkey, CPubKey& vchPubKey, bool fSkipCheck);
152 //! Check whether an element of a signature (r or s) is valid.
153 static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
157 unsigned char nDepth;
158 unsigned char vchFingerprint[4];
160 unsigned char vchChainCode[32];
163 friend bool operator==(const CExtKey& a, const CExtKey& b)
165 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
166 memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
169 void Encode(unsigned char code[74]) const;
170 void Decode(const unsigned char code[74]);
171 bool Derive(CExtKey& out, unsigned int nChild) const;
172 CExtPubKey Neuter() const;
173 void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
176 /** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
177 void ECC_Start(void);
179 /** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
182 /** Check that required EC support is available at runtime. */
183 bool ECC_InitSanityCheck(void);
185 #endif // BITCOIN_KEY_H