Skip to content

Commit 70e55b7

Browse files
willieyzmkannwischer
authored andcommitted
Add test exercising top-level API with unaligned buffers
- This commit is porting from mlkem-native PR#1241 - Previously `test_mldsa.c` did not cover the unaligned buffers test, this commit add test for top-level API using intentionally unaligned (1 mod 32) buffers to verify alignment safety. Signed-off-by: willieyz <[email protected]>
1 parent 257c38d commit 70e55b7

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

test/test_mldsa.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,31 @@
2828
} \
2929
} while (0)
3030

31-
static int test_sign(void)
31+
32+
static int test_sign_core(uint8_t pk[CRYPTO_PUBLICKEYBYTES],
33+
uint8_t sk[CRYPTO_SECRETKEYBYTES],
34+
uint8_t sm[MLEN + CRYPTO_BYTES], uint8_t m[MLEN],
35+
uint8_t m2[MLEN + CRYPTO_BYTES], uint8_t ctx[CTXLEN])
3236
{
33-
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
34-
uint8_t sk[CRYPTO_SECRETKEYBYTES];
35-
uint8_t sm[MLEN + CRYPTO_BYTES];
36-
uint8_t m[MLEN];
37-
uint8_t m2[MLEN + CRYPTO_BYTES];
38-
uint8_t ctx[CTXLEN];
3937
size_t smlen;
4038
size_t mlen;
4139
int rc;
4240

4341

4442
CHECK(crypto_sign_keypair(pk, sk) == 0);
4543
randombytes(ctx, CTXLEN);
46-
MLD_CT_TESTING_SECRET(ctx, sizeof(ctx));
44+
MLD_CT_TESTING_SECRET(ctx, CTXLEN);
4745
randombytes(m, MLEN);
48-
MLD_CT_TESTING_SECRET(m, sizeof(m));
46+
MLD_CT_TESTING_SECRET(m, MLEN);
4947

5048
CHECK(crypto_sign(sm, &smlen, m, MLEN, ctx, CTXLEN, sk) == 0);
5149

5250
rc = crypto_sign_open(m2, &mlen, sm, smlen, ctx, CTXLEN, pk);
5351

5452
/* Constant time: Declassify outputs to check them. */
5553
MLD_CT_TESTING_DECLASSIFY(rc, sizeof(int));
56-
MLD_CT_TESTING_DECLASSIFY(m, sizeof(m));
57-
MLD_CT_TESTING_DECLASSIFY(m2, sizeof(m2));
54+
MLD_CT_TESTING_DECLASSIFY(m, MLEN);
55+
MLD_CT_TESTING_DECLASSIFY(m2, (MLEN + CRYPTO_BYTES));
5856

5957
if (rc)
6058
{
@@ -83,6 +81,30 @@ static int test_sign(void)
8381
return 0;
8482
}
8583

84+
static int test_sign(void)
85+
{
86+
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
87+
uint8_t sk[CRYPTO_SECRETKEYBYTES];
88+
uint8_t sm[MLEN + CRYPTO_BYTES];
89+
uint8_t m[MLEN];
90+
uint8_t m2[MLEN + CRYPTO_BYTES];
91+
uint8_t ctx[CTXLEN];
92+
93+
return test_sign_core(pk, sk, sm, m, m2, ctx);
94+
}
95+
96+
static int test_sign_unaligned(void)
97+
{
98+
MLD_ALIGN uint8_t pk[CRYPTO_PUBLICKEYBYTES + 1];
99+
MLD_ALIGN uint8_t sk[CRYPTO_SECRETKEYBYTES + 1];
100+
MLD_ALIGN uint8_t sm[MLEN + CRYPTO_BYTES + 1];
101+
MLD_ALIGN uint8_t m[MLEN + 1];
102+
MLD_ALIGN uint8_t m2[MLEN + CRYPTO_BYTES + 1];
103+
MLD_ALIGN uint8_t ctx[CTXLEN + 1];
104+
105+
return test_sign_core(pk + 1, sk + 1, sm + 1, m + 1, m2 + 1, ctx + 1);
106+
}
107+
86108
static int test_wrong_pk(void)
87109
{
88110
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
@@ -249,6 +271,7 @@ int main(void)
249271
for (i = 0; i < NTESTS; i++)
250272
{
251273
r = test_sign();
274+
r |= test_sign_unaligned();
252275
r |= test_wrong_pk();
253276
r |= test_wrong_sig();
254277
r |= test_wrong_ctx();

0 commit comments

Comments
 (0)