/* -------------------------------------------------------------------------------------------------------------------@udN | |
* °æÈ¨ËùÓÐ(C)2012, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾ | |
* | |
* ÎÄ ¼þ Ãû: drv_hash.c | |
* | |
------------------------------------------------------------------------------------------------------------*/ | |
#include "drv_hash.h" | |
#include <sdio.h> | |
/**-------------------------------------------------------------------------------------------------------------------@n | |
*Hash_set_len ¹¦ÄÜÏêÊö: | |
½«ÏûÏ¢³¤¶ÈÒÔbitΪµ¥Î»ÅäÖõ½³¤¶È¼Ä´æÆ÷ÖÐ | |
-------------------------------------------------------------- | |
×¢Òâ: | |
±¾º¯Êý¿¼Âǵ½¼ÆËãhashµÄÊý¾Ý³¤¶ÈÒ»°ã¶¼Êǰ´byte´«È룬ÇÒ×ÜÁ¿²»»á³¬¹ý4G byte£¬ | |
Òò´Ë¼ò»¯Á˺¯Êý¹¦ÄÜ¡£ | |
byte_Len ×ÜÊý²»Äܳ¬¹ý4G£¬·ñÔò¸Ãº¯ÊýÐèÒªÖØÐÂÐ޸ġ£ | |
*--------------------------------------------------------------------------------------------------------------------*/ | |
static void hash_SetLen(u32 byte_Len) | |
{ | |
REG32(HASH_LENGTH0) = byte_Len<<3; | |
REG32(HASH_LENGTH1) = byte_Len>>29; | |
REG32(HASH_LENGTH2) = 0; | |
REG32(HASH_LENGTH3) = 0; | |
} | |
#ifdef HASH_CODE_SUPPORT_ALL | |
static void hash_SetHmacKey(u32* pHmacKey, u32* HmacKey_len) | |
{ | |
u32 udI; | |
for(udI=0;udI<32;udI++) | |
{ | |
//¼Ä´æÆ÷¸ßµØÖ·´æ·ÅµÍλÊý¾Ý£¬Êý¾Ý´æ·ÅÊǵ͵ØÖ··Å¸ßλÊý¾Ý | |
if(udI<(32-HmacKey_len)) | |
REG32(HASH_HMACKEY+udI*4) = 0; | |
else | |
REG32(HASH_HMACKEY+udI*4) = pHmacKey[udI-(32-HmacKey_len)]; | |
} | |
} | |
#endif | |
/* ·µ»Øhash ¼ÆËã½á¹ûºÍ½á¹ûµÄ³¤¶È£¬³¤¶Èµ¥Î»ÊÇword */ | |
static void hash_GetResult(u32 udMode, u32* HashResult, u32* ResultLen) | |
{ | |
u32 udI, udResultLen=0; | |
#ifdef HASH_CODE_SUPPORT_ALL | |
switch(udMode) | |
{ | |
case HASH_MODE_MD5: | |
case HASH_MODE_HMAC_MD5: | |
udResultLen = 4; /* 4*4bytes = 128bit*/ | |
break; | |
case HASH_MODE_SHA1: | |
case HASH_MODE_HMAC_SHA1: | |
udResultLen = 5; /* 160 bit */ | |
break; | |
case HASH_MODE_SHA224: | |
case HASH_MODE_HMAC_SHA224: | |
udResultLen = 7; /* 224 bit */ | |
break; | |
case HASH_MODE_SHA256: | |
case HASH_MODE_HMAC_SHA256: | |
udResultLen = 8; /* 256 bit */ | |
break; | |
case HASH_MODE_SHA384: | |
case HASH_MODE_HMAC_SHA384: | |
udResultLen = 12; /* 384 bit */ | |
break; | |
case HASH_MODE_SHA512: | |
case HASH_MODE_HMAC_SHA512: | |
udResultLen = 16; /* 512 bit */ | |
break; | |
default: | |
break; | |
} | |
#else | |
udResultLen = 4; | |
#endif | |
*ResultLen = udResultLen; | |
for(udI=0;udI<udResultLen;udI++) //hash¼ÆËãÖ§³Ö128bitÊý¾Ý | |
{ | |
//¼Ä´æÆ÷µÍµØÖ·´æ·Åresult µÄ¸ßλ | |
HashResult[udI] = REG32(HASH_RESULT+(16-udResultLen+udI)*4); | |
} | |
} | |
/**-------------------------------------------------------------------------------------------------------------------@n | |
* @brief HASHÄ£¿éÇý¶¯½Ó¿Ú | |
* | |
* ¹¦ÄÜÏêÊö: | |
* - Hash_Computeº¯ÊýÊôÓÚ½Ó¿Úº¯Êý, Æä¹¦ÄÜÊÇ: | |
* - HASHÄ£¿éÇý¶¯½Ó¿Ú | |
* | |
* ²ÎÊý¸ÅÊö: | |
input: | |
udMode--------hashģʽ | |
udBigEndian----´óС¶Ë¸ñʽ | |
ploadAddr------¼ÆËãhashµÄÔ´Êý¾ÝÖ¸Õë | |
loadLen--------Ô´Êý¾Ý³¤¶È | |
pHmacKey------HMACģʽϵÄÃÜÔ¿ | |
HmacKey_len---ÃÜÔ¿³¤¶È | |
output: | |
HashResult-----hash¼ÆËã½á¹û | |
ResultLen------hash½á¹ûµÄ³¤¶È | |
* ·µ »Ø Öµ: 0:³É¹¦ 1:ʧ°Ü | |
*--------------------------------------------------------------------------------------------------------------------*/ | |
u32 Hash_Calculate(u32 udMode, u32 udBigEndian, u32* ploadAddr, u32 loadLen, u32* pHmacKey, u32* HmacKey_len, u32* HashResult, u32* ResultLen) | |
{ | |
u32 udMessageLen_word; | |
u32 udI; | |
if((loadLen == 0) || (ploadAddr == NULL) || (HashResult == NULL) ) | |
{ | |
return 1; | |
} | |
//config hash mode and enable module | |
REG32(HASH_CONTROL) = (udMode<<4)|(udBigEndian<<12)|1; | |
hash_SetLen(loadLen); | |
#ifdef HASH_CODE_SUPPORT_ALL | |
//write HMAC key | |
if(udMode&0x10) | |
{ | |
hash_SetHmacKey(pHmacKey, HmacKey_len); | |
} | |
#endif | |
REG32(HASH_START) = 1;//Hash_Start | |
//start write message data into fifo | |
udMessageLen_word = (loadLen+3)/4; | |
for(udI=0;udI<udMessageLen_word;udI++) | |
{ | |
REG32(HASH_DATA) = ploadAddr[udI]; | |
} | |
while(REG32(HASH_START) & 0x1); /* if cal busy, continue to wait*/ | |
//read Result | |
hash_GetResult(udMode, HashResult, ResultLen); | |
REG32(HASH_CONTROL) = 0;//Hash_DisableModule; | |
return 0; | |
} | |
/** @} */ | |