Skip to content

Commit 17fa391

Browse files
committed
Specify ECDSA constant sizes as constants
1 parent e4a1086 commit 17fa391

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

src/key.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
8787
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
8888
* included.
8989
*
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.
9093
* key32 must point to a 32-byte raw private key.
9194
*/
9295
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);
9397
secp256k1_pubkey pubkey;
9498
size_t pubkeylen = 0;
9599
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
115119
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
116120
memcpy(ptr, key32, 32); ptr += 32;
117121
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
118-
pubkeylen = 33;
122+
pubkeylen = COMPRESSED_PUBLIC_KEY_SIZE;
119123
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
120124
ptr += pubkeylen;
121125
*privkeylen = ptr - privkey;
126+
assert(*privkeylen == COMPRESSED_PRIVATE_KEY_SIZE);
122127
} else {
123128
static const unsigned char begin[] = {
124129
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
140145
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
141146
memcpy(ptr, key32, 32); ptr += 32;
142147
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
143-
pubkeylen = 65;
148+
pubkeylen = PUBLIC_KEY_SIZE;
144149
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
145150
ptr += pubkeylen;
146151
*privkeylen = ptr - privkey;
152+
assert(*privkeylen == PRIVATE_KEY_SIZE);
147153
}
148154
return 1;
149155
}
@@ -165,8 +171,8 @@ CPrivKey CKey::GetPrivKey() const {
165171
CPrivKey privkey;
166172
int ret;
167173
size_t privkeylen;
168-
privkey.resize(279);
169-
privkeylen = 279;
174+
privkey.resize(PRIVATE_KEY_SIZE);
175+
privkeylen = PRIVATE_KEY_SIZE;
170176
ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*) privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
171177
assert(ret);
172178
privkey.resize(privkeylen);
@@ -176,7 +182,7 @@ CPrivKey CKey::GetPrivKey() const {
176182
CPubKey CKey::GetPubKey() const {
177183
assert(fValid);
178184
secp256k1_pubkey pubkey;
179-
size_t clen = 65;
185+
size_t clen = PUBLIC_KEY_SIZE;
180186
CPubKey result;
181187
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
182188
assert(ret);
@@ -189,8 +195,8 @@ CPubKey CKey::GetPubKey() const {
189195
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
190196
if (!fValid)
191197
return false;
192-
vchSig.resize(72);
193-
size_t nSigLen = 72;
198+
vchSig.resize(SIGNATURE_SIZE);
199+
size_t nSigLen = SIGNATURE_SIZE;
194200
unsigned char extra_entropy[32] = {0};
195201
WriteLE32(extra_entropy, test_case);
196202
secp256k1_ecdsa_signature sig;
@@ -218,7 +224,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
218224
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
219225
if (!fValid)
220226
return false;
221-
vchSig.resize(65);
227+
vchSig.resize(COMPACT_SIGNATURE_SIZE);
222228
int rec = -1;
223229
secp256k1_ecdsa_recoverable_signature sig;
224230
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
248254
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
249255
if ((nChild >> 31) == 0) {
250256
CPubKey pubkey = GetPubKey();
251-
assert(pubkey.size() == 33);
257+
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
252258
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
253259
} else {
254260
assert(size() == 32);

src/key.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
22
// Copyright (c) 2009-2016 The Bitcoin Core developers
3+
// Copyright (c) 2017 The Zcash developers
34
// Distributed under the MIT software license, see the accompanying
45
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
56

@@ -17,17 +18,18 @@
1718

1819
/**
1920
* secp256k1:
20-
* const unsigned int PRIVATE_KEY_SIZE = 279;
21-
* const unsigned int PUBLIC_KEY_SIZE = 65;
22-
* const unsigned int SIGNATURE_SIZE = 72;
23-
*
21+
*/
22+
const unsigned int PRIVATE_KEY_SIZE = 279;
23+
const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
24+
/**
2425
* see www.keylength.com
2526
* script supports up to 75 for single byte push
2627
*/
2728

2829
/**
2930
* secure_allocator is defined in allocators.h
30-
* CPrivKey is a serialized private key, with all parameters included (279 bytes)
31+
* CPrivKey is a serialized private key, with all parameters included
32+
* (PRIVATE_KEY_SIZE bytes)
3133
*/
3234
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
3335

src/pubkey.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchS
185185
}
186186

187187
bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
188-
if (vchSig.size() != 65)
188+
if (vchSig.size() != COMPACT_SIGNATURE_SIZE)
189189
return false;
190190
int recid = (vchSig[0] - 27) & 3;
191191
bool fComp = ((vchSig[0] - 27) & 4) != 0;
@@ -197,8 +197,8 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
197197
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
198198
return false;
199199
}
200-
unsigned char pub[65];
201-
size_t publen = 65;
200+
unsigned char pub[PUBLIC_KEY_SIZE];
201+
size_t publen = PUBLIC_KEY_SIZE;
202202
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
203203
Set(pub, pub + publen);
204204
return true;
@@ -218,8 +218,8 @@ bool CPubKey::Decompress() {
218218
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, &(*this)[0], size())) {
219219
return false;
220220
}
221-
unsigned char pub[65];
222-
size_t publen = 65;
221+
unsigned char pub[PUBLIC_KEY_SIZE];
222+
size_t publen = PUBLIC_KEY_SIZE;
223223
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
224224
Set(pub, pub + publen);
225225
return true;
@@ -228,7 +228,7 @@ bool CPubKey::Decompress() {
228228
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
229229
assert(IsValid());
230230
assert((nChild >> 31) == 0);
231-
assert(size() == 33);
231+
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE);
232232
unsigned char out[64];
233233
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
234234
memcpy(ccChild.begin(), out+32, 32);
@@ -239,8 +239,8 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
239239
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
240240
return false;
241241
}
242-
unsigned char pub[33];
243-
size_t publen = 33;
242+
unsigned char pub[COMPRESSED_PUBLIC_KEY_SIZE];
243+
size_t publen = COMPRESSED_PUBLIC_KEY_SIZE;
244244
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
245245
pubkeyChild.Set(pub, pub + publen);
246246
return true;
@@ -252,8 +252,8 @@ void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
252252
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
253253
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
254254
memcpy(code+9, chaincode.begin(), 32);
255-
assert(pubkey.size() == 33);
256-
memcpy(code+41, pubkey.begin(), 33);
255+
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
256+
memcpy(code+41, pubkey.begin(), COMPRESSED_PUBLIC_KEY_SIZE);
257257
}
258258

