AnsweredAssumed Answered

STM32 Crypto Lib SHA256 on STM32F091RC

Question asked by daniel_s on May 13, 2015
Latest reply on May 18, 2015 by Clive One

I'm having trouble using the STM32 Crytpographic Library SHA256 hash in an application running on an STM32F091RC MCU. I'm using it to verify contents in flash (FW download), but have now retraced to just try to encode a simple string "asdf" and still getting incorrect results. During the development phase, we've used a different MCU on which my code was working fine on.

I see in the documentation for the HASH algorithm: "This algorithm can run with the STM32F1, STM32L1, STM32F20x, STM32F05x, STM32F40x, STM32F37x and the STM32F30x series using a software algorithm implementation". I figured this may be out of date (documentation is from 2013), and it would work with other STM32F0x series MCU:s. But maybe I'm wrong?

I've been through the code multiple times, but I've not completely ruled there out could be some code error. I'll give a simple example of what I'm doing which yields an incorrect SHA256 key. I am not getting any indication of errors from the library, just HASH_SUCCESS all the way.

uint8_t MessageDigest[CRL_SHA256_SIZE];
int32_t MessageDigestLength;
ErrorStatus HashSHA256_Buffer(uint8_t* buffer, int32_t size)
  int32_t status = HASH_SUCCESS;
  /* DeInitialize STM32 Cryptographic Library */
  SHA256ctx_stt P_pSHA256ctx;
  /* Set the size of the desired hash digest */
  P_pSHA256ctx.mTagSize = CRL_SHA256_SIZE;
  /* Set flag field to default value */
  P_pSHA256ctx.mFlags = E_HASH_DEFAULT;
  status = SHA256_Init(&P_pSHA256ctx);
  if(status != HASH_SUCCESS)
    return ERROR;
  /* Generate the SHA256 */
  /* check for initialization errors - then add data to be hashed */
  if (status == HASH_SUCCESS)
    status = SHA256_Append(&P_pSHA256ctx, buffer, size);
  /* retrieve */
  if(status == HASH_SUCCESS)
    status = SHA256_Finish(&P_pSHA256ctx, MessageDigest, &MessageDigestLength);
  if (status == HASH_SUCCESS)
    return SUCCESS;
  return ERROR;

From my main function, I'm simply doing

uint8_t buffer[4] = "asdf";
HashSHA256_Buffer(buffer, sizeof(buffer));

I can debug the program and see that the string "asdf" enters the P_pSHA256ctx context struct's amBuffer and that the bit size (amCount) is 32. So all that seems correct before SHA256_Finish is called.

I should be getting this: f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b in hex string format (or equivalent 32 byte byte-array). I've tried several online, Linux and Windows sha256 calculators giving this same results, but I'm getting something completely different in my application (but of the same size).

Is there any other SHA256/Cryptographic library that is known to work well with STM32 I could consider using for cross-referencing with this and/or replace the currently used library?

Any help or advise would be appreciated!