Add AES CBC PKCS#7 padding #25
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This work is an attempt to continue from #14 by implementing the solution mentioned in #14 (comment), using PKCS#7 padding.
Currently, the CBC mode of AES does not pad the plaintext before encrypting. As suggested in #14, the industry standard is to pad the plaintext, regardless of whether it is a multiple of the block size.
PKCS#7 ) is widely used in the industry and is considered secure, which is why I have implemented it here.
As mentioned in the issue:
To approach this, I modified
AES256_CBC_ctxto pass the ciphertext length. Then, the entire data is decrypted before checking whether the padding is correct.The decryption function returns the plaintext including the padding, as well as the length of the plaintext with the padding removed, a boolean value whether the decryption was done correctly i.e padding is correct
This approach does not detect incorrect padding in constant time; the ciphertext has to be decrypted before checking the padding correctness, as if I understand correctly in CBC Mode you have to use the iv, the first block, to get the next iv, and do that continuously to reach the last block with the padding.
If there is a better approach, I would appreciate guidance on how to improve this.