Staging: rt28x0: updates from vendor's V2.1.0.0 drivers
[sfrench/cifs-2.6.git] / drivers / staging / rt2860 / common / crypt_hmac.c
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 ========================================================================
33 Routine Description:
34     HMAC using SHA1 hash function
35
36 Arguments:
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
43 Return Value:
44     mac             Message authentication code
45
46 Note:
47     None
48 ========================================================================
49 */
50 VOID 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     RT_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     RT_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 #ifdef HMAC_MD5_SUPPORT
113 /*
114 ========================================================================
115 Routine Description:
116     HMAC using MD5 hash function
117
118 Arguments:
119     key             Secret key
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
124
125 Return Value:
126     mac             Message authentication code
127
128 Note:
129     None
130 ========================================================================
131 */
132 VOID HMAC_MD5(
133     IN  const UINT8 Key[],
134     IN  UINT KeyLen,
135     IN  const UINT8 Message[],
136     IN  UINT MessageLen,
137     OUT UINT8 MAC[],
138     IN  UINT MACLen)
139 {
140     MD5_CTX_STRUC md5_ctx1;
141     MD5_CTX_STRUC md5_ctx2;
142     UINT8 K0[MD5_BLOCK_SIZE];
143     UINT8 Digest[MD5_DIGEST_SIZE];
144     UINT index;
145
146     NdisZeroMemory(&md5_ctx1, sizeof(MD5_CTX_STRUC));
147     NdisZeroMemory(&md5_ctx2, sizeof(MD5_CTX_STRUC));
148     /*
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
153      */
154     NdisZeroMemory(K0, MD5_BLOCK_SIZE);
155     if (KeyLen <= MD5_BLOCK_SIZE) {
156         NdisMoveMemory(K0, Key, KeyLen);
157     } else {
158         RT_MD5(Key, KeyLen, K0);
159     }
160
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++)
164         K0[index] ^= 0x36;
165         /* End of for */
166
167     MD5_Init(&md5_ctx1);
168     /* H(K0^ipad) */
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);
173
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;
178         /* End of for */
179
180     MD5_Init(&md5_ctx2);
181     /* H(K0^opad) */
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);
186
187     if (MACLen > MD5_DIGEST_SIZE)
188         NdisMoveMemory(MAC, Digest, MD5_DIGEST_SIZE);
189     else
190         NdisMoveMemory(MAC, Digest, MACLen);
191 } /* End of HMAC_SHA256 */
192 #endif /* HMAC_MD5_SUPPORT */
193
194 /* End of crypt_hmac.c */