Skip to content

Commit f3af02b

Browse files
authored
Merge pull request wolfSSL#12232 from danielinux/mcdc-10-ecc
MCDC part 10: close ECC coverage gaps in signed math and key validation
2 parents d31d625 + d607083 commit f3af02b

2 files changed

Lines changed: 277 additions & 19 deletions

File tree

‎tests/api/test_ecc.c‎

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,9 @@ int test_wc_ecc_ecies_ctx_devid_steps(void)
38793879
typedef struct EciesFindSeen {
38803880
int invoked;
38813881
int devId;
3882+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
3883+
int error;
3884+
#endif
38823885
} EciesFindSeen;
38833886

38843887
static int eciesFindArmed = 0;
@@ -3906,6 +3909,12 @@ static int myEciesFindCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
39063909
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
39073910
}
39083911
seen->devId = devIdArg;
3912+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
3913+
if (seen->error != 0) {
3914+
seen->invoked = 1;
3915+
return seen->error;
3916+
}
3917+
#endif
39093918
eciesFindArmed = 0;
39103919
ret = myEciesApiCryptoCb(devIdArg, info, &seen->invoked);
39113920
eciesFindArmed = 1;
@@ -4018,6 +4027,42 @@ int test_wc_ecc_ecies_find_cb(void)
40184027
ExpectIntEQ(XMEMCMP(plain, msg, sizeof(msg)), 0);
40194028
}
40204029

