AnsweredAssumed Answered

STM32F767ZI Nucleo : Cryptographic Library AES CBC Decryption Wrong Plaintext

Question asked by yarema.andriy on Oct 12, 2017
Latest reply on Oct 13, 2017 by yarema.andriy

Hardware and Software Versions:

MCU: STM32F767ZI Nucleo Board

En X Cube CryptoLib V3.1.0

Cube MX Version 4.22.1

Latest System Workbench 6 installation.

 

Problem:

Using AES 256 in CBC mode attempting to encrypt block of data on the Host which is the PC, and decrypt that same block on the MCU using the CryptoLib. Encryption on host is done correctly, decryption of the first block of data is done correctly on the MCU, but decryption of the second block of data fails(Bytes 16-31). Below is my test code.

 

uint8_t KeyTemp[32];
uint8_t KeyIvTemp[16];
uint8_t DataRaw[32];
uint8_t DataEncrypted[32];

for(int i = 0; i < 32; i++)
{
KeyTemp[i] = (uint8_t)i;
DataRaw[i] = 0;
}
for(int i = 0; i < 16; i++)
{
KeyIvTemp[i] = (uint8_t)i;
}
DataEncrypted[0] = 242;
DataEncrypted[1] = 144;
DataEncrypted[2] = 0;
DataEncrypted[3] = 182;
DataEncrypted[4] = 42;
DataEncrypted[5] = 73;
DataEncrypted[6] = 159;
DataEncrypted[7] = 208;
DataEncrypted[8] = 169;
DataEncrypted[9] = 243;
DataEncrypted[10] = 154;
DataEncrypted[11] = 106;
DataEncrypted[12] = 221;
DataEncrypted[13] = 46;
DataEncrypted[14] = 119;
DataEncrypted[15] = 128;
DataEncrypted[16] = 149;
DataEncrypted[17] = 67;
DataEncrypted[18] = 187;
DataEncrypted[19] = 111;
DataEncrypted[20] = 192;
DataEncrypted[21] = 70;
DataEncrypted[22] = 250;
DataEncrypted[23] = 136;
DataEncrypted[24] = 58;
DataEncrypted[25] = 148;
DataEncrypted[26] = 70;
DataEncrypted[27] = 184;
DataEncrypted[28] = 46;
DataEncrypted[29] = 71;
DataEncrypted[30] = 209;
DataEncrypted[31] = 45;

 

aesCTX.mIvSize = 16;
aesCTX.mKeySize = 32;
aesCTX.mFlags = E_SK_DEFAULT;
aeserror = AES_CBC_Decrypt_Init(&aesCTX, (uint8_t*)KeyTemp, (uint8_t*)KeyIvTemp);

aeserror = AES_CBC_Decrypt_Append(&aesCTX,(uint8_t*)DataEncrypted,32,DataRaw,&aesoutputsize);
aeserror = AES_CBC_Decrypt_Finish(&aesCTX,(uint8_t*)DataRaw,&aesoutputsize);

 

The decryption only works for the first 16 bytes, fails for the next 16 bytes. The following website shows what the decryption should be AES Encryption – Easily encrypt or decrypt strings or files.

 

This is what i get when i run it.

Decrypted Data in the debugger

Which is wrong since the plaintext goes from 0-31.

 

Anyone know what im doing wrong?

 

-Andriy

Outcomes