cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to make HW AES work for STM32L476

estratos
Associate

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.

1 ACCEPTED SOLUTION

Accepted Solutions

The L486 is what a L476 with crypto sells as.

View solution in original post

4 REPLIES 4
Clive1 (HNL)
Senior II

The L486 is what a L476 with crypto sells as.

How did I miss this little detail?? I thought AES was a standard feature for the whole STM32L4 family

Thanks Clive

No, there are a couple of parts that have AES randomly on them, but for the most part ST sells two distinct variants based on export restrictions and the like.

ST frequently presents AES as available in a pairing of products, and puts a little asterisk at the bottom to see if you were paying attention.

You drove down a lot of rough road to get here.

When there are a lot of hurdles purposefully erected in the road, with red flags on them, it usually means the road is closed or the bridge is out.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..