Add test for chacha20 counter overflow bug

Signed-off-by: Ben Taylor <[email protected]>
This commit is contained in:
Ben Taylor
2026-05-29 13:16:38 +01:00
parent 935e271d13
commit e05f169677
2 changed files with 44 additions and 0 deletions
+3
View File
@@ -22,5 +22,8 @@ chacha20_crypt:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0
ChaCha20 RFC 7539 Test Vector #3 (Decrypt)
chacha20_crypt:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0":"000000000000000000000002":42:"62e6347f95ed87a45ffae7426f27a1df5fb69110044c0d73118effa95b01e5cf166d3df2d721caf9b21e5fb14c616871fd84c54f9d65b283196c7fe4f60553ebf39c6402c42234e32a356b3e764312a61a5532055716ead6962568f87d3f3f7704c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1":"2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e"
ChaCha20 Counter Overflow
chacha20_input_len_too_long:
ChaCha20 Selftest
chacha20_self_test:
+41
View File
@@ -67,6 +67,47 @@ void chacha20_crypt(data_t *key_str,
}
/* END_CASE */
/* BEGIN_CASE */
void chacha20_input_len_too_long(void)
{
mbedtls_chacha20_context ctx;
unsigned char key[32] = { 0 };
unsigned char nonce[12] = { 0 };
unsigned char input[65] = { 0 };
unsigned char output[65] = { 0 };
size_t i;
mbedtls_chacha20_init(&ctx);
TEST_ASSERT(mbedtls_chacha20_setkey(&ctx, key) == 0);
TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce, UINT32_MAX) == 0);
/*
* An overlong call must fail before consuming buffered keystream bytes,
* writing partial output, or advancing the context.
*/
TEST_EQUAL(mbedtls_chacha20_update(&ctx, 1, input, output), 0);
memset(output, 0x2a, sizeof(output));
TEST_EQUAL(mbedtls_chacha20_update(&ctx, 64, input, output),
MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA);
for (i = 0; i < sizeof(output); i++) {
TEST_EQUAL(output[i], 0x2a);
}
/* Consume the rest of the final counter block, then reject more input. */
TEST_EQUAL(mbedtls_chacha20_update(&ctx, 63, input, output), 0);
TEST_EQUAL(mbedtls_chacha20_update(&ctx, 1, input, output),
MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA);
/* The one-shot API should report the same error. */
TEST_EQUAL(mbedtls_chacha20_crypt(key, nonce, UINT32_MAX,
sizeof(input), input, output),
MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA);
mbedtls_chacha20_free(&ctx);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void chacha20_self_test()
{