Skip to content

Commit c1e1a00

Browse files
committed
Add functional tests for new API
Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 69b4e2c commit c1e1a00

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/test_mlkem.c

+60
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,64 @@ static int test_keys(void)
5050
return 0;
5151
}
5252

53+
static int test_keys_struct_no_marshal(void)
54+
{
55+
mlk_public_key pk;
56+
mlk_secret_key sk;
57+
uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
58+
uint8_t key_a[CRYPTO_BYTES];
59+
uint8_t key_b[CRYPTO_BYTES];
60+
61+
62+
/* Alice generates a public key */
63+
CHECK(crypto_kem_keypair_struct(&pk, &sk) == 0);
64+
/* Bob derives a secret key and creates a response */
65+
CHECK(crypto_kem_enc_struct(ct, key_b, &pk) == 0);
66+
/* Alice uses Bobs response to get her shared key */
67+
CHECK(crypto_kem_dec_struct(key_a, ct, &sk) == 0);
68+
69+
/* mark as defined, so we can compare */
70+
MLK_CT_TESTING_DECLASSIFY(key_a, CRYPTO_BYTES);
71+
MLK_CT_TESTING_DECLASSIFY(key_b, CRYPTO_BYTES);
72+
73+
CHECK(memcmp(key_a, key_b, CRYPTO_BYTES) == 0);
74+
return 0;
75+
}
76+
77+
static int test_keys_struct_marshal(void)
78+
{
79+
mlk_public_key pk;
80+
mlk_secret_key sk;
81+
uint8_t pkb[CRYPTO_PUBLICKEYBYTES];
82+
uint8_t skb[CRYPTO_SECRETKEYBYTES];
83+
uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
84+
uint8_t key_a[CRYPTO_BYTES];
85+
uint8_t key_b[CRYPTO_BYTES];
86+
87+
/* Alice generates a public key */
88+
CHECK(crypto_kem_keypair_struct(&pk, &sk) == 0);
89+
90+
crypto_kem_marshal_pk(pkb, &pk);
91+
crypto_kem_marshal_sk(skb, &sk);
92+
memset(&pk, 0, sizeof(mlk_public_key));
93+
memset(&sk, 0, sizeof(mlk_secret_key));
94+
95+
/* Bob derives a secret key and creates a response */
96+
CHECK(crypto_kem_parse_pk(&pk, pkb));
97+
CHECK(crypto_kem_enc_struct(ct, key_b, &pk) == 0);
98+
99+
/* Alice uses Bobs response to get her shared key */
100+
CHECK(crypto_kem_parse_sk(&sk, skb));
101+
CHECK(crypto_kem_dec_struct(key_a, ct, &sk) == 0);
102+
103+
/* mark as defined, so we can compare */
104+
MLK_CT_TESTING_DECLASSIFY(key_a, CRYPTO_BYTES);
105+
MLK_CT_TESTING_DECLASSIFY(key_b, CRYPTO_BYTES);
106+
107+
CHECK(memcmp(key_a, key_b, CRYPTO_BYTES) == 0);
108+
return 0;
109+
}
110+
53111
static int test_invalid_pk(void)
54112
{
55113
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
@@ -224,6 +282,8 @@ int main(void)
224282
for (i = 0; i < NTESTS; i++)
225283
{
226284
CHECK(test_keys() == 0);
285+
CHECK(test_keys_struct_no_marshal() == 0);
286+
CHECK(test_keys_struct_marshal() == 0);
227287
CHECK(test_invalid_pk() == 0);
228288
CHECK(test_invalid_sk_a() == 0);
229289
CHECK(test_invalid_sk_b() == 0);

0 commit comments

Comments
 (0)