cancel
Showing results for 
Search instead for 
Did you mean: 

sha-1 problem with stm32 crypto lib

ari_v2
Senior
Posted on June 14, 2015 at 15:15

Hi,

I'm developing on the stm32f207 with the M3_CryptoFW_RngHW_2_0_6.lib

I would like to use the sha-1 from lib but from some reason I do not get the correct result.

Here is my test...

My input buffer is 8 bytes: 1,2,3,4,5,6,7,8

============================================

The result I receive (abySHA_result) from SHA-1 crypto lib is:

AC A7 3F C9 71 45 CB BE 5C 41 

4B 76 9E DF FD 50 6E EB 28 A9

============================================

The expected result is:

DD 57 83 BC F1 E9 00 2B C0 0A 

D5 B8 3A 95 ED 6E 4E BB 4A D5

============================================

What am I missing???

Thanks,

Ari

Here is my code:

=======================================

SHA1ctx_stt SHA1ctx_st;

uint8_t MyData[8] = {1,2,3,4,5,6,7,8};

int DataLengthOut = 0;

uint8_t abySHA_result[20];

int Test_Hash(void)

{

int32_t status = HASH_SUCCESS;

bool result;

/* DeInitialize STM32 Cryptographic Library */

Crypto_DeInit();

/* Initialize context */

memset (&SHA1ctx_st, 0, sizeof(SHA1ctx_st)) ;

/* Set the size of the desired hash digest */

/* Set flag field to default value */

SHA1ctx_st.mTagSize = 20;

SHA1ctx_st.mFlags   = E_HASH_DEFAULT;

result = SHA1_Init(&SHA1ctx_st);

if (result != HASH_SUCCESS) 

return false ;

// Compute hash

SHA1_Append(&SHA1ctx_st, MyData, 8);

if (status != HASH_SUCCESS)

return false;

result = SHA1_Finish(&SHA1ctx_st, abySHA_result, &DataLengthOut);

if (result != HASH_SUCCESS) 

return false ;

return true;

}

8 REPLIES 8
nesrine
Senior
Posted on June 16, 2015 at 18:52

Hello,

Make sure that STM32F2XX define appear in your project settings,

Also you can work with the template of the STM32 cryptographic Library package 2.0.6 and then put your code:

STM32 Cryptographic library package V2.0.6__\STM32 Cryptographic library package V2.0.6\Project\STM32F2xx_Cryptographic_Templates\''choose your tools''

I hope this answered your questions.

Syrine

Posted on June 16, 2015 at 21:57

What am I missing???

I'm seeing the same values, and got the same number cross checking SHA1

Not sure what the deal is with the library

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ari_v2
Senior
Posted on June 17, 2015 at 16:05

Thanks - I've checked both of your recommendations. But I get the same results.

ari_v2
Senior
Posted on June 17, 2015 at 16:10

Hi - Thanks.

Do you mean that the correct result is?

AC A7 3F C9 71 45 CB BE 5C 41 

4B 76 9E DF FD 50 6E EB 28 A9

I checked with several SHA1 calculators and I get:

DD 57 83 BC F1 E9 00 2B C0 0A 

D5 B8 3A 95 ED 6E 4E BB 4A D5

Regards,

Ari

Posted on June 17, 2015 at 16:48

The library doesn't seem to function properly with the 8 byte example, neither SHA1 or MD5 provide the values computed externally.

Your externally computed SHA1 matches what I can generate with independent tools.

The value returned by the library is incorrect, but matches the value I was able to generate with it using one of the other library files. I need to go find my F215 part and check the HW computed version, and that computed by my own HW version.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 17, 2015 at 18:29

Ok, with the STM32F215, and Standard Peripheral Library HASH implementations.

MD5/SHA1 HASH Testing - STM32F215 HW
SYS:120000000 H:120000000, P1:30000000, P2:60000000
---------------------------------------
MD5 Hash Data:
---------------------------------------
0E E0 64 6C 1C 77 D8 13 1C C8 F4 EE 65 C7 67 3B 
---------------------------------------
SHA1 Hash Data:
---------------------------------------
DD 57 83 BC F1 E9 00 2B C0 0A D5 B8 3A 95 ED 6E 4E BB 4A D5 
//
// File Checksum Integrity Verifier version 2.
//
MD5 SHA-1
-------------------------------------------------------------------------
0ee0646c1c77d8131cc8f4ee65c7673b dd5783bcf1e9002bc00ad5b83a95ed6e4ebb4ad5 test.bin

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 17, 2015 at 18:45

Bogus results from M4_CryptoFW_2_0_6.lib

SHA1 20
AC A7 3F C9 71 45 CB BE 5C 41 4B 76 9E DF FD 50 6E EB 28 A9 

MD5 16
01 23 45 67 89 AB CD EF ED 4F F0 C3 76 54 32 10 

There's some stupidity in the library where it callsRCC_AHBPeriphClockCmd(), looking to enable the CRC hardware on the STM32 F1, to validate it has STM32 hardware and not on some ATMEL or NXP CPU. This linkage fails on the F4, and turns on GPIOG on the F2

void RCC_AHBPeriphClockCmd(int x, int y)
{
printf(''AHB %d %d
'', x, y);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
}

If I enable the CRC unit, the HASH now works...

AHB 64 1
SHA1 20
DD 57 83 BC F1 E9 00 2B C0 0A D5 B8 3A 95 ED 6E 4E BB 4A D5 
MD5 16
0E E0 64 6C 1C 77 D8 13 1C C8 F4 EE 65 C7 67 3B 

Suggest you add

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);

To your code. I'm going to push this to moderation, as this is going to break on other STM32 devices, where the AHB/CRC aren't the same.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ari_v2
Senior
Posted on June 18, 2015 at 10:33

With additional code - It works!

Many thanks,

Ari