Unable to make HW AES work for STM32L476
I'm taking STM32Cube and the CRYP example for NUCLEO-L496ZG as the basis for a new HW AES encryption Arduino library. I'm currently developing for the STM32L476 MCU and here I found the first problem since stm32l476xx.h does not seem to include the necessary AES register definitions. Anyway, I took these definitions from stm32l486xx.h which does include them.
Finally, I was able to compile this example (taken from the STM32Cube distribution) from the Arduino IDE:
CRYP_HandleTypeDef CrypHandle;
/*##- Configure the CRYP peripheral ######################################*/
/* Set the common CRYP parameters */
CrypHandle.Instance = AES;
/******************************************************************************/
/* AES mode CTR */
/******************************************************************************/
/*=====================================================
Encryption CTR mode
======================================================*/
if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
{
Serial.println("error");
}
//***************** AES 128 ***************
// Initialize the CRYP peripheral
CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
CrypHandle.Init.KeySize = CRYP_KEYSIZE_128B;
CrypHandle.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
CrypHandle.Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
CrypHandle.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
CrypHandle.Init.pKey = aAES128key;
CrypHandle.Init.pInitVect = aInitVector;
if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
{
Serial.println("error");
}
// Start encrypting aPlaintext, the cypher data is available in aEncryptedtext
if (HAL_CRYPEx_AES(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
{
Display_EncryptedData(CTR, 128, AES_TEXT_SIZE);
}
else
{
Serial.println("error");
}The issue is that the above program halts on HAL_CRYPEx_AES. Some debug showed that this halt is in fact happening in stm32l4xx_hal_cryp_ex->CRYP_WaitOnCCFlag, more specifically on:
while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
{
...
}For some reason the AES encryption process is never completing. I've double-checked that the register definitions are right so I'd say that I'm missing an important step before calling HAL_CRYPEx_AES.
Thanks in advance for your help.
