Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/api/test_dsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ int test_wc_DsaSignVerify(void)
mp_free(&key.q);
ExpectIntEQ(mp_init(&key.q), 0);
ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#ifdef NO_DSA_PUBKEY_CHECK
/* With domain checks disabled, verify must still reject an empty q. */
ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif

mp_set(&key.q, 1);
ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand Down Expand Up @@ -647,6 +652,8 @@ int test_wc_DsaCheckPubKey(void)
byte digest[WC_SHA_DIGEST_SIZE];
/* signature is r || s, each q-sized (20 bytes for 160-bit q). */
byte sig[2 * 20];
const byte oversizedQ[DSA_MAX_HALF_SIZE + 1] = { 1 };
byte oversizedSig[2 * sizeof(oversizedQ)] = { 0 };

XMEMSET(&key, 0, sizeof(DsaKey));
XMEMSET(digest, 0xAA, sizeof(digest));
Expand Down Expand Up @@ -743,6 +750,17 @@ int test_wc_DsaCheckPubKey(void)
ExpectIntEQ(mp_set(&key.q, 1), 0);
ExpectIntEQ(wc_DsaCheckPubKey(&key), WC_NO_ERR_TRACE(BAD_FUNC_ARG));

/* q = p-1 is even and g = y = p-1, so both exponentiation checks
* evaluate to 1. Verify independently enforces the signature's q limit. */
ExpectIntEQ(mp_read_unsigned_bin(&key.q, oversizedQ, sizeof(oversizedQ)),
MP_OKAY);
ExpectIntEQ(mp_add_d(&key.q, 1, &key.p), MP_OKAY);
ExpectIntEQ(mp_copy(&key.q, &key.g), MP_OKAY);
ExpectIntEQ(mp_copy(&key.q, &key.y), MP_OKAY);
ExpectIntEQ(wc_DsaCheckPubKey(&key), 0);
ExpectIntEQ(wc_DsaVerify(digest, oversizedSig, &key, &answer),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

wc_FreeDsaKey(&key);
#endif
return EXPECT_RESULT();
Expand Down
5 changes: 5 additions & 0 deletions tests/api/test_ed448.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ int test_wc_ed448_init_id(void)
ExpectIntEQ(wc_ed448_init_id(&key, NULL, 0, NULL, INVALID_DEVID), 0);
ExpectIntEQ(key.idLen, 0);
wc_ed448_free(&key);
#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
ExpectIntEQ(wc_ed448_init_id(&key, id, 0, NULL, INVALID_DEVID), 0);
ExpectIntEQ(key.idLen, 0);
wc_ed448_free(&key);
#endif
ExpectIntEQ(wc_ed448_init_id(&key, id, ED448_MAX_ID_LEN, NULL,
INVALID_DEVID), 0);
ExpectIntEQ(key.idLen, ED448_MAX_ID_LEN);
Expand Down
16 changes: 16 additions & 0 deletions tests/api/test_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,22 @@ int test_wc_HmacId_NoDevice(void)
XFREE(bigKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);

#ifdef WOLF_CRYPTO_CB
{
word32 macSz = sizeof(mac);

ExpectIntEQ(wc_HmacInit_Label(&hmac, "hmac-key", NULL,
INVALID_DEVID), 0);
ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, 0), 0);
ExpectIntEQ(wc_HmacUpdate(&hmac, msg, sizeof(msg)),
WC_NO_ERR_TRACE(MISSING_KEY));
ExpectIntEQ(wc_HmacFinal(&hmac, mac), WC_NO_ERR_TRACE(MISSING_KEY));
ExpectIntEQ(wc_HmacGenerate_ex(&hmac, WC_SHA256, mac, &macSz, msg,
sizeof(msg), NULL, 0, NULL, INVALID_DEVID),
WC_NO_ERR_TRACE(MISSING_KEY));
ExpectIntEQ(hmac.idLen, 0);
ExpectIntGT(hmac.labelLen, 0);
wc_HmacFree(&hmac);
}
ExpectIntEQ(wc_HmacInit_Id(&hmac, id, (int)sizeof(id), NULL,
INVALID_DEVID), 0);
ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, 0), 0);
Expand Down
108 changes: 108 additions & 0 deletions tests/unit-mcdc/test_cryptocb_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,32 @@ int main(void)
NULL) != 0)
wb_fail = 1;

