Skip to content

Commit fb46c83

Browse files
committedDec 21, 2017
Merge bitcoin-core#463: Reduce usage of hardcoded size constants
c7680e5 Reduce usage of hardcoded size constants (Thomas Snider) Pull request description: In particular the usage of keylen in nonce_function_rfc6979 seemed precarious - in one conditional it was unconditionally set, then in the next it was added to. While it was clearly correct as written, I think this change makes it easier to reason about for new eyes and more resistant to breakage if there is any future change to what gets fed into the PRNG. Tree-SHA512: 2241c183acc0f318f85a11ccff7fe28de7777bc53dea93ab8308bad15871047a268c6a2b36f77a599dce536fca48ab305ea746223840bc10953c893daffa0a50
2 parents 02f5001 + c7680e5 commit fb46c83

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed
 

‎src/hash_impl.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *
133133
hash->bytes += len;
134134
while (bufsize + len >= 64) {
135135
/* Fill the buffer, and process it. */
136-
memcpy(((unsigned char*)hash->buf) + bufsize, data, 64 - bufsize);
137-
data += 64 - bufsize;
138-
len -= 64 - bufsize;
136+
size_t chunk_len = 64 - bufsize;
137+
memcpy(((unsigned char*)hash->buf) + bufsize, data, chunk_len);
138+
data += chunk_len;
139+
len -= chunk_len;
139140
secp256k1_sha256_transform(hash->s, hash->buf);
140141
bufsize = 0;
141142
}
@@ -162,11 +163,11 @@ static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out
162163
}
163164

164165
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t keylen) {
165-
int n;
166+
size_t n;
166167
unsigned char rkey[64];
167-
if (keylen <= 64) {
168+
if (keylen <= sizeof(rkey)) {
168169
memcpy(rkey, key, keylen);
169-
memset(rkey + keylen, 0, 64 - keylen);
170+
memset(rkey + keylen, 0, sizeof(rkey) - keylen);
170171
} else {
171172
secp256k1_sha256 sha256;
172173
secp256k1_sha256_initialize(&sha256);
@@ -176,17 +177,17 @@ static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const
176177
}
177178

178179
secp256k1_sha256_initialize(&hash->outer);
179-
for (n = 0; n < 64; n++) {
180+
for (n = 0; n < sizeof(rkey); n++) {
180181
rkey[n] ^= 0x5c;
181182
}
182-
secp256k1_sha256_write(&hash->outer, rkey, 64);
183+
secp256k1_sha256_write(&hash->outer, rkey, sizeof(rkey));
183184

184185
secp256k1_sha256_initialize(&hash->inner);
185-
for (n = 0; n < 64; n++) {
186+
for (n = 0; n < sizeof(rkey); n++) {
186187
rkey[n] ^= 0x5c ^ 0x36;
187188
}
188-
secp256k1_sha256_write(&hash->inner, rkey, 64);
189-
memset(rkey, 0, 64);
189+
secp256k1_sha256_write(&hash->inner, rkey, sizeof(rkey));
190+
memset(rkey, 0, sizeof(rkey));
190191
}
191192

192193
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size) {

‎src/secp256k1.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge,
132132
* representation inside secp256k1_pubkey, as conversion is very fast.
133133
* Note that secp256k1_pubkey_save must use the same representation. */
134134
secp256k1_ge_storage s;
135-
memcpy(&s, &pubkey->data[0], 64);
135+
memcpy(&s, &pubkey->data[0], sizeof(s));
136136
secp256k1_ge_from_storage(ge, &s);
137137
} else {
138138
/* Otherwise, fall back to 32-byte big endian for X and Y. */
@@ -149,7 +149,7 @@ static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
149149
if (sizeof(secp256k1_ge_storage) == 64) {
150150
secp256k1_ge_storage s;
151151
secp256k1_ge_to_storage(&s, ge);
152-
memcpy(&pubkey->data[0], &s, 64);
152+
memcpy(&pubkey->data[0], &s, sizeof(s));
153153
} else {
154154
VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
155155
secp256k1_fe_normalize_var(&ge->x);
@@ -319,9 +319,14 @@ int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_s
319319
secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
320320
}
321321

322+
static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
323+
memcpy(buf + *offset, data, len);
324+
*offset += len;
325+
}
326+
322327
static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
323328
unsigned char keydata[112];
324-
int keylen = 64;
329+
unsigned int offset = 0;
325330
secp256k1_rfc6979_hmac_sha256 rng;
326331
unsigned int i;
327332
/* We feed a byte array to the PRNG as input, consisting of:
@@ -332,17 +337,15 @@ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *m
332337
* different argument mixtures to emulate each other and result in the same
333338
* nonces.
334339
*/
335-
memcpy(keydata, key32, 32);
336-
memcpy(keydata + 32, msg32, 32);
340+
buffer_append(keydata, &offset, key32, 32);
341+
buffer_append(keydata, &offset, msg32, 32);
337342
if (data != NULL) {
338-
memcpy(keydata + 64, data, 32);
339-
keylen = 96;
343+
buffer_append(keydata, &offset, data, 32);
340344
}
341345
if (algo16 != NULL) {
342-
memcpy(keydata + keylen, algo16, 16);
343-
keylen += 16;
346+
buffer_append(keydata, &offset, algo16, 16);
344347
}
345-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, keylen);
348+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
346349
memset(keydata, 0, sizeof(keydata));
347350
for (i = 0; i <= counter; i++) {
348351
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);

0 commit comments

Comments
 (0)
Please sign in to comment.