diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index c156aa1d4..d8bcdcfb2 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -430,6 +430,11 @@ WOLFSSL_API int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, *outl = 0; return WOLFSSL_SUCCESS; } + if ((ctx->bufUsed % ctx->block_size) != 0) { + *outl = 0; + /* not enough padding for decrypt */ + return WOLFSSL_FAILURE; + } if (ctx->lastUsed) { PRINT_BUF(ctx->lastBlock, ctx->block_size); if ((fl = checkPad(ctx, ctx->lastBlock)) >= 0) { diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 122c08112..64a54db33 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -13399,32 +13399,44 @@ int openssl_test(void) if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0) return -7417; - if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, cipherSz) == 0) + /* check partial decrypt (not enough padding for full block) */ + if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, 1) == 0) return -7418; plainSz = idx; - if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) == 0) + if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) != 0) return -7419; + + EVP_CIPHER_CTX_init(&ctx); + if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0) + return -7420; + + if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, cipherSz) == 0) + return -7421; + + plainSz = idx; + if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) == 0) + return -7422; plainSz += idx; if ((plainSz != sizeof(msg)) || XMEMCMP(plain, msg, sizeof(msg))) - return -7420; + return -7423; EVP_CIPHER_CTX_init(&ctx); if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0) - return -7421; + return -7424; if (EVP_CipherUpdate(&ctx, cipher, &idx, msg, AES_BLOCK_SIZE) == 0) - return -7422; + return -7425; cipherSz = idx; if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0) - return -7423; + return -7426; cipherSz += idx; if ((cipherSz != (int)sizeof(verify2)) || XMEMCMP(cipher, verify2, cipherSz)) - return -7424; + return -7427; } /* end evp_cipher test: EVP_aes_128_cbc*/ #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */