Skip to content

Commit b59d5c3

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 37fda89 commit b59d5c3

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

tests/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36299,6 +36299,7 @@ TEST_CASE testCases[] = {
3629936299
#endif
3630036300
TEST_DECL(test_tls_ems_downgrade),
3630136301
TEST_DECL(test_tls_ems_resumption_downgrade),
36302+
TEST_DECL(test_tls12_chacha20_poly1305_bad_tag),
3630236303
TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
3630336304
TEST_DECL(test_certificate_authorities_certificate_request),
3630436305
TEST_DECL(test_certificate_authorities_client_hello),

tests/api/test_tls_ext.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,72 @@ 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+
/* Tamper with a byte from the encrypted record payload on the first
166+
* read that spans past the 5-byte TLS record header, so the Poly1305
167+
* authentication check no longer matches. */
168+
if (test_chacha_bad_tag_trigger && ret > 5) {
169+
buf[ret - 1] ^= 0xFF;
170+
test_chacha_bad_tag_trigger = 0;
171+
}
172+
return ret;
173+
}
174+
#endif
175+
176+
/* F-2921: TLS 1.2 ChaCha20-Poly1305 must surface VERIFY_MAC_ERROR when
177+
* the Poly1305 tag is corrupted. */
178+
int test_tls12_chacha20_poly1305_bad_tag(void)
179+
{
180+
EXPECT_DECLS;
181+
#if !defined(WOLFSSL_NO_TLS12) && \
182+
defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
183+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
184+
struct test_memio_ctx test_ctx;
185+
WOLFSSL_CTX *ctx_c = NULL;
186+
WOLFSSL_CTX *ctx_s = NULL;
187+
WOLFSSL *ssl_c = NULL;
188+
WOLFSSL *ssl_s = NULL;
189+
const char msg[] = "tamper me";
190+
char recvBuf[32];
191+
int ret;
192+
193+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
194+
test_ctx.c_ciphers = test_ctx.s_ciphers =
195+
"ECDHE-RSA-CHACHA20-POLY1305";
196+
197+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
198+
wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
199+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
200+
201+
wolfSSL_SSLSetIORecv(ssl_s, test_chacha_bad_tag_io_recv);
202+
203+
ExpectIntEQ(wolfSSL_write(ssl_c, msg, (int)XSTRLEN(msg)),
204+
(int)XSTRLEN(msg));
205+
206+
test_chacha_bad_tag_trigger = 1;
207+
ret = wolfSSL_read(ssl_s, recvBuf, sizeof(recvBuf));
208+
ExpectIntLE(ret, 0);
209+
ExpectIntEQ(wolfSSL_get_error(ssl_s, ret),
210+
WC_NO_ERR_TRACE(VERIFY_MAC_ERROR));
211+
212+
test_chacha_bad_tag_trigger = 0;
213+
wolfSSL_free(ssl_c);
214+
wolfSSL_free(ssl_s);
215+
wolfSSL_CTX_free(ctx_c);
216+
wolfSSL_CTX_free(ctx_s);
217+
#endif
218+
return EXPECT_RESULT();
219+
}
220+
221+
156222
int test_wolfSSL_DisableExtendedMasterSecret(void)
157223
{
158224
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)