diff --git a/mldsa/sign.c b/mldsa/sign.c index f129046d4..135be8af5 100644 --- a/mldsa/sign.c +++ b/mldsa/sign.c @@ -231,6 +231,41 @@ int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) return result; } +static void mld_shake256_absorb_with_residual(mld_shake256ctx *state, + const uint8_t *in, size_t inlen, + uint8_t *residual, size_t *pos) +{ + size_t nb; + if (in) + { + if (*pos) + { + nb = inlen < 8 - *pos ? inlen : 8 - *pos; + memcpy(residual + *pos, in, nb); + inlen -= nb; + in += nb; + *pos += nb; + if (*pos == 8) + { + mld_shake256_absorb(state, residual, 8U); + *pos = 0; + } + } + nb = inlen & ~7UL; + if (nb) + { + mld_shake256_absorb(state, in, nb); + in += nb; + inlen -= nb; + } + if (inlen) + { + memcpy(residual, in, inlen); + *pos = inlen; + } + } +} + /************************************************* * Name: mld_H * @@ -269,22 +304,22 @@ __contract__( ) { mld_shake256ctx state; + uint8_t buf[8]; + size_t pos = 0; mld_shake256_init(&state); - mld_shake256_absorb(&state, in1, in1len); - if (in2 != NULL) - { - mld_shake256_absorb(&state, in2, in2len); - } - if (in3 != NULL) + mld_shake256_absorb_with_residual(&state, in1, in1len, buf, &pos); + mld_shake256_absorb_with_residual(&state, in2, in2len, buf, &pos); + mld_shake256_absorb_with_residual(&state, in3, in3len, buf, &pos); + if (pos) { - mld_shake256_absorb(&state, in3, in3len); + mld_shake256_absorb(&state, buf, pos); } mld_shake256_finalize(&state); mld_shake256_squeeze(out, outlen, &state); mld_shake256_release(&state); /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ - mld_zeroize(&state, sizeof(state)); + mld_zeroize(&buf, sizeof(buf)); } /* Reference: The reference implementation does not explicitly */ diff --git a/proofs/cbmc/H/Makefile b/proofs/cbmc/H/Makefile index 70640ffc8..fa87c4422 100644 --- a/proofs/cbmc/H/Makefile +++ b/proofs/cbmc/H/Makefile @@ -37,7 +37,7 @@ FUNCTION_NAME = mld_h # EXPENSIVE = true # This function is large enough to need... -CBMC_OBJECT_BITS = 8 +CBMC_OBJECT_BITS = 9 # If you require access to a file-local ("static") function or object to conduct # your proof, set the following (and do not include the original source file