Skip to content

Commit 3769783

Browse files
committed
Restructure nonce clearing
Make sure we clear the nonce data even if the nonce function fails (it may have written partial data), and call memset only once in the case we iterate to produce a valid signature.
1 parent 0f9e69d commit 3769783

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/modules/recovery/main_impl.h

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,23 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecd
138138
secp256k1_scalar_set_b32(&sec, seckey, &overflow);
139139
/* Fail if the secret key is invalid. */
140140
if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
141+
unsigned char nonce32[32];
141142
unsigned int count = 0;
142143
secp256k1_scalar_set_b32(&msg, msg32, NULL);
143144
while (1) {
144-
unsigned char nonce32[32];
145145
ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
146146
if (!ret) {
147147
break;
148148
}
149149
secp256k1_scalar_set_b32(&non, nonce32, &overflow);
150-
memset(nonce32, 0, 32);
151150
if (!secp256k1_scalar_is_zero(&non) && !overflow) {
152151
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
153152
break;
154153
}
155154
}
156155
count++;
157156
}
157+
memset(nonce32, 0, 32);
158158
secp256k1_scalar_clear(&msg);
159159
secp256k1_scalar_clear(&non);
160160
secp256k1_scalar_clear(&sec);

0 commit comments

Comments
 (0)