cancel
Showing results for 
Search instead for 
Did you mean: 

X-CUBE-CRYPTOLIB computed SHA256 hash wrong

Martin42
Associate III

Hello,

I would like to use the X-CUBE-CRYPTOLIB to calculate a SHA256 with a key. My implementation or the example HMAC_SHA2_AuthenticateVerify works in STM32CubeIDE 1.13.1 for an STM32L432KCU microcontroller.

Source: https://www.st.com/en/embedded-software/x-cube-cryptolib.html

To rule out the possibility that the example project has implemented something that I forgot to copy into Atollic Studio, I created a new project in STM32CubeIDE and integrated the X-CUBE-CRYPTOLIB there as I did in Atollic Studio. The hash was calculated correctly there.

Now my problem:

The development environment Atollic TrueSTUDIO for STM32 9.3.0 is used for an existing project. I would like to use the X-CUBE-CRYPTOLIB there. If I put the "simple" example there, then the hash is calculated incorrectly.

The error occurs on the line if (memcmp(Expected_Tag, Computed_Tag, computed_size) != 0) because the Computed_Tag was not calculated correctly.

 

What could be the reason?

 

 

 

 

  /* Initialize cryptographic library */
  if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
  {
    Error_Handler();
  }

  /* --------------------------------------------------------------------------
   * SINGLE CALL USAGE
   * --------------------------------------------------------------------------
   */

  /* Compute directly the authentication tag passing all the needed parameters */
  retval = cmox_mac_compute(CMOX_HMAC_SHA256_ALGO,     /* Use HMAC SHA256 algorithm */
                            Message, sizeof(Message),  /* Message to authenticate */
                            Key, sizeof(Key),          /* HMAC Key to use */
                            NULL, 0,                   /* Custom data */
                            Computed_Tag,              /* Data buffer to receive generated authnetication tag */
                            sizeof(Expected_Tag),      /* Expected authentication tag size */
                            &computed_size);           /* Generated tag size */

  /* Verify API returned value */
  if (retval != CMOX_MAC_SUCCESS)
  {
    Error_Handler();
  }

  /* Verify generated data size is the expected one */
  if (computed_size != sizeof(Expected_Tag))
  {
    Error_Handler();
  }

  /* Verify generated data are the expected ones */
  if (memcmp(Expected_Tag, Computed_Tag, computed_size) != 0)
  {
    Error_Handler();
  }

  /* Verify directly the message passing all the needed parameters */
  retval = cmox_mac_verify(CMOX_HMAC_SHA256_ALGO,     /* Use HMAC SHA256 algorithm */
                           Message, sizeof(Message),  /* Message to authenticate */
                           Key, sizeof(Key),          /* HMAC Key to use */
                           NULL, 0,                   /* Custom data */
                           Expected_Tag,              /* Authentication tag */
                           sizeof(Expected_Tag));     /* tag size */

  /* Verify API returned value */
  if (retval != CMOX_MAC_AUTH_SUCCESS)
  {
    Error_Handler();
  }

 

 

computed  

10 REPLIES 10

After I couldn't find the old project anymore, I set up a new one. For whatever reason, it worked now.
Thanks.