cancel
Showing results for 
Search instead for 
Did you mean: 

SHA1 produces wrong output when comparing with online SHA1 calculator

EJose.1
Associate II

Hi,

I am using stm32f429bi microcontroller for sha1 authentication. For sha1, i used x-cude-crypto (https://www.st.com/en/embedded-software/x-cube-cryptolib.html) library because the stm32f429bi don't hash peripheral , the example code for HASH run perfectly but the problem is that i am not able get correct digest from it, when comparing with online sha1 calculator, I don't know why this happening. Kindly help me to solve the issue.

5 REPLIES 5
KnarfB
Principal III

Works for me correctly, but in a slightly different context (STM32L4 SHA256) using the test vectors form here: https://www.di-mgt.com.au/sha_testvectors.html.

Do you have a code example?

Check if the CRC peripheral is enable, ST use it to lock functionality in library to STM32 parts.

The SHA and AES peripheral examples have been gone over before on the forum.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
EJose.1
Associate II

Here I am attaching the code?? kindly help me find the solution

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "crypto.h"

#include "main.h"

#include <string.h>

#include <stdio.h>

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

const uint8_t InputMessage[] ={"0123456789ABCDEFFEDCBA98765432101B1548A85A3CB982EE2DF9FEACA0BA351F829048"};

uint32_t InputMessageLength = (sizeof(InputMessage) - 1);

uint8_t MAC[CRL_SHA1_SIZE];

int32_t MACLength = 0;

CRC_HandleTypeDef hcrc;

RNG_HandleTypeDef hrng;

int32_t STM32_SHA1_Compute(uint8_t* InputMessage,

                uint32_t InputMessageLength,

                uint8_t *MessageDigest,

                int32_t* MessageDigestLength);

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_CRC_Init(void);

static void MX_RNG_Init(void);

/* USER CODE BEGIN PFP */

extern void initialise_monitor_handles(void);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

 */

int main(void)

{

int32_t status = HASH_SUCCESS;

int i;

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* Configure the system clock */

 initialise_monitor_handles();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 SystemClock_Config();

 MX_CRC_Init();

 MX_RNG_Init();

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6 | GPIO_PIN_7, GPIO_PIN_SET);

 /* Toggle the green led before starting the algorithm */

 printf("string: %s\r\n",InputMessage);

 printf("size of input string: %d\r\n",InputMessageLength);

 /* Enable CRC clock */

 __HAL_RCC_CRC_CLK_ENABLE();

 status = STM32_SHA1_Compute((uint8_t*)InputMessage,

         InputMessageLength,

 (uint8_t*)MAC,

 &MACLength);

  if (status == HASH_SUCCESS)

  {

  printf("printf digest\r\n");

  for(i=0;i<MACLength;i++)

  {

  printf("%02X\r\n",MAC[i]);

  }

  }

  else

  {

    Error_Handler();

   }

 while (1)

 {

 }

}

/**

 * @brief SHA1 compute example.

 * @param InputMessage: pointer to input message to be hashed.

 * @param InputMessageLength: input data message length in byte.

 * @param MessageDigest: pointer to output parameter that will handle message digest

 * @param MessageDigestLength: pointer to output digest length.

 * @retval error status: can be HASH_SUCCESS if success or one of

 *     HASH_ERR_BAD_PARAMETER, HASH_ERR_BAD_CONTEXT,

 *     HASH_ERR_BAD_OPERATION if error occured.

 */

int32_t STM32_SHA1_Compute(uint8_t* InputMessage,

                uint32_t InputMessageLength,

                uint8_t *MessageDigest,

                int32_t* MessageDigestLength)

{

 SHA1ctx_stt SHA1ctx;

 uint32_t error_status = HASH_SUCCESS;

 /* Set the size of the desired MAC*/

 SHA1ctx.mTagSize = CRL_SHA1_SIZE;

 /* Set flag field to default value */

 SHA1ctx.mFlags = E_HASH_DEFAULT;

 /* Initialize the context */

 error_status = SHA1_Init(&SHA1ctx);

 /* check for initialization errors */

 if (error_status == HASH_SUCCESS)

 {

  /* Add data to be hashed */

  error_status = SHA1_Append(&SHA1ctx, InputMessage, InputMessageLength);

  if (error_status == HASH_SUCCESS)

  {

   /* retrieve */

   error_status = SHA1_Finish(&SHA1ctx, MessageDigest, MessageDigestLength);

  }

 }

 return error_status;

}

Result is E4A12ABDD9571D77C44C700D4E35E2F5FCF6AE87 both, online and with your code on STM32L432KC linking libSTM32CryptographicV3.0.0_CM4_GCC*.a.

EJose.1
Associate II

Thank you for your reply. I really appreciate it. My issue is solved.