From c6ed62532de1dfc75967a1a6c05048bb50ce7835 Mon Sep 17 00:00:00 2001 From: kaage Date: Thu, 26 Nov 2026 08:58:40 +0900 Subject: [PATCH 1/2] Fix EVP AES-CFB1 length overflow --- wolfcrypt/src/evp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index 8288fa07d87..9b091ca375e 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -664,6 +664,8 @@ static int evpCipherBlock(WOLFSSL_EVP_CIPHER_CTX *ctx, case WC_AES_128_CFB1_TYPE: case WC_AES_192_CFB1_TYPE: case WC_AES_256_CFB1_TYPE: + if (inl > WOLFSSL_MAX_32BIT / WOLFSSL_BIT_SIZE) + return WOLFSSL_FAILURE; if (ctx->enc) ret = wc_AesCfb1Encrypt(&ctx->cipher.aes, out, in, inl * WOLFSSL_BIT_SIZE); From a021a405cdeb72a116684977e041c2a6488cdfcd Mon Sep 17 00:00:00 2001 From: kaage Date: Thu, 26 Nov 2026 09:06:43 +0900 Subject: [PATCH 2/2] Test EVP AES-CFB1 length overflow rejection --- tests/api/test_evp_cipher.c | 61 +++++++++++++++++++++++++++++++++++++ tests/api/test_evp_cipher.h | 4 ++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/api/test_evp_cipher.c b/tests/api/test_evp_cipher.c index bfc84542367..f38ee968b36 100644 --- a/tests/api/test_evp_cipher.c +++ b/tests/api/test_evp_cipher.c @@ -3418,3 +3418,64 @@ int test_evp_cipher_update_no_padding_buffered(void) #endif return EXPECT_RESULT(); } + +int test_evp_cipher_cfb1_length_overflow(void) +{ + EXPECT_DECLS; +#if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_128) && \ + defined(WOLFSSL_AES_CFB) && !defined(WOLFSSL_NO_AES_CFB_1_8) && \ + defined(HAVE_AES_DECRYPT) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) + const int boundary = 0x20000000; + byte key[AES_BLOCK_SIZE] = {0}; + byte iv[AES_BLOCK_SIZE] = {0}; + byte input[2] = {0x12, 0x34}; + byte output[2]; + byte expected[2]; + byte* large = NULL; + EVP_CIPHER_CTX* ctx = NULL; + EVP_CIPHER_CTX* reference = NULL; + int direction; + int size; + int outSz; + int refSz; + + /* The API requires a buffer as large as the supplied length, even though + * the overflow check must reject the call before accessing it. */ + ExpectNotNull(large = (byte*)XMALLOC((size_t)boundary + 1, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(ctx = EVP_CIPHER_CTX_new()); + ExpectNotNull(reference = EVP_CIPHER_CTX_new()); + + for (direction = 0; EXPECT_SUCCESS() && direction <= 1; direction++) { + ExpectIntEQ(EVP_CipherInit(ctx, EVP_aes_128_cfb1(), key, iv, + direction), WOLFSSL_SUCCESS); + ExpectIntEQ(EVP_CipherInit(reference, EVP_aes_128_cfb1(), key, iv, + direction), WOLFSSL_SUCCESS); + for (size = boundary; EXPECT_SUCCESS() && size <= boundary + 1; + size++) { + large[0] = 0x5a; + large[size - 1] = 0xa5; + outSz = -1; + ExpectIntEQ(EVP_CipherUpdate(ctx, large, &outSz, large, size), + WOLFSSL_FAILURE); + ExpectIntEQ(outSz, 0); + ExpectIntEQ(large[0], 0x5a); + ExpectIntEQ(large[size - 1], 0xa5); + } + /* A rejected update must not advance the CFB feedback state. */ + outSz = refSz = -1; + ExpectIntEQ(EVP_CipherUpdate(ctx, output, &outSz, input, sizeof(input)), + WOLFSSL_SUCCESS); + ExpectIntEQ(EVP_CipherUpdate(reference, expected, &refSz, input, + sizeof(input)), WOLFSSL_SUCCESS); + ExpectIntEQ(outSz, (int)sizeof(input)); + ExpectIntEQ(refSz, outSz); + ExpectBufEQ(output, expected, sizeof(output)); + } + EVP_CIPHER_CTX_free(reference); + EVP_CIPHER_CTX_free(ctx); + XFREE(large, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_evp_cipher.h b/tests/api/test_evp_cipher.h index 82aaefad125..738cdbc00b3 100644 --- a/tests/api/test_evp_cipher.h +++ b/tests/api/test_evp_cipher.h @@ -72,6 +72,7 @@ int test_evp_cipher_pkcs7_pad_zero(void); int test_evp_cipher_aead_aad_overflow(void); int test_evp_cipher_update_chunked_bound(void); int test_evp_cipher_update_no_padding_buffered(void); +int test_evp_cipher_cfb1_length_overflow(void); #define TEST_EVP_CIPHER_DECLS \ TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_CIPHER_CTX), \ @@ -121,6 +122,7 @@ int test_evp_cipher_update_no_padding_buffered(void); TEST_DECL_GROUP("evp_cipher", test_evp_cipher_pkcs7_pad_zero), \ TEST_DECL_GROUP("evp_cipher", test_evp_cipher_aead_aad_overflow), \ TEST_DECL_GROUP("evp_cipher", test_evp_cipher_update_chunked_bound), \ - TEST_DECL_GROUP("evp_cipher", test_evp_cipher_update_no_padding_buffered) + TEST_DECL_GROUP("evp_cipher", test_evp_cipher_update_no_padding_buffered), \ + TEST_DECL_GROUP("evp_cipher", test_evp_cipher_cfb1_length_overflow) #endif /* WOLFCRYPT_TEST_EVP_CIPHER_H */