Skip to content

Commit 695ed0f

Browse files
authored
Merge pull request #767 from pq-code-package/ct_macro
Rename ENABLE_CT_TESTING -> MLK_CT_TESTING_ENABLED
2 parents cd13f29 + f29a8b4 commit 695ed0f

File tree

7 files changed

+40
-67
lines changed

7 files changed

+40
-67
lines changed

.github/actions/ct-test/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ runs:
3939
- shell: ${{ env.SHELL }}
4040
run: |
4141
make clean
42-
tests func --exec-wrapper="valgrind --error-exitcode=1 --track-origins=yes ${{ inputs.valgrind_flags }}" --cflags="-DENABLE_CT_TESTING -DNTESTS=50 ${{ inputs.cflags }}"
42+
tests func --exec-wrapper="valgrind --error-exitcode=1 --track-origins=yes ${{ inputs.valgrind_flags }}" --cflags="-DMLK_CT_TESTING_ENABLED -DNTESTS=50 ${{ inputs.cflags }}"

examples/monolithic_build/mlkem_native_monobuild.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@
284284
#undef MLK_ALIGN
285285
#undef MLK_ALWAYS_INLINE
286286
#undef MLK_CET_ENDBR
287+
#undef MLK_CT_TESTING_DECLASSIFY
288+
#undef MLK_CT_TESTING_SECRET
287289
#undef MLK_DEFAULT_ALIGN
288290
#undef MLK_INLINE
289291
#undef MLK_RESTRICT

mlkem/indcpa.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include "sampling.h"
1717
#include "symmetric.h"
1818

19-
#ifdef ENABLE_CT_TESTING
20-
#include <valgrind/memcheck.h>
21-
#endif
22-
2319
/* Static namespacing
2420
* This is to facilitate building multiple instances
2521
* of mlkem-native (e.g. with varying security levels)
@@ -302,16 +298,13 @@ void indcpa_keypair_derand(uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
302298

303299
hash_g(buf, coins_with_domain_separator, MLKEM_SYMBYTES + 1);
304300

305-
306-
#ifdef ENABLE_CT_TESTING
307301
/*
308302
* Declassify the public seed.
309303
* Required to use it in conditional-branches in rejection sampling.
310304
* This is needed because all output of randombytes is marked as secret
311305
* (=undefined)
312306
*/
313-
VALGRIND_MAKE_MEM_DEFINED(publicseed, MLKEM_SYMBYTES);
314-
#endif
307+
MLK_CT_TESTING_DECLASSIFY(publicseed, MLKEM_SYMBYTES);
315308

316309
gen_matrix(a, publicseed, 0 /* no transpose */);
317310

