Reputation: 83
I want to use OpenSSL for data transmission between server and client. I want to do it using EVP with AES in CBC mode. But when I try to decode second message on client, EVP_EncryptFinal_ex returns 0. The my scheme is shown on picture.
I think, this behavior because I call EVP_EncryptFinal_ex (and EVP_DecryptFinal_ex) twice for one EVP context. How to do it correctly?
Upvotes: 0
Views: 442
Reputation:
You cannot call EVP_EncryptUpdate()
after calling EVP_EncryptFinal_ex()
according to the EVP docs.
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 in outl. After this function is called the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made.
Instead, you should setup the cipher ctx for encryption again by calling EVP_EncryptInit_ex()
. Note that unlike EVP_EncryptInit()
, with EVP_EncryptInit_ex()
, you can continue reusing an existing context without allocating and freeing it up on each call.
Upvotes: 1