STM32 加密库的 SHA-1 散列错误

Wrong SHA-1 hash with STM32 Crypto Library

使用 TrueStudio,我正在使用 STM32 加密库包 'STM32CubeExpansion_Crypto_V3.1.0' 在 STM32f103RB 上进行开发。我想使用 lib 中的 sha-1 但由于某些原因我没有得到正确的结果。

这是我的测试...

我的输入缓冲区是:"('1543409074.11', '1702635382a7b4243308035dfecc1e5e31678356bdfa39f92b6409a2')"

来自 SHA1 and other hash functions online generator,sha1 的结果:c6818ce06b79c91cda7cc89f1af243e3d1373c1f

使用 STM32 加密库,我似乎无法生成正确的 SHA-1 和。 例如,我使用以下代码调用 SHA-1 哈希函数:

  SHA1ctx_stt SHA1ctx_st;            // The SHA1 context
  membuf_stt mb_st;                  // structure that will contain the preallocated buffer
  uint8_t Digest[CRL_SHA1_SIZE];     // Buffer that will contain the SHA-1 digest of the message
  uint8_t preallocated_buffer[4096]; // buffer required for internal allocation of memory

  int32_t status = HASH_SUCCESS;
  int32_t outputSize;

  const char* Message="('1543409074.11', '1702635382a7b4243308035dfecc1e5e31678356bdfa39f92b6409a2')";
  int32_t MessageSize = strlen(Message);


  // Initialize the membuf_st that must be passed to the ECC functions
  mb_st.mSize = sizeof(preallocated_buffer);
  mb_st.mUsed = 0;
  mb_st.pmBuf = preallocated_buffer;

  //Initialize it the SHA-1 Context
  SHA1ctx_st.mFlags = E_HASH_DEFAULT;

  // 20 byte of output
  SHA1ctx_st.mTagSize = CRL_SHA1_SIZE;

  // Init SHA-1
  status = SHA1_Init(&SHA1ctx_st);
  if (status == HASH_SUCCESS)
  {
    // Process the message with SHA-1
    status = SHA1_Append(&SHA1ctx_st, (const uint8_t *)Message, MessageSize);
    if (status == HASH_SUCCESS)
    {
      // Output the Digest
      status = SHA1_Finish(&SHA1ctx_st, Digest, &outputSize);
      if (status == HASH_SUCCESS)
      {
        // It's OK, but result in Digest isn't correct
      }
    }
  }

我错过了什么?有谁知道可能出了什么问题?

谢谢,

我找到了解决方案。 ST 历来通过探测 CRC 外设将这些库锁定到 STM32 部件,基本上是挑战-响应测试。 为此,有必要启用 RCC_CRC_CLK。使用CubeMX,您需要在计算选项卡中激活"CRC Mode and Configuration"。