Skip to content

Commit 7b50124

Browse files
Merge pull request #10876 from danielinux/mcdc-test-coverage
Mcdc test coverage campaign - part 2
2 parents e87f7e8 + 7a80753 commit 7b50124

39 files changed

Lines changed: 7957 additions & 24 deletions

tests/api/test_aes.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8077,6 +8077,14 @@ int test_wc_AesEaxArgMcdc(void)
80778077
/* cond: in == NULL */
80788078
ExpectIntEQ(wc_AesEaxEncryptUpdate(&eax, out, NULL, sizeof(in), NULL, 0),
80798079
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
8080+
/* cond: authInSz > 0 && authIn == NULL -> BAD_FUNC_ARG (both the
8081+
* authInSz>0 and authIn==NULL conditions true). */
8082+
ExpectIntEQ(wc_AesEaxEncryptUpdate(&eax, out, in, sizeof(in), NULL,
8083+
sizeof(in)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
8084+
/* cond: authInSz > 0 && authIn != NULL -> valid (authIn==NULL false while
8085+
* authInSz>0 true), completing that pair. */
8086+
ExpectIntEQ(wc_AesEaxEncryptUpdate(&eax, out, in, sizeof(in), in,
8087+
sizeof(in)), 0);
80808088
ExpectIntEQ(wc_AesEaxFree(&eax), 0);
80818089

80828090
/* ---- wc_AesEaxDecryptUpdate(): eax/out/in OR-chain ---- */
@@ -8094,6 +8102,12 @@ int test_wc_AesEaxArgMcdc(void)
80948102
/* cond: in == NULL */
80958103
ExpectIntEQ(wc_AesEaxDecryptUpdate(&eax, out, NULL, sizeof(in), NULL, 0),
80968104
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
8105+
/* cond: authInSz > 0 && authIn == NULL -> BAD_FUNC_ARG. */
8106+
ExpectIntEQ(wc_AesEaxDecryptUpdate(&eax, out, in, sizeof(in), NULL,
8107+
sizeof(in)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
8108+
/* cond: authInSz > 0 && authIn != NULL -> valid, completing the pair. */
8109+
ExpectIntEQ(wc_AesEaxDecryptUpdate(&eax, out, in, sizeof(in), in,
8110+
sizeof(in)), 0);
80978111
ExpectIntEQ(wc_AesEaxFree(&eax), 0);
80988112

80998113
/* ---- wc_AesEaxEncryptFinal(): authTag == NULL / authTagSz == 0 ---- */

tests/api/test_chacha.c

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ int test_wc_Chacha_SetKey(void)
6161
/* Test bad args. */
6262
ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0),
6363
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
64+
/* ctx valid, inIv NULL: independence pair for the SetIV OR's 2nd arg. */
65+
ExpectIntEQ(wc_Chacha_SetIV(&ctx, NULL, 0),
66+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
67+
68+
/* ctx valid, key NULL: independence pair for the SetKey OR's 2nd arg. */
69+
ExpectIntEQ(wc_Chacha_SetKey(&ctx, NULL, keySz),
70+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
71+
/* half-length key (16 bytes / tau constant) is also valid. */
72+
ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, CHACHA_MAX_KEY_SZ / 2), 0);
73+
74+
/* misaligned key pointer: exercises the (wc_ptr_t)key % 4 realignment
75+
* decision in wc_Chacha_SetKey when XSTREAM_ALIGN is forced on (the
76+
* xstream_align campaign variant). settings.h compiles XSTREAM_ALIGN out
77+
* by default on x86_64/i386/ia64 (NO_XSTREAM_ALIGN), so this call is a
78+
* harmless no-op realignment-free copy on every other build. */
79+
{
80+
byte keybuf[CHACHA_MAX_KEY_SZ + 1];
81+
XMEMCPY(keybuf + 1, key, sizeof(key));
82+
ExpectIntEQ(wc_Chacha_SetKey(&ctx, keybuf + 1, keySz), 0);
83+
}
6484
#endif
6585
return EXPECT_RESULT();
6686
} /* END test_wc_Chacha_SetKey */
@@ -179,9 +199,25 @@ int test_wc_Chacha_Process(void)
179199
}
180200
#endif
181201

182-
/* Test bad args. */
202+
/* Test bad args: independence pairs for each operand of the 3-way OR. */
183203
ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
184204
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
205+
ExpectIntEQ(wc_Chacha_Process(&enc, cipher, NULL, (word32)inlen),
206+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
207+
ExpectIntEQ(wc_Chacha_Process(&enc, NULL, (byte*)input, (word32)inlen),
208+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
209+
210+
/* msglen==0 with a pending leftover keystream block: independence pair
211+
* for the (msglen > 0 && ctx->left > 0) decision's first operand, held
212+
* with ctx->left > 0 (the other operand) true. Deliberately NOT under
213+
* the USE_INTEL_CHACHA_SPEEDUP-exclude guard above: portable C and the
214+
* intel-speedup dispatch each carry their own copy of this decision at
215+
* a different line, and this call is variant-agnostic so it closes
216+
* whichever one a given build compiled. */
217+
ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
218+
ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
219+
ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, 1), 0);
220+
ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, 0), 0);
185221
#endif
186222
return EXPECT_RESULT();
187223
} /* END test_wc_Chacha_Process */
@@ -617,3 +653,53 @@ int test_wc_Chacha_UnalignedBuffers(void)
617653
#endif
618654
return EXPECT_RESULT();
619655
} /* END test_wc_Chacha_UnalignedBuffers */
656+
657+
/*
658+
* Testing wc_XChacha_SetKey(). Exercises the XChaCha20 24-byte-nonce setup
659+
* (wc_HChacha_block, the two internal wc_Chacha_SetIV calls) and its
660+
* nonceSz argument check.
661+
*/
662+
int test_wc_Chacha_XChachaSetKey(void)
663+
{
664+
EXPECT_DECLS;
665+
#if defined(HAVE_CHACHA) && defined(HAVE_XCHACHA)
666+
ChaCha enc, dec;
667+
static const byte key[CHACHA_MAX_KEY_SZ] = {
668+
0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07,
669+
0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
670+
0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17,
671+
0x18,0x19,0x1a,0x1b, 0x1c,0x1d,0x1e,0x1f
672+
};
673+
static const byte nonce[XCHACHA_NONCE_BYTES] = {
674+
0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07,
675+
0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
676+
0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17
677+
};
678+
static const byte plain[32] = { 0 };
679+
byte cipher[sizeof(plain)];
680+
byte roundtrip[sizeof(plain)];
681+
682+
XMEMSET(&enc, 0, sizeof(enc));
683+
XMEMSET(&dec, 0, sizeof(dec));
684+
685+
/* Valid 24-byte nonce round trip. */
686+
ExpectIntEQ(wc_XChacha_SetKey(&enc, key, sizeof(key), nonce,
687+
sizeof(nonce), 0), 0);
688+
ExpectIntEQ(wc_XChacha_SetKey(&dec, key, sizeof(key), nonce,
689+
sizeof(nonce), 0), 0);
690+
ExpectIntEQ(wc_Chacha_Process(&enc, cipher, plain, sizeof(plain)), 0);
691+
ExpectIntEQ(wc_Chacha_Process(&dec, roundtrip, cipher, sizeof(plain)), 0);
692+
ExpectBufEQ(roundtrip, plain, sizeof(plain));
693+
694+
/* Bad nonceSz: independence pair for the nonceSz != XCHACHA_NONCE_BYTES
695+
* decision (single-condition, but still needs both outcomes for branch
696+
* coverage). */
697+
ExpectIntEQ(wc_XChacha_SetKey(&enc, key, sizeof(key), nonce,
698+
sizeof(nonce) - 1, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
699+
700+
/* Bad keySz propagates the wc_Chacha_SetKey() failure through. */
701+
ExpectIntEQ(wc_XChacha_SetKey(&enc, key, 18, nonce, sizeof(nonce), 0),
702+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
703+
#endif
704+
return EXPECT_RESULT();
705+
} /* END test_wc_Chacha_XChachaSetKey */

tests/api/test_chacha.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int test_wc_Chacha_MonteCarlo(void);
3131
int test_wc_Chacha_CounterOverflow(void);
3232
int test_wc_Chacha_InPlace(void);
3333
int test_wc_Chacha_UnalignedBuffers(void);
34+
int test_wc_Chacha_XChachaSetKey(void);
3435

3536
#define TEST_CHACHA_DECLS \
3637
TEST_DECL_GROUP("chacha", test_wc_Chacha_SetKey), \
@@ -39,6 +40,7 @@ int test_wc_Chacha_UnalignedBuffers(void);
3940
TEST_DECL_GROUP("chacha", test_wc_Chacha_MonteCarlo), \
4041
TEST_DECL_GROUP("chacha", test_wc_Chacha_CounterOverflow), \
4142
TEST_DECL_GROUP("chacha", test_wc_Chacha_InPlace), \
42-
TEST_DECL_GROUP("chacha", test_wc_Chacha_UnalignedBuffers)
43+
TEST_DECL_GROUP("chacha", test_wc_Chacha_UnalignedBuffers), \
44+
TEST_DECL_GROUP("chacha", test_wc_Chacha_XChachaSetKey)
4345

4446
#endif /* WOLFCRYPT_TEST_CHACHA_H */

0 commit comments

Comments
 (0)