Skip to main content
Graduate
October 22, 2025
Solved

mbedTLS: memset vs hash_zeroize

  • October 22, 2025
  • 1 reply
  • 157 views

Hello!

This function was defined and used instead of memset to reset mbedtls's sha256 context. Is there a good reason behind this choice?

https://github.com/STMicroelectronics/STM32CubeH7/blob/master/Middlewares/Third_Party/mbedTLS/library/templates/hash_stm32.c  

/* Implementation that should never be optimized out by the compiler */
void hash_zeroize( void *v, size_t n )
{
 volatile unsigned char *p = (unsigned char *)v;
 while (n--)
 {
 *p++ = 0;
 }
}

 

    This topic has been closed for replies.
    Best answer by TDK

    By assigning to a volatile pointer, this ensures the code is not optimized out. Otherwise, if the operation has no effect, it can be optimized away.

    If this resets bytes such as a hash or password or other sensitive information that you want to protect from other parts of the code and ensure are no longer in memory, it is imperative that the operation actually take place.

    1 reply

    TDKAnswer
    Super User
    October 22, 2025

    By assigning to a volatile pointer, this ensures the code is not optimized out. Otherwise, if the operation has no effect, it can be optimized away.

    If this resets bytes such as a hash or password or other sensitive information that you want to protect from other parts of the code and ensure are no longer in memory, it is imperative that the operation actually take place.