功能HMAC_MD5:返回成功但没有值

时间:2017-08-30 08:07:44

标签: c md5 hmac stm32f4

我在尝试编程HMAC_MD5代码时遇到了一些问题。

我在C上使用STM32F4微处理器。

这是我的(更新的)代码:

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);

这是HMAC_MD5函数的正式描述(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])

该函数返回值&#34; SUCCESS&#34;但摘要&#34;输出&#34;仍然是空的(充满&#39; \ 0&#39;)。

我没有从编译器(Attolic TrueStudio)收到任何警告,我无法更改密钥或挑战(连接)的值,因为服务器已经在使用旧系统运行。

2 个答案:

答案 0 :(得分:1)

让我猜猜。您使用的是 STM32F405 或 STM32F407,对吗?这些部分缺少哈希处理器,因此总是为摘要返回零。 ST 通过打开手册中有关哈希处理器的部分“本部分适用于 STM32F415/417xx 和 STM32F43xxx 设备”来记录它,但是快速的 Google 搜索表明您不是第一个期望此类信息的人出现在更显眼的地方(数据表、系列比较文档、产品选择器等)。

是的,我完全同意缺少散列处理器的 MCU 不应该报告使用所述散列处理器的操作成功,但这显然不是 ST 公司的人的做法。

无论如何。您需要升级到 SMT32F415(或 STM32F417)以获得硬件加速散列。如果这不是一个选项,那么总有一个软件实现。

答案 1 :(得分:0)

我遇到了与使用STM32 Hashing硬件相同的问题。经过几次尝试后,我决定使用md5库

由于我在项目中使用lwip,我注意到LWIP在ppp中有一个md5模块。

从lwip(在lwip /src/netif/ppp/md5.c中)获取所需文件(md5.c,md5.h),并将其复制到您的proyect。

更改非工作线

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

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

编辑:由于我没有在项目中使用ppp,我已经将ppp中的md5文件复制到了项目中并对其进行了一些编辑,删除了所有包含引用(md5.h和string.h除外)并删除了条件编译:

这是我在开始时删除的内容

//#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"

最后这个:

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

//#endif /* PPP_SUPPORT */

您可以在此处下载md5.c和md5.h的源代码

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

相关问题