]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/rt3090/common/crypt_hmac.c
Staging: rt2860: add RT3090 chipset support
[net-next-2.6.git] / drivers / staging / rt3090 / common / crypt_hmac.c
CommitLineData
36c7928c
BZ
1/*
2 *************************************************************************
3 * Ralink Tech Inc.
4 * 5F., No.36, Taiyuan St., Jhubei City,
5 * Hsinchu County 302,
6 * Taiwan, R.O.C.
7 *
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 * *
25 *************************************************************************/
26
27#include "../crypt_hmac.h"
28
29
30#ifdef HMAC_SHA1_SUPPORT
31/*
32========================================================================
33Routine Description:
34 HMAC using SHA1 hash function
35
36Arguments:
37 key Secret key
38 key_len The length of the key in bytes
39 message Message context
40 message_len The length of message in bytes
41 macLen Request the length of message authentication code
42
43Return Value:
44 mac Message authentication code
45
46Note:
47 None
48========================================================================
49*/
50VOID HMAC_SHA1 (
51 IN const UINT8 Key[],
52 IN UINT KeyLen,
53 IN const UINT8 Message[],
54 IN UINT MessageLen,
55 OUT UINT8 MAC[],
56 IN UINT MACLen)
57{
58 SHA1_CTX_STRUC sha_ctx1;
59 SHA1_CTX_STRUC sha_ctx2;
60 UINT8 K0[SHA1_BLOCK_SIZE];
61 UINT8 Digest[SHA1_DIGEST_SIZE];
62 UINT index;
63
64 NdisZeroMemory(&sha_ctx1, sizeof(SHA1_CTX_STRUC));
65 NdisZeroMemory(&sha_ctx2, sizeof(SHA1_CTX_STRUC));
66 /*
67 * If the length of K = B(Block size): K0 = K.
68 * If the length of K > B: hash K to obtain an L byte string,
69 * then append (B-L) zeros to create a B-byte string K0 (i.e., K0 = H(K) || 00...00).
70 * If the length of K < B: append zeros to the end of K to create a B-byte string K0
71 */
72 NdisZeroMemory(K0, SHA1_BLOCK_SIZE);
73 if (KeyLen <= SHA1_BLOCK_SIZE)
74 NdisMoveMemory(K0, Key, KeyLen);
75 else
76 RT_SHA1(Key, KeyLen, K0);
77 /* End of if */
78
79 /* Exclusive-Or K0 with ipad */
80 /* ipad: Inner pad; the byte x��36�� repeated B times. */
81 for (index = 0; index < SHA1_BLOCK_SIZE; index++)
82 K0[index] ^= 0x36;
83 /* End of for */
84
85 SHA1_Init(&sha_ctx1);
86 /* H(K0^ipad) */
87 SHA1_Append(&sha_ctx1, K0, sizeof(K0));
88 /* H((K0^ipad)||text) */
89 SHA1_Append(&sha_ctx1, Message, MessageLen);
90 SHA1_End(&sha_ctx1, Digest);
91
92 /* Exclusive-Or K0 with opad and remove ipad */
93 /* opad: Outer pad; the byte x��5c�� repeated B times. */
94 for (index = 0; index < SHA1_BLOCK_SIZE; index++)
95 K0[index] ^= 0x36^0x5c;
96 /* End of for */
97
98 SHA1_Init(&sha_ctx2);
99 /* H(K0^opad) */
100 SHA1_Append(&sha_ctx2, K0, sizeof(K0));
101 /* H( (K0^opad) || H((K0^ipad)||text) ) */
102 SHA1_Append(&sha_ctx2, Digest, SHA1_DIGEST_SIZE);
103 SHA1_End(&sha_ctx2, Digest);
104
105 if (MACLen > SHA1_DIGEST_SIZE)
106 NdisMoveMemory(MAC, Digest, SHA1_DIGEST_SIZE);
107 else
108 NdisMoveMemory(MAC, Digest, MACLen);
109} /* End of HMAC_SHA1 */
110#endif /* HMAC_SHA1_SUPPORT */
111
112
113#ifdef HMAC_SHA256_SUPPORT
114/*
115========================================================================
116Routine Description:
117 HMAC using SHA256 hash function
118
119Arguments:
120 key Secret key
121 key_len The length of the key in bytes
122 message Message context
123 message_len The length of message in bytes
124 macLen Request the length of message authentication code
125
126Return Value:
127 mac Message authentication code
128
129Note:
130 None
131========================================================================
132*/
133VOID HMAC_SHA256 (
134 IN const UINT8 Key[],
135 IN UINT KeyLen,
136 IN const UINT8 Message[],
137 IN UINT MessageLen,
138 OUT UINT8 MAC[],
139 IN UINT MACLen)
140{
141 SHA256_CTX_STRUC sha_ctx1;
142 SHA256_CTX_STRUC sha_ctx2;
143 UINT8 K0[SHA256_BLOCK_SIZE];
144 UINT8 Digest[SHA256_DIGEST_SIZE];
145 UINT index;
146
147 NdisZeroMemory(&sha_ctx1, sizeof(SHA256_CTX_STRUC));
148 NdisZeroMemory(&sha_ctx2, sizeof(SHA256_CTX_STRUC));
149 /*
150 * If the length of K = B(Block size): K0 = K.
151 * If the length of K > B: hash K to obtain an L byte string,
152 * then append (B-L) zeros to create a B-byte string K0 (i.e., K0 = H(K) || 00...00).
153 * If the length of K < B: append zeros to the end of K to create a B-byte string K0
154 */
155 NdisZeroMemory(K0, SHA256_BLOCK_SIZE);
156 if (KeyLen <= SHA256_BLOCK_SIZE) {
157 NdisMoveMemory(K0, Key, KeyLen);
158 } else {
159 RT_SHA256(Key, KeyLen, K0);
160 }
161
162 /* Exclusive-Or K0 with ipad */
163 /* ipad: Inner pad; the byte x��36�� repeated B times. */
164 for (index = 0; index < SHA256_BLOCK_SIZE; index++)
165 K0[index] ^= 0x36;
166 /* End of for */
167
168 SHA256_Init(&sha_ctx1);
169 /* H(K0^ipad) */
170 SHA256_Append(&sha_ctx1, K0, sizeof(K0));
171 /* H((K0^ipad)||text) */
172 SHA256_Append(&sha_ctx1, Message, MessageLen);
173 SHA256_End(&sha_ctx1, Digest);
174
175 /* Exclusive-Or K0 with opad and remove ipad */
176 /* opad: Outer pad; the byte x��5c�� repeated B times. */
177 for (index = 0; index < SHA256_BLOCK_SIZE; index++)
178 K0[index] ^= 0x36^0x5c;
179 /* End of for */
180
181 SHA256_Init(&sha_ctx2);
182 /* H(K0^opad) */
183 SHA256_Append(&sha_ctx2, K0, sizeof(K0));
184 /* H( (K0^opad) || H((K0^ipad)||text) ) */
185 SHA256_Append(&sha_ctx2, Digest, SHA256_DIGEST_SIZE);
186 SHA256_End(&sha_ctx2, Digest);
187
188 if (MACLen > SHA256_DIGEST_SIZE)
189 NdisMoveMemory(MAC, Digest,SHA256_DIGEST_SIZE);
190 else
191 NdisMoveMemory(MAC, Digest, MACLen);
192
193} /* End of HMAC_SHA256 */
194#endif /* HMAC_SHA256_SUPPORT */
195
196
197#ifdef HMAC_MD5_SUPPORT
198/*
199========================================================================
200Routine Description:
201 HMAC using MD5 hash function
202
203Arguments:
204 key Secret key
205 key_len The length of the key in bytes
206 message Message context
207 message_len The length of message in bytes
208 macLen Request the length of message authentication code
209
210Return Value:
211 mac Message authentication code
212
213Note:
214 None
215========================================================================
216*/
217VOID HMAC_MD5(
218 IN const UINT8 Key[],
219 IN UINT KeyLen,
220 IN const UINT8 Message[],
221 IN UINT MessageLen,
222 OUT UINT8 MAC[],
223 IN UINT MACLen)
224{
225 MD5_CTX_STRUC md5_ctx1;
226 MD5_CTX_STRUC md5_ctx2;
227 UINT8 K0[MD5_BLOCK_SIZE];
228 UINT8 Digest[MD5_DIGEST_SIZE];
229 UINT index;
230
231 NdisZeroMemory(&md5_ctx1, sizeof(MD5_CTX_STRUC));
232 NdisZeroMemory(&md5_ctx2, sizeof(MD5_CTX_STRUC));
233 /*
234 * If the length of K = B(Block size): K0 = K.
235 * If the length of K > B: hash K to obtain an L byte string,
236 * then append (B-L) zeros to create a B-byte string K0 (i.e., K0 = H(K) || 00...00).
237 * If the length of K < B: append zeros to the end of K to create a B-byte string K0
238 */
239 NdisZeroMemory(K0, MD5_BLOCK_SIZE);
240 if (KeyLen <= MD5_BLOCK_SIZE) {
241 NdisMoveMemory(K0, Key, KeyLen);
242 } else {
243 RT_MD5(Key, KeyLen, K0);
244 }
245
246 /* Exclusive-Or K0 with ipad */
247 /* ipad: Inner pad; the byte x��36�� repeated B times. */
248 for (index = 0; index < MD5_BLOCK_SIZE; index++)
249 K0[index] ^= 0x36;
250 /* End of for */
251
252 MD5_Init(&md5_ctx1);
253 /* H(K0^ipad) */
254 MD5_Append(&md5_ctx1, K0, sizeof(K0));
255 /* H((K0^ipad)||text) */
256 MD5_Append(&md5_ctx1, Message, MessageLen);
257 MD5_End(&md5_ctx1, Digest);
258
259 /* Exclusive-Or K0 with opad and remove ipad */
260 /* opad: Outer pad; the byte x��5c�� repeated B times. */
261 for (index = 0; index < MD5_BLOCK_SIZE; index++)
262 K0[index] ^= 0x36^0x5c;
263 /* End of for */
264
265 MD5_Init(&md5_ctx2);
266 /* H(K0^opad) */
267 MD5_Append(&md5_ctx2, K0, sizeof(K0));
268 /* H( (K0^opad) || H((K0^ipad)||text) ) */
269 MD5_Append(&md5_ctx2, Digest, MD5_DIGEST_SIZE);
270 MD5_End(&md5_ctx2, Digest);
271
272 if (MACLen > MD5_DIGEST_SIZE)
273 NdisMoveMemory(MAC, Digest, MD5_DIGEST_SIZE);
274 else
275 NdisMoveMemory(MAC, Digest, MACLen);
276} /* End of HMAC_SHA256 */
277#endif /* HMAC_MD5_SUPPORT */
278
279/* End of crypt_hmac.c */