2017-08-30 34 views
1

我試圖編程HMAC_MD5代碼時遇到了一些問題。函數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]) 

該函數返回值 「成功」,但摘要「輸出」仍爲空(滿''0')。

我沒有從編譯器(Attolic TrueStudio)得到任何警告,我也無法更改密鑰或挑戰(串聯)的值,因爲服務器已經在使用較舊的系統運行。

+1

你是什麼意思'「輸出」仍然是空的'?零?一個測試模式(之前寫入的非零,以便能夠檢測零寫入)仍在那裏? 「結果」如何初始化?不是,如在報價中?將其初始化爲「SUCCESS」以外的內容。 – Yunnosch

+0

您可能想要創建一個實際的[mcve]。對於「V」,你可以假設讀者有可用的庫,即你不需要提供它。 – Yunnosch

+0

是的,'輸出'滿了零('\ 0')。我沒有初始化'Result'和'HMAC_Response'。現在,我已經完成了,帶有ERROR和'1',但是我得到了相同的結果(SUCCES和\ 0)。 好吧,我會嘗試一個「M,C和V例子」 – Indri

回答

0

我有和使用STM32散列硬件相同的問題。幾次嘗試後,我決定使用md5庫

由於我在我的項目中使用lwip,我注意到LWIP在ppp內部有一個md5模塊。

只需從lwip(lwip /src/netif/ppp/md5.c中)獲取需要的文件(md5.c,md5.h),並將其複製到您的項目中。

更改非工作線

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,我抄購買力平價的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