2025-10-15 11:55 PM - last edited on 2025-10-16 2:12 AM by mƎALLEm
Post edited by ST moderator to be inline with the community rules especially with the code sharing. In next time please use </> button to paste your code. Please read this post: How to insert source code
Hello,
I need to calculate SHA-256 on STM32L562 MCU. I need a final single Hash output digest for an input of 8192 keys where each key is of 32 bytes. I am accumulating each key using: HAL_HASHEx_SHA256_Accmlt(&hhash, data, 32); upto 8191 keys. The final key i.e. 8192th key is fed to Hash engine using: HAL_HASHEx_SHA256_Accmlt_End(&hhash, last_data, 32, out_digest, 0xFF);
Using this above approach I understood that a final "out_digest" will result a final 32-byte Hash value but I am not obtaining the correct Hash. I tried to reduce keys from 8192 to 2048 thinking that a small data may be processed correctly by Hash processor but still got the wrong Hash value.
Instead of accumulating keys one by one, if I take a buffer of size (2048 * 32) bytes and accumulate the first 2047 keys using:
for(uint8_t i = 0; i<2047: i++)
{
uint8_t *data = &valid_packets[i][3];
HAL_HASHEx_SHA256_Accmlt(&hhash, data, 32);
}
and then feed final 2048th key using:
uint8_t *last_data = &valid_packets[2047][3];
if (HAL_HASHEx_SHA256_Accmlt_End(&hhash, last_data, 32, out_digest, 0xFF) != HAL_OK)
{
Error_Handler();
}
then I get the correct result. But taking a large variable of size (2048 * 32) bytes or (8192 * 32) bytes may lead to RAM overflow. Hence, I thought to feed each key individually upto 2047 or 8191 and then obtaining final Hash by feeding last key i.e. 2048th or 8192th key by HAL_HASHEx_SHA256_Accmlt_End() function.
Please suggest what is wrong with my approach and how can we get the correct Hash ?
With regard,
Sudarshan Chaudhary