Skip to content

Commit 6a62ca6

Browse files
Use secp256k1_mem_clear() to clear stack memory instead of memset()
All of the invocations of secp256k1_mem_clear() operate on stack memory and happen after the function is done with the memory object. This commit replaces existing memset() invocations and also adds secp256k1_mem_clear() to code locations where clearing was missing; there is no guarantee that this commit covers all code locations where clearing is necessary. Co-Authored-By: isle2983 <isle2983@yahoo.com>
1 parent 995fde9 commit 6a62ca6

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

src/ecmult_gen_impl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
151151
secp256k1_ge_from_storage(&add, &adds);
152152
secp256k1_gej_add_ge(r, r, &add);
153153
}
154-
bits = 0;
154+
secp256k1_mem_clear(&bits, sizeof(bits));
155155
secp256k1_ge_clear(&add);
156156
secp256k1_scalar_clear(&gnb);
157157
}
@@ -182,7 +182,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
182182
memcpy(keydata + 32, seed32, 32);
183183
}
184184
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
185-
memset(keydata, 0, sizeof(keydata));
185+
secp256k1_mem_clear(keydata, sizeof(keydata));
186186
/* Retry for out of range results to achieve uniformity. */
187187
do {
188188
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
@@ -199,11 +199,12 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
199199
retry = retry || secp256k1_scalar_is_zero(&b);
200200
} while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
201201
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
202-
memset(nonce32, 0, 32);
203202
secp256k1_ecmult_gen(ctx, &gb, &b);
204203
secp256k1_scalar_negate(&b, &b);
205204
ctx->blind = b;
206205
ctx->initial = gb;
206+
207+
secp256k1_mem_clear(nonce32, sizeof(nonce32));
207208
secp256k1_scalar_clear(&b);
208209
secp256k1_gej_clear(&gb);
209210
}

src/hash_impl.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out
161161
hash->s[i] = 0;
162162
}
163163
memcpy(out32, (const unsigned char*)out, 32);
164+
165+
secp256k1_mem_clear(sizedesc, sizeof(sizedesc));
166+
secp256k1_mem_clear(out, sizeof(out));
167+
secp256k1_mem_clear(hash, sizeof(secp256k1_sha256));
164168
}
165169

166170
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t keylen) {
@@ -188,7 +192,7 @@ static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const
188192
rkey[n] ^= 0x5c ^ 0x36;
189193
}
190194
secp256k1_sha256_write(&hash->inner, rkey, sizeof(rkey));
191-
memset(rkey, 0, sizeof(rkey));
195+
secp256k1_mem_clear(rkey, sizeof(rkey));
192196
}
193197

194198
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size) {
@@ -199,7 +203,7 @@ static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned
199203
unsigned char temp[32];
200204
secp256k1_sha256_finalize(&hash->inner, temp);
201205
secp256k1_sha256_write(&hash->outer, temp, 32);
202-
memset(temp, 0, 32);
206+
secp256k1_mem_clear(temp, sizeof(temp));
203207
secp256k1_sha256_finalize(&hash->outer, out32);
204208
}
205209

@@ -266,9 +270,7 @@ static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256
266270
}
267271

268272
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng) {
269-
memset(rng->k, 0, 32);
270-
memset(rng->v, 0, 32);
271-
rng->retry = 0;
273+
secp256k1_mem_clear(rng, sizeof(secp256k1_rfc6979_hmac_sha256));
272274
}
273275

274276
#undef BE32

src/modules/ecdh/main_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const se
5858
secp256k1_fe_get_b32(y, &pt.y);
5959

6060
ret = hashfp(output, x, y, data);
61+
secp256k1_mem_clear(x, sizeof(x));
62+
secp256k1_mem_clear(y, sizeof(y));
6163
}
6264

6365
secp256k1_scalar_clear(&s);
66+
secp256k1_ge_clear(&pt);
67+
6468
return ret;
6569
}
6670

src/modules/recovery/main_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context *ctx, cons
121121
}
122122

