0
votes

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?

1
Welcome to StackOverflow. In order to help you with your coding issues, we need to have sufficient details. Questions that come down to "Why isn't this code working" need a minimal reproducible example. - JHBonarius

1 Answers

0
votes

You are overwriting init_vector before you even use it. That can't be right! I'm not 100% sure, but I think you just have to move your CopyMemory call two lines down.