Closed JeremyTubongbanua closed 4 months ago
As I wrote this, I decided it isn't worth doing for two reasons (I had wrote everything out already anyways so thought I would just post it):
const size_t ciphertextsize = atchops_aesctr_encrypt_size(plaintextlen) + 1
unsigned char ciphertext[ciphertextsize];
size_t ciphertextlen = 0;
// ...
atchops_aesctr_encrypt(key, ATCHOPS_AES_256, iv, plaintext, plaintextlen, ciphertext, ciphertextsize, &ciphertextlen);
// At this point in code, ciphertextlen == ciphertextsize - 1
2. If the plaintextlen was 4 bytes, then it is padded to 16 bytes, then the ciphertext would also be 16 bytes (by definition of AES) . The caller could easily make the mistake and expect `ciphertext` to be of length `4` without knowledge of how AES works or may skip reading the documentation altogether causing in making a mistake like this.
ciphertextlen would be something like 16 bytes (but actually be 4 bytes, where the other 12 bytes are padding)
Then we can simply return length as ciphertextlen == 4 and not have to touch or do anything to the 12 pad bytes.
Synopsis
The problem is best explained through an example.
Take
atchops_aesctr_encrypt
function signature as an example:This is how usage of this function would typically look like:
Issues with Code above
ciphertextlen
is a completely useless variable because it will ALWAYS equal ciphertextsize as long asatchops_aesctr_ciphertext_size
is used to calculateciphertextsize
.My Proposal
Change atchops_aesctr_encrypt function signature to:
Documentation of this function would mention a couple of important notes:
ciphertext
is allocated by the function and freed by the caller of the function."plaintextlen
rounded up the next 16 bytes.