2025-07-01 2:00 AM
Hello,
I would like to use the X-CUBE-CRYPTOLIB to calculate a SHA256 with a key, i'm using STM32CubeIDE 1.18.0 for an STM32u083 microcontroller, STM32CubeExpansion_Crypto_V4.2.0.
The issue I have is the first time I call cmox_mac_compute the wrong result is returned.
cmox_init_arg_t init_target = {CMOX_INIT_TARGET_AUTO, NULL};
if (cmox_initialize(&init_target) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
uint8_t tResult[32] = {0};
uint8_t tMessage[63] = {153,234,98,104,200,115,43,208,16,24,124,106,63,66,15,0,0,0,0,0,250,191,113,33,0,0,0,0,0,0,0,53,231,216,222,211,203,112,30,58,145,125,101,150,57,215,149,108,50,229,247,80,239,35,157,119,19,121,218,120,231,50,70};
uint8_t tKey[32] = {173,182,187,187,244,184,224,88,49,224,82,119,184,125,189,202,255,179,154,153,213,5,120,138,162,120,25,250,40,96,170,122};
//Test 1
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult, // Data buffer to receive generated authnetication tag
sizeof(tResult), // Expected authentication tag size
&computed_size);
//Test 2
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult, // Data buffer to receive generated authnetication tag
sizeof(tResult), // Expected authentication tag size
&computed_size);
The test 1 result is
Solved! Go to Solution.
2025-07-01 5:41 AM - edited 2025-07-01 6:18 AM
I have done further testing. I am using HAL_CRC_Calculate in a couple of places which seems to be causing the issue. If I change my test code to to do a CRC calculation, this reproduces the bug
cmox_init_arg_t init_target = {CMOX_INIT_TARGET_AUTO, NULL};
if (cmox_initialize(&init_target) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
uint32_t test[2] = {0,0};
HAL_CRC_Calculate(&hcrc, (uint32_t*)test, 2);
cmox_mac_retval_t retval;
size_t computed_size;
uint8_t tResult[32] = {0};
uint8_t tResult2[32] = {0};
uint8_t tMessage[63] = {153,234,98,104,200,115,43,208,16,24,124,106,63,66,15,0,0,0,0,0,250,191,113,33,0,0,0,0,0,0,0,53,231,216,222,211,203,112,30,58,145,125,101,150,57,215,149,108,50,229,247,80,239,35,157,119,19,121,218,120,231,50,70};
uint8_t tKey[32] = {173,182,187,187,244,184,224,88,49,224,82,119,184,125,189,202,255,179,154,153,213,5,120,138,162,120,25,250,40,96,170,122};
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult, // Data buffer to receive generated authnetication tag
sizeof(tResult), // Expected authentication tag size
&computed_size);
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult2, // Data buffer to receive generated authnetication tag
sizeof(tResult2), // Expected authentication tag size
&computed_size);
If I add __HAL_CRC_DR_RESET(&hcrc);
After the HAL_CRC_CALCULATE but before the cmox_mac_compute it fixes the issue.
Why would that be?
2025-07-01 3:03 AM
Not checking computed_size first time?
2025-07-01 3:04 AM
Computed size in both cases return 32, and retval in both cases is CMOX_MAC_SUCCESS.
2025-07-01 5:41 AM - edited 2025-07-01 6:18 AM
I have done further testing. I am using HAL_CRC_Calculate in a couple of places which seems to be causing the issue. If I change my test code to to do a CRC calculation, this reproduces the bug
cmox_init_arg_t init_target = {CMOX_INIT_TARGET_AUTO, NULL};
if (cmox_initialize(&init_target) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
uint32_t test[2] = {0,0};
HAL_CRC_Calculate(&hcrc, (uint32_t*)test, 2);
cmox_mac_retval_t retval;
size_t computed_size;
uint8_t tResult[32] = {0};
uint8_t tResult2[32] = {0};
uint8_t tMessage[63] = {153,234,98,104,200,115,43,208,16,24,124,106,63,66,15,0,0,0,0,0,250,191,113,33,0,0,0,0,0,0,0,53,231,216,222,211,203,112,30,58,145,125,101,150,57,215,149,108,50,229,247,80,239,35,157,119,19,121,218,120,231,50,70};
uint8_t tKey[32] = {173,182,187,187,244,184,224,88,49,224,82,119,184,125,189,202,255,179,154,153,213,5,120,138,162,120,25,250,40,96,170,122};
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult, // Data buffer to receive generated authnetication tag
sizeof(tResult), // Expected authentication tag size
&computed_size);
retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO, // Use HMAC SHA256 algorithm
tMessage, sizeof(tMessage), // Message to authenticate
tKey,sizeof(tKey), //HMAC Key to use
NULL, 0, // Custom data
tResult2, // Data buffer to receive generated authnetication tag
sizeof(tResult2), // Expected authentication tag size
&computed_size);
If I add __HAL_CRC_DR_RESET(&hcrc);
After the HAL_CRC_CALCULATE but before the cmox_mac_compute it fixes the issue.
Why would that be?
2025-07-01 12:15 PM - edited 2025-07-01 12:16 PM
> I am using HAL_CRC_Calculate in a couple of places,
Aha. The ST cryptolib reserves the CRC for something (proprietary locking?) so don't use the CRC in your code. Or use it with caution.
2025-07-02 12:20 AM
Thank you Pavel.
Looking at the stm32u0xx_hal_crc.h the first thing that is called is __HAL_CRC_DR_RESET(hcrc);
If the cryptolib reserves the CRC for something, shouldn't any functions that use it do the same, so developers don't encounter unexpected behaviour when implementing these functions?
2025-07-02 12:37 AM
>>Why would that be?
The library is locked to STM32's by use of the CRC peripheral and obfuscation of initialization constants
2025-07-02 12:42 AM
Perhaps, but the CRC isn't something that can be shared or threaded.
If the STM32 you're using supports hashing in hardware, use it more directly, and not use the CMOX to add an additional level of abstraction.
2025-07-02 12:46 AM
I use the CRC peripheral to generate a CRC value of configuration data stored in flash, when reading configuration data from flash I recalculate the CRC to ensure that it is not corrupted.
When doing this, I wouldn't expect that another library would not return the correct results from an unrelated function because I have used a peripheral for the purpose it was intended.
I'm using a STM32U083 which unfortunately doesn't have hardware hashing, if it did I would have used it, same reason I am using the CRC peripheral for performance.
STM are clearly aware of the need to reset the CRC as previously mentioned because it is done when within HAL_CRC_Calculate, so just questioning why it is also not done in cmox_mac_compute, if it is required to ensure the correct behaviour of the function
2025-07-02 1:00 AM
Whilst this is very annoying, and has been complained about for more than a decade in various forms, this is what ST has decided to do. Expecting it to change is perhaps optimistic.
Adding additional overhead in the library punishes everyone, better your use of the CRC is scoped so it leaves it in a reset state. Some STM32 support multiple polynomials and shift directions, others just support their odd CRC32 mechanics. I'd prefer they remove this nonsense entirely.
Most of the docs at least mention to use of the CRC peripheral by the libraries, often more subtly than they should
The HW CRC isn't that efficient