cancel
Showing results for 
Search instead for 
Did you mean: 

Crypto library SHA1 problem.

francescatodiego
Associate II
Posted on February 23, 2015 at 17:51

I am starting using the crypto library and with a STM32F407 device

I don't have the hw hash peripheral and I must use the software library My version: M4_CryptoFW_RngHW_2_0_6.a My code

bool Lib_SHA1_hash (uint8_t* hash, uint32_t hash_len, uint8_t* p, uint32_t len)
{
SHA1ctx_stt SHA1ctx_st;
bool result ;
/* 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 ;
result = SHA1_Append(&SHA1ctx_st, p, len) ;
if (result != HASH_SUCCESS) return false ;
result = SHA1_Finish(&SHA1ctx_st, hash, &hash_len);
if (result != HASH_SUCCESS) return false ;
return true ;
}

The library manual say the SHA1_Finish must return the hash value in hash buffer and the length of data written in hash buffer in hash_len the hash buffer contain correct data but my hash_len is always a random value (The library is precompiled and I see only the asm code)
2 REPLIES 2
Posted on February 23, 2015 at 18:21

Perhaps you should pass in a pointer, instead of using the address of a throw away parameter on the stack?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
francescatodiego
Associate II
Posted on February 23, 2015 at 18:44

Oooooooooooooopsssssssssssss!

Stupid mistake !! It work! thanks Clive

bool Lib_SHA1_hash (uint8_t* hash, uint32_t* hash_len, uint8_t* p, uint32_t len)
{
SHA1ctx_stt SHA1ctx_st;
bool result ;
/* 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 ;
result = SHA1_Append(&SHA1ctx_st, p, len) ;
if (result != HASH_SUCCESS) return false ;
result = SHA1_Finish(&SHA1ctx_st, hash, hash_len);
if (result != HASH_SUCCESS) return false ;
return true ;
}