3

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.

Indri
  • 61
  • 2
  • 1
    What do you mean by '"Output" is still empty'? Zeros? A test pattern (non-zeros written before, to be able to detect zeros being written over it) still in there? How is `Result` initialised? Not, as in the quote? Init it to something other than "SUCCESS". – Yunnosch Aug 30 '17 at 08:42
  • You might want to create an actual [mcve]. For the "V" you can assume that readers have the library available, i.e. you do not need to provide it. – Yunnosch Aug 30 '17 at 08:45
  • Yes,`Output` is full of zeros ('\0'). I hadn't initialised `Result` and `HMAC_Response`. Now, I have done it, with ERROR and '1', but I get the same results (SUCCES and \0). Ok, I will try with a "M, C and V example" – Indri Aug 30 '17 at 10:08
  • It still doesn't work. I have been trying since thuedsay. Unsucessful.. – Indri Sep 01 '17 at 06:44
  • Same problem here with IAR, just using HASH_MD5 (I get zeroes on HASH_MD5), did you fix it? – Hamboy75 Feb 16 '18 at 10:35

2 Answers2

1

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.

Miloslaw Smyk
  • 1,305
  • 10
  • 15
0

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

Hamboy75
  • 938
  • 1
  • 11
  • 22