4030+
#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) && \
4031+
(defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
4032+
!defined(WOLFSSL_ECIES_GEN_IV))
4033+
/* The finder can route a NULL context, too. Exercise both device
4034+
* errors and a successful round trip through that route. */
4035+
eciesFindArmed = 1;
4036+
wc_CryptoCb_SetDeviceFindCb(myEciesFindCb);
4037+
seen.error = WC_NO_ERR_TRACE(WC_HW_E);
4038+
seen.invoked = 0;
4039+
outSz = (word32)sizeof(out);
4040+
ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, msg, sizeof(msg), out,
4041+
&outSz, NULL), WC_NO_ERR_TRACE(WC_HW_E));
4042+
ExpectIntEQ(seen.invoked, 1);
4043+
4044+
seen.error = 0;
4045+
seen.invoked = 0;
4046+
ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, msg, sizeof(msg), out,
4047+
&outSz, NULL), 0);
4048+
ExpectIntEQ(seen.invoked, 1);
4049+
4050+
seen.error = WC_NO_ERR_TRACE(WC_HW_E);
4051+
seen.invoked = 0;
4052+
plainSz = (word32)sizeof(plain);
4053+
ExpectIntEQ(wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
4054+
&plainSz, NULL), WC_NO_ERR_TRACE(WC_HW_E));
4055+
ExpectIntEQ(seen.invoked, 1);
4056+
4057+
seen.error = 0;
4058+
seen.invoked = 0;
4059+
ExpectIntEQ(wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
4060+
&plainSz, NULL), 0);
4061+
ExpectIntEQ(seen.invoked, 1);
4062+
ExpectIntEQ(plainSz, sizeof(msg));
4063+
ExpectBufEQ(plain, msg, sizeof(msg));
4064+
#endif
4065+
40214066
wc_CryptoCb_SetDeviceFindCb(NULL);
40224067
eciesFindArmed = 0;
40234068
wc_ecc_ctx_free(srvCtx);
@@ -4903,6 +4948,8 @@ int test_wc_ecc_mulmod_negative(void)
49034948
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
49044949
ExpectIntEQ(wc_ecc_mulmod(&wide, &base.pubkey, r, curve.pubkey.x,
49054950
curve.pubkey.y, 1), WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
4951+
ExpectIntEQ(wc_ecc_mulmod_ex(&wide, &base.pubkey, r, curve.pubkey.x,
4952+
curve.pubkey.y, 1, NULL), WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
49064953
ExpectIntEQ(wc_ecc_mulmod_ex2(&wide, &base.pubkey, r, curve.pubkey.x,
49074954
curve.pubkey.y, &order, NULL, 1, NULL),
49084955
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E));
@@ -4916,12 +4963,30 @@ int test_wc_ecc_mulmod_negative(void)
49164963
ExpectIntEQ(ecc_mul2add(&base.pubkey, NULL, &base.pubkey, &five, r,
49174964
curve.pubkey.x, curve.pubkey.y, NULL),
49184965
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
4966+
ExpectIntEQ(ecc_mul2add(NULL, &five, &base.pubkey, &five, r,
4967+
curve.pubkey.x, curve.pubkey.y, NULL),
4968+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
4969+
ExpectIntEQ(ecc_mul2add(&base.pubkey, &five, NULL, &five, r,
4970+
curve.pubkey.x, curve.pubkey.y, NULL),
4971+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
4972+
ExpectIntEQ(ecc_mul2add(&base.pubkey, &five, &base.pubkey, NULL, r,
4973+
curve.pubkey.x, curve.pubkey.y, NULL),
4974+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
4975+
ExpectIntEQ(ecc_mul2add(&base.pubkey, &five, &base.pubkey, &five, NULL,
4976+
curve.pubkey.x, curve.pubkey.y, NULL),
4977+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
4978+
ExpectIntEQ(ecc_mul2add(&base.pubkey, &five, &base.pubkey, &five, r,
4979+
curve.pubkey.x, NULL, NULL), WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
49194980
ExpectIntEQ(ecc_mul2add(&base.pubkey, &five, &base.pubkey, &five, r,
49204981
curve.pubkey.x, curve.pubkey.y, NULL), 0);
49214982
#endif
49224983
/* Positive scalars are still accepted. */
49234984
ExpectIntEQ(wc_ecc_mulmod(&five, &base.pubkey, r, curve.pubkey.x,
49244985
curve.pubkey.y, 1), 0);
4986+
ExpectIntEQ(wc_ecc_mulmod_ex(&five, &base.pubkey, r, curve.pubkey.x,
4987+
curve.pubkey.y, 1, NULL), 0);
4988+
ExpectIntEQ(wc_ecc_mulmod_ex2(&five, &base.pubkey, r, curve.pubkey.x,
4989+
curve.pubkey.y, &order, NULL, 1, NULL), 0);
49254990

49264991
wc_ecc_del_point(r);
49274992
mp_free(&wide);
@@ -6105,23 +6170,6 @@ int test_wc_EccDecisionCoverage4(void)
61056170
}
61066171
ExpectIntEQ(ret, 0);
61076172

6108-
/* ---- ecc_mul2add argument guard: the uncovered-condition report 8446 ----
6109-
* NOT closeable by any current variant, API or white-box: both bodies
6110-
* of ecc_mul2add() (the argument-checked "normal" one at line ~8417 and
6111-
* the Shamir/fixed-point-cache one at line ~13909 that supersedes it
6112-
* when FP_ECC is also on) live inside an outer #ifdef ECC_SHAMIR /
6113-
* #endif block (lines 8349-8701 and 13616-14035). Every one of this
6114-
* module's 6 variants has ECC_SHAMIR either ON-with-FP_ECC-ON (base:
6115-
* sp_default/sp_ecc/sp_ecc_nonblock/fastmath/small_stack, which
6116-
* exercises the unchecked Shamir body under the name ecc_mul2add) or
6117-
* turns BOTH ECC_SHAMIR and FP_ECC OFF together (no_fp_shamir, per its
6118-
* config_base's philosophy of flipping the FALSE side of both feature
6119-
* guards at once -- see the module registry's ecc notes), which compiles
6120-
* *neither* body, making ecc_mul2add an undefined symbol there (link
6121-
* failure, confirmed empirically). Reaching this decision needs a new,
6122-
* not-yet-scaffolded variant: ECC_SHAMIR on + FP_ECC off. Classified as
6123-
* a needs-variant residual; see RESIDUALS.md. */
6124-
61256173
/* ---- wc_ecc_ctx_set_kdf_salt: the uncovered-condition report 14607 ----
61266174
* if (ctx == NULL || (salt == NULL && sz != 0))
61276175
* ctx==NULL already the common BAD_FUNC_ARG idiom shown elsewhere; add
@@ -6143,12 +6191,26 @@ int test_wc_EccDecisionCoverage4(void)
61436191
#if defined(WOLFSSL_CUSTOM_CURVES)
61446192
{
61456193
ecc_key ccKey2;
6194+
ecc_set_type customDp;
6195+
61466196
XMEMSET(&ccKey2, 0, sizeof(ccKey2));
61476197
ExpectIntEQ(wc_ecc_init(&ccKey2), 0);
61486198
ExpectIntEQ(wc_ecc_set_custom_curve(NULL, key.dp),
61496199
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
61506200
ExpectIntEQ(wc_ecc_set_custom_curve(&ccKey2, NULL),
61516201
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
6202+
ExpectNotNull(key.dp);
6203+
if (EXPECT_SUCCESS()) {
6204+
XMEMCPY(&customDp, key.dp, sizeof(customDp));
6205+
customDp.size = 0;
6206+
ExpectIntEQ(wc_ecc_set_custom_curve(&ccKey2, &customDp),
6207+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
6208+
customDp.size = MAX_ECC_BYTES + 1;
6209+
ExpectIntEQ(wc_ecc_set_custom_curve(&ccKey2, &customDp),
6210+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
6211+
customDp.size = key.dp->size;
6212+
ExpectIntEQ(wc_ecc_set_custom_curve(&ccKey2, &customDp), 0);
6213+
}
61526214
wc_ecc_free(&ccKey2);
61536215
}
61546216
#endif

‎tests/unit-mcdc/test_ecc_whitebox.c‎

Lines changed: 198 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,8 +3960,195 @@ static void wb_fp_mul2add_kb_size(void)
39603960
}
39613961
#endif
39623962

3963+
/* These checks distinguish unavailable fixtures from assertion failures. */
3964+
enum wb_check_result {
3965+
WB_CHECK_FAILED = 0,
3966+
WB_CHECK_PASSED = 1,
3967+
WB_CHECK_SKIPPED
3968+
};
3969+
3970+
#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
3971+
defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ECC_SECP) && \
3972+
!defined(NO_ECC256) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
3973+
!defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) && \
3974+
(!defined(ECC_MIN_KEY_SZ) || (ECC_MIN_KEY_SZ <= 256))
3975+
static enum wb_check_result wb_private_order_size(void)
3976+
{
3977+
const ecc_set_type* curve;
3978+
ecc_key key;
3979+
byte scalar[32] = {0};
3980+
word32 len = 0;
3981+
int idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
3982+
int ok;
3983+
3984+
curve = wc_ecc_get_curve_params(idx);
3985+
if (curve == NULL || wc_ecc_init(&key) != 0) {
3986+
return WB_CHECK_SKIPPED;
3987+
}
3988+
3989+
ok = wc_ecc_export_private_order(NULL, NULL, &len) ==
3990+
WC_NO_ERR_TRACE(BAD_FUNC_ARG);
3991+
ok &= wc_ecc_export_private_order(&key, NULL, NULL) ==
3992+
WC_NO_ERR_TRACE(BAD_FUNC_ARG);
3993+
ok &= wc_ecc_export_private_order(&key, NULL, &len) ==
3994+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E);
3995+
ok &= ecc_check_privkey_range(NULL) == WC_NO_ERR_TRACE(BAD_FUNC_ARG);
3996+
ok &= ecc_check_privkey_range(&key) == WC_NO_ERR_TRACE(BAD_FUNC_ARG);
3997+
3998+
scalar[sizeof(scalar) - 1] = 1;
3999+
if (wc_ecc_import_private_key_ex(scalar, sizeof(scalar), NULL, 0, &key,
4000+
ECC_SECP256R1) != 0) {
4001+
wc_ecc_free(&key);
4002+
return ok ? WB_CHECK_SKIPPED : WB_CHECK_FAILED;
4003+
}
4004+
ok &= wc_ecc_export_private_order(&key, NULL, &len) ==
4005+
WC_NO_ERR_TRACE(LENGTH_ONLY_E);
4006+
ok &= len == sizeof(scalar);
4007+
ok &= ecc_check_privkey_range(&key) == 0;
4008+
#if defined(WOLFSSL_SP_INT_NEGATIVE) && !defined(WOLFSSL_ECC_BLIND_K)
4009+
mp_setneg(ecc_get_k(&key));
4010+
ok &= ecc_check_privkey_range(&key) == WC_NO_ERR_TRACE(ECC_PRIV_KEY_E);
4011+
ok &= mp_abs(ecc_get_k(&key), ecc_get_k(&key)) == 0;
4012+
#endif
4013+
4014+
/* The export rejects an invalid index even with valid curve parameters. */
4015+
key.idx = -2;
4016+
ok &= wc_ecc_export_private_order(&key, NULL, &len) ==
4017+
WC_NO_ERR_TRACE(ECC_BAD_ARG_E);
4018+
key.idx = idx;
4019+
wc_ecc_free(&key);
4020+
4021+
if (wc_ecc_init(&key) != 0) {
4022+
return ok ? WB_CHECK_SKIPPED : WB_CHECK_FAILED;
4023+
}
4024+
if (wc_ecc_import_raw_ex(&key, curve->Gx, curve->Gy, NULL,
4025+
ECC_SECP256R1) != 0) {
4026+
wc_ecc_free(&key);
4027+
return ok ? WB_CHECK_SKIPPED : WB_CHECK_FAILED;
4028+
}
4029+
ok &= wc_ecc_export_private_order(&key, NULL, &len) ==
4030+
WC_NO_ERR_TRACE(BAD_FUNC_ARG);
4031+
wc_ecc_free(&key);
4032+
4033+
if (wc_ecc_init(&key) != 0) {
4034+
return ok ? WB_CHECK_SKIPPED : WB_CHECK_FAILED;
4035+
}
4036+
if (wc_ecc_import_raw_ex(&key, curve->Gx, curve->Gy, "01",
4037+
ECC_SECP256R1) != 0) {
4038+
wc_ecc_free(&key);
4039+
return ok ? WB_CHECK_SKIPPED : WB_CHECK_FAILED;
4040+
}
4041+
ok &= wc_ecc_export_private_order(&key, NULL, &len) ==
4042+
WC_NO_ERR_TRACE(LENGTH_ONLY_E);
4043+
ok &= len == sizeof(scalar);
4044+
#if defined(HAVE_ECC_CHECK_PUBKEY_ORDER) && defined(WOLFSSL_HAVE_SP_ECC)
4045+
ok &= wc_ecc_check_key(&key) == 0;
4046+
ok &= _ecc_validate_public_key(&key, 0, 0) == 0;
4047+
#endif
4048+
wc_ecc_free(&key);
4049+
return ok ? WB_CHECK_PASSED : WB_CHECK_FAILED;
4050+
}
4051+
#define WB_ECC_PRIVATE_ORDER_SIZE
4052+
#endif
4053+
4054+
#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
4055+
defined(WOLFSSL_SP_INT_NEGATIVE) && defined(WOLFSSL_HAVE_SP_ECC) && \
4056+
!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_NO_256) && \
4057+
!defined(NO_ECC_SECP) && !defined(NO_ECC256) && \
4058+
!defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
4059+
!defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) && \
4060+
(!defined(ECC_MIN_KEY_SZ) || (ECC_MIN_KEY_SZ <= 256))
4061+
static enum wb_check_result wb_sp_mulmod_dispatch(void)
4062+
{
4063+
const ecc_set_type* curve =
4064+
wc_ecc_get_curve_params(wc_ecc_get_curve_idx(ECC_SECP256R1));
4065+
ecc_key base;
4066+
ecc_key params;
4067+
ecc_point* result = NULL;
4068+
mp_int k;
4069+
enum wb_check_result status = WB_CHECK_SKIPPED;
4070+
int fallback = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
4071+
4072+
if (curve == NULL || wc_ecc_init(&base) != 0) {
4073+
return status;
4074+
}
4075+
if (wc_ecc_init(&params) != 0) {
4076+
wc_ecc_free(&base);
4077+
return status;
4078+
}
4079+
if (mp_init(&k) != 0) {
4080+
goto out_keys;
4081+
}
4082+
result = wc_ecc_new_point();
4083+
if (result != NULL &&
4084+
wc_ecc_import_raw_ex(&base, curve->Gx, curve->Gy, NULL,
4085+
curve->id) == 0 &&
4086+
mp_read_radix(params.pubkey.x, curve->Af, MP_RADIX_HEX) == 0 &&
4087+
mp_read_radix(params.pubkey.y, curve->prime, MP_RADIX_HEX) == 0 &&
4088+
mp_set(&k, 1) == 0) {
4089+
mp_int* a = params.pubkey.x;
4090+
mp_int* p = params.pubkey.y;
4091+
mp_int* coords[] = {base.pubkey.x, result->x, result->y, result->z};
4092+
unsigned int i;
4093+
int ok;
4094+
4095+
ok = ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) == 0;
4096+
ok &= wc_ecc_cmp_point(&base.pubkey, result) == MP_EQ;
4097+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, NULL, p, 1, NULL) ==
4098+
fallback;
4099+
/* These representations must fall back before invoking SP math. */
4100+
for (i = 0; i < sizeof(coords) / sizeof(coords[0]); i++) {
4101+
mp_setneg(coords[i]);
4102+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) ==
4103+
fallback;
4104+
ok &= mp_abs(coords[i], coords[i]) == 0;
4105+
}
4106+
mp_setneg(&k);
4107+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) ==
4108+
fallback;
4109+
ok &= mp_2expt(&k, 256) == 0;
4110+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) ==
4111+
fallback;
4112+
ok &= mp_set(&k, 1) == 0;
4113+
ok &= mp_set(base.pubkey.z, 2) == 0;
4114+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) ==
4115+
fallback;
4116+
ok &= mp_set(base.pubkey.z, 1) == 0;
4117+
ok &= mp_set(a, 0) == 0;
4118+
ok &= ecc_sp_mulmod(&k, &base.pubkey, result, a, p, 1, NULL) ==
4119+
fallback;
4120+
status = ok ? WB_CHECK_PASSED : WB_CHECK_FAILED;
4121+
}
4122+
wc_ecc_del_point(result);
4123+
mp_clear(&k);
4124+
out_keys:
4125+
wc_ecc_free(&params);
4126+
wc_ecc_free(&base);
4127+
return status;
4128+
}
4129+
#define WB_ECC_SP_MULMOD_DISPATCH
4130+
#endif
4131+
4132+
#if defined(WB_ECC_PRIVATE_ORDER_SIZE) || defined(WB_ECC_SP_MULMOD_DISPATCH)
4133+
static void wb_record_check(enum wb_check_result result, const char* name,
4134+
int* failed)
4135+
{
4136+
if (result == WB_CHECK_SKIPPED) {
4137+
printf(" [wb] %s setup failed; skipped\n", name);
4138+
wb_fail = 1;
4139+
}
4140+
else if (result == WB_CHECK_FAILED) {
4141+
printf(" [wb][FAIL] %s returned an unexpected result\n", name);
4142+
*failed = 1;
4143+
}
4144+
}
4145+
#endif
4146+
39634147
int main(void)
39644148
{
4149+
#if defined(HAVE_ECC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
4150+
int failed = 0;
4151+
#endif
39654152
/* Unbuffered: on a timeout or a fault the process is killed and anything
39664153
* still buffered is lost, which reads as an empty log. */
39674154
setvbuf(stdout, NULL, _IONBF, 0);
@@ -3989,6 +4176,14 @@ int main(void)
39894176
wb_export_point_der();
39904177
wb_export_x963_internal();
39914178
wb_idx_dp_guard_export_paths();
4179+
#ifdef WB_ECC_PRIVATE_ORDER_SIZE
4180+
wb_record_check(wb_private_order_size(), "private-order size query",
4181+
&failed);
4182+
#endif
4183+
#ifdef WB_ECC_SP_MULMOD_DISPATCH
4184+
wb_record_check(wb_sp_mulmod_dispatch(), "SP point multiplication dispatch",
4185+
&failed);
4186+
#endif
39924187
wb_make_pub_privatekey_only();
39934188
wb_arg_guards();
39944189
wb_gap_pass2();
@@ -4007,9 +4202,10 @@ int main(void)
40074202
wb_deterministic_k_retry();
40084203
wb_ecies_bad_kdf();
40094204
wb_fp_mul2add_kb_size();
4010-
printf("done (%s)\n", wb_fail ? "with skips" : "ok");
4205+
printf("done (%s)\n", failed ? "with failures" :
4206+
(wb_fail ? "with skips" : "ok"));
40114207
/* Setup failures are surfaced as skips, not test failures: the harness
40124208
* treats a nonzero exit as a failed variant and discards its coverage. */
4013-
return 0;
4209+
return failed ? 1 : 0;
40144210
#endif
40154211
}

0 commit comments

Comments
 (0)