#if defined(WC_CRYPTOCB_KEYGEN_ID_CHECK) && !defined(HAVE_FIPS) && \
!defined(HAVE_SELFTEST)
{
CryptoCb* publishing = wc_CryptoCb_GetFreeDevice();

if (!wb_fail && (wc_CryptoCb_HasPkDevice(WB_DEVID) != 1 ||
wc_CryptoCb_HasPkDevice(WB_DEVID_NONE) != 0 ||
wc_CryptoCb_HasPkDevice(WB_DEVID_NOCB) != 0)) {
WB_NOTE("unexpected public-key device lookup result");
wb_bad = 1;
}
/* Registration publishes the callback before publishing its ID. */
if (publishing == NULL) {
wb_fail = 1;
}
else {
publishing->cb = wb_cb;
if (wc_CryptoCb_HasPkDevice(INVALID_DEVID) != 0) {
WB_NOTE("unpublished public-key device found");
wb_bad = 1;
}
publishing->cb = NULL;
}
}
#endif

/* ---- RSA ---- */
#ifndef NO_RSA
{
Expand Down Expand Up @@ -423,6 +449,31 @@ int main(void)
/* Residual: EccCheckPubKey's "key == NULL || key->dp == NULL". */
(void)wc_CryptoCb_EccCheckPubKey(NULL, 0, 0); /* key==NULL */
(void)wc_CryptoCb_EccCheckPubKey(&eccKeyNoDp, 0, 0); /* dp==NULL */
#if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
defined(WOLFSSL_SP_INT_NEGATIVE)) && !defined(HAVE_FIPS) && \
!defined(HAVE_SELFTEST)
{
int type = eccKey.type;

eccKey.type = ECC_PUBLICKEY;
if (mp_read_radix(eccKey.pubkey.x, "-1", MP_RADIX_HEX) !=
Comment thread
danielinux marked this conversation as resolved.
Resolved
MP_OKAY ||
wc_CryptoCb_EccCheckPubKey(&eccKey, 0, 0) !=
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) {
WB_NOTE("negative ECC x coordinate check failed");
wb_bad = 1;
}
mp_zero(eccKey.pubkey.x);
if (mp_read_radix(eccKey.pubkey.y, "-1", MP_RADIX_HEX) !=
MP_OKAY ||
wc_CryptoCb_EccCheckPubKey(&eccKey, 0, 0) !=
WC_NO_ERR_TRACE(ECC_OUT_OF_RANGE_E)) {
WB_NOTE("negative ECC y coordinate check failed");
wb_bad = 1;
}
eccKey.type = type;
}
#endif
#endif
WB_NOTE("ECC: EccMakePub/EccCheckPubKey arg-validation guard "
"(key==NULL / pubOut==NULL / key->dp==NULL) driven");
Expand All @@ -437,6 +488,7 @@ int main(void)
"ecc_point (not the zeroed pubOutPt used above) so the "
"subsequent mp_read_unsigned_bin calls are safe; left "
"out of scope for this pass rather than risk it");
(void)wc_ecc_free(&eccKeyNoDp);
}
else {
WB_NOTE("ECC: wc_ecc_set_curve failed; EccMakePub/"
Expand All @@ -445,6 +497,8 @@ int main(void)

WB_NOTE("ECC: MakeEccKey/Ecdh/EccSign/EccVerify/EccCheckPrivKey/"
"EccGetSize/EccGetSigSize dev&&dev->cb driven");
(void)wc_ecc_free(&eccKey2);
(void)wc_ecc_free(&eccKey);
}
#else
WB_NOTE("HAVE_ECC not defined; ECC dispatch skipped");
Expand Down Expand Up @@ -519,6 +573,14 @@ int main(void)
wc_CryptoCb_Ed25519MakePub(&e1, edPub, sizeof(edPub)));

WB_DRIVE3(e1.devId, wc_CryptoCb_Ed25519CheckKey(&e1));
#if defined(ED25519_MAX_ID_LEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
e1.devId = WB_DEVID;
e1.idLen = 1;
(void)wc_CryptoCb_Ed25519CheckKey(&e1);
e1.idLen = 0;
e1.labelLen = 1;
(void)wc_CryptoCb_Ed25519CheckKey(&e1);
#endif

