@@ -5369,6 +5369,114 @@ int test_wc_AesGcmStream_ReinitAfterFinal(void)
53695369 return EXPECT_RESULT();
53705370} /* END test_wc_AesGcmStream_ReinitAfterFinal */
53715371
5372+ /*
5373+ * MC/DC for the stream wc_AesGcmEncryptFinal() invocation-count guard
5374+ * (aes.c, stream builds):
5375+ *
5376+ * if ((ret == 0) && aes->ctrSet && (aes->aSz == 0) && (aes->cSz == 0))
5377+ * ret = GcmCountInvocation(aes);
5378+ *
5379+ * State model (verified against the source):
5380+ * - wc_AesGcmInit() with an IV sets gcmKeySet and nonceSet only.
5381+ * - wc_AesGcmSetIV() sets ctrSet and nonceSz only, not nonceSet.
5382+ * - clang-21's MC/DC pair analysis only pairs a vector with another one
5383+ * when every operand not varied is evaluated in at least one of the two
5384+ * vectors, so each operand below is paired against a row where the rest
5385+ * of the chain is fully evaluated.
5386+ *
5387+ * Vectors:
5388+ * A (T,T,T,T): IV at init + SetIV, no AAD, no data -> guard counts.
5389+ * B (T,T,F,T): same, AAD only.
5390+ * C (T,T,T,F): same, data only.
5391+ * D (F,-,-,-): same, bad tag size -> BAD_FUNC_ARG before the guard body.
5392+ * E (F,-,-,-): key only + SetIV (no nonceSet) -> MISSING_IV.
5393+ * F (T,F,-,-): IV at init, no SetIV -> ctrSet still 0, guard skipped.
5394+ *
5395+ * FIPS builds before v7 and the --enable-selftest build compile the
5396+ * boundary aes.c, whose stream wc_AesGcmEncryptFinal() path never calls
5397+ * the invocation-count guard, so the counter vectors do not hold there.
5398+ */
5399+ int test_wc_AesGcmStreamMcdc(void)
5400+ {
5401+ EXPECT_DECLS;
5402+ #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
5403+ defined(WOLFSSL_AESGCM_STREAM) && !defined(WC_NO_RNG) && \
5404+ (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0)) && \
5405+ !defined(HAVE_SELFTEST)
5406+ WC_RNG rng[1];
5407+ Aes aes[1];
5408+ byte key[AES_128_KEY_SIZE] = { 0 };
5409+ byte iv[AES_IV_SIZE] = { 1 };
5410+ byte in[WC_AES_BLOCK_SIZE] = { 0 };
5411+ byte out[WC_AES_BLOCK_SIZE];
5412+ byte aad[8] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11 };
5413+ byte tag[WC_AES_BLOCK_SIZE];
5414+
5415+ XMEMSET(rng, 0, sizeof(WC_RNG));
5416+ XMEMSET(aes, 0, sizeof(Aes));
5417+ ExpectIntEQ(wc_InitRng(rng), 0);
5418+
5419+ /* A (T,T,T,T): IV at init, SetIV for ctrSet, no AAD, no data -> the
5420+ * guard counts the invocation. */
5421+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5422+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), iv, sizeof(iv)), 0);
5423+ ExpectIntEQ(wc_AesGcmSetIV(aes, GCM_NONCE_MID_SZ, NULL, 0, rng), 0);
5424+ ExpectIntEQ(aes->invokeCtr[0], 0);
5425+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, sizeof(tag)), 0);
5426+ ExpectIntEQ(aes->invokeCtr[0], 1);
5427+ wc_AesFree(aes);
5428+
5429+ /* B (T,T,F,T): AAD only, no data. */
5430+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5431+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), iv, sizeof(iv)), 0);
5432+ ExpectIntEQ(wc_AesGcmSetIV(aes, GCM_NONCE_MID_SZ, NULL, 0, rng), 0);
5433+ ExpectIntEQ(wc_AesGcmEncryptUpdate(aes, NULL, NULL, 0, aad,
5434+ sizeof(aad)), 0);
5435+ ExpectIntEQ(aes->invokeCtr[0], 1);
5436+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, sizeof(tag)), 0);
5437+ ExpectIntEQ(aes->invokeCtr[0], 1);
5438+ wc_AesFree(aes);
5439+
5440+ /* C (T,T,T,F): data only, no AAD. */
5441+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5442+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), iv, sizeof(iv)), 0);
5443+ ExpectIntEQ(wc_AesGcmSetIV(aes, GCM_NONCE_MID_SZ, NULL, 0, rng), 0);
5444+ ExpectIntEQ(wc_AesGcmEncryptUpdate(aes, out, in, sizeof(in), NULL, 0),
5445+ 0);
5446+ ExpectIntEQ(aes->invokeCtr[0], 1);
5447+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, sizeof(tag)), 0);
5448+ ExpectIntEQ(aes->invokeCtr[0], 1);
5449+ wc_AesFree(aes);
5450+
5451+ /* D (F,-,-,-): an invalid tag size fails before the guard body. */
5452+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5453+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), iv, sizeof(iv)), 0);
5454+ ExpectIntEQ(wc_AesGcmSetIV(aes, GCM_NONCE_MID_SZ, NULL, 0, rng), 0);
5455+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, WC_AES_BLOCK_SIZE + 1),
5456+ WC_NO_ERR_TRACE(BAD_FUNC_ARG));
5457+ wc_AesFree(aes);
5458+
5459+ /* E (F,-,-,-): no IV at init, SetIV does not set nonceSet -> MISSING_IV. */
5460+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5461+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), NULL, 0), 0);
5462+ ExpectIntEQ(wc_AesGcmSetIV(aes, GCM_NONCE_MID_SZ, NULL, 0, rng), 0);
5463+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, sizeof(tag)),
5464+ WC_NO_ERR_TRACE(MISSING_IV));
5465+ wc_AesFree(aes);
5466+
5467+ /* F (T,F,-,-): IV at init but no SetIV: ret is 0, ctrSet is still 0,
5468+ * the guard is skipped and the tag is produced. */
5469+ ExpectIntEQ(wc_AesInit(aes, NULL, INVALID_DEVID), 0);
5470+ ExpectIntEQ(wc_AesGcmInit(aes, key, sizeof(key), iv, sizeof(iv)), 0);
5471+ ExpectIntEQ(wc_AesGcmEncryptFinal(aes, tag, sizeof(tag)), 0);
5472+ ExpectIntEQ(aes->invokeCtr[0], 0);
5473+ wc_AesFree(aes);
5474+
5475+ wc_FreeRng(rng);
5476+ #endif
5477+ return EXPECT_RESULT();
5478+ } /* END test_wc_AesGcmStreamMcdc */
5479+
53725480int test_wc_AesGcmStream_BadAuthTag(void)
53735481{
53745482 EXPECT_DECLS;
@@ -5722,6 +5830,14 @@ int test_wc_AesGcmDecrypt_WipeOnAuthFail(void)
57225830 ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, ct, sizeof(ct), iv, sizeof(iv),
57235831 tag, sizeof(tag), NULL, 0), 0);
57245832 ExpectBufEQ(dec, pt, sizeof(pt));
5833+
5834+ /* GMAC (sz == 0) with a bad tag: the wipe guard's "sz > 0" operand
5835+ * takes its false half (in/out are don't-cares for a zero-size decrypt,
5836+ * so the argument check accepts them). */
5837+ tag[0] ^= 0x01;
5838+ ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, NULL, 0, iv, sizeof(iv),
5839+ tag, sizeof(tag), NULL, 0), WC_NO_ERR_TRACE(AES_GCM_AUTH_E));
5840+ tag[0] ^= 0x01;
57255841 wc_AesFree(&aes);
57265842#endif
57275843 return EXPECT_RESULT();
@@ -10765,6 +10881,12 @@ int test_wc_AesCcmArgMcdc(void)
1076510881 ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
1076610882 ExpectIntEQ(wc_AesCcmSetKey(&aes, key, sizeof(key)), 0);
1076710883
10884+ /* wc_AesCcmSetKey() "aes != NULL" operand, false half: the NULL
10885+ * context is rejected by the wc_AesSetKey() it forwards to, so the
10886+ * call is safe in every build. */
10887+ ExpectIntEQ(wc_AesCcmSetKey(NULL, key, sizeof(key)),
10888+ WC_NO_ERR_TRACE(BAD_FUNC_ARG));
10889+
1076810890 /* ---- wc_AesCcmEncrypt(): authTagSz > WC_AES_BLOCK_SIZE cond. */
1076910891 ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipher, plain, sizeof(plain),
1077010892 nonce13, sizeof(nonce13), tag, sizeof(tag), aad, sizeof(aad)),
0 commit comments