Skip to content

Commit 48fd61f

Browse files
committed
Move buffer destruction to centralized exit path
Signed-off-by: jammychiou1 <[email protected]>
1 parent 6a9e7f3 commit 48fd61f

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

mldsa/sign.c

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ __contract__(
322322
mld_polyveck w2, w1, w0, h;
323323
mld_poly cp;
324324
uint32_t z_invalid, w0_invalid, h_invalid;
325+
int res;
325326

326327
/* Sample intermediate vector y */
327328
mld_polyvecl_uniform_gamma1(&y, rhoprime, nonce);
@@ -363,16 +364,8 @@ __contract__(
363364
MLD_CT_TESTING_DECLASSIFY(&z_invalid, sizeof(uint32_t));
364365
if (z_invalid)
365366
{
366-
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
367-
mld_zeroize(challenge_bytes, MLDSA_CTILDEBYTES);
368-
mld_zeroize(&y, sizeof(y));
369-
mld_zeroize(&z, sizeof(z));
370-
mld_zeroize(&w2, sizeof(w2));
371-
mld_zeroize(&w1, sizeof(w1));
372-
mld_zeroize(&w0, sizeof(w0));
373-
mld_zeroize(&h, sizeof(h));
374-
mld_zeroize(&cp, sizeof(cp));
375-
return -1; /* reject */
367+
res = -1; /* reject */
368+
goto cleanup;
376369
}
377370

378371
/* If z is valid, then its coefficients are bounded by */
@@ -394,16 +387,8 @@ __contract__(
394387
MLD_CT_TESTING_DECLASSIFY(&w0_invalid, sizeof(uint32_t));
395388
if (w0_invalid)
396389
{
397-
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
398-
mld_zeroize(challenge_bytes, sizeof(challenge_bytes));
399-
mld_zeroize(&y, sizeof(y));
400-
mld_zeroize(&z, sizeof(z));
401-
mld_zeroize(&w2, sizeof(w2));
402-
mld_zeroize(&w1, sizeof(w1));
403-
mld_zeroize(&w0, sizeof(w0));
404-
mld_zeroize(&h, sizeof(h));
405-
mld_zeroize(&cp, sizeof(cp));
406-
return -1; /* reject */
390+
res = -1; /* reject */
391+
goto cleanup;
407392
}
408393

409394
/* Compute hints for w1 */
@@ -416,16 +401,8 @@ __contract__(
416401
MLD_CT_TESTING_DECLASSIFY(&h_invalid, sizeof(uint32_t));
417402
if (h_invalid)
418403
{
419-
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
420-
mld_zeroize(challenge_bytes, MLDSA_CTILDEBYTES);
421-
mld_zeroize(&y, sizeof(y));
422-
mld_zeroize(&z, sizeof(z));
423-
mld_zeroize(&w2, sizeof(w2));
424-
mld_zeroize(&w1, sizeof(w1));
425-
mld_zeroize(&w0, sizeof(w0));
426-
mld_zeroize(&h, sizeof(h));
427-
mld_zeroize(&cp, sizeof(cp));
428-
return -1; /* reject */
404+
res = -1; /* reject */
405+
goto cleanup;
429406
}
430407

431408
mld_polyveck_add(&w0, &h);
@@ -443,16 +420,8 @@ __contract__(
443420
n = mld_polyveck_make_hint(&h, &w0, &w2);
444421
if (n > MLDSA_OMEGA)
445422
{
446-
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
447-
mld_zeroize(challenge_bytes, MLDSA_CTILDEBYTES);
448-
mld_zeroize(&y, sizeof(y));
449-
mld_zeroize(&z, sizeof(z));
450-
mld_zeroize(&w2, sizeof(w2));
451-
mld_zeroize(&w1, sizeof(w1));
452-
mld_zeroize(&w0, sizeof(w0));
453-
mld_zeroize(&h, sizeof(h));
454-
mld_zeroize(&cp, sizeof(cp));
455-
return -1; /* reject */
423+
res = -1; /* reject */
424+
goto cleanup;
456425
}
457426

458427
/* All is well - write signature */
@@ -462,6 +431,9 @@ __contract__(
462431
MLD_CT_TESTING_DECLASSIFY(&z, sizeof(z));
463432
mld_pack_sig(sig, challenge_bytes, &z, &h, n);
464433

434+
res = 0; /* success */
435+
436+
cleanup:
465437
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
466438
mld_zeroize(challenge_bytes, MLDSA_CTILDEBYTES);
467439
mld_zeroize(&y, sizeof(y));
@@ -472,7 +444,7 @@ __contract__(
472444
mld_zeroize(&h, sizeof(h));
473445
mld_zeroize(&cp, sizeof(cp));
474446

475-
return 0; /* success */
447+
return res;
476448
}
477449
MLD_MUST_CHECK_RETURN_VALUE
478450
int crypto_sign_signature_internal(uint8_t *sig, size_t *siglen,

0 commit comments

Comments
 (0)