cancel
Showing results for 
Search instead for 
Did you mean: 

HMAC digest doesn't match expected (STM3221G-EVAL)

OOmen
Associate II

I just purchased the STM3221G-EVAL board and I am working through some examples. I am currently using running the HMAC example and it runs fine. However I am unable to get the HMAC to match the test vectors provided in the specification (https://tools.ietf.org/html/rfc2202).

Specifically for the following:

test_case = 1

key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b

key_len = 20

data = "Hi There"

data_len = 8

digest = 0xb617318655057264e28bc0b6fb378c8ef146be00

But I get a completely different digest.

Any ideas?

Many thanks in advance.

5 REPLIES 5

> Any ideas?

I don't think so, at least not until you show us your code which doesn't work as expected 🙂

OOmen
Associate II

Thanks [After Forever] - that was just a "hello world"

Here is the code:

/**
  ******************************************************************************
  * @file    HASH/HASH_HMAC_SHA1MD5/Src/main.c 
  * @author  MCD Application Team
  * @brief   This example provides a short description of HASH digest calculation
  *          using SHA1 and MD5 example.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/** @addtogroup STM32F2xx_HAL_Examples
  * @{
  */
 
/** @addtogroup HMAC_SHA1_MD5
  * @{
  */ 
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* HASH handler declaration */
HASH_HandleTypeDef     HashHandle;
 
/* 
  "The hash processor is a fully compliant implementation of the secure
   hash algorithm (SHA-1), the MD5 (message-digest algorithm 5) hash 
   algorithm and the HMAC (keyed-hash message authentication code)
   algorithm suitable for a variety of applications.*** STM32 ***"
*/
 
 
#define cINPUT_TAB_SIZE    ((uint32_t) 8)/* The size of the input data "cInput" */
#define cKEY_TAB_SIZE      ((uint32_t) 20)
 
// hex for "Hi There"
__ALIGN_BEGIN const uint8_t cInput[cINPUT_TAB_SIZE] __ALIGN_END =  {0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65};
 
__ALIGN_BEGIN const uint8_t cKey[cKEY_TAB_SIZE] __ALIGN_END =      {,
                                                                    0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 
                                                                    0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0xb0, 
                                                                    0xb0, 0xb0, 0xb0, 0x0b};
 
 
 
__ALIGN_BEGIN static uint8_t cSHA1Digest[20] __ALIGN_END;
__ALIGN_BEGIN static uint8_t cExpectSHA1Digest[20] __ALIGN_END = {0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 
                                                                  0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 
                                                                  0xf1, 0x46, 0xbe, 0x00};
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
 
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F2xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 120 MHz */
  SystemClock_Config();
  
  /* Configure LED1, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  /****************************************************************************/
  /*************************** HMAC-SHA1 2 ************************************/
  /****************************************************************************/
  BSP_LED_Off(LED1);  
  HAL_HASH_DeInit(&HashHandle);
  HashHandle.Init.DataType = HASH_DATATYPE_8B;
  HashHandle.Init.pKey = (uint8_t*)cKey;
  HashHandle.Init.KeySize = cKEY_TAB_SIZE;
  
  if(HAL_HASH_Init(&HashHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Compute HMAC-SHA1 */
  if(HAL_HMAC_SHA1_Start(&HashHandle, (uint8_t*)cInput, cINPUT_TAB_SIZE, cSHA1Digest, 0xFF) != HAL_OK)
  {
    Error_Handler();
  }
  /* Compare computed digest with expected one */
  if(memcmp(cSHA1Digest, cExpectSHA1Digest, sizeof(cExpectSHA1Digest)/sizeof(cExpectSHA1Digest[0])) != 0)
  {
    Error_Handler();
  }
  else
  {
    BSP_LED_On(LED1);
  }
 
/****************************************************************************/
  /*************************** HMAC-SHA1 2 repeat *****************************/
  /****************************************************************************/
  BSP_LED_Off(LED1);  
  HAL_HASH_DeInit(&HashHandle);
  HashHandle.Init.DataType = HASH_DATATYPE_8B;
  HashHandle.Init.pKey = (uint8_t*)cKey;
  HashHandle.Init.KeySize = cKEY_TAB_SIZE;
  
  if(HAL_HASH_Init(&HashHandle) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Compute HMAC-SHA1 */
  if(HAL_HMAC_SHA1_Start(&HashHandle, (uint8_t*)cInput, cINPUT_TAB_SIZE, cSHA1Digest, 0xFF) != HAL_OK)
  {
    Error_Handler();
  }
  /* Compare computed digest with expected one */
  if(memcmp(cSHA1Digest, cExpectSHA1Digest, sizeof(cExpectSHA1Digest)/sizeof(cExpectSHA1Digest[0])) != 0)
  {
    Error_Handler();
  }
  else
  {
    BSP_LED_On(LED1);
  }
 
}
 
/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 120000000
  *            HCLK(Hz)                       = 120000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 4
  *            APB2 Prescaler                 = 2
  *            HSE Frequency(Hz)              = 25000000
  *            PLL_M                          = 25
  *            PLL_N                          = 240
  *            PLL_P                          = 2
  *            PLL_Q                          = 5
  *            VDD(V)                         = 3.3
  *            Flash Latency(WS)              = 3
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 240;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 5;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler(void)
{
  /* Turn LED3 on */
  BSP_LED_On(LED3);
  while(1)
  {
  }
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */ 
 
/**
  * @}
  */ 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Now compare the key you mentioned and the key you wrote:

0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b

0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0bb0b0b0b00b

OOmen
Associate II

Aha - thanks.

Fixed it but still no match.

__ALIGN_BEGIN const uint8_t cInput[cINPUT_TAB_SIZE] __ALIGN_END = {0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65};

__ALIGN_BEGIN const uint8_t cKey[cKEY_TAB_SIZE] __ALIGN_END =   {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 

                                  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 

                                  0x0b, 0x0b, 0x0b, 0x0b};

OOmen
Associate II

Here is what I get:

0x75733ACAE127DD1DBFB2F8156E6A6E0C650B94B2