07-26-2023, 02:35 AM
Trying to implement CTR mode and this is part of my kernel file:
It decodes correct and save to `decrypted_data` only first 16 bytes(1 iteration of loop). Needs your help
Code:
DECLSPEC void aes256_decrypt_ctr (GLOBAL_AS const u32 *encrypted_data, int data_len, PRIVATE_AS const u32 *iv, PRIVATE_AS const u32 *key, PRIVATE_AS u32 *decrypted_data,
SHM_TYPE u32 *s_te0, SHM_TYPE u32 *s_te1, SHM_TYPE u32 *s_te2, SHM_TYPE u32 *s_te3, SHM_TYPE u32 *s_te4)
{
u32 ks[60];
u32 ctr_block[4];
// Initialize the counter block (IV), which will be modified during encryption
ctr_block[0] = iv[0];
ctr_block[1] = iv[1];
ctr_block[2] = iv[2];
ctr_block[3] = iv[3];
aes256_set_encrypt_key(ks, key, s_te0, s_te1, s_te2, s_te3);
// Decrypt in CTR mode
for (int i = 0; i < data_len / 4; i+=4) {
// Encrypt the counter block to generate the keystream
aes256_encrypt(ks, ctr_block, decrypted_data + i, s_te0, s_te1, s_te2, s_te3, s_te4);
decrypted_data[i + 0] ^= encrypted_data[i + 0];
decrypted_data[i + 1] ^= encrypted_data[i + 1];
decrypted_data[i + 2] ^= encrypted_data[i + 2];
decrypted_data[i + 3] ^= encrypted_data[i + 3];
// Increment the counter block (IV) for the next iteration
for (int j = 3; j >= 0; --j)
{
if (++ctr_block[j] != 0) break;
}
}
}