@@ -2731,40 +2731,46 @@ int test_evp_cipher_pkcs7_pad_zero(void)
27312731 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f
27322732 };
27332733 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 ] = {
2734+ /* Two plaintext blocks, with the last byte set to 0x00. When decrypted
2735+ * with padding enabled, the last byte (0x00) will be interpreted as the
2736+ * PKCS#7 padding length, which is invalid (valid range is 1..block_size).
2737+ * Using two blocks ensures CipherUpdate outputs the first block and
2738+ * CipherFinal processes the second (last) block through checkPad. */
2739+ byte plain [AES_BLOCK_SIZE * 2 ] = {
2740+ 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 ,
2741+ 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 ,
27382742 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 ,
27392743 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x41 , 0x00
27402744 };
2741- byte cipher [AES_BLOCK_SIZE * 2 ];
2742- byte decrypted [AES_BLOCK_SIZE * 2 ];
2745+ byte cipher [AES_BLOCK_SIZE * 3 ];
2746+ byte decrypted [AES_BLOCK_SIZE * 3 ];
27432747 int outl = 0 ;
27442748 int total = 0 ;
27452749
2746- /* Encrypt the plaintext block with padding disabled so the ciphertext
2747- * is exactly one block . */
2750+ /* Encrypt two plaintext blocks with padding disabled so the ciphertext
2751+ * is exactly two blocks . */
27482752 ExpectNotNull (ctx = EVP_CIPHER_CTX_new ());
27492753 ExpectIntEQ (EVP_CipherInit (ctx , EVP_aes_128_cbc (), key , iv , 1 ),
27502754 WOLFSSL_SUCCESS );
2751- EVP_CIPHER_CTX_set_padding (ctx , 0 );
2752- ExpectIntEQ (EVP_CipherUpdate (ctx , cipher , & outl , plain , AES_BLOCK_SIZE ),
2753- WOLFSSL_SUCCESS );
2755+ ExpectIntEQ ( EVP_CIPHER_CTX_set_padding (ctx , 0 ), WOLFSSL_SUCCESS );
2756+ ExpectIntEQ (EVP_CipherUpdate (ctx , cipher , & outl , plain ,
2757+ AES_BLOCK_SIZE * 2 ), WOLFSSL_SUCCESS );
27542758 total = outl ;
27552759 ExpectIntEQ (EVP_CipherFinal (ctx , cipher + total , & outl ), WOLFSSL_SUCCESS );
27562760 total += outl ;
2757- ExpectIntEQ (total , AES_BLOCK_SIZE );
2761+ ExpectIntEQ (total , AES_BLOCK_SIZE * 2 );
27582762 EVP_CIPHER_CTX_free (ctx );
27592763 ctx = NULL ;
27602764
27612765 /* Decrypt the ciphertext with padding enabled (the default).
2762- * checkPad should reject padding value 0 and CipherFinal must fail. */
2766+ * CipherUpdate should output the first block. CipherFinal processes
2767+ * the last block through checkPad, which should reject padding value 0. */
27632768 ExpectNotNull (ctx = EVP_CIPHER_CTX_new ());
27642769 ExpectIntEQ (EVP_CipherInit (ctx , EVP_aes_128_cbc (), key , iv , 0 ),
27652770 WOLFSSL_SUCCESS );
27662771 ExpectIntEQ (EVP_CipherUpdate (ctx , decrypted , & outl , cipher , total ),
27672772 WOLFSSL_SUCCESS );
2773+ ExpectIntEQ (outl , AES_BLOCK_SIZE );
27682774 ExpectIntNE (EVP_CipherFinal (ctx , decrypted + outl , & outl ),
27692775 WOLFSSL_SUCCESS );
27702776 EVP_CIPHER_CTX_free (ctx );
0 commit comments