blob: 3d89edb85677f0bb4d86b99af1d3669e916f61c6 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* -------------------------------------------------------------------------------------------------------------------@udN
2 * °æÈ¨ËùÓÐ(C)2012, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾
3 *
4 * ÎÄ ¼þ Ãû: drv_hash.c
5 *
6------------------------------------------------------------------------------------------------------------*/
7
8#include "drv_hash.h"
9#include <sdio.h>
10
11/**-------------------------------------------------------------------------------------------------------------------@n
12 *Hash_set_len ¹¦ÄÜÏêÊö:
13 ½«ÏûÏ¢³¤¶ÈÒÔbitΪµ¥Î»ÅäÖõ½³¤¶È¼Ä´æÆ÷ÖÐ
14 --------------------------------------------------------------
15 ×¢Òâ:
16 ±¾º¯Êý¿¼Âǵ½¼ÆËãhashµÄÊý¾Ý³¤¶ÈÒ»°ã¶¼Êǰ´byte´«È룬ÇÒ×ÜÁ¿²»»á³¬¹ý4G byte£¬
17 Òò´Ë¼ò»¯Á˺¯Êý¹¦ÄÜ¡£
18 byte_Len ×ÜÊý²»Äܳ¬¹ý4G£¬·ñÔò¸Ãº¯ÊýÐèÒªÖØÐÂÐ޸ġ£
19 *--------------------------------------------------------------------------------------------------------------------*/
20static void hash_SetLen(u32 byte_Len)
21{
22 REG32(HASH_LENGTH0) = byte_Len<<3;
23 REG32(HASH_LENGTH1) = byte_Len>>29;
24 REG32(HASH_LENGTH2) = 0;
25 REG32(HASH_LENGTH3) = 0;
26}
27
28#ifdef HASH_CODE_SUPPORT_ALL
29static void hash_SetHmacKey(u32* pHmacKey, u32* HmacKey_len)
30{
31 u32 udI;
32
33 for(udI=0;udI<32;udI++)
34 {
35 //¼Ä´æÆ÷¸ßµØÖ·´æ·ÅµÍλÊý¾Ý£¬Êý¾Ý´æ·ÅÊǵ͵ØÖ··Å¸ßλÊý¾Ý
36 if(udI<(32-HmacKey_len))
37 REG32(HASH_HMACKEY+udI*4) = 0;
38 else
39 REG32(HASH_HMACKEY+udI*4) = pHmacKey[udI-(32-HmacKey_len)];
40 }
41}
42#endif
43
44/* ·µ»Øhash ¼ÆËã½á¹ûºÍ½á¹ûµÄ³¤¶È£¬³¤¶Èµ¥Î»ÊÇword */
45static void hash_GetResult(u32 udMode, u32* HashResult, u32* ResultLen)
46{
47 u32 udI, udResultLen=0;
48
49#ifdef HASH_CODE_SUPPORT_ALL
50 switch(udMode)
51 {
52 case HASH_MODE_MD5:
53 case HASH_MODE_HMAC_MD5:
54 udResultLen = 4; /* 4*4bytes = 128bit*/
55 break;
56
57 case HASH_MODE_SHA1:
58 case HASH_MODE_HMAC_SHA1:
59 udResultLen = 5; /* 160 bit */
60 break;
61
62 case HASH_MODE_SHA224:
63 case HASH_MODE_HMAC_SHA224:
64 udResultLen = 7; /* 224 bit */
65 break;
66
67 case HASH_MODE_SHA256:
68 case HASH_MODE_HMAC_SHA256:
69 udResultLen = 8; /* 256 bit */
70 break;
71
72 case HASH_MODE_SHA384:
73 case HASH_MODE_HMAC_SHA384:
74 udResultLen = 12; /* 384 bit */
75 break;
76
77 case HASH_MODE_SHA512:
78 case HASH_MODE_HMAC_SHA512:
79 udResultLen = 16; /* 512 bit */
80 break;
81
82 default:
83 break;
84 }
85#else
86 udResultLen = 4;
87#endif
88
89 *ResultLen = udResultLen;
90
91 for(udI=0;udI<udResultLen;udI++) //hash¼ÆËãÖ§³Ö128bitÊý¾Ý
92 {
93 //¼Ä´æÆ÷µÍµØÖ·´æ·Åresult µÄ¸ßλ
94 HashResult[udI] = REG32(HASH_RESULT+(16-udResultLen+udI)*4);
95 }
96}
97
98/**-------------------------------------------------------------------------------------------------------------------@n
99 * @brief HASHÄ£¿éÇý¶¯½Ó¿Ú
100 *
101 * ¹¦ÄÜÏêÊö:
102 * - Hash_Computeº¯ÊýÊôÓÚ½Ó¿Úº¯Êý, Æä¹¦ÄÜÊÇ:
103 * - HASHÄ£¿éÇý¶¯½Ó¿Ú
104 *
105 * ²ÎÊý¸ÅÊö:
106 input:
107 udMode--------hashģʽ
108 udBigEndian----´óС¶Ë¸ñʽ
109 ploadAddr------¼ÆËãhashµÄÔ´Êý¾ÝÖ¸Õë
110 loadLen--------Ô´Êý¾Ý³¤¶È
111 pHmacKey------HMACģʽϵÄÃÜÔ¿
112 HmacKey_len---ÃÜÔ¿³¤¶È
113 output:
114 HashResult-----hash¼ÆËã½á¹û
115 ResultLen------hash½á¹ûµÄ³¤¶È
116 * ·µ »Ø Öµ: 0:³É¹¦ 1:ʧ°Ü
117
118 *--------------------------------------------------------------------------------------------------------------------*/
119u32 Hash_Calculate(u32 udMode, u32 udBigEndian, u32* ploadAddr, u32 loadLen, u32* pHmacKey, u32* HmacKey_len, u32* HashResult, u32* ResultLen)
120{
121 u32 udMessageLen_word;
122 u32 udI;
123
124 if((loadLen == 0) || (ploadAddr == NULL) || (HashResult == NULL) )
125 {
126 return 1;
127 }
128
129 //config hash mode and enable module
130 REG32(HASH_CONTROL) = (udMode<<4)|(udBigEndian<<12)|1;
131
132 hash_SetLen(loadLen);
133
134#ifdef HASH_CODE_SUPPORT_ALL
135 //write HMAC key
136 if(udMode&0x10)
137 {
138 hash_SetHmacKey(pHmacKey, HmacKey_len);
139 }
140#endif
141
142 REG32(HASH_START) = 1;//Hash_Start
143
144 //start write message data into fifo
145 udMessageLen_word = (loadLen+3)/4;
146 for(udI=0;udI<udMessageLen_word;udI++)
147 {
148 REG32(HASH_DATA) = ploadAddr[udI];
149 }
150
151 while(REG32(HASH_START) & 0x1); /* if cal busy, continue to wait*/
152
153 //read Result
154 hash_GetResult(udMode, HashResult, ResultLen);
155
156 REG32(HASH_CONTROL) = 0;//Hash_DisableModule;
157
158 return 0;
159}
160
161/** @} */
162