diff --git a/src/cryptography/AES.c b/src/cryptography/AES.c index ba9be05..c92c6b1 100755 --- a/src/cryptography/AES.c +++ b/src/cryptography/AES.c @@ -172,13 +172,14 @@ Result(u32) AESStreamEncryptor_encrypt(AESStreamEncryptor* ptr, Array(u8) src, Array(u8) dst) { Deferral(4); - u32 encrypted_size = AESStreamEncryptor_calcDstSize(src.size); - try_assert(dst.size >= encrypted_size); - + + u32 encrypted_size = src.size; // if it is the beginning of the stream, write IV if(ptr->block_counter == 0){ __Array_writeNext(&dst, ptr->iv, __AES_STREAM_IV_SIZE); + encrypted_size = AESStreamEncryptor_calcDstSize(encrypted_size); } + try_assert(dst.size >= encrypted_size); // encrypt full buffers while(src.size > __AES_BUFFER_SIZE){ diff --git a/src/cryptography/AES.h b/src/cryptography/AES.h index 2dd8fc7..9597f68 100644 --- a/src/cryptography/AES.h +++ b/src/cryptography/AES.h @@ -101,7 +101,7 @@ void AESStreamEncryptor_changeKey(AESStreamEncryptor* ptr, Array(u8) key); /// @brief If ptr->block_counter == 0, writes random IV to `dst`. After that writes encrypted data to dst. /// @param src array of any size -/// @param dst array of size >= AESStreamEncryptor_calcDstSize(src.size) +/// @param dst array of size >= `AESStreamEncryptor_calcDstSize(src.size)` for first block and `src.size `for other blocks /// @return size of encrypted data Result(u32) AESStreamEncryptor_encrypt(AESStreamEncryptor* ptr, Array(u8) src, Array(u8) dst);