Merge #10114: [tests] sync_with_ping should assert that ping hasn't timed out
[bitcoinplatinum.git] / src / key.cpp
blobb4f0dc82027f8a379794eb2d5131fc81d2ec6c5b
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "key.h"
7 #include "arith_uint256.h"
8 #include "crypto/common.h"
9 #include "crypto/hmac_sha512.h"
10 #include "pubkey.h"
11 #include "random.h"
13 #include <secp256k1.h>
14 #include <secp256k1_recovery.h>
16 static secp256k1_context* secp256k1_context_sign = NULL;
18 /** These functions are taken from the libsecp256k1 distribution and are very ugly. */
19 static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
20 const unsigned char *end = privkey + privkeylen;
21 int lenb = 0;
22 int len = 0;
23 memset(out32, 0, 32);
24 /* sequence header */
25 if (end < privkey+1 || *privkey != 0x30) {
26 return 0;
28 privkey++;
29 /* sequence length constructor */
30 if (end < privkey+1 || !(*privkey & 0x80)) {
31 return 0;
33 lenb = *privkey & ~0x80; privkey++;
34 if (lenb < 1 || lenb > 2) {
35 return 0;
37 if (end < privkey+lenb) {
38 return 0;
40 /* sequence length */
41 len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
42 privkey += lenb;
43 if (end < privkey+len) {
44 return 0;
46 /* sequence element 0: version number (=1) */
47 if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
48 return 0;
50 privkey += 3;
51 /* sequence element 1: octet string, up to 32 bytes */
52 if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
53 return 0;
55 memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
56 if (!secp256k1_ec_seckey_verify(ctx, out32)) {
57 memset(out32, 0, 32);
58 return 0;
60 return 1;
63 static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
64 secp256k1_pubkey pubkey;
65 size_t pubkeylen = 0;
66 if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
67 *privkeylen = 0;
68 return 0;
70 if (compressed) {
71 static const unsigned char begin[] = {
72 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
74 static const unsigned char middle[] = {
75 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
76 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
77 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
78 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
79 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
80 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
81 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
82 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
83 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
85 unsigned char *ptr = privkey;
86 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
87 memcpy(ptr, key32, 32); ptr += 32;
88 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
89 pubkeylen = 33;
90 secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
91 ptr += pubkeylen;
92 *privkeylen = ptr - privkey;
93 } else {
94 static const unsigned char begin[] = {
95 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
97 static const unsigned char middle[] = {
98 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
99 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
100 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
101 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
102 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
103 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
104 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
105 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
106 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
107 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
108 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
110 unsigned char *ptr = privkey;
111 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
112 memcpy(ptr, key32, 32); ptr += 32;
113 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
114 pubkeylen = 65;
115 secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
116 ptr += pubkeylen;
117 *privkeylen = ptr - privkey;
119 return 1;
122 bool CKey::Check(const unsigned char *vch) {
123 return secp256k1_ec_seckey_verify(secp256k1_context_sign, vch);
126 void CKey::MakeNewKey(bool fCompressedIn) {
127 do {
128 GetStrongRandBytes(keydata.data(), keydata.size());
129 } while (!Check(keydata.data()));
130 fValid = true;
131 fCompressed = fCompressedIn;
134 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
135 if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
136 return false;
137 fCompressed = fCompressedIn;
138 fValid = true;
139 return true;
142 CPrivKey CKey::GetPrivKey() const {
143 assert(fValid);
144 CPrivKey privkey;
145 int ret;
146 size_t privkeylen;
147 privkey.resize(279);
148 privkeylen = 279;
149 ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*)&privkey[0], &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
150 assert(ret);
151 privkey.resize(privkeylen);
152 return privkey;
155 CPubKey CKey::GetPubKey() const {
156 assert(fValid);
157 secp256k1_pubkey pubkey;
158 size_t clen = 65;
159 CPubKey result;
160 int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
161 assert(ret);
162 secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
163 assert(result.size() == clen);
164 assert(result.IsValid());
165 return result;
168 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
169 if (!fValid)
170 return false;
171 vchSig.resize(72);
172 size_t nSigLen = 72;
173 unsigned char extra_entropy[32] = {0};
174 WriteLE32(extra_entropy, test_case);
175 secp256k1_ecdsa_signature sig;
176 int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
177 assert(ret);
178 secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig);
179 vchSig.resize(nSigLen);
180 return true;
183 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
184 if (pubkey.IsCompressed() != fCompressed) {
185 return false;
187 unsigned char rnd[8];
188 std::string str = "Bitcoin key verification\n";
189 GetRandBytes(rnd, sizeof(rnd));
190 uint256 hash;
191 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
192 std::vector<unsigned char> vchSig;
193 Sign(hash, vchSig);
194 return pubkey.Verify(hash, vchSig);
197 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
198 if (!fValid)
199 return false;
200 vchSig.resize(65);
201 int rec = -1;
202 secp256k1_ecdsa_recoverable_signature sig;
203 int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL);
204 assert(ret);
205 secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, (unsigned char*)&vchSig[1], &rec, &sig);
206 assert(ret);
207 assert(rec != -1);
208 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
209 return true;
212 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
213 if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
214 return false;
215 fCompressed = vchPubKey.IsCompressed();
216 fValid = true;
218 if (fSkipCheck)
219 return true;
221 return VerifyPubKey(vchPubKey);
224 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
225 assert(IsValid());
226 assert(IsCompressed());
227 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
228 if ((nChild >> 31) == 0) {
229 CPubKey pubkey = GetPubKey();
230 assert(pubkey.begin() + 33 == pubkey.end());
231 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
232 } else {
233 assert(begin() + 32 == end());
234 BIP32Hash(cc, nChild, 0, begin(), vout.data());
236 memcpy(ccChild.begin(), vout.data()+32, 32);
237 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
238 bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());
239 keyChild.fCompressed = true;
240 keyChild.fValid = ret;
241 return ret;
244 bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
245 out.nDepth = nDepth + 1;
246 CKeyID id = key.GetPubKey().GetID();
247 memcpy(&out.vchFingerprint[0], &id, 4);
248 out.nChild = _nChild;
249 return key.Derive(out.key, out.chaincode, _nChild, chaincode);
252 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
253 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
254 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
255 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
256 key.Set(&vout[0], &vout[32], true);
257 memcpy(chaincode.begin(), &vout[32], 32);
258 nDepth = 0;
259 nChild = 0;
260 memset(vchFingerprint, 0, sizeof(vchFingerprint));
263 CExtPubKey CExtKey::Neuter() const {
264 CExtPubKey ret;
265 ret.nDepth = nDepth;
266 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
267 ret.nChild = nChild;
268 ret.pubkey = key.GetPubKey();
269 ret.chaincode = chaincode;
270 return ret;
273 void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
274 code[0] = nDepth;
275 memcpy(code+1, vchFingerprint, 4);
276 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
277 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
278 memcpy(code+9, chaincode.begin(), 32);
279 code[41] = 0;
280 assert(key.size() == 32);
281 memcpy(code+42, key.begin(), 32);
284 void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
285 nDepth = code[0];
286 memcpy(vchFingerprint, code+1, 4);
287 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
288 memcpy(chaincode.begin(), code+9, 32);
289 key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
292 bool ECC_InitSanityCheck() {
293 CKey key;
294 key.MakeNewKey(true);
295 CPubKey pubkey = key.GetPubKey();
296 return key.VerifyPubKey(pubkey);
299 void ECC_Start() {
300 assert(secp256k1_context_sign == NULL);
302 secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
303 assert(ctx != NULL);
306 // Pass in a random blinding seed to the secp256k1 context.
307 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
308 GetRandBytes(vseed.data(), 32);
309 bool ret = secp256k1_context_randomize(ctx, vseed.data());
310 assert(ret);
313 secp256k1_context_sign = ctx;
316 void ECC_Stop() {
317 secp256k1_context *ctx = secp256k1_context_sign;
318 secp256k1_context_sign = NULL;
320 if (ctx) {
321 secp256k1_context_destroy(ctx);