Skip to content

Commit 7f487b9

Browse files
committed
Fix checkPad to test for zero padding
1 parent 1c8d593 commit 7f487b9

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

tests/api/test_evp_cipher.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
27222776
int test_evp_cipher_aead_aad_overflow(void)
27232777
{
27242778
EXPECT_DECLS;

tests/api/test_evp_cipher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int test_wolfSSL_EVP_rc4(void);
6363
int test_wolfSSL_EVP_enc_null(void);
6464
int test_wolfSSL_EVP_rc2_cbc(void);
6565
int test_wolfSSL_EVP_mdc2(void);
66+
int test_evp_cipher_pkcs7_pad_zero(void);
6667
int test_evp_cipher_aead_aad_overflow(void);
6768

6869
#define TEST_EVP_CIPHER_DECLS \
@@ -105,6 +106,7 @@ int test_evp_cipher_aead_aad_overflow(void);
105106
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_enc_null), \
106107
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_rc2_cbc), \
107108
TEST_DECL_GROUP("evp_cipher", test_wolfSSL_EVP_mdc2), \
109+
TEST_DECL_GROUP("evp_cipher", test_evp_cipher_pkcs7_pad_zero), \
108110
TEST_DECL_GROUP("evp_cipher", test_evp_cipher_aead_aad_overflow)
109111

110112
#endif /* WOLFCRYPT_TEST_EVP_CIPHER_H */

wolfcrypt/src/evp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ static int checkPad(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *buff)
12641264
int i;
12651265
int n;
12661266
n = buff[ctx->block_size-1];
1267-
if (n > ctx->block_size) return -1;
1267+
if (n > ctx->block_size || n == 0) return -1;
12681268
for (i = 0; i < n; i++) {
12691269
if (buff[ctx->block_size-i-1] != n)
12701270
return -1;

0 commit comments

Comments
 (0)