@@ -87,9 +87,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
87
87
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
88
88
* included.
89
89
*
90
+ * privkey must point to an output buffer of length at least PRIVATE_KEY_SIZE bytes.
91
+ * privkeylen must initially be set to the size of the privkey buffer. Upon return it
92
+ * will be set to the number of bytes used in the buffer.
90
93
* key32 must point to a 32-byte raw private key.
91
94
*/
92
95
static int ec_privkey_export_der (const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
96
+ assert (*privkeylen >= PRIVATE_KEY_SIZE);
93
97
secp256k1_pubkey pubkey;
94
98
size_t pubkeylen = 0 ;
95
99
if (!secp256k1_ec_pubkey_create (ctx, &pubkey, key32)) {
@@ -115,10 +119,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
115
119
memcpy (ptr, begin, sizeof (begin)); ptr += sizeof (begin);
116
120
memcpy (ptr, key32, 32 ); ptr += 32 ;
117
121
memcpy (ptr, middle, sizeof (middle)); ptr += sizeof (middle);
118
- pubkeylen = 33 ;
122
+ pubkeylen = COMPRESSED_PUBLIC_KEY_SIZE ;
119
123
secp256k1_ec_pubkey_serialize (ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
120
124
ptr += pubkeylen;
121
125
*privkeylen = ptr - privkey;
126
+ assert (*privkeylen == COMPRESSED_PRIVATE_KEY_SIZE);
122
127
} else {
123
128
static const unsigned char begin[] = {
124
129
0x30 ,0x82 ,0x01 ,0x13 ,0x02 ,0x01 ,0x01 ,0x04 ,0x20
@@ -140,10 +145,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
140
145
memcpy (ptr, begin, sizeof (begin)); ptr += sizeof (begin);
141
146
memcpy (ptr, key32, 32 ); ptr += 32 ;
142
147
memcpy (ptr, middle, sizeof (middle)); ptr += sizeof (middle);
143
- pubkeylen = 65 ;
148
+ pubkeylen = PUBLIC_KEY_SIZE ;
144
149
secp256k1_ec_pubkey_serialize (ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
145
150
ptr += pubkeylen;
146
151
*privkeylen = ptr - privkey;
152
+ assert (*privkeylen == PRIVATE_KEY_SIZE);
147
153
}
148
154
return 1 ;
149
155
}
@@ -165,8 +171,8 @@ CPrivKey CKey::GetPrivKey() const {
165
171
CPrivKey privkey;
166
172
int ret;
167
173
size_t privkeylen;
168
- privkey.resize (279 );
169
- privkeylen = 279 ;
174
+ privkey.resize (PRIVATE_KEY_SIZE );
175
+ privkeylen = PRIVATE_KEY_SIZE ;
170
176
ret = ec_privkey_export_der (secp256k1_context_sign, (unsigned char *) privkey.data (), &privkeylen, begin (), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
171
177
assert (ret);
172
178
privkey.resize (privkeylen);
@@ -176,7 +182,7 @@ CPrivKey CKey::GetPrivKey() const {
176
182
CPubKey CKey::GetPubKey () const {
177
183
assert (fValid );
178
184
secp256k1_pubkey pubkey;
179
- size_t clen = 65 ;
185
+ size_t clen = PUBLIC_KEY_SIZE ;
180
186
CPubKey result;
181
187
int ret = secp256k1_ec_pubkey_create (secp256k1_context_sign, &pubkey, begin ());
182
188
assert (ret);
@@ -189,8 +195,8 @@ CPubKey CKey::GetPubKey() const {
189
195
bool CKey::Sign (const uint256 &hash, std::vector<unsigned char >& vchSig, uint32_t test_case) const {
190
196
if (!fValid )
191
197
return false ;
192
- vchSig.resize (72 );
193
- size_t nSigLen = 72 ;
198
+ vchSig.resize (SIGNATURE_SIZE );
199
+ size_t nSigLen = SIGNATURE_SIZE ;
194
200
unsigned char extra_entropy[32 ] = {0 };
195
201
WriteLE32 (extra_entropy, test_case);
196
202
secp256k1_ecdsa_signature sig;
@@ -218,7 +224,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
218
224
bool CKey::SignCompact (const uint256 &hash, std::vector<unsigned char >& vchSig) const {
219
225
if (!fValid )
220
226
return false ;
221
- vchSig.resize (65 );
227
+ vchSig.resize (COMPACT_SIGNATURE_SIZE );
222
228
int rec = -1 ;
223
229
secp256k1_ecdsa_recoverable_signature sig;
224
230
int ret = secp256k1_ecdsa_sign_recoverable (secp256k1_context_sign, &sig, hash.begin (), begin (), secp256k1_nonce_function_rfc6979, NULL );
@@ -248,7 +254,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
248
254
std::vector<unsigned char , secure_allocator<unsigned char >> vout (64 );
249
255
if ((nChild >> 31 ) == 0 ) {
250
256
CPubKey pubkey = GetPubKey ();
251
- assert (pubkey.size () == 33 );
257
+ assert (pubkey.size () == COMPRESSED_PUBLIC_KEY_SIZE );
252
258
BIP32Hash (cc, nChild, *pubkey.begin (), pubkey.begin ()+1 , vout.data ());
253
259
} else {
254
260
assert (size () == 32 );
0 commit comments