Skip to content

Commit d5fa1f1

Browse files
Merge pull request #12228 from danielinux/mcdc-10-sp-all
MCDC part 10: close reachable host SP math MC/DC gaps
2 parents f4f52d9 + d79e53c commit d5fa1f1

9 files changed

Lines changed: 282 additions & 7 deletions

‎tests/api/test_ecc.c‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,100 @@ int test_wc_ecc_check_key_invalid_pubkey(void)
445445
return EXPECT_RESULT();
446446
} /* END test_wc_ecc_check_key_invalid_pubkey */
447447

448+
/* A generator passes validation. A coordinate equal to the field prime
449+
* fits in the field width and must fail the field range check. */
450+
#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
451+
defined(WOLFSSL_HAVE_SP_ECC) && \
452+
!defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) && \
453+
!defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_NO_ECC_SW) && \
454+
!defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
455+
!defined(WOLFSSL_MICROCHIP_TA100) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
456+
!defined(WOLFSSL_SE050) && !defined(WOLFSSL_STM32_PKA) && \
457+
!defined(WOLFSSL_KCAPI_ECC) && !defined(NO_ECC_CHECK_PUBKEY_ORDER)
458+
#define TEST_ECC_SP_PUBKEY_RANGE
459+
static int ecc_check_raw(int curveId, const char* x, const char* y)
460+
{
461+
ecc_key key;
462+
int ret;
463+
464+
ret = wc_ecc_init(&key);
465+
if (ret == 0) {
466+
ret = wc_ecc_import_raw_ex(&key, x, y, NULL, curveId);
467+
if (ret == 0)
468+
ret = wc_ecc_check_key(&key);
469+
wc_ecc_free(&key);
470+
}
471+
472+
return ret;
473+
}
474+
475+
#ifdef WOLFSSL_SP_INT_NEGATIVE
476+
static int ecc_check_negative_point(const ecc_set_type* curve, int negateY)
477+
{
478+
EXPECT_DECLS;
479+
ecc_key key;
480+
int ret = wc_ecc_init(&key);
481+
482+
ExpectIntEQ(ret, 0);
483+
if (ret == 0) {
484+
ExpectIntEQ(wc_ecc_import_raw_ex(&key, curve->Gx, curve->Gy, NULL,
485+
curve->id), 0);
486+
if (EXPECT_SUCCESS()) {
487+
mp_int* coord = negateY ? key.pubkey.y : key.pubkey.x;
488+
mp_setneg(coord);
489+
ExpectIntEQ(wc_ecc_check_key(&key),
490+
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
491+
}
492+
wc_ecc_free(&key);
493+
}
494+
return EXPECT_RESULT();
495+
}
496+
#endif
497+
#endif
498+
499+
int test_wc_ecc_sp_pubkey_range(void)
500+
{
501+
EXPECT_DECLS;
502+
#ifdef TEST_ECC_SP_PUBKEY_RANGE
503+
static const int curveIds[] = {
504+
#ifndef WOLFSSL_SP_NO_256
505+
ECC_SECP256R1,
506+
#endif
507+
#ifdef WOLFSSL_SP_384
508+
ECC_SECP384R1,
509+
#endif
510+
#ifdef WOLFSSL_SP_521
511+
ECC_SECP521R1,
512+
#endif
513+
ECC_CURVE_INVALID
514+
};
515+
int i;
516+
517+
for (i = 0; EXPECT_SUCCESS() && curveIds[i] != ECC_CURVE_INVALID; i++) {
518+
int curveId = curveIds[i];
519+
int idx = wc_ecc_get_curve_idx(curveId);
520+
const ecc_set_type* curve;
521+
522+
if (idx < 0)
523+
continue;
524+
curve = wc_ecc_get_curve_params(idx);
525+
ExpectNotNull(curve);
526+
if (EXPECT_SUCCESS()) {
527+
ExpectIntEQ(ecc_check_raw(curveId, curve->Gx, curve->Gy), 0);
528+
ExpectIntEQ(ecc_check_raw(curveId, curve->prime, curve->Gy),
529+
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
530+
ExpectIntEQ(ecc_check_raw(curveId, curve->Gx, curve->prime),
531+
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
532+
#ifdef WOLFSSL_SP_INT_NEGATIVE
533+
EXPECT_TEST(ecc_check_negative_point(curve, 0));
534+
EXPECT_TEST(ecc_check_negative_point(curve, 1));
535+
#endif
536+
}
537+
}
538+
#endif
539+
return EXPECT_RESULT();
540+
} /* END test_wc_ecc_sp_pubkey_range */
541+
448542
/*
449543
* Testing wc_ecc_get_generator()
450544
*/

‎tests/api/test_ecc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ int test_wc_ecc_make_key(void);
3333
int test_wc_ecc_init(void);
3434
int test_wc_ecc_check_key(void);
3535
int test_wc_ecc_check_key_invalid_pubkey(void);
36+
int test_wc_ecc_sp_pubkey_range(void);
3637
int test_wc_ecc_get_generator(void);
3738
int test_wc_ecc_size(void);
3839
int test_wc_ecc_params(void);
@@ -107,6 +108,7 @@ int test_wc_ecc_fp_cache_curve_binding(void);
107108
TEST_DECL_GROUP("ecc", test_wc_ecc_init), \
108109
TEST_DECL_GROUP("ecc", test_wc_ecc_check_key), \
109110
TEST_DECL_GROUP("ecc", test_wc_ecc_check_key_invalid_pubkey), \
111+
TEST_DECL_GROUP("ecc", test_wc_ecc_sp_pubkey_range), \
110112
TEST_DECL_GROUP("ecc", test_wc_ecc_get_generator), \
111113
TEST_DECL_GROUP("ecc", test_wc_ecc_size), \
112114
TEST_DECL_GROUP("ecc", test_wc_ecc_params), \

‎tests/api/test_wolfmath.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,23 @@ int test_wc_SpIntExptGcdDecisionCoverage(void)
14411441
ExpectIntEQ(sp_set(&a, 20), 0);
14421442
ExpectIntEQ(sp_exptmod_ex(&a, &b, 0, &m, &m), WC_NO_ERR_TRACE(MP_VAL));
14431443

1444+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
1445+
/* Base 2 with a multi-digit exponent, using both a single-digit and
1446+
* a multi-digit odd modulus. The exponent is 2^64 + 5. */
1447+
ExpectIntEQ(sp_set(&a, 2), MP_OKAY);
1448+
ExpectIntEQ(sp_2expt(&b, 64), MP_OKAY);
1449+
ExpectIntEQ(sp_add_d(&b, 5, &b), MP_OKAY);
1450+
ExpectIntEQ(sp_set(&m, 17), MP_OKAY);
1451+
ExpectIntEQ(mp_exptmod(&a, &b, &m, &r), MP_OKAY);
1452+
ExpectIntEQ(mp_cmp_d(&r, 15), MP_EQ);
1453+
1454+
/* 2^128 == -1 modulo (2^128 + 1), so 2^256 == 1. */
1455+
ExpectIntEQ(sp_2expt(&m, 128), MP_OKAY);
1456+
ExpectIntEQ(sp_add_d(&m, 1, &m), MP_OKAY);
1457+
ExpectIntEQ(mp_exptmod(&a, &b, &m, &r), MP_OKAY);
1458+
ExpectIntEQ(mp_cmp_d(&r, 32), MP_EQ);
1459+
#endif
1460+
14441461
/* sp_gcd: NULL args; a or b too big (>= SP_INT_DIGITS, skipped: needs
14451462
* an operand at the compile limit, documented residual); undersized
14461463
* dest; both zero (undefined); a zero, b nonzero (gcd = b); normal;

‎tests/unit-mcdc/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ justified in `iso26262/mcdc-per-module/reports/aes/RESIDUALS.md`:
6262
- **15833:0** - a dead defensive branch on a loop index provably bounded to
6363
`[0,7)`.
6464

65+
## Host SP coverage
66+
67+
Run the `sp-host-backends` matrix with `-DWOLFSSL_SP_INT_NEGATIVE` for
68+
each x86-64, portable C32, and portable C64 variant to exercise the ECC
69+
ordinate sign checks. Use the same flags in the library, API tests, and
70+
white-box translation units because this flag changes the SP integer layout.
71+
72+
The `sp-math` matrix uses both single-digit and multi-digit odd moduli for
73+
base-2 exponentiation. The allocation-failure driver includes both modulus
74+
sizes in the same executable so MC/DC can pair the generic engine's
75+
Montgomery and non-Montgomery paths.
76+
6577
## Adding a new white-box module
6678

6779
1. Create `test_<file>_whitebox.c` that `#include`s the target `.c` and, in

‎tests/unit-mcdc/test_sp_c32_whitebox.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,10 +1221,11 @@ int main(void)
12211221
wb_run_gap_521();
12221222
wb_spc_all();
12231223

1224-
printf("done (%s)\n", wb_fail ? "with skips" : "ok");
1224+
printf("done (%s)\n", WB_SPC_RANGE_FAILED ? "with failures" :
1225+
(wb_fail ? "with skips" : "ok"));
12251226
#else
12261227
printf(" no SP feature; nothing to exercise\n");
12271228
#endif
12281229
(void)wb_fail;
1229-
return 0;
1230+
return WB_SPC_RANGE_FAILED;
12301231
}

‎tests/unit-mcdc/test_sp_c64_whitebox.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,11 @@ int main(void)
965965
wb_run_point_specials_all();
966966
wb_spc_all();
967967

968-
printf("done (%s)\n", wb_bad ? "with failures" :
968+
printf("done (%s)\n", (wb_bad || WB_SPC_RANGE_FAILED) ? "with failures" :
969969
(wb_fail ? "with skips" : "ok"));
970970
#else
971971
printf(" no SP feature; nothing to exercise\n");
972972
#endif
973973
(void)wb_fail;
974-
return wb_bad ? 1 : 0;
974+
return wb_bad || WB_SPC_RANGE_FAILED;
975975
}

‎tests/unit-mcdc/test_sp_crafted_common.h‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,66 @@ static const byte wb_spc_zdigest[32] = {
256256
#define WB_SPC_HAVE_CHECK_KEY
257257
#endif
258258

259+
#if (defined(WOLFSSL_SP_X86_64_ASM) || !defined(WOLFSSL_SP_ASM)) && \
260+
!defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
261+
static int wb_spc_range_failed;
262+
#define WB_SPC_RANGE_FAILED wb_spc_range_failed
263+
264+
static void wb_spc_check_point_range(int bits, const ecc_point* g,
265+
const mp_int* prime, mp_int* other,
266+
int (*is_point)(const mp_int*, const mp_int*))
267+
{
268+
int xRet;
269+
int yRet;
270+
const int rangeErr = WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E);
271+
272+
/* Raw import screens out wide ordinates before this SP entry point. */
273+
if (mp_2expt(other, bits) != MP_OKAY || mp_count_bits(prime) != bits) {
274+
printf(" [wb][FAIL] P-%d point range test setup\n", bits);
275+
wb_spc_range_failed = 1;
276+
return;
277+
}
278+
279+
xRet = is_point(other, g->y);
280+
yRet = is_point(g->x, other);
281+
if (xRet != rangeErr || yRet != rangeErr) {
282+
printf(" [wb][FAIL] P-%d wide point range (X: %d, Y: %d)\n",
283+
bits, xRet, yRet);
284+
wb_spc_range_failed = 1;
285+
}
286+
287+
xRet = is_point(prime, g->y);
288+
yRet = is_point(g->x, prime);
289+
if (xRet != rangeErr || yRet != rangeErr) {
290+
printf(" [wb][FAIL] P-%d field prime range (X: %d, Y: %d)\n",
291+
bits, xRet, yRet);
292+
wb_spc_range_failed = 1;
293+
}
294+
295+
#ifdef WOLFSSL_SP_INT_NEGATIVE
296+
if (mp_set(other, 1) != MP_OKAY) {
297+
printf(" [wb][FAIL] P-%d signed point range test setup\n", bits);
298+
wb_spc_range_failed = 1;
299+
return;
300+
}
301+
mp_setneg(other);
302+
xRet = is_point(other, g->y);
303+
yRet = is_point(g->x, other);
304+
if (xRet != rangeErr || yRet != rangeErr) {
305+
printf(" [wb][FAIL] P-%d signed point range (X: %d, Y: %d)\n",
306+
bits, xRet, yRet);
307+
wb_spc_range_failed = 1;
308+
}
309+
#endif
310+
}
311+
312+
#define WB_SPC_POINT_RANGE(BITS) \
313+
wb_spc_check_point_range(BITS, &key.pubkey, &modv, &bigv, \
314+
sp_ecc_is_point_##BITS);
315+
#else
316+
#define WB_SPC_POINT_RANGE(BITS)
317+
#endif
318+
259319
#define WB_SPC_DEFINE_CURVE(BITS, SZ, CURVE_ID, AMOK) \
260320
static void wb_spc_ecc_##BITS(void) \
261321
{ \
@@ -344,6 +404,7 @@ static void wb_spc_ecc_##BITS(void) \
344404
/* A real point, so the on-curve test passes and everything \
345405
* behind it runs for the first time. */ \
346406
(void)sp_ecc_is_point_##BITS(key.pubkey.x, key.pubkey.y); \
407+
WB_SPC_POINT_RANGE(BITS) \
347408
} \
348409
\
349410
WB_SPC_CHECK_KEY_BODY(BITS) \
@@ -572,6 +633,10 @@ static void wb_spc_ecc_all(void)
572633
}
573634
#endif
574635

636+
#ifndef WB_SPC_RANGE_FAILED
637+
#define WB_SPC_RANGE_FAILED 0
638+
#endif
639+
575640
/* ======================================================================= *
576641
* RSA / DH / ModExp: argument-range guards and the two data-dependent
577642
* shapes inside sp_DhExp_<n>.

‎tests/unit-mcdc/test_sp_int_fault_whitebox.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ static const char* WB_M1024_EVEN =
158158
#endif
159159

160160
/* Number of distinct operations swept. */
161-
#define WB_OP_COUNT 34
161+
#if defined(WB_HAVE_EXPTMOD_BASE_2) && !defined(HAVE_SELFTEST) && \
162+
!defined(HAVE_FIPS)
163+
#define WB_SWEEP_BASE_2_SINGLE_DIGIT
164+
#define WB_OP_COUNT 35
165+
#else
166+
#define WB_OP_COUNT 34
167+
#endif
162168

163169
#ifndef WC_NO_RNG
164170
static WC_RNG wb_rng;
@@ -232,6 +238,16 @@ static void wb_op(int op, int n)
232238
goto done;
233239
}
234240
break;
241+
#ifdef WB_SWEEP_BASE_2_SINGLE_DIGIT
242+
case 34:
243+
/* Pair the single-digit modulus with case 10's multi-digit one
244+
* in the same binary for the generic engine's useMont decisions. */
245+
if (mp_set(&m, 17) != MP_OKAY || mp_set(&b, 2) != MP_OKAY) {
246+
wb_fail = 1;
247+
goto done;
248+
}
249+
break;
250+
#endif
235251
default:
236252
if (mp_read_radix(&m, WB_M_ODD, MP_RADIX_HEX) != MP_OKAY) {
237253
goto done;
@@ -398,6 +414,11 @@ static void wb_op(int op, int n)
398414
(void)_sp_div(&a, &m, &q, &r, (unsigned int)(a.used + 1U));
399415
#endif
400416
break;
417+
#ifdef WB_SWEEP_BASE_2_SINGLE_DIGIT
418+
case 34:
419+
(void)mp_exptmod(&b, &e, &m, &r);
420+
break;
421+
#endif
401422
default:
402423
break;
403424
}

0 commit comments

Comments
 (0)