Skip to content

Commit 9acb45d

Browse files
committed
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 95824cd commit 9acb45d

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
@@ -178,7 +178,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
178178
memcpy(keydata + 32, seed32, 32);
179179
}
180180
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
181-
memset(keydata, 0, sizeof(keydata));
181+
SECP256K1_CLEANSE(keydata);
182182
/* Retry for out of range results to achieve uniformity. */
183183
do {
184184
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
@@ -195,7 +195,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
195195
retry |= secp256k1_scalar_is_zero(&b);
196196
} while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
197197
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
198-
memset(nonce32, 0, 32);
198+
SECP256K1_CLEANSE(nonce32);
199199
secp256k1_ecmult_gen(ctx, &gb, &b);
200200
secp256k1_scalar_negate(&b, &b);
201201
ctx->blind = b;

src/hash_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256_t *hash, cons
186186
rkey[n] ^= 0x5c ^ 0x36;
187187
}
188188
secp256k1_sha256_write(&hash->inner, rkey, 64);
189-
memset(rkey, 0, 64);
189+
SECP256K1_CLEANSE(rkey);
190190
}
191191

192192
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256_t *hash, const unsigned char *data, size_t size) {
@@ -197,7 +197,7 @@ static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256_t *hash, unsign
197197
unsigned char temp[32];
198198
secp256k1_sha256_finalize(&hash->inner, temp);
199199
secp256k1_sha256_write(&hash->outer, temp, 32);
200-
memset(temp, 0, 32);
200+
SECP256K1_CLEANSE(temp);
201201
secp256k1_sha256_finalize(&hash->outer, out32);
202202
}
203203

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
@@ -335,7 +335,7 @@ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *m
335335
keylen += 16;
336336
}
337337
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, keylen);
338-
memset(keydata, 0, sizeof(keydata));
338+
SECP256K1_CLEANSE(keydata);
339339
for (i = 0; i <= counter; i++) {
340340
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
341341
}
@@ -379,7 +379,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
379379
}
380380
count++;
381381
}
382-
memset(nonce32, 0, 32);
382+
SECP256K1_CLEANSE(nonce32);
383383
SECP256K1_CLEANSE(msg);
384384
SECP256K1_CLEANSE(non);
385385
SECP256K1_CLEANSE(sec);

0 commit comments

Comments
 (0)