Skip to content

Commit ffc4bbe

Browse files
committed
Address Falcon and ML-KEM white-box review findings
1 parent 06321f4 commit ffc4bbe

3 files changed

Lines changed: 128 additions & 52 deletions

File tree

‎tests/unit-mcdc/test_falcon_whitebox.c‎

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,117 @@ static void wb_put_gaussian_draw(unsigned int coefficient, unsigned int mag,
115115
word64 magDraw = gauss_1024_12289[mag];
116116
unsigned int i;
117117

118+
if (mag == 0) {
119+
XMEMSET(p, 0, WB_FALCON_DRAW_SZ);
120+
return;
121+
}
118122
for (i = 0; i < 8; i++) {
119123
p[i] = (byte)(signDraw >> (8 * i));
120124
p[8 + i] = (byte)(magDraw >> (8 * i));
121125
}
122126
}
123127

128+
/* Screen candidates through the checks before keygen writes its public
129+
* output. This leaves the trim guard as the only reason not to write h. */
130+
static int wb_falcon_reaches_public(const sword8* f, const sword8* g, fpr* work)
131+
{
132+
fpr* a = work;
133+
fpr* b = a + WB_FALCON_N;
134+
fpr* inv = b + WB_FALCON_N;
135+
fpr norm = fpr_zero;
136+
unsigned int i;
137+
138+
if (poly_small_sqnorm(f, WB_FALCON_LOGN) +
139+
poly_small_sqnorm(g, WB_FALCON_LOGN) >= 16823)
140+
return 0;
141+
poly_small_to_fp(a, f, WB_FALCON_LOGN);
142+
poly_small_to_fp(b, g, WB_FALCON_LOGN);
143+
falcon_FFT(a, WB_FALCON_LOGN);
144+
falcon_FFT(b, WB_FALCON_LOGN);
145+
falcon_poly_invnorm2_fft(inv, a, b, WB_FALCON_LOGN);
146+
falcon_poly_adj_fft(a, WB_FALCON_LOGN);
147+
falcon_poly_adj_fft(b, WB_FALCON_LOGN);
148+
falcon_poly_mulconst(a, fpr_q, WB_FALCON_LOGN);
149+
falcon_poly_mulconst(b, fpr_q, WB_FALCON_LOGN);
150+
falcon_poly_mul_autoadj_fft(a, inv, WB_FALCON_LOGN);
151+
falcon_poly_mul_autoadj_fft(b, inv, WB_FALCON_LOGN);
152+
falcon_iFFT(a, WB_FALCON_LOGN);
153+
falcon_iFFT(b, WB_FALCON_LOGN);
154+
for (i = 0; i < WB_FALCON_N; i++) {
155+
norm = fpr_add(norm, fpr_sqr(a[i]));
156+
norm = fpr_add(norm, fpr_sqr(b[i]));
157+
}
158+
if (!fpr_lt(norm, fpr_bnorm_max))
159+
return 0;
160+
return falcon_compute_public((word16*)work, f, g, WB_FALCON_LOGN,
161+
(word16*)work + WB_FALCON_N);
162+
}
163+
124164
static int wb_keygen_draw_limits(WC_RNG* rng)
125165
{
166+
sword8 seedF[WB_FALCON_N], seedG[WB_FALCON_N];
126167
sword8 f[WB_FALCON_N], g[WB_FALCON_N], bigF[WB_FALCON_N];
127-
unsigned int poly;
168+
word16 h[WB_FALCON_N];
169+
fpr* work = (fpr*)XMALLOC(3 * WB_FALCON_N * sizeof(fpr), NULL,
170+
DYNAMIC_TYPE_TMP_BUFFER);
171+
unsigned int poly, i, attempt;
128172
int negative;
173+
int ok = 0;
129174

175+
if (work == NULL)
176+
return 0;
130177
for (poly = 0; poly < 2; poly++) {
131178
for (negative = 0; negative < 2; negative++) {
179+
int found = 0;
132180
int ret;
133181

134-
XMEMSET(wb_falcon_draws, 0, sizeof(wb_falcon_draws));
135-
/* A coefficient at either trim limit is representable by the
136-
* Gaussian sampler but must reject the entire candidate. Give
137-
* each polynomial odd parity so sampling completes. */
138-
wb_put_gaussian_draw(poly * WB_FALCON_N, 16, negative);
139-
wb_put_gaussian_draw(WB_FALCON_N - 1, 1, 0);
140-
wb_put_gaussian_draw(2 * WB_FALCON_N - 1, 1, 0);
182+
for (attempt = 0; attempt < 8 && !found; attempt++) {
183+
sword8* target = poly == 0 ? seedF : seedG;
184+
sword8 limit = (sword8)(negative ? -16 : 16);
185+
186+
if (falcon_keygen(rng, seedF, seedG, bigF, NULL, NULL,
187+
WB_FALCON_LOGN) != 0)
188+
goto out;
189+
for (i = 0; i < WB_FALCON_N && !found; i++) {
190+
sword8 old = target[i];
191+
192+
/* Keep the sampler's required odd polynomial parity. */
193+
if (old % 2 != 0)
194+
continue;
195+
target[i] = limit;
196+
found = wb_falcon_reaches_public(seedF, seedG, work);
197+
if (!found)
198+
target[i] = old;
199+
}
200+
}
201+
if (!found)
202+
goto out;
203+
204+
for (i = 0; i < WB_FALCON_N; i++) {
205+
int x = seedF[i], y = seedG[i];
206+
wb_put_gaussian_draw(i, (unsigned int)(x < 0 ? -x : x),
207+
x < 0);
208+
wb_put_gaussian_draw(i + WB_FALCON_N,
209+
(unsigned int)(y < 0 ? -y : y), y < 0);
210+
h[i] = 0xffff;
211+
}
141212
wb_falcon_draw_offset = 0;
142213
wb_falcon_draw_active = 1;
143-
ret = falcon_keygen(rng, f, g, bigF, NULL, NULL, WB_FALCON_LOGN);
214+
ret = falcon_keygen(rng, f, g, bigF, NULL, h, WB_FALCON_LOGN);
144215
wb_falcon_draw_active = 0;
145-
/* The stream ends during resampling, so no key is produced. */
146216
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG) ||
147-
wb_falcon_draw_offset != sizeof(wb_falcon_draws)) {
148-
return 0;
217+
wb_falcon_draw_offset != sizeof(wb_falcon_draws))
218+
goto out;
219+
for (i = 0; i < WB_FALCON_N; i++) {
220+
if (h[i] != 0xffff)
221+
goto out;
149222
}
150223
}
151224
}
152-
return 1;
225+
ok = 1;
226+
out:
227+
XFREE(work, NULL, DYNAMIC_TYPE_TMP_BUFFER);
228+
return ok;
153229
}
154230
#endif
155231

