cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Crypto Lib SHA256 on STM32F091RC

daniel239955_st
Associate II
Posted on May 13, 2015 at 11:11

Hello!

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 algorithmimplementation''. 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 */
Crypto_DeInit();
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 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! Regards Daniel #stm32f091-crypto-sha256
4 REPLIES 4
qwer.asdf
Senior
Posted on May 18, 2015 at 16:20

STM32F0x MCUs don't have cryptographic module. You can use the CRC module for computing your checksums because implementing SHA256 in software for ARMv6 architecture (Cortex-M0) is not very efficient, but you can try.

daniel239955_st
Associate II
Posted on May 18, 2015 at 16:47

I ended up using a different cryptographic library: sphlib

http://www.saphir2.com/sphlib/

I am now able to produce correct SHA-256 hash code.

Posted on May 18, 2015 at 22:36

STM32F0x MCUs don't have cryptographic module. You can use the CRC module for computing your checksums because implementing SHA256 in software for ARMv6 architecture (Cortex-M0) is not very efficient, but you can try.

ST has a library it can use hardware or software, but I'd hazard that the library requires a better subset of THUMB instructions than the Cortex-M0 provides.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kesavan
Associate
Posted on September 01, 2016 at 19:30

I had a similar issue with an F4 and eventually fixed it by enabling the CRC module. In my case, it meant calling

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);