CRYPT HAL driver of STM32WB not compatible with openssl and STM32 crypto library (bug?)
Hello
The crypto HAL driver ( function CRYP_SetKey in file stm32wbxx_hal_cryp.c ) of the STM32WB accesses the key as "array of uint32_t" (lines 2919 etc):
hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey);
hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 1U);
hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 2U);
hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 3U);
thus the key and nonce must be swapped by customer software. This is not compatible with openssl generated key nor STM32 crypto library usage on other STM32 mcus (could be declared as bug). The "CRYP_DATATYPE_8B" byte swapping only applies to cipher and plain buffer.
Better would be:
hcryp->Instance->KEYR3 = __REV( *(uint32_t *)(hcryp->Init.pKey) );
hcryp->Instance->KEYR2 = __REV( *(uint32_t *)(hcryp->Init.pKey + 1U) );
hcryp->Instance->KEYR1 = __REV( *(uint32_t *)(hcryp->Init.pKey + 2U) );
hcryp->Instance->KEYR0 = __REV( *(uint32_t *)(hcryp->Init.pKey + 3U) );
Same for nonce (lines 1685 etc):
hcryp->Instance->IVR3 = __REV( *(uint32_t *)(hcryp->Init.pInitVect) );
hcryp->Instance->IVR2 = __REV( *(uint32_t *)(hcryp->Init.pInitVect + 1U) );
hcryp->Instance->IVR1 = __REV( *(uint32_t *)(hcryp->Init.pInitVect + 2U) );
hcryp->Instance->IVR0 = __REV( *(uint32_t *)(hcryp->Init.pInitVect + 3U) );
Best regards
Paul
