2023-08-31 03:07 AM
Hi,
Im trying to encryp using AES 128 CTR but i dont get the same result that online calculators.
Im using this online calculator: https://cryptii.com/pipes/aes-encryption
My code is the next:
uint8_t IVKey[] = {0xDD, 0xF9, 0x11, 0xA6, 0xDD, 0xF9, 0x11, 0xA6, 0xDD, 0xF9, 0x11, 0xA6, 0xDD, 0xF9, 0x11, 0xA6};
AesDeInit();
// CTR AES
hcryp.Instance = AES;
hcryp.Init.DataType = CRYP_DATATYPE_1B;
hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
hcryp.Init.pKey = (uint32_t *)AESKey;
hcryp.Init.Algorithm = CRYP_AES_CTR;
hcryp.Init.pInitVect = (uint32_t *)IVKey;
if (HAL_CRYP_Init(&hcryp) != HAL_OK)
{
Error_Handler();
}
and the AESKey is the next:
const uint8_t AESKey[] =
{0x44, 0x26, 0x44, 0x20, 0x45, 0x6C, 0x65, 0x74, 0x74, 0x72, 0x6F, 0x6E, 0x69, 0x63, 0x61, 0x20
};
My AES function is the next:
int16_t AesEncrypt(char *In,int16_t Len)
{
uint8_t Buf[16];
if (Len & 0x0F)
Len+=16-(Len & 0x0F);
if (HAL_CRYP_DeInit(&hcryp)!=HAL_OK)
Len=0;
else if (HAL_CRYP_Init(&hcryp)!=HAL_OK)
Len=0;
else
{
for (int16_t i=Len;i>0;i-=16) // Encripta paquete
{
if (HAL_CRYP_Encrypt(&hcryp,(uint32_t *)In,16,(uint32_t *)Buf,255)!=HAL_OK)
{
Len=0;
break;
}
memcpy(In,Buf,16);
In+=16;
}
}
return Len;
}
and the string insert into the function is
char Message2[] = {0x50, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x01, 0x03, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00};
The result with the online calculator is: 6c 9d 95 e6 9a 75 ed 03 7c a2 81 80 f9 a6 c3 18
The result that im getting is: c4, b0, c3, 8d, 2d, b2, 5b, 20, 89,75, 57 76, 83, 06, be, 1e
My result is wrong t i dont know what am i doing wrong.
Any suggestion?
Thanks,
Best Regards,
Solved! Go to Solution.
2023-09-02 01:06 AM
Hi all,
Its working now.
My problem was that the AESKey and IVKey were declared as uint8_t and works with uint32_t
and both keys must be declared as a global variables, not a local variable.
Thanks,
Best REgards,
2023-08-31 10:38 AM
Instead of use CRYP_DATATYPE_1B, use CRYP_DATATYPE_8B.
Also verify if all needed struct fields are initialized.
2023-08-31 11:03 AM
Confirming on-line result with C library here, Decrypting in this case, but symmetrical..
KEY
0000 : 44 26 44 20 45 6C 65 74-74 72 6F 6E 69 63 61 20 D&D Elettronica
IV
0000 : DD F9 11 A6 DD F9 11 A6-DD F9 11 A6 DD F9 11 A6 ................
CRYPT-TEXT
0000 : 50 00 30 30 30 30 30 31-01 03 08 20 00 00 00 00 P.000001... ....
PLAIN-TEXT
0000 : 6C 9D 95 E6 9A 75 ED 03-7C A2 81 80 F9 A6 C3 18 l....u..|.......
2023-08-31 10:19 PM
How do you get to this result?
2023-09-01 09:03 AM
I tested the premise using my AES library, on a PC, because I don't have much trust in on-line calculators, and I don't have a crypto G0 to hand currently. And I haven't enough coffee yet..
2023-09-02 01:06 AM
Hi all,
Its working now.
My problem was that the AESKey and IVKey were declared as uint8_t and works with uint32_t
and both keys must be declared as a global variables, not a local variable.
Thanks,
Best REgards,