WB_NOTE("Ed25519: Gen/Sign/Verify/MakePub/CheckKey dev&&dev->cb "
"driven");
Expand All @@ -543,6 +605,9 @@ int main(void)
#ifdef HAVE_ED448
{
ed448_key e4;
#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
byte edPub[ED448_PUB_KEY_SIZE];
#endif
XMEMSET(&e4, 0, sizeof(e4));
(void)wc_ed448_init_ex(&e4, NULL, 0);

Expand All @@ -553,12 +618,55 @@ int main(void)
WB_DRIVE3(e4.devId, wc_CryptoCb_Ed448Verify(out, sizeof(out), in,
sizeof(in), &res, &e4, 0, NULL, 0));

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
XMEMSET(edPub, 0, sizeof(edPub));
WB_DRIVE3(e4.devId, wc_CryptoCb_Ed448Gen(NULL, ED448_KEY_SIZE, &e4));
WB_DRIVE3(e4.devId,
wc_CryptoCb_Ed448MakePub(&e4, edPub, sizeof(edPub)));
WB_DRIVE3(e4.devId, wc_CryptoCb_Ed448CheckKey(&e4));

e4.devId = WB_DEVID;
(void)wc_CryptoCb_Ed448MakePub(NULL, edPub, sizeof(edPub));
(void)wc_CryptoCb_Ed448MakePub(&e4, NULL, sizeof(edPub));
(void)wc_CryptoCb_Ed448MakePub(&e4, edPub, sizeof(edPub) - 1);
#ifdef ED448_MAX_ID_LEN
e4.idLen = 1;
(void)wc_CryptoCb_Ed448CheckKey(&e4);
e4.idLen = 0;
e4.labelLen = 1;
(void)wc_CryptoCb_Ed448CheckKey(&e4);
#endif
#endif
wc_ed448_free(&e4);

WB_NOTE("Ed448: Ed448Sign/Ed448Verify dev&&dev->cb driven");
}
#else
WB_NOTE("HAVE_ED448 not defined; Ed448 dispatch skipped");
#endif /* HAVE_ED448 */

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
#ifndef NO_DH
{
DhKey dh;
XMEMSET(&dh, 0, sizeof(dh));
outLen = sizeof(out);
WB_DRIVE3(dh.devId, wc_CryptoCb_Dh(&dh, in, sizeof(in), in,
sizeof(in), out, &outLen));
}
#endif
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
WB_DRIVE3(devId, wc_CryptoCb_Chacha20Poly1305Encrypt(devId, in, nonce,
NULL, 0, in, sizeof(in), out, tag));
WB_DRIVE3(devId, wc_CryptoCb_Chacha20Poly1305Decrypt(devId, in, nonce,
NULL, 0, in, sizeof(in), tag, out));
#endif
#if defined(HAVE_PBKDF2) && !defined(NO_HMAC) && !defined(NO_PWDBASED)
WB_DRIVE3(devId, wc_CryptoCb_Pbkdf2(out, in, sizeof(in), nonce,
sizeof(nonce), 1, sizeof(out), WC_HASH_TYPE_SHA256, devId));
#endif
#endif

/* ---- AES ---- */
#ifndef NO_AES
{
Expand Down
29 changes: 28 additions & 1 deletion tests/unit-mcdc/test_eccsi_fault_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,27 @@ static void wb_mp_sweeps(WC_RNG* rng)
WB_NOTE("big-integer fault sweeps done");
}

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
static int wb_check_ssk_order(EccsiKey* key)
{
int ret;
int retFault;

/* With a readable order, the order itself is an out-of-range SSK.
* Hold that input fixed when forcing the order read to fail. */
ret = eccsi_check_ssk(key, &key->params.order);
mcdc_fm_arm(1);
retFault = eccsi_check_ssk(key, &key->params.order);
mcdc_fm_disarm();

if (ret != WC_NO_ERR_TRACE(ECC_PRIV_KEY_E) || retFault != MCDC_FM_ERR) {
printf(" [wb] SSK order check returned %d and %d\n", ret, retFault);
return 1;
}
return 0;
}
#endif

