Function HMAC_MD5 : Return succes but no value

442 views Asked by At

I have got some problems trying to programm the HMAC_MD5 code.

I am working in C on a STM32F4 microprocessor.

Here is my (updated) code:

RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_HASH, ENABLE); static uint8_t 

static Challenge[16] = "ldopwhjtsnkiaq8f";
static uint8_t Key[16] = "abcdefghijklmnop";
static uint8_t* HMAC_Key;
static uint8_t* HMAC_Input;
static uint8_t HMAC_Response1[16];
static uint8_t HMAC_Response2[16];

int m = 0;

HMAC_Input = &Challenge[0];
HMAC_Key = &Key[0];

ErrorStatus Result = ERROR;
for(m=0;m<16;m++){
    HMAC_Response1[m]=1;
    HMAC_Response2[m]=2;
}

Result = HASH_MD5(HMAC_Input, 16, HMAC_Response1);
Result = HMAC_MD5(HMAC_Key, 16, HMAC_Input, 16, HMAC_Response2);

That is the official description of the HMAC_MD5 function (https://github.com/espruino/Espruino/blob/master/targetlibs/stm32f4/lib/stm32f4xx_hash_md5.c):

/**
    * @brief  Compute the HMAC MD5 digest.
    * @param  Key: pointer to the Key used for HMAC.
    * @param  Keylen: length of the Key used for HMAC.
    * @param  Input: pointer to the Input buffer to be treated.
    * @param  Ilen: length of the Input buffer
    * @param  Output: the returned digest
    * @retval An ErrorStatus enumeration value:
    *          - SUCCESS: digest computation done
    *          - ERROR: digest computation failed
    */

ErrorStatus HMAC_MD5(uint8_t *Key, uint32_t Keylen, uint8_t *Input, 
                     uint32_t Ilen, uint8_t Output[16])

The function returns the value "SUCCESS" but the digest "Output" is still empty (full of '\0').

I don't get any warning from the compiler (Attolic TrueStudio) and I cannot change the value of the Key or of the Challenge (Concatenation), because the server is already running with older systems.

2

There are 2 answers

0
Miloslaw Smyk On

Let me guess. You're using STM32F405 or STM32F407, right? These parts lack the hash processor, and thus always return zeros for the digest. ST kind of documented it by opening the section on hash processor in the manual with "This section applies to STM32F415/417xx and STM32F43xxx devices.", but a quick Google search demonstrates that you are not the first person that expected this sort of information to be present in a more prominent place (datasheet, family comparison doc, product selector, etc).

And yes, I fully agree that an MCU with a missing hash processor should not report success on operations that use said hash processor, but that's apparently not how folks at ST roll.

Anyway. You will need to upgrade to SMT32F415 (or STM32F417) to get hardware accelerated hashing. If that's not an option, well, there is always a software implementation.

0
Hamboy75 On

I had the same problem that you had using STM32 Hashing hardware. After a few tries I decided to use a md5 library

Since I'm using lwip in my project I noticed that LWIP has a md5 module inside ppp.

Just get the files needed (md5.c, md5.h) from lwip (inside lwip /src/netif/ppp/md5.c), and copy it to your proyect.

Change the non-working line

uint32_t dev=HASH_MD5((uint8_t *) input, strlen((char *) input), Md5);

for

MD5_CTX mdContext;
MD5Init(&mdContext);
MD5Update(&mdContext, input, strlen((char *) input));
MD5Final(Md5,&mdContext);

Edited: Since i dont use ppp in the project, i have copied the md5 file from ppp to the project and I edited it a bit, removing all include references (except md5.h and string.h) and removing conditional compilation:

This is the stuff that I removed at the beginning

//#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */

//#if CHAP_SUPPORT || MD5_SUPPORT

//#include "ppp.h"
//#include "pppdebug.h"

And this at the end:

//#endif /* CHAP_SUPPORT || MD5_SUPPORT */

//#endif /* PPP_SUPPORT */

You can download the source code for md5.c and md5.h here

https://github.com/goertzenator/lwip/blob/master/lwip-1.4.0/src/netif/ppp/md5.c https://github.com/goertzenator/lwip/blob/master/lwip-1.4.0/src/netif/ppp/md5.h