Skip to content

Commit 7e5cf7b

Browse files
committed
Use CT memcmp in crypto_sign_verify_internal
This has negligible impact on performance while improving readability and provability. For now, we still declassify the result of the verification, but this may be further improved in the future. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 81c7c9a commit 7e5cf7b

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

mldsa/src/sign.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,7 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
874874
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
875875
int externalmu)
876876
{
877-
unsigned int i;
878-
int ret;
877+
int ret, cmp;
879878
MLD_ALLOC(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES));
880879
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES);
881880
MLD_ALLOC(mu, uint8_t, MLDSA_CRHBYTES);
@@ -961,28 +960,12 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
961960
mld_H(c2, MLDSA_CTILDEBYTES, mu, MLDSA_CRHBYTES, buf,
962961
MLDSA_K * MLDSA_POLYW1_PACKEDBYTES, NULL, 0);
963962

964-
/* Constant time: All data in verification is usually considered public.
965-
* However, in our constant-time tests we do not declassify the message and
966-
* context string.
967-
* The following conditional is the only place in verification whose run-time
968-
* depends on the message. As all that can be leakaged here is the output of
969-
* a hash call (that should behave like a random oracle), it is safe to
970-
* declassify here even with a secret message.
971-
*/
972-
MLD_CT_TESTING_DECLASSIFY(c2, MLDSA_CTILDEBYTES);
973-
ret = MLD_ERR_FAIL;
974-
for (i = 0; i < MLDSA_CTILDEBYTES; ++i)
975-
__loop__(
976-
invariant(i <= MLDSA_CTILDEBYTES)
977-
)
978-
{
979-
if (c[i] != c2[i])
980-
{
981-
goto cleanup;
982-
}
983-
}
963+
cmp = mld_ct_memcmp(c, c2, MLDSA_CTILDEBYTES);
984964

985-
ret = 0;
965+
/* Declassify the result of the verification. */
966+
MLD_CT_TESTING_DECLASSIFY(&cmp, sizeof(cmp));
967+
968+
ret = cmp == 0 ? 0 : MLD_ERR_FAIL;
986969

987970
cleanup:
988971
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */

proofs/cbmc/crypto_sign_verify_internal/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ USE_FUNCTION_CONTRACTS+=$(MLD_NAMESPACE)polyveck_caddq
3939
USE_FUNCTION_CONTRACTS+=$(MLD_NAMESPACE)polyveck_use_hint
4040
USE_FUNCTION_CONTRACTS+=$(MLD_NAMESPACE)polyveck_pack_w1
4141
USE_FUNCTION_CONTRACTS+=mld_zeroize
42+
USE_FUNCTION_CONTRACTS+=mld_ct_memcmp
4243

4344
APPLY_LOOP_CONTRACTS=on
4445
USE_DYNAMIC_FRAMES=1

0 commit comments

Comments
 (0)