Skip to content

Commit e48be7b

Browse files
mkannwischertob-joecodex
authored andcommitted
Clear ML-DSA key generation outputs on failure
Zeroize the pk and sk output buffers on the keypair_internal error paths, so a caller that ignores the return value cannot mistake a partially written or PCT-failed key pair for a valid one. Clear with mld_zeroize so scrubbing of secret key material is not optimized out. Fold the keypair_internal error paths into a single cleanup. Per API-CONVENTIONS.md, an output buffer must be left either unchanged or fully zeroized on error. keypair_internal may have partially written its outputs before failing, so it zeroizes them; the randomized mld_sign_keypair wrapper needs no zeroization of its own, as it leaves the outputs untouched when randomness or allocation fails before keypair_internal runs. Co-authored-by: Joe Doyle <joseph.doyle@trailofbits.com> Co-authored-by: Codex <codex@openai.com> Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
1 parent f10e8f1 commit e48be7b

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

mldsa/src/sign.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,21 @@ int mld_sign_keypair_internal(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
391391
MLD_FREE(inbuf, uint8_t, MLDSA_SEEDBYTES + 2, context);
392392
MLD_FREE(seedbuf, uint8_t, 2 * MLDSA_SEEDBYTES + MLDSA_CRHBYTES, context);
393393

394+
/* Pairwise Consistency Test (PCT) @[FIPS140_3_IG, p.87] */
395+
/* Do this after freeing all temporaries. */
396+
if (ret == 0)
397+
{
398+
ret = mld_check_pct(pk, sk, context);
399+
}
400+
394401
if (ret != 0)
395402
{
396-
return ret;
403+
/* Clear caller outputs on failure. */
404+
mld_zeroize(pk, MLDSA_CRYPTO_PUBLICKEYBYTES);
405+
mld_zeroize(sk, MLDSA_CRYPTO_SECRETKEYBYTES);
397406
}
398407

399-
/* Pairwise Consistency Test (PCT) @[FIPS140_3_IG, p.87] */
400-
/* Do this after freeing all temporaries. */
401-
return mld_check_pct(pk, sk, context);
408+
return ret;
402409
}
403410

404411
#if !defined(MLD_CONFIG_CORE_API_ONLY)

0 commit comments

Comments
 (0)