I am following the example program of File I/O OpenSSL EVP functions for AES CBC in
https://www.openssl.org/docs/man1.0.2/crypto/EVP_CIPHER_CTX_cleanup.html
I am looking for examples to see why the output buffer for encryption needs to be (inl + cipher_block_size - 1).
I understand how and why PKCS#7 padding works and I am using this default padding.
However, I don't see the need for output buffer to have an extra block_size space.
Example 1:
* Block size = 16
* Input size = 16
* Output/Cipher size = 16+16
By the time we exit the for loop, we would have encrypted first 16 bytes and our ctx->buf_len would be 0. So, the EVP_CipherFinal_ex() would add the padding bytes and add 16 bytes only (which is written back to the output buffer)
Example 2:
* Block size = 16
* Input size = 15
15 bytes are not encrypted and we break out of for loop with ctx->buf_len = 15. So, EVP_CipherFinal_ex() adds 1-byte padding and encrypts 16 bytes which are written into the output buffer.
Example 3:
* Block size = 16
* Input size = 17
First 16 bytes are encrypted in the for loop and ctx->buf_len = 1. So, EVP_CipherFinal_ex() adds 15 bytes padding and encrypts the 16 bytes.
In all the above corner cases considered, the output buffer size never exceeded the input buffer size. Which are the special cases under which we need output buffer size to be input_buffer_size+block_size-1 ?
Any examples on this are appreciated.