1
1
// Copyright (c) 2009-2016 The Bitcoin Core developers
2
+ // Copyright (c) 2017 The Zcash developers
2
3
// Distributed under the MIT software license, see the accompanying
3
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
15
16
static secp256k1_context* secp256k1_context_sign = nullptr ;
16
17
17
18
/* * These functions are taken from the libsecp256k1 distribution and are very ugly. */
19
+
20
+ /* *
21
+ * This parses a format loosely based on a DER encoding of the ECPrivateKey type from
22
+ * section C.4 of SEC 1 <http://www.secg.org/sec1-v2.pdf>, with the following caveats:
23
+ *
24
+ * * The octet-length of the SEQUENCE must be encoded as 1 or 2 octets. It is not
25
+ * required to be encoded as one octet if it is less than 256, as DER would require.
26
+ * * The octet-length of the SEQUENCE must not be greater than the remaining
27
+ * length of the key encoding, but need not match it (i.e. the encoding may contain
28
+ * junk after the encoded SEQUENCE).
29
+ * * The privateKey OCTET STRING is zero-filled on the left to 32 octets.
30
+ * * Anything after the encoding of the privateKey OCTET STRING is ignored, whether
31
+ * or not it is validly encoded DER.
32
+ *
33
+ * out32 must point to an output buffer of length at least 32 bytes.
34
+ */
18
35
static int ec_privkey_import_der (const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
19
36
const unsigned char *end = privkey + privkeylen;
20
- int lenb = 0 ;
21
- int len = 0 ;
22
37
memset (out32, 0 , 32 );
23
38
/* sequence header */
24
- if (end < privkey+ 1 || *privkey != 0x30 ) {
39
+ if (end - privkey < 1 || *privkey != 0x30u ) {
25
40
return 0 ;
26
41
}
27
42
privkey++;
28
43
/* sequence length constructor */
29
- if (end < privkey+ 1 || !(*privkey & 0x80 )) {
44
+ if (end - privkey < 1 || !(*privkey & 0x80u )) {
30
45
return 0 ;
31
46
}
32
- lenb = *privkey & ~0x80 ; privkey++;
47
+ size_t lenb = *privkey & ~0x80u ; privkey++;
33
48
if (lenb < 1 || lenb > 2 ) {
34
49
return 0 ;
35
50
}
36
- if (end < privkey+ lenb) {
51
+ if (end - privkey < lenb) {
37
52
return 0 ;
38
53
}
39
54
/* sequence length */
40
- len = privkey[lenb-1 ] | (lenb > 1 ? privkey[lenb-2 ] << 8 : 0 );
55
+ size_t len = privkey[lenb-1 ] | (lenb > 1 ? privkey[lenb-2 ] << 8 : 0u );
41
56
privkey += lenb;
42
- if (end < privkey+ len) {
57
+ if (end - privkey < len) {
43
58
return 0 ;
44
59
}
45
60
/* sequence element 0: version number (=1) */
46
- if (end < privkey+ 3 || privkey[0 ] != 0x02 || privkey[1 ] != 0x01 || privkey[2 ] != 0x01 ) {
61
+ if (end - privkey < 3 || privkey[0 ] != 0x02u || privkey[1 ] != 0x01u || privkey[2 ] != 0x01u ) {
47
62
return 0 ;
48
63
}
49
64
privkey += 3 ;
50
65
/* sequence element 1: octet string, up to 32 bytes */
51
- if (end < privkey+2 || privkey[0 ] != 0x04 || privkey[1 ] > 0x20 || end < privkey+2 +privkey[1 ]) {
66
+ if (end - privkey < 2 || privkey[0 ] != 0x04u ) {
67
+ return 0 ;
68
+ }
69
+ size_t oslen = privkey[1 ];
70
+ privkey += 2 ;
71
+ if (oslen > 32 || end - privkey < oslen) {
52
72
return 0 ;
53
73
}
54
- memcpy (out32 + 32 - privkey[ 1 ] , privkey + 2 , privkey[ 1 ] );
74
+ memcpy (out32 + ( 32 - oslen) , privkey, oslen );
55
75
if (!secp256k1_ec_seckey_verify (ctx, out32)) {
56
76
memset (out32, 0 , 32 );
57
77
return 0 ;
58
78
}
59
79
return 1 ;
60
80
}
61
81
82
+ /* *
83
+ * This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1
84
+ * <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
85
+ * included.
86
+ *
87
+ * privkey must point to an output buffer of length at least CKey::PRIVATE_KEY_SIZE bytes.
88
+ * privkeylen must initially be set to the size of the privkey buffer. Upon return it
89
+ * will be set to the number of bytes used in the buffer.
90
+ * key32 must point to a 32-byte raw private key.
91
+ */
62
92
static int ec_privkey_export_der (const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
93
+ assert (*privkeylen >= CKey::PRIVATE_KEY_SIZE);
63
94
secp256k1_pubkey pubkey;
64
95
size_t pubkeylen = 0 ;
65
96
if (!secp256k1_ec_pubkey_create (ctx, &pubkey, key32)) {
@@ -85,10 +116,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
85
116
memcpy (ptr, begin, sizeof (begin)); ptr += sizeof (begin);
86
117
memcpy (ptr, key32, 32 ); ptr += 32 ;
87
118
memcpy (ptr, middle, sizeof (middle)); ptr += sizeof (middle);
88
- pubkeylen = 33 ;
119
+ pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE ;
89
120
secp256k1_ec_pubkey_serialize (ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
90
121
ptr += pubkeylen;
91
122
*privkeylen = ptr - privkey;
123
+ assert (*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
92
124
} else {
93
125
static const unsigned char begin[] = {
94
126
0x30 ,0x82 ,0x01 ,0x13 ,0x02 ,0x01 ,0x01 ,0x04 ,0x20
@@ -110,10 +142,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
110
142
memcpy (ptr, begin, sizeof (begin)); ptr += sizeof (begin);
111
143
memcpy (ptr, key32, 32 ); ptr += 32 ;
112
144
memcpy (ptr, middle, sizeof (middle)); ptr += sizeof (middle);
113
- pubkeylen = 65 ;
145
+ pubkeylen = CPubKey::PUBLIC_KEY_SIZE ;
114
146
secp256k1_ec_pubkey_serialize (ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
115
147
ptr += pubkeylen;
116
148
*privkeylen = ptr - privkey;
149
+ assert (*privkeylen == CKey::PRIVATE_KEY_SIZE);
117
150
}
118
151
return 1 ;
119
152
}
@@ -135,8 +168,8 @@ CPrivKey CKey::GetPrivKey() const {
135
168
CPrivKey privkey;
136
169
int ret;
137
170
size_t privkeylen;
138
- privkey.resize (279 );
139
- privkeylen = 279 ;
171
+ privkey.resize (PRIVATE_KEY_SIZE );
172
+ privkeylen = PRIVATE_KEY_SIZE ;
140
173
ret = ec_privkey_export_der (secp256k1_context_sign, (unsigned char *) privkey.data (), &privkeylen, begin (), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
141
174
assert (ret);
142
175
privkey.resize (privkeylen);
@@ -146,7 +179,7 @@ CPrivKey CKey::GetPrivKey() const {
146
179
CPubKey CKey::GetPubKey () const {
147
180
assert (fValid );
148
181
secp256k1_pubkey pubkey;
149
- size_t clen = 65 ;
182
+ size_t clen = CPubKey::PUBLIC_KEY_SIZE ;
150
183
CPubKey result;
151
184
int ret = secp256k1_ec_pubkey_create (secp256k1_context_sign, &pubkey, begin ());
152
185
assert (ret);
@@ -159,8 +192,8 @@ CPubKey CKey::GetPubKey() const {
159
192
bool CKey::Sign (const uint256 &hash, std::vector<unsigned char >& vchSig, uint32_t test_case) const {
160
193
if (!fValid )
161
194
return false ;
162
- vchSig.resize (72 );
163
- size_t nSigLen = 72 ;
195
+ vchSig.resize (CPubKey::SIGNATURE_SIZE );
196
+ size_t nSigLen = CPubKey::SIGNATURE_SIZE ;
164
197
unsigned char extra_entropy[32 ] = {0 };
165
198
WriteLE32 (extra_entropy, test_case);
166
199
secp256k1_ecdsa_signature sig;
@@ -188,7 +221,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
188
221
bool CKey::SignCompact (const uint256 &hash, std::vector<unsigned char >& vchSig) const {
189
222
if (!fValid )
190
223
return false ;
191
- vchSig.resize (65 );
224
+ vchSig.resize (CPubKey::COMPACT_SIGNATURE_SIZE );
192
225
int rec = -1 ;
193
226
secp256k1_ecdsa_recoverable_signature sig;
194
227
int ret = secp256k1_ecdsa_sign_recoverable (secp256k1_context_sign, &sig, hash.begin (), begin (), secp256k1_nonce_function_rfc6979, nullptr );
@@ -218,10 +251,10 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
218
251
std::vector<unsigned char , secure_allocator<unsigned char >> vout (64 );
219
252
if ((nChild >> 31 ) == 0 ) {
220
253
CPubKey pubkey = GetPubKey ();
221
- assert (pubkey.begin () + 33 == pubkey. end () );
254
+ assert (pubkey.size () == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE );
222
255
BIP32Hash (cc, nChild, *pubkey.begin (), pubkey.begin ()+1 , vout.data ());
223
256
} else {
224
- assert (begin () + 32 == end () );
257
+ assert (size () == 32 );
225
258
BIP32Hash (cc, nChild, 0 , begin (), vout.data ());
226
259
}
227
260
memcpy (ccChild.begin (), vout.data ()+32 , 32 );
0 commit comments