int main(int argc, char** argv)
{
int do_sweep = !(argc > 1 && strcmp(argv[1], "baseline") == 0);
Expand All @@ -818,6 +839,7 @@ int main(int argc, char** argv)
mp_digit mp = 0;
int ret;
int n_idx;
int sskFailed = 0;
const int K = 60; /* over-sweep past the mulmod/point-add allocation sites */

/* Unbuffered: if an armed call ever crashes, the notes printed so far
Expand Down Expand Up @@ -873,6 +895,11 @@ int main(int argc, char** argv)
goto cleanup;
}

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
if (do_sweep)
sskFailed = wb_check_ssk_order(&key);
#endif

/* ---- baseline: one unarmed success of each targeted helper (err==0
* TRUE side of the map guard, both map polarities). ---- */
(void)reload_base(&key);
Expand Down Expand Up @@ -965,7 +992,7 @@ int main(int argc, char** argv)
printf("done (%s)\n", wb_error ? "with failures" :
(wb_fail ? "with skips" : "ok"));
(void)wb_fail;
return 0;
return sskFailed;
}

#endif /* WOLFCRYPT_HAVE_ECCSI && WOLFCRYPT_ECCSI_CLIENT */
28 changes: 28 additions & 0 deletions tests/unit-mcdc/test_logging_globalq_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,30 @@ static void wb_pull_frees_node(void)
#endif
}

#if defined(WOLFSSL_HAVE_ERROR_QUEUE) && !defined(ERROR_QUEUE_PER_THREAD) && \
!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
static void wb_error_queue_marks(void)
{
wc_ClearErrorNodes();
WB_CHECK(wc_SetErrorNodeMark() == 0, "cannot mark an empty queue");
WB_CHECK(wc_PopErrorNodesToMark() == 0, "no mark in empty queue");
if (wc_AddErrorNode(WC_NO_ERR_TRACE(BAD_FUNC_ARG), 1, (char*)"mark",
(char*)"synthetic.c") != 0 || wc_last_node == NULL) {
WB_CHECK(0, "cannot add an entry for marking");
wc_ClearErrorNodes();
return;
}

wc_last_node->marks = WOLFSSL_MAX_32BIT;
WB_CHECK(wc_SetErrorNodeMark() == 0, "cannot add a mark at saturation");
WB_CHECK(wc_last_node->marks == WOLFSSL_MAX_32BIT, "mark did not wrap");
wc_last_node->marks = 0;
WB_CHECK(wc_SetErrorNodeMark() == 1, "mark a populated queue");
WB_CHECK(wc_PopErrorNodesToMark() == 1, "pop the existing mark");
wc_ClearErrorNodes();
}
#endif

int main(void)
{
printf("logging.c global error-queue white-box supplement\n");
Expand All @@ -323,6 +347,10 @@ int main(void)
wb_peek_line_data_ignore_err();
wb_remove_error_node();
wb_pull_frees_node();
#if defined(WOLFSSL_HAVE_ERROR_QUEUE) && !defined(ERROR_QUEUE_PER_THREAD) && \
!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
wb_error_queue_marks();
#endif

#if defined(WOLFSSL_HAVE_ERROR_QUEUE) && !defined(ERROR_QUEUE_PER_THREAD)
(void)wc_LoggingCleanup();
Expand Down
24 changes: 24 additions & 0 deletions tests/unit-mcdc/test_logging_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ static void wb_error_queue(void)

wc_ClearErrorNodes();

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
if (wc_SetErrorNodeMark() != 0)
wb_fail = 1;
#endif

/* get_abs_idx() via wc_PeekErrorNode(): (count==0)=T -> short circuit,
* decision TRUE regardless of relative_idx. */
ret = wc_PeekErrorNode(0, &file, &reason, &line);
Expand All @@ -250,6 +255,25 @@ static void wb_error_queue(void)
(char*)"synthetic.c") != 0)
wb_fail = 1;

#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
{
struct wc_error_entry* entry = get_entry(-1);

if (entry == NULL) {
wb_fail = 1;
}
else {
entry->marks = WOLFSSL_MAX_32BIT;
if (wc_SetErrorNodeMark() != 0 ||
entry->marks != WOLFSSL_MAX_32BIT)
wb_fail = 1;
entry->marks = 0;
if (wc_SetErrorNodeMark() != 1 || wc_PopErrorNodesToMark() != 1)
wb_fail = 1;
}
}
#endif

/* (count==0)=F, (relative_idx >= count)=T -> decision TRUE via 2nd
* operand. */
ret = wc_PeekErrorNode(5, &file, &reason, &line);
Expand Down
Loading
Loading