Reputation: 63
I'm trying to implement AES symmetric encryption using the OpenSSL library. In the encryption examples, after calling the EVP_EncryptUpdate
function, the EVP_EncryptFinal_ex
function is immediately called. What is the purpose of calling the EVP_EncryptFinal_ex
function?
Upvotes: 1
Views: 1800
Reputation: 94058
This is strangely only explained in the API documentation of EVP_EncryptUpdate()
:
If padding is enabled (the default) then
EVP_EncryptFinal_ex()
encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding) as described in the NOTES section, below. The encrypted final data is written to out which should have sufficient space for one cipher block. The number of bytes written is placed inoutl
. After this function is called the encryption operation is finished and no further calls toEVP_EncryptUpdate()
should be made.
In general, it will perform any final calculations that cannot be performed without knowing that the last part of the message are being encrypted. Most (lower level) cryptographic libraries contain an update / final notion. Note that in case of OpenSSL the authentication tag is not considered part of the ciphertext.
Although it is considered good practice to call EVP_EncryptFinal_ex
, the function doesn't really do much for modes that don't require full blocks of plaintext or padding. Most cryptographic libraries - including OpenSSL - try and encrypt the delivered plaintext as soon as possible; you should however not take that for granted.
Upvotes: 1