Question
AES ECB initialization fails
Hi,
I'm trying to encrypt and decrypt in my device, running on a STM32H753, using AES ECB.
I'm initializing crypto like this:
hcryp.Instance = CRYP;
hcryp.Init.DataType = CRYP_DATATYPE_8B;
hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE;
hcryp.Init.pKey = AESKey256;
hcryp.Init.Algorithm = CRYP_AES_ECB;
With following clocks:
/* Enable CRYP clock */
__HAL_RCC_CRYP_CLK_ENABLE();
/* Force the CRYP Peripheral Clock Reset */
__HAL_RCC_CRYP_FORCE_RESET();
/* Release the CRYP Peripheral Clock Reset */
__HAL_RCC_CRYP_RELEASE_RESET();
After that, I just call HAL_CRYP_Encrypt & HAL_CRYP_Decrypt and everything goes fine. At least in the first two devices I used.
When testing in more devices (same hardware, same firmware), I found many of them that were getting blocked inside HAL_CRYP_Encrypt. Debugging a bit, they are blocked in the actual encryption function CRYP_TDES_Process. It is the exact blocking point someone had already pointed out in this post: https://community.st.com/t5/stm32-mcus-security/hal-stm32f2-version-1-9-2-potential-locked-in-cryp-tdes-process/td-p/210046
However, it should not even be in TDES function, as it should be AES. That's how I found that was is actually not working correctly is the initialization. Inside HAL_CRYP_Init, following piece of code is making no effect in the crypt handler:
MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE,
hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
When debugging, HWs that work OK run that code as expected, changing values of the CRYP control register. However, in those devices with the problem, after this Modify_reg line I see that CR is all in 0.
Any ideas why this could happen? it bugs me the fact that it is actually working in some hardwares but not in others...
TIA, regards.
