@@ -2719,6 +2719,60 @@ int test_wolfSSL_EVP_mdc2(void)
27192719 * AAD that triggers the overflow. A properly-fixed implementation should detect
27202720 * the overflow and return WOLFSSL_FAILURE before attempting the allocation.
27212721 */
2722+ int test_evp_cipher_pkcs7_pad_zero (void )
2723+ {
2724+ EXPECT_DECLS ;
2725+ #if !defined(NO_AES ) && defined(HAVE_AES_CBC ) && defined(WOLFSSL_AES_128 ) && \
2726+ defined(OPENSSL_EXTRA )
2727+ EVP_CIPHER_CTX * ctx = NULL ;
2728+ /* AES-128-CBC key and IV */
2729+ byte key [AES_BLOCK_SIZE ] = {
2730+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
2731+ 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f
2732+ };
2733+ byte iv [AES_BLOCK_SIZE ] = {0 };
2734+ /* Plaintext block ending in 0x00 - when decrypted with padding enabled,
2735+ * the last byte (0x00) will be interpreted as the PKCS#7 padding length,
2736+ * which is invalid (valid range is 1..block_size). */
2737+ byte plain [AES_BLOCK_SIZE ] = {
2738+ 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 ,
2739+ 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x00
2740+ };
2741+ byte cipher [AES_BLOCK_SIZE * 2 ];
2742+ byte decrypted [AES_BLOCK_SIZE * 2 ];
2743+ int outl = 0 ;
2744+ int total = 0 ;
2745+
2746+ /* Encrypt the plaintext block with padding disabled so the ciphertext
2747+ * is exactly one block. */
2748+ ExpectNotNull (ctx = EVP_CIPHER_CTX_new ());
2749+ ExpectIntEQ (EVP_CipherInit (ctx , EVP_aes_128_cbc (), key , iv , 1 ),
2750+ WOLFSSL_SUCCESS );
2751+ EVP_CIPHER_CTX_set_padding (ctx , 0 );
2752+ ExpectIntEQ (EVP_CipherUpdate (ctx , cipher , & outl , plain , AES_BLOCK_SIZE ),
2753+ WOLFSSL_SUCCESS );
2754+ total = outl ;
2755+ ExpectIntEQ (EVP_CipherFinal (ctx , cipher + total , & outl ), WOLFSSL_SUCCESS );
2756+ total += outl ;
2757+ ExpectIntEQ (total , AES_BLOCK_SIZE );
2758+ EVP_CIPHER_CTX_free (ctx );
2759+ ctx = NULL ;
2760+
2761+ /* Decrypt the ciphertext with padding enabled (the default).
2762+ * checkPad should reject padding value 0 and CipherFinal must fail. */
2763+ ExpectNotNull (ctx = EVP_CIPHER_CTX_new ());
2764+ ExpectIntEQ (EVP_CipherInit (ctx , EVP_aes_128_cbc (), key , iv , 0 ),
2765+ WOLFSSL_SUCCESS );
2766+ ExpectIntEQ (EVP_CipherUpdate (ctx , decrypted , & outl , cipher , total ),
2767+ WOLFSSL_SUCCESS );
2768+ ExpectIntNE (EVP_CipherFinal (ctx , decrypted + outl , & outl ),
2769+ WOLFSSL_SUCCESS );
2770+ EVP_CIPHER_CTX_free (ctx );
2771+
2772+ #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 && OPENSSL_EXTRA */
2773+ return EXPECT_RESULT ();
2774+ }
2775+
27222776int test_evp_cipher_aead_aad_overflow (void )
27232777{
27242778 EXPECT_DECLS ;
0 commit comments