@@ -2379,8 +2455,6 @@ int main(void)
23792455
#ifdef WB_FALCON_DRAW_TEST
23802456
if (!wb_keygen_draw_limits(&rng)) {
23812457
WB_FAIL("keygen did not resample Gaussian trim-limit draws");
2382-
wc_FreeRng(&rng);
2383-
return 1;
23842458
}
23852459
#endif
23862460
}

‎tests/unit-mcdc/test_mlkem_fault_whitebox.c‎

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static int wb_fail_matrix;
119119
#include <string.h>
120120

121121
static int wb_fail = 0;
122+
static int wb_failed_check;
122123
#define WB_NOTE(msg) do { printf(" [wb] %s\n", (msg)); } while (0)
123124

124125
#if !defined(WOLFSSL_HAVE_MLKEM) || \
@@ -222,10 +223,8 @@ int main(int argc, char** argv)
222223
if (allocError != WC_NO_ERR_TRACE(MEMORY_E) ||
223224
matrixError != WC_NO_ERR_TRACE(WC_HW_E) || filled ||
224225
ret != 0 || !cached) {
225-
wc_MlKemKey_Free(&key);
226-
mcdc_fa_restore();
227-
wc_FreeRng(&rng);
228-
return 1;
226+
puts(" [wb][FAIL] matrix caching");
227+
wb_failed_check = 1;
229228
}
230229
}
231230
#endif
@@ -238,22 +237,21 @@ int main(int argc, char** argv)
238237

