]>
Commit | Line | Data |
---|---|---|
f914f1a7 | 1 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
50fa0797 | 2 | // Copyright (c) 2017 The Zcash developers |
ffd8edda | 3 | // Distributed under the MIT software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
93db3fce | 5 | |
51ed9ec9 BD |
6 | #include "key.h" |
7 | ||
734f85c4 | 8 | #include "arith_uint256.h" |
437ada3e | 9 | #include "crypto/common.h" |
36fa4a78 | 10 | #include "crypto/hmac_sha512.h" |
d2e74c55 | 11 | #include "pubkey.h" |
001a53d7 | 12 | #include "random.h" |
977cdade | 13 | |
fda3fed1 | 14 | #include <secp256k1.h> |
c1afe40a | 15 | #include <secp256k1_recovery.h> |
dfa23b94 | 16 | |
c1afe40a PW |
17 | static secp256k1_context* secp256k1_context_sign = NULL; |
18 | ||
19 | /** These functions are taken from the libsecp256k1 distribution and are very ugly. */ | |
c032f1b6 JG |
20 | |
21 | /** | |
22 | * This parses a format loosely based on a DER encoding of the ECPrivateKey type from | |
23 | * section C.4 of SEC 1 <http://www.secg.org/sec1-v2.pdf>, with the following caveats: | |
24 | * | |
25 | * * The octet-length of the SEQUENCE must be encoded as 1 or 2 octets. It is not | |
26 | * required to be encoded as one octet if it is less than 256, as DER would require. | |
27 | * * The octet-length of the SEQUENCE must not be greater than the remaining | |
28 | * length of the key encoding, but need not match it (i.e. the encoding may contain | |
29 | * junk after the encoded SEQUENCE). | |
30 | * * The privateKey OCTET STRING is zero-filled on the left to 32 octets. | |
31 | * * Anything after the encoding of the privateKey OCTET STRING is ignored, whether | |
32 | * or not it is validly encoded DER. | |
33 | * | |
34 | * out32 must point to an output buffer of length at least 32 bytes. | |
35 | */ | |
c1afe40a PW |
36 | static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) { |
37 | const unsigned char *end = privkey + privkeylen; | |
c1afe40a PW |
38 | memset(out32, 0, 32); |
39 | /* sequence header */ | |
50fa0797 | 40 | if (end - privkey < 1 || *privkey != 0x30u) { |
c1afe40a PW |
41 | return 0; |
42 | } | |
43 | privkey++; | |
44 | /* sequence length constructor */ | |
50fa0797 | 45 | if (end - privkey < 1 || !(*privkey & 0x80u)) { |
c1afe40a PW |
46 | return 0; |
47 | } | |
1f594106 | 48 | size_t lenb = *privkey & ~0x80u; privkey++; |
c1afe40a PW |
49 | if (lenb < 1 || lenb > 2) { |
50 | return 0; | |
51 | } | |
50fa0797 | 52 | if (end - privkey < lenb) { |
c1afe40a PW |
53 | return 0; |
54 | } | |
55 | /* sequence length */ | |
1f594106 | 56 | size_t len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0u); |
c1afe40a | 57 | privkey += lenb; |
50fa0797 | 58 | if (end - privkey < len) { |
c1afe40a PW |
59 | return 0; |
60 | } | |
61 | /* sequence element 0: version number (=1) */ | |
50fa0797 | 62 | if (end - privkey < 3 || privkey[0] != 0x02u || privkey[1] != 0x01u || privkey[2] != 0x01u) { |
c1afe40a PW |
63 | return 0; |
64 | } | |
65 | privkey += 3; | |
66 | /* sequence element 1: octet string, up to 32 bytes */ | |
50fa0797 | 67 | if (end - privkey < 2 || privkey[0] != 0x04u) { |
c1afe40a PW |
68 | return 0; |
69 | } | |
50fa0797 JG |
70 | size_t oslen = privkey[1]; |
71 | privkey += 2; | |
72 | if (oslen > 32 || end - privkey < oslen) { | |
73 | return 0; | |
74 | } | |
75 | memcpy(out32 + (32 - oslen), privkey, oslen); | |
c1afe40a PW |
76 | if (!secp256k1_ec_seckey_verify(ctx, out32)) { |
77 | memset(out32, 0, 32); | |
78 | return 0; | |
79 | } | |
80 | return 1; | |
81 | } | |
82 | ||
c032f1b6 JG |
83 | /** |
84 | * This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1 | |
85 | * <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are | |
86 | * included. | |
87 | * | |
877964c8 JG |
88 | * privkey must point to an output buffer of length at least PRIVATE_KEY_SIZE bytes. |
89 | * privkeylen must initially be set to the size of the privkey buffer. Upon return it | |
90 | * will be set to the number of bytes used in the buffer. | |
c032f1b6 JG |
91 | * key32 must point to a 32-byte raw private key. |
92 | */ | |
c1afe40a | 93 | static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) { |
877964c8 | 94 | assert(*privkeylen >= PRIVATE_KEY_SIZE); |
fd0d1c7d JG |
95 | static_assert( |
96 | PRIVATE_KEY_SIZE >= COMPRESSED_PRIVATE_KEY_SIZE, | |
97 | "COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE"); | |
c1afe40a PW |
98 | secp256k1_pubkey pubkey; |
99 | size_t pubkeylen = 0; | |
100 | if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) { | |
101 | *privkeylen = 0; | |
102 | return 0; | |
103 | } | |
104 | if (compressed) { | |
105 | static const unsigned char begin[] = { | |
106 | 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20 | |
107 | }; | |
108 | static const unsigned char middle[] = { | |
109 | 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48, | |
110 | 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
111 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
112 | 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04, | |
113 | 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87, | |
114 | 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8, | |
115 | 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
116 | 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E, | |
117 | 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00 | |
118 | }; | |
119 | unsigned char *ptr = privkey; | |
120 | memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); | |
121 | memcpy(ptr, key32, 32); ptr += 32; | |
122 | memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); | |
877964c8 | 123 | pubkeylen = COMPRESSED_PUBLIC_KEY_SIZE; |
c1afe40a PW |
124 | secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED); |
125 | ptr += pubkeylen; | |
126 | *privkeylen = ptr - privkey; | |
877964c8 | 127 | assert(*privkeylen == COMPRESSED_PRIVATE_KEY_SIZE); |
c1afe40a PW |
128 | } else { |
129 | static const unsigned char begin[] = { | |
130 | 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20 | |
131 | }; | |
132 | static const unsigned char middle[] = { | |
133 | 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48, | |
134 | 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
135 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
136 | 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04, | |
137 | 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87, | |
138 | 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8, | |
139 | 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11, | |
140 | 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10, | |
141 | 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, | |
142 | 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E, | |
143 | 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00 | |
144 | }; | |
145 | unsigned char *ptr = privkey; | |
146 | memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); | |
147 | memcpy(ptr, key32, 32); ptr += 32; | |
148 | memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); | |
877964c8 | 149 | pubkeylen = PUBLIC_KEY_SIZE; |
c1afe40a PW |
150 | secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED); |
151 | ptr += pubkeylen; | |
152 | *privkeylen = ptr - privkey; | |
877964c8 | 153 | assert(*privkeylen == PRIVATE_KEY_SIZE); |
c1afe40a PW |
154 | } |
155 | return 1; | |
156 | } | |
dfa23b94 PW |
157 | |
158 | bool CKey::Check(const unsigned char *vch) { | |
c1afe40a | 159 | return secp256k1_ec_seckey_verify(secp256k1_context_sign, vch); |
096e06db GA |
160 | } |
161 | ||
dfa23b94 PW |
162 | void CKey::MakeNewKey(bool fCompressedIn) { |
163 | do { | |
001a53d7 | 164 | GetRandBytes(vch, sizeof(vch)); |
dfa23b94 PW |
165 | } while (!Check(vch)); |
166 | fValid = true; | |
167 | fCompressed = fCompressedIn; | |
096e06db GA |
168 | } |
169 | ||
dfa23b94 | 170 | bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) { |
c1afe40a | 171 | if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size())) |
dfa23b94 | 172 | return false; |
dfa23b94 PW |
173 | fCompressed = fCompressedIn; |
174 | fValid = true; | |
175 | return true; | |
096e06db GA |
176 | } |
177 | ||
dfa23b94 PW |
178 | CPrivKey CKey::GetPrivKey() const { |
179 | assert(fValid); | |
fda3fed1 | 180 | CPrivKey privkey; |
c1afe40a PW |
181 | int ret; |
182 | size_t privkeylen; | |
877964c8 JG |
183 | privkey.resize(PRIVATE_KEY_SIZE); |
184 | privkeylen = PRIVATE_KEY_SIZE; | |
c1afe40a | 185 | ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*)&privkey[0], &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); |
fda3fed1 PW |
186 | assert(ret); |
187 | privkey.resize(privkeylen); | |
dfa23b94 | 188 | return privkey; |
096e06db GA |
189 | } |
190 | ||
dfa23b94 PW |
191 | CPubKey CKey::GetPubKey() const { |
192 | assert(fValid); | |
c1afe40a | 193 | secp256k1_pubkey pubkey; |
877964c8 | 194 | size_t clen = PUBLIC_KEY_SIZE; |
bdaec6ab | 195 | CPubKey result; |
c1afe40a | 196 | int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin()); |
fda3fed1 | 197 | assert(ret); |
c1afe40a PW |
198 | secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); |
199 | assert(result.size() == clen); | |
bdaec6ab CF |
200 | assert(result.IsValid()); |
201 | return result; | |
096e06db GA |
202 | } |
203 | ||
a53fd414 | 204 | bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const { |
dfa23b94 | 205 | if (!fValid) |
096e06db | 206 | return false; |
877964c8 JG |
207 | vchSig.resize(SIGNATURE_SIZE); |
208 | size_t nSigLen = SIGNATURE_SIZE; | |
437ada3e PW |
209 | unsigned char extra_entropy[32] = {0}; |
210 | WriteLE32(extra_entropy, test_case); | |
c1afe40a PW |
211 | secp256k1_ecdsa_signature sig; |
212 | int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL); | |
1a9576de | 213 | assert(ret); |
c1afe40a | 214 | secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig); |
1a9576de PW |
215 | vchSig.resize(nSigLen); |
216 | return true; | |
096e06db GA |
217 | } |
218 | ||
d0c41a73 PW |
219 | bool CKey::VerifyPubKey(const CPubKey& pubkey) const { |
220 | if (pubkey.IsCompressed() != fCompressed) { | |
221 | return false; | |
222 | } | |
223 | unsigned char rnd[8]; | |
224 | std::string str = "Bitcoin key verification\n"; | |
225 | GetRandBytes(rnd, sizeof(rnd)); | |
226 | uint256 hash; | |
1a9576de | 227 | CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin()); |
d0c41a73 PW |
228 | std::vector<unsigned char> vchSig; |
229 | Sign(hash, vchSig); | |
230 | return pubkey.Verify(hash, vchSig); | |
231 | } | |
232 | ||
dfa23b94 PW |
233 | bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const { |
234 | if (!fValid) | |
096e06db | 235 | return false; |
877964c8 | 236 | vchSig.resize(COMPACT_SIGNATURE_SIZE); |
dfa23b94 | 237 | int rec = -1; |
c1afe40a PW |
238 | secp256k1_ecdsa_recoverable_signature sig; |
239 | int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL); | |
240 | assert(ret); | |
241 | secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, (unsigned char*)&vchSig[1], &rec, &sig); | |
1a9576de | 242 | assert(ret); |
dfa23b94 PW |
243 | assert(rec != -1); |
244 | vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); | |
245 | return true; | |
246 | } | |
096e06db | 247 | |
6e51b3bd | 248 | bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) { |
c1afe40a | 249 | if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size())) |
6e51b3bd | 250 | return false; |
6e51b3bd | 251 | fCompressed = vchPubKey.IsCompressed(); |
252 | fValid = true; | |
fda3fed1 | 253 | |
a42eef6f | 254 | if (fSkipCheck) |
255 | return true; | |
fda3fed1 | 256 | |
d0c41a73 | 257 | return VerifyPubKey(vchPubKey); |
6e51b3bd | 258 | } |
259 | ||
a5748996 | 260 | bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const { |
eb2c9990 PW |
261 | assert(IsValid()); |
262 | assert(IsCompressed()); | |
263 | unsigned char out[64]; | |
264 | LockObject(out); | |
265 | if ((nChild >> 31) == 0) { | |
266 | CPubKey pubkey = GetPubKey(); | |
877964c8 | 267 | assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE); |
eb2c9990 PW |
268 | BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out); |
269 | } else { | |
50fa0797 | 270 | assert(size() == 32); |
eb2c9990 PW |
271 | BIP32Hash(cc, nChild, 0, begin(), out); |
272 | } | |
a5748996 | 273 | memcpy(ccChild.begin(), out+32, 32); |
fda3fed1 | 274 | memcpy((unsigned char*)keyChild.begin(), begin(), 32); |
c1afe40a | 275 | bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), out); |
eb2c9990 PW |
276 | UnlockObject(out); |
277 | keyChild.fCompressed = true; | |
278 | keyChild.fValid = ret; | |
279 | return ret; | |
280 | } | |
281 | ||
eb2c9990 PW |
282 | bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const { |
283 | out.nDepth = nDepth + 1; | |
284 | CKeyID id = key.GetPubKey().GetID(); | |
285 | memcpy(&out.vchFingerprint[0], &id, 4); | |
286 | out.nChild = nChild; | |
a5748996 | 287 | return key.Derive(out.key, out.chaincode, nChild, chaincode); |
eb2c9990 PW |
288 | } |
289 | ||
290 | void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) { | |
977cdade | 291 | static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'}; |
eb2c9990 PW |
292 | unsigned char out[64]; |
293 | LockObject(out); | |
977cdade | 294 | CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out); |
eb2c9990 | 295 | key.Set(&out[0], &out[32], true); |
a5748996 | 296 | memcpy(chaincode.begin(), &out[32], 32); |
eb2c9990 PW |
297 | UnlockObject(out); |
298 | nDepth = 0; | |
299 | nChild = 0; | |
300 | memset(vchFingerprint, 0, sizeof(vchFingerprint)); | |
301 | } | |
302 | ||
303 | CExtPubKey CExtKey::Neuter() const { | |
304 | CExtPubKey ret; | |
305 | ret.nDepth = nDepth; | |
306 | memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4); | |
307 | ret.nChild = nChild; | |
308 | ret.pubkey = key.GetPubKey(); | |
a5748996 | 309 | ret.chaincode = chaincode; |
eb2c9990 PW |
310 | return ret; |
311 | } | |
312 | ||
313 | void CExtKey::Encode(unsigned char code[74]) const { | |
314 | code[0] = nDepth; | |
315 | memcpy(code+1, vchFingerprint, 4); | |
316 | code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF; | |
317 | code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF; | |
a5748996 | 318 | memcpy(code+9, chaincode.begin(), 32); |
eb2c9990 PW |
319 | code[41] = 0; |
320 | assert(key.size() == 32); | |
321 | memcpy(code+42, key.begin(), 32); | |
322 | } | |
323 | ||
324 | void CExtKey::Decode(const unsigned char code[74]) { | |
325 | nDepth = code[0]; | |
326 | memcpy(vchFingerprint, code+1, 4); | |
327 | nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8]; | |
a5748996 | 328 | memcpy(chaincode.begin(), code+9, 32); |
eb2c9990 PW |
329 | key.Set(code+42, code+74, true); |
330 | } | |
331 | ||
4a09e1df | 332 | bool ECC_InitSanityCheck() { |
f321d6bf PW |
333 | CKey key; |
334 | key.MakeNewKey(true); | |
335 | CPubKey pubkey = key.GetPubKey(); | |
336 | return key.VerifyPubKey(pubkey); | |
4a09e1df | 337 | } |
a56054be | 338 | |
a56054be | 339 | void ECC_Start() { |
c1afe40a | 340 | assert(secp256k1_context_sign == NULL); |
a56054be | 341 | |
c1afe40a | 342 | secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
a56054be PW |
343 | assert(ctx != NULL); |
344 | ||
345 | { | |
346 | // Pass in a random blinding seed to the secp256k1 context. | |
347 | unsigned char seed[32]; | |
348 | LockObject(seed); | |
349 | GetRandBytes(seed, 32); | |
350 | bool ret = secp256k1_context_randomize(ctx, seed); | |
351 | assert(ret); | |
352 | UnlockObject(seed); | |
353 | } | |
354 | ||
c1afe40a | 355 | secp256k1_context_sign = ctx; |
a56054be PW |
356 | } |
357 | ||
358 | void ECC_Stop() { | |
c1afe40a PW |
359 | secp256k1_context *ctx = secp256k1_context_sign; |
360 | secp256k1_context_sign = NULL; | |
a56054be PW |
361 | |
362 | if (ctx) { | |
363 | secp256k1_context_destroy(ctx); | |
364 | } | |
365 | } |