Skip to content

Commit 8ea06d4

Browse files
committed
DSA Parameter Generation: init g earlier
Ensure dsa->g is initialized with other mp_ints so that it can be cleared at the end regardless of failures. Don't clear tmp or tmp2 if allocation or initialization failed as you will access uninitialized data.
1 parent 19cba1c commit 8ea06d4

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

wolfcrypt/src/dsa.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
323323

324324
if (err == MP_OKAY)
325325
#endif
326-
err = mp_init_multi(tmp, tmp2, &dsa->p, &dsa->q, 0, 0);
326+
err = mp_init_multi(tmp, tmp2, &dsa->p, &dsa->q, &dsa->g, 0);
327327

328328
if (err == MP_OKAY)
329329
err = mp_read_unsigned_bin(tmp2, buf, (word32)(msize - qsize));
@@ -368,9 +368,6 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
368368
err = mp_add_d(tmp2, 2 * (mp_digit)loop_check_prime, tmp2);
369369
}
370370

371-
if (err == MP_OKAY)
372-
err = mp_init(&dsa->g);
373-
374371
/* find a value g for which g^tmp2 != 1 */
375372
if (err == MP_OKAY)
376373
err = mp_set(&dsa->g, 1);
@@ -399,11 +396,15 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
399396
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
400397
XFREE(buf, dsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
401398
if (tmp != NULL) {
402-
mp_clear(tmp);
399+
if ((err != WC_NO_ERR_TRACE(MP_INIT_E)) &&
400+
(err != WC_NO_ERR_TRACE(MEMORY_E)))
401+
mp_clear(tmp);
403402
XFREE(tmp, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
404403
}
405404
if (tmp2 != NULL) {
406-
mp_clear(tmp2);
405+
if ((err != WC_NO_ERR_TRACE(MP_INIT_E)) &&
406+
(err != WC_NO_ERR_TRACE(MEMORY_E)))
407+
mp_clear(tmp2);
407408
XFREE(tmp2, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
408409
}
409410
#else

0 commit comments

Comments
 (0)