2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
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. *
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. *
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. *
25 *************************************************************************/
27 #include "../crypt_hmac.h"
30 #ifdef HMAC_SHA1_SUPPORT
32 ========================================================================
34 HMAC using SHA1 hash function
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
44 mac Message authentication code
48 ========================================================================
53 IN const UINT8 Message[],
58 SHA1_CTX_STRUC sha_ctx1;
59 SHA1_CTX_STRUC sha_ctx2;
60 UINT8 K0[SHA1_BLOCK_SIZE];
61 UINT8 Digest[SHA1_DIGEST_SIZE];
64 NdisZeroMemory(&sha_ctx1, sizeof(SHA1_CTX_STRUC));
65 NdisZeroMemory(&sha_ctx2, sizeof(SHA1_CTX_STRUC));
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
72 NdisZeroMemory(K0, SHA1_BLOCK_SIZE);
73 if (KeyLen <= SHA1_BLOCK_SIZE)
74 NdisMoveMemory(K0, Key, KeyLen);
76 RT_SHA1(Key, KeyLen, K0);
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++)
85 RT_SHA1_Init(&sha_ctx1);
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);
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;
98 RT_SHA1_Init(&sha_ctx2);
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);
105 if (MACLen > SHA1_DIGEST_SIZE)
106 NdisMoveMemory(MAC, Digest, SHA1_DIGEST_SIZE);
108 NdisMoveMemory(MAC, Digest, MACLen);
109 } /* End of HMAC_SHA1 */
110 #endif /* HMAC_SHA1_SUPPORT */
112 #ifdef HMAC_MD5_SUPPORT
114 ========================================================================
116 HMAC using MD5 hash function
120 key_len The length of the key in bytes
121 message Message context
122 message_len The length of message in bytes
123 macLen Request the length of message authentication code
126 mac Message authentication code
130 ========================================================================
133 IN const UINT8 Key[],
135 IN const UINT8 Message[],
140 MD5_CTX_STRUC md5_ctx1;
141 MD5_CTX_STRUC md5_ctx2;
142 UINT8 K0[MD5_BLOCK_SIZE];
143 UINT8 Digest[MD5_DIGEST_SIZE];
146 NdisZeroMemory(&md5_ctx1, sizeof(MD5_CTX_STRUC));
147 NdisZeroMemory(&md5_ctx2, sizeof(MD5_CTX_STRUC));
149 * If the length of K = B(Block size): K0 = K.
150 * If the length of K > B: hash K to obtain an L byte string,
151 * then append (B-L) zeros to create a B-byte string K0 (i.e., K0 = H(K) || 00...00).
152 * If the length of K < B: append zeros to the end of K to create a B-byte string K0
154 NdisZeroMemory(K0, MD5_BLOCK_SIZE);
155 if (KeyLen <= MD5_BLOCK_SIZE) {
156 NdisMoveMemory(K0, Key, KeyLen);
158 RT_MD5(Key, KeyLen, K0);
161 /* Exclusive-Or K0 with ipad */
162 /* ipad: Inner pad; the byte x¡¦36¡¦ repeated B times. */
163 for (index = 0; index < MD5_BLOCK_SIZE; index++)
169 MD5_Append(&md5_ctx1, K0, sizeof(K0));
170 /* H((K0^ipad)||text) */
171 MD5_Append(&md5_ctx1, Message, MessageLen);
172 MD5_End(&md5_ctx1, Digest);
174 /* Exclusive-Or K0 with opad and remove ipad */
175 /* opad: Outer pad; the byte x¡¦5c¡¦ repeated B times. */
176 for (index = 0; index < MD5_BLOCK_SIZE; index++)
177 K0[index] ^= 0x36^0x5c;
182 MD5_Append(&md5_ctx2, K0, sizeof(K0));
183 /* H( (K0^opad) || H((K0^ipad)||text) ) */
184 MD5_Append(&md5_ctx2, Digest, MD5_DIGEST_SIZE);
185 MD5_End(&md5_ctx2, Digest);
187 if (MACLen > MD5_DIGEST_SIZE)
188 NdisMoveMemory(MAC, Digest, MD5_DIGEST_SIZE);
190 NdisMoveMemory(MAC, Digest, MACLen);
191 } /* End of HMAC_SHA256 */
192 #endif /* HMAC_MD5_SUPPORT */
194 /* End of crypt_hmac.c */