@@ -367,16 +360,13 @@ void indcpa_enc(uint8_t c[MLKEM_INDCPA_BYTES],
367360
unpack_pk(&pkpv, seed, pk);
368361
poly_frommsg(&k, m);
369362

370-
371-
#ifdef ENABLE_CT_TESTING
372363
/*
373364
* Declassify the public seed.
374365
* Required to use it in conditional-branches in rejection sampling.
375366
* This is needed because in re-encryption the publicseed originated from sk
376367
* which is marked undefined.
377368
*/
378-
VALGRIND_MAKE_MEM_DEFINED(seed, MLKEM_SYMBYTES);
379-
#endif
369+
MLK_CT_TESTING_DECLASSIFY(seed, MLKEM_SYMBYTES);
380370

381371
gen_matrix(at, seed, 1 /* transpose */);
382372

mlkem/kem.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
#include "symmetric.h"
1313
#include "verify.h"
1414

15-
#ifdef ENABLE_CT_TESTING
16-
#include <valgrind/memcheck.h>
17-
#endif
18-
1915
/* Static namespacing
2016
* This is to facilitate building multiple instances
2117
* of mlkem-native (e.g. with varying security levels)
@@ -84,13 +80,11 @@ static int check_sk(const uint8_t sk[MLKEM_INDCCA_SECRETKEYBYTES])
8480
* of this function.
8581
*/
8682

87-
#ifdef ENABLE_CT_TESTING
8883
/* Declassify the public part of the secret key */
89-
VALGRIND_MAKE_MEM_DEFINED(sk + MLKEM_INDCPA_SECRETKEYBYTES,
84+
MLK_CT_TESTING_DECLASSIFY(sk + MLKEM_INDCPA_SECRETKEYBYTES,
9085
MLKEM_INDCCA_PUBLICKEYBYTES);
91-
VALGRIND_MAKE_MEM_DEFINED(
86+
MLK_CT_TESTING_DECLASSIFY(
9287
sk + MLKEM_INDCCA_SECRETKEYBYTES - 2 * MLKEM_SYMBYTES, MLKEM_SYMBYTES);
93-
#endif
9488

9589
hash_h(test, sk + MLKEM_INDCPA_SECRETKEYBYTES, MLKEM_INDCCA_PUBLICKEYBYTES);
9690
if (memcmp(sk + MLKEM_INDCCA_SECRETKEYBYTES - 2 * MLKEM_SYMBYTES, test,

mlkem/sys.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,21 @@
135135
#endif
136136
#endif
137137

138+
#if defined(MLK_CT_TESTING_ENABLED) && !defined(__ASSEMBLER__)
139+
#include <valgrind/memcheck.h>
140+
#define MLK_CT_TESTING_SECRET(ptr, len) \
141+
VALGRIND_MAKE_MEM_UNDEFINED((ptr), (len))
142+
#define MLK_CT_TESTING_DECLASSIFY(ptr, len) \
143+
VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
144+
#else
145+
#define MLK_CT_TESTING_SECRET(ptr, len) \
146+
do \
147+
{ \
148+
} while (0)
149+
#define MLK_CT_TESTING_DECLASSIFY(ptr, len) \
150+
do \
151+
{ \
152+
} while (0)
153+
#endif /* MLK_CT_TESTING_ENABLED */
154+
138155
#endif /* MLK_SYS_H */

test/notrandombytes/notrandombytes.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
#include <stdint.h>
1717
#include <string.h>
1818

19-
#ifdef ENABLE_CT_TESTING
20-
#include <valgrind/memcheck.h>
21-
#endif
19+
#include "../../mlkem/sys.h"
2220

2321
static uint32_t seed[32] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3,
2422
2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5};
@@ -81,7 +79,7 @@ static void surf(void)
8179

8280
void randombytes(uint8_t *buf, size_t n)
8381
{
84-
#ifdef ENABLE_CT_TESTING
82+
#ifdef MLK_CT_TESTING_ENABLED
8583
uint8_t *buf_orig = buf;
8684
size_t n_orig = n;
8785
#endif
@@ -108,11 +106,9 @@ void randombytes(uint8_t *buf, size_t n)
108106
--n;
109107
}
110108

111-
#ifdef ENABLE_CT_TESTING
112109
/*
113110
* Mark all randombytes output as secret (undefined).
114111
* Valgrind will propagate this to everything derived from it.
115112
*/
116-
VALGRIND_MAKE_MEM_UNDEFINED(buf_orig, n_orig);
117-
#endif
113+
MLK_CT_TESTING_SECRET(buf_orig, n_orig);
118114
}

test/test_mlkem.c

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
#include "notrandombytes/notrandombytes.h"
1212

13-
#ifdef ENABLE_CT_TESTING
14-
#include <valgrind/memcheck.h>
15-
#endif
16-
1713
#ifndef NTESTS
1814
#define NTESTS 1000
1915
#endif
@@ -29,22 +25,18 @@ static int test_keys(void)
2925
/* Alice generates a public key */
3026
crypto_kem_keypair(pk, sk);
3127

32-
#ifdef ENABLE_CT_TESTING
3328
/* mark public key as public (=defined) */
34-
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
35-
#endif
29+
MLK_CT_TESTING_DECLASSIFY(pk, CRYPTO_PUBLICKEYBYTES);
3630

3731
/* Bob derives a secret key and creates a response */
3832
crypto_kem_enc(ct, key_b, pk);
3933

4034
/* Alice uses Bobs response to get her shared key */
4135
crypto_kem_dec(key_a, ct, sk);
4236

43-
#ifdef ENABLE_CT_TESTING
4437
/* mark as defined, so we can compare */
45-
VALGRIND_MAKE_MEM_DEFINED(key_a, CRYPTO_BYTES);
46-
VALGRIND_MAKE_MEM_DEFINED(key_b, CRYPTO_BYTES);
47-
#endif
38+
MLK_CT_TESTING_DECLASSIFY(key_a, CRYPTO_BYTES);
39+
MLK_CT_TESTING_DECLASSIFY(key_b, CRYPTO_BYTES);
4840

4941
if (memcmp(key_a, key_b, CRYPTO_BYTES))
5042
{
@@ -65,10 +57,8 @@ static int test_invalid_pk(void)
6557
/* Alice generates a public key */
6658
crypto_kem_keypair(pk, sk);
6759

68-
#ifdef ENABLE_CT_TESTING
6960
/* mark public key as public (=defined) */
70-
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
71-
#endif
61+
MLK_CT_TESTING_DECLASSIFY(pk, CRYPTO_PUBLICKEYBYTES);
7262

7363
/* Bob derives a secret key and creates a response */
7464
rc = crypto_kem_enc(ct, key_b, pk);
@@ -105,10 +95,8 @@ static int test_invalid_sk_a(void)
10595
/* Alice generates a public key */
10696
crypto_kem_keypair(pk, sk);
10797

108-
#ifdef ENABLE_CT_TESTING
10998
/* mark public key as public (=defined) */
110-
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
111-
#endif
99+
MLK_CT_TESTING_DECLASSIFY(pk, CRYPTO_PUBLICKEYBYTES);
112100

113101
/* Bob derives a secret key and creates a response */
114102
crypto_kem_enc(ct, key_b, pk);
@@ -127,11 +115,9 @@ static int test_invalid_sk_a(void)
127115
return 1;
128116
}
129117

130-
#ifdef ENABLE_CT_TESTING
131118
/* mark as defined, so we can compare */
132-
VALGRIND_MAKE_MEM_DEFINED(key_a, CRYPTO_BYTES);
133-
VALGRIND_MAKE_MEM_DEFINED(key_b, CRYPTO_BYTES);
134-
#endif
119+
MLK_CT_TESTING_DECLASSIFY(key_a, CRYPTO_BYTES);
120+
MLK_CT_TESTING_DECLASSIFY(key_b, CRYPTO_BYTES);
135121

136122
if (!memcmp(key_a, key_b, CRYPTO_BYTES))
137123
{
@@ -154,10 +140,8 @@ static int test_invalid_sk_b(void)
154140
/* Alice generates a public key */
155141
crypto_kem_keypair(pk, sk);
156142

157-
#ifdef ENABLE_CT_TESTING
158143
/* mark public key as public (=defined) */
159-
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
160-
#endif
144+
MLK_CT_TESTING_DECLASSIFY(pk, CRYPTO_PUBLICKEYBYTES);
161145

162146
/* Bob derives a secret key and creates a response */
163147
crypto_kem_enc(ct, key_b, pk);
@@ -192,25 +176,17 @@ static int test_invalid_ciphertext(void)
192176
do
193177
{
194178
randombytes(&b, sizeof(uint8_t));
195-
196-
#ifdef ENABLE_CT_TESTING
197-
VALGRIND_MAKE_MEM_DEFINED(&b, sizeof(uint8_t));
198-
#endif
179+
MLK_CT_TESTING_DECLASSIFY(&b, sizeof(uint8_t));
199180
} while (!b);
200181
randombytes((uint8_t *)&pos, sizeof(size_t));
201182

202-
203-
#ifdef ENABLE_CT_TESTING
204-
VALGRIND_MAKE_MEM_DEFINED(&pos, sizeof(size_t));
205-
#endif
183+
MLK_CT_TESTING_DECLASSIFY(&pos, sizeof(size_t));
206184

207185
/* Alice generates a public key */
208186
crypto_kem_keypair(pk, sk);
209187

210-
#ifdef ENABLE_CT_TESTING
211188
/* mark public key as public (=defined) */
212-
VALGRIND_MAKE_MEM_DEFINED(pk, CRYPTO_PUBLICKEYBYTES);
213-
#endif
189+
MLK_CT_TESTING_DECLASSIFY(pk, CRYPTO_PUBLICKEYBYTES);
214190

215191
/* Bob derives a secret key and creates a response */
216192
crypto_kem_enc(ct, key_b, pk);
@@ -221,11 +197,9 @@ static int test_invalid_ciphertext(void)
221197
/* Alice uses Bobs response to get her shared key */
222198
crypto_kem_dec(key_a, ct, sk);
223199

224-
#ifdef ENABLE_CT_TESTING
225200
/* mark as defined, so we can compare */
226-
VALGRIND_MAKE_MEM_DEFINED(key_a, CRYPTO_BYTES);
227-
VALGRIND_MAKE_MEM_DEFINED(key_b, CRYPTO_BYTES);
228-
#endif
201+
MLK_CT_TESTING_DECLASSIFY(key_a, CRYPTO_BYTES);
202+
MLK_CT_TESTING_DECLASSIFY(key_b, CRYPTO_BYTES);
229203

230204
if (!memcmp(key_a, key_b, CRYPTO_BYTES))
231205
{

0 commit comments

Comments
 (0)