Skip to content

Commit 5df717e

Browse files
committed
tests: add ChaCha20-Poly1305 AEAD tag negative test (F-2921)
Cover the Poly1305 ConstantCompare tag check in ChachaAEADDecrypt that no existing test was hitting (VERIFY_MAC_ERROR never expected in the suite). A memio-based TLS 1.2 handshake over ECDHE-RSA-CHACHA20-POLY1305 completes, the server's IORecv is then replaced with a wrapper that flips the final byte of the next record body so the forged Poly1305 tag no longer matches. The server's wolfSSL_read must surface VERIFY_MAC_ERROR.
1 parent e32d4c0 commit 5df717e

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

tests/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35902,6 +35902,7 @@ TEST_CASE testCases[] = {
3590235902
#endif
3590335903
TEST_DECL(test_tls_ems_downgrade),
3590435904
TEST_DECL(test_tls_ems_resumption_downgrade),
35905+
TEST_DECL(test_tls12_chacha20_poly1305_bad_tag),
3590535906
TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
3590635907
TEST_DECL(test_certificate_authorities_certificate_request),
3590735908
TEST_DECL(test_certificate_authorities_client_hello),

tests/api/test_tls_ext.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,71 @@ int test_tls_ems_resumption_downgrade(void)
153153
}
154154

155155

156+
#if !defined(WOLFSSL_NO_TLS12) && \
157+
defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
158+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
159+
static int test_chacha_bad_tag_trigger = 0;
160+
161+
static int test_chacha_bad_tag_io_recv(WOLFSSL* ssl, char* buf, int sz,
162+
void* ctx)
163+
{
164+
int ret = test_memio_read_cb(ssl, buf, sz, ctx);
165+
/* Flip the final byte of the record body (skips the 5-byte header
166+
* read) so the Poly1305 tag no longer matches. */
167+
if (test_chacha_bad_tag_trigger && ret > 5) {
168+
buf[ret - 1] ^= 0xFF;
169+
test_chacha_bad_tag_trigger = 0;
170+
}
171+
return ret;
172+
}
173+
#endif
174+
175+
/* F-2921: TLS 1.2 ChaCha20-Poly1305 must surface VERIFY_MAC_ERROR when
176+
* the Poly1305 tag is corrupted. */
177+
int test_tls12_chacha20_poly1305_bad_tag(void)
178+
{
179+
EXPECT_DECLS;
180+
#if !defined(WOLFSSL_NO_TLS12) && \
181+
defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
182+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
183+
struct test_memio_ctx test_ctx;
184+
WOLFSSL_CTX *ctx_c = NULL;
185+
WOLFSSL_CTX *ctx_s = NULL;
186+
WOLFSSL *ssl_c = NULL;
187+
WOLFSSL *ssl_s = NULL;
188+
const char msg[] = "tamper me";
189+
char recvBuf[32];
190+
int ret;
191+
192+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
193+
test_ctx.c_ciphers = test_ctx.s_ciphers =
194+
"ECDHE-RSA-CHACHA20-POLY1305";
195+
196+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
197+
wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
198+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
199+
200+
wolfSSL_SSLSetIORecv(ssl_s, test_chacha_bad_tag_io_recv);
201+
202+
ExpectIntEQ(wolfSSL_write(ssl_c, msg, (int)XSTRLEN(msg)),
203+
(int)XSTRLEN(msg));
204+
205+
test_chacha_bad_tag_trigger = 1;
206+
ret = wolfSSL_read(ssl_s, recvBuf, sizeof(recvBuf));
207+
ExpectIntLE(ret, 0);
208+
ExpectIntEQ(wolfSSL_get_error(ssl_s, ret),
209+
WC_NO_ERR_TRACE(VERIFY_MAC_ERROR));
210+
211+
test_chacha_bad_tag_trigger = 0;
212+
wolfSSL_free(ssl_c);
213+
wolfSSL_free(ssl_s);
214+
wolfSSL_CTX_free(ctx_c);
215+
wolfSSL_CTX_free(ctx_s);
216+
#endif
217+
return EXPECT_RESULT();
218+
}
219+
220+
156221
int test_wolfSSL_DisableExtendedMasterSecret(void)
157222
{
158223
EXPECT_DECLS;

tests/api/test_tls_ext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
int test_tls_ems_downgrade(void);
2626
int test_tls_ems_resumption_downgrade(void);
27+
int test_tls12_chacha20_poly1305_bad_tag(void);
2728
int test_wolfSSL_DisableExtendedMasterSecret(void);
2829
int test_certificate_authorities_certificate_request(void);
2930
int test_certificate_authorities_client_hello(void);

0 commit comments

Comments
 (0)