259259
void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {

src/pubkey.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
22
// Copyright (c) 2009-2016 The Bitcoin Core developers
3+
// Copyright (c) 2017 The Zcash developers
34
// Distributed under the MIT software license, see the accompanying
45
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
56

@@ -15,10 +16,12 @@
1516

1617
/**
1718
* secp256k1:
18-
* const unsigned int PRIVATE_KEY_SIZE = 279;
19-
* const unsigned int PUBLIC_KEY_SIZE = 65;
20-
* const unsigned int SIGNATURE_SIZE = 72;
21-
*
19+
*/
20+
const unsigned int PUBLIC_KEY_SIZE = 65;
21+
const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
22+
const unsigned int SIGNATURE_SIZE = 72;
23+
const unsigned int COMPACT_SIGNATURE_SIZE = 65;
24+
/**
2225
* see www.keylength.com
2326
* script supports up to 75 for single byte push
2427
*/
@@ -44,15 +47,15 @@ class CPubKey
4447
* Just store the serialized data.
4548
* Its length can very cheaply be computed from the first byte.
4649
*/
47-
unsigned char vch[65];
50+
unsigned char vch[PUBLIC_KEY_SIZE];
4851

4952
//! Compute the length of a pubkey with a given first byte.
5053
unsigned int static GetLen(unsigned char chHeader)
5154
{
5255
if (chHeader == 2 || chHeader == 3)
53-
return 33;
56+
return COMPRESSED_PUBLIC_KEY_SIZE;
5457
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
55-
return 65;
58+
return PUBLIC_KEY_SIZE;
5659
return 0;
5760
}
5861

@@ -127,7 +130,7 @@ class CPubKey
127130
void Unserialize(Stream& s)
128131
{
129132
unsigned int len = ::ReadCompactSize(s);
130-
if (len <= 65) {
133+
if (len <= PUBLIC_KEY_SIZE) {
131134
s.read((char*)vch, len);
132135
} else {
133136
// invalid pubkey, skip available data
@@ -166,7 +169,7 @@ class CPubKey
166169
//! Check whether this is a compressed public key.
167170
bool IsCompressed() const
168171
{
169-
return size() == 33;
172+
return size() == COMPRESSED_PUBLIC_KEY_SIZE;
170173
}
171174

172175
/**

0 commit comments

Comments
 (0)