239238
ret = wc_MlKemKey_Init(&other, MLKEM_WB_TYPE, NULL, INVALID_DEVID);
240239
if (ret != 0) {
241-
wc_MlKemKey_Free(&key);
242-
mcdc_fa_restore();
243-
wc_FreeRng(&rng);
244-
return 1;
240+
puts(" [wb] streamed row setup failed; skipped");
241+
wb_fail = 1;
245242
}
246-
wb_fail_row = 1;
247-
encapError = wc_MlKemKey_Encapsulate(&key, ct, ss, &rng);
248-
keygenError = wc_MlKemKey_MakeKeyWithRandom(&other, seed, sizeof(seed));
249-
wb_fail_row = 0;
250-
wc_MlKemKey_Free(&other);
251-
if (encapError != WC_NO_ERR_TRACE(WC_HW_E) ||
252-
keygenError != WC_NO_ERR_TRACE(WC_HW_E)) {
253-
wc_MlKemKey_Free(&key);
254-
mcdc_fa_restore();
255-
wc_FreeRng(&rng);
256-
return 1;
243+
else {
244+
wb_fail_row = 1;
245+
encapError = wc_MlKemKey_Encapsulate(&key, ct, ss, &rng);
246+
keygenError = wc_MlKemKey_MakeKeyWithRandom(&other, seed,
247+
sizeof(seed));
248+
wb_fail_row = 0;
249+
wc_MlKemKey_Free(&other);
250+
if (encapError != WC_NO_ERR_TRACE(WC_HW_E) ||
251+
keygenError != WC_NO_ERR_TRACE(WC_HW_E)) {
252+
puts(" [wb][FAIL] streamed row errors");
253+
wb_failed_check = 1;
254+
}
257255
}
258256
}
259257
#endif
@@ -269,10 +267,8 @@ int main(int argc, char** argv)
269267
key.flags = flags;
270268
if (withoutPub != WC_NO_ERR_TRACE(BAD_STATE_E) ||
271269
shortCt != WC_NO_ERR_TRACE(BUFFER_E)) {
272-
wc_MlKemKey_Free(&key);
273-
mcdc_fa_restore();
274-
wc_FreeRng(&rng);
275-
return 1;
270+
puts(" [wb][FAIL] decapsulation arguments");
271+
wb_failed_check = 1;
276272
}
277273
}
278274
#endif
@@ -291,10 +287,8 @@ int main(int argc, char** argv)
291287
key.type = type;
292288
if (withCompare != WC_NO_ERR_TRACE(BAD_FUNC_ARG) ||
293289
unknownType != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) {
294-
wc_MlKemKey_Free(&key);
295-
mcdc_fa_restore();
296-
wc_FreeRng(&rng);
297-
return 1;
290+
puts(" [wb][FAIL] encapsulation arguments");
291+
wb_failed_check = 1;
298292
}
299293
}
300294
#endif
@@ -323,10 +317,8 @@ int main(int argc, char** argv)
323317
if (pubLen <= 0 || privLen <= 0 ||
324318
pubError != WC_NO_ERR_TRACE(BAD_FUNC_ARG) ||
325319
privError != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) {
326-
wc_MlKemKey_Free(&key);
327-
mcdc_fa_restore();
328-
wc_FreeRng(&rng);
329-
return 1;
320+
puts(" [wb][FAIL] DER export");
321+
wb_failed_check = 1;
330322
}
331323
}
332324
#endif
@@ -436,10 +428,8 @@ int main(int argc, char** argv)
436428
key.type = type;
437429
if (empty != WC_NO_ERR_TRACE(BAD_FUNC_ARG) || replace != 0 ||
438430
unknownType != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) {
439-
wc_MlKemKey_Free(&key);
440-
mcdc_fa_restore();
441-
wc_FreeRng(&rng);
442-
return 1;
431+
puts(" [wb][FAIL] key generation arguments");
432+
wb_failed_check = 1;
443433
}
444434
}
445435
#endif
@@ -449,7 +439,8 @@ int main(int argc, char** argv)
449439
wc_MlKemKey_Free(&key);
450440
wc_FreeRng(&rng);
451441

452-
printf("done (%s)\n", wb_fail ? "with skips" : "ok");
442+
printf("done (%s)\n", wb_failed_check ? "with failures" :
443+
(wb_fail ? "with skips" : "ok"));
453444
(void)wb_fail;
454445
return 0;
455446
}

‎tests/unit-mcdc/test_mlkem_poly_hash_fault_whitebox.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ static int wb_encapsulate_compare(void)
506506
#endif
507507
const int argErr = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
508508
int cmpFail = 0;
509-
int fail = 0;
509+
int fail = -1;
510510
int ok;
511511

512512
if (mlkem_prf_new(&prf, NULL, INVALID_DEVID) != 0) {
@@ -525,6 +525,17 @@ static int wb_encapsulate_compare(void)
525525
u, tmp, y, k, msg, coins) == 0;
526526
ok &= (cmpFail == 0 && fail == 0);
527527

528+
ct[0] ^= 1;
529+
XMEMSET(y, 0, sizeof(y));
530+
cmpFail = 0;
531+
fail = 0;
532+
ok &= mlkem_encapsulate_seeds_row(&prf, block, ct, &cmpFail, u, tmp, y,
533+
k, seed, coins, NULL, NULL, 0) == 0;
534+
ok &= mlkem_encapsulate_seeds_tail(pub, &prf, block, ct, &fail, cmpFail,
535+
u, tmp, y, k, msg, coins) == 0;
536+
ok &= (cmpFail != 0 && fail == -1);
537+
ct[0] ^= 1;
538+
528539
ok &= mlkem_encapsulate_seeds_row(&prf, NULL, NULL, NULL, u, tmp, y, k,
529540
seed, coins, NULL, NULL, 0) == argErr;
530541
ok &= mlkem_encapsulate_seeds_row(&prf, NULL, ct, &cmpFail, u, tmp, y, k,

0 commit comments

Comments
 (0)