Skip to content

Commit 3148b28

Browse files
isle2983real-or-random
authored andcommitted
Use SECP256K1_CLEANSE() to zero stack memory instead of memset()
All of these conversions: 1) operate on stack memory. 2) happen after the function is done with the variable 3) had an existing memset() action to be replaced These were found by visual inspection and may not be the total set of places where SECP256K1_CLEANSE should ideally be applied.
1 parent 11bef89 commit 3148b28

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

src/ecmult_gen_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
176176
memcpy(keydata + 32, seed32, 32);
177177
}
178178
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
179-
memset(keydata, 0, sizeof(keydata));
179+
SECP256K1_CLEANSE(keydata);
180180
/* Retry for out of range results to achieve uniformity. */
181181
do {
182182
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
@@ -193,7 +193,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
193193
retry = retry || secp256k1_scalar_is_zero(&b);
194194
} while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
195195
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
196-
memset(nonce32, 0, 32);
196+
SECP256K1_CLEANSE(nonce32);
197197
secp256k1_ecmult_gen(ctx, &gb, &b);
198198
secp256k1_scalar_negate(&b, &b);
199199
ctx->blind = b;

src/hash_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const
188188
rkey[n] ^= 0x5c ^ 0x36;
189189
}
190190
secp256k1_sha256_write(&hash->inner, rkey, sizeof(rkey));
191-
memset(rkey, 0, sizeof(rkey));
191+
SECP256K1_CLEANSE(rkey);
192192
}
193193

194194
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size) {
@@ -199,7 +199,7 @@ static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned
199199
unsigned char temp[32];
200200
secp256k1_sha256_finalize(&hash->inner, temp);
201201
secp256k1_sha256_write(&hash->outer, temp, 32);
202-
memset(temp, 0, 32);
202+
SECP256K1_CLEANSE(temp);
203203
secp256k1_sha256_finalize(&hash->outer, out32);
204204
}
205205

src/modules/recovery/main_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecd
154154
}
155155
count++;
156156
}
157-
memset(nonce32, 0, 32);
157+
SECP256K1_CLEANSE(nonce32);
158158
SECP256K1_CLEANSE(msg);
159159
SECP256K1_CLEANSE(non);
160160
SECP256K1_CLEANSE(sec);

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_CLEANSE(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_CLEANSE(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_CLEANSE(g);
143+
SECP256K1_CLEANSE(u);
144+
SECP256K1_CLEANSE(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_CLEANSE(tmp);
260260
}
261261

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

src/secp256k1.c

Lines changed: 2 additions & 2 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_CLEANSE(keydata);
439439
for (i = 0; i <= counter; i++) {
440440
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
441441
}
@@ -479,7 +479,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
479479
}
480480
count++;
481481
}
482-
memset(nonce32, 0, 32);
482+
SECP256K1_CLEANSE(nonce32);
483483
SECP256K1_CLEANSE(msg);
484484
SECP256K1_CLEANSE(non);
485485
SECP256K1_CLEANSE(sec);

0 commit comments

Comments
 (0)