123123
int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
124-
secp256k1_scalar r, s;
125-
secp256k1_scalar sec, non, msg;
124+
secp256k1_scalar r, s, sec;
126125
int recid;
127126
int ret = 0;
128127
int overflow = 0;
@@ -138,6 +137,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecd
138137
secp256k1_scalar_set_b32(&sec, seckey, &overflow);
139138
/* Fail if the secret key is invalid. */
140139
if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
140+
secp256k1_scalar non, msg;
141141
unsigned char nonce32[32];
142142
unsigned int count = 0;
143143
secp256k1_scalar_set_b32(&msg, msg32, NULL);
@@ -154,11 +154,11 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecd
154154
}
155155
count++;
156156
}
157-
memset(nonce32, 0, 32);
157+
secp256k1_mem_clear(nonce32, sizeof(nonce32));
158158
secp256k1_scalar_clear(&msg);
159159
secp256k1_scalar_clear(&non);
160-
secp256k1_scalar_clear(&sec);
161160
}
161+
secp256k1_scalar_clear(&sec);
162162
if (ret) {
163163
secp256k1_ecdsa_recoverable_signature_save(signature, &r, &s, recid);
164164
} else {

src/num_gmp_impl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const sec
3939
if (len > shift) {
4040
memcpy(r + rlen - len + shift, tmp + shift, len - shift);
4141
}
42-
memset(tmp, 0, sizeof(tmp));
42+
secp256k1_mem_clear(tmp, sizeof(tmp));
4343
}
4444

4545
static void secp256k1_num_set_bin(secp256k1_num *r, const unsigned char *a, unsigned int alen) {
@@ -85,7 +85,7 @@ static void secp256k1_num_mod(secp256k1_num *r, const secp256k1_num *m) {
8585
if (r->limbs >= m->limbs) {
8686
mp_limb_t t[2*NUM_LIMBS];
8787
mpn_tdiv_qr(t, r->data, 0, r->data, r->limbs, m->data, m->limbs);
88-
memset(t, 0, sizeof(t));
88+
secp256k1_mem_clear(t, sizeof(t));
8989
r->limbs = m->limbs;
9090
while (r->limbs > 1 && r->data[r->limbs-1]==0) {
9191
r->limbs--;
@@ -139,9 +139,9 @@ static void secp256k1_num_mod_inverse(secp256k1_num *r, const secp256k1_num *a,
139139
} else {
140140
r->limbs = sn;
141141
}
142-
memset(g, 0, sizeof(g));
143-
memset(u, 0, sizeof(u));
144-
memset(v, 0, sizeof(v));
142+
secp256k1_mem_clear(g, sizeof(g));
143+
secp256k1_mem_clear(u, sizeof(u));
144+
secp256k1_mem_clear(v, sizeof(v));
145145
}
146146

147147
static int secp256k1_num_jacobi(const secp256k1_num *a, const secp256k1_num *b) {
@@ -256,7 +256,7 @@ static void secp256k1_num_mul(secp256k1_num *r, const secp256k1_num *a, const se
256256
VERIFY_CHECK(r->limbs <= 2*NUM_LIMBS);
257257
mpn_copyi(r->data, tmp, r->limbs);
258258
r->neg = a->neg ^ b->neg;
259-
memset(tmp, 0, sizeof(tmp));
259+
secp256k1_mem_clear(tmp, sizeof(tmp));
260260
}
261261

262262
static void secp256k1_num_shift(secp256k1_num *r, int bits) {

src/secp256k1.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *m
435435
buffer_append(keydata, &offset, algo16, 16);
436436
}
437437
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
438-
memset(keydata, 0, sizeof(keydata));
438+
secp256k1_mem_clear(keydata, sizeof(keydata));
439439
for (i = 0; i <= counter; i++) {
440440
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
441441
}
@@ -447,8 +447,7 @@ const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function
447447
const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
448448

449449
int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
450-
secp256k1_scalar r, s;
451-
secp256k1_scalar sec, non, msg;
450+
secp256k1_scalar r, s, sec;
452451
int ret = 0;
453452
int overflow = 0;
454453
VERIFY_CHECK(ctx != NULL);
@@ -463,6 +462,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
463462
secp256k1_scalar_set_b32(&sec, seckey, &overflow);
464463
/* Fail if the secret key is invalid. */
465464
if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
465+
secp256k1_scalar non, msg;
466466
unsigned char nonce32[32];
467467
unsigned int count = 0;
468468
secp256k1_scalar_set_b32(&msg, msg32, NULL);
@@ -479,11 +479,11 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
479479
}
480480
count++;
481481
}
482-
memset(nonce32, 0, 32);
482+
secp256k1_mem_clear(nonce32, sizeof(nonce32));
483483
secp256k1_scalar_clear(&msg);
484484
secp256k1_scalar_clear(&non);
485-
secp256k1_scalar_clear(&sec);
486485
}
486+
secp256k1_scalar_clear(&sec);
487487
if (ret) {
488488
secp256k1_ecdsa_signature_save(signature, &r, &s);
489489
} else {

0 commit comments

Comments
 (0)