I have AES encryption implementation. It has several modes such as ECB, CBC and CTR. I want to use CBC mode. I have encryption code but can't make decryption.
encryption:
void AES_CBC_encrypt_buffer(uint8_t* buf, uint32_t length, uint8_t* key, uint8_t* init_vector)
{
uint8_t RoundKey[240];
KeyExpansion(RoundKey, key);
for (uintptr_t i = 0; i < length; i += AES_BLOCKLEN)
{
XorWithIv(buf, init_vector);
Cipher((state_t*)buf, RoundKey);
init_vector = buf;
buf += AES_BLOCKLEN;
}
}
decryption:
void AES_CBC_decrypt_buffer(uint8_t* buf, uint32_t length, uint8_t* key, uint8_t* init_vector)
{
uint8_t RoundKey[240];
KeyExpansion(RoundKey, key);
for (uintptr_t i = 0; i < length; i += AES_BLOCKLEN)
{
CopyMemory(init_vector, buf, 16);
InvCipher((state_t*)buf, RoundKey);
XorWithIv(buf, init_vector);
buf += AES_BLOCKLEN;
}
}
AES_BLOCKLEN = 16
Where I've made a mistake?