xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/mvl88w8977/mlan/esa/common/md5.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /** @file md5.c
2  *
3  *  @brief This file defines the function for md5
4  *
5  * Copyright (C) 2014-2017, Marvell International Ltd.
6  *
7  * This software file (the "File") is distributed by Marvell International
8  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
9  * (the "License").  You may use, redistribute and/or modify this File in
10  * accordance with the terms and conditions of the License, a copy of which
11  * is available by writing to the Free Software Foundation, Inc.,
12  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
13  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14  *
15  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
18  * this warranty disclaimer.
19  */
20 
21 /******************************************************
22 Change log:
23     03/07/2014: Initial version
24 ******************************************************/
25 #include "wltypes.h"
26 #include "md5.h"
27 
28 #include "hostsa_ext_def.h"
29 #include "authenticator.h"
30 
31 /* Constants for wpa_MD5Transform routine.
32  */
33 #define S11 7
34 #define S12 12
35 #define S13 17
36 #define S14 22
37 #define S21 5
38 #define S22 9
39 #define S23 14
40 #define S24 20
41 #define S31 4
42 #define S32 11
43 #define S33 16
44 #define S34 23
45 #define S41 6
46 #define S42 10
47 #define S43 15
48 #define S44 21
49 
50 #define Mrvl_MD5_memcpy(utils, output, input, len) memcpy(utils, output, input, len)
51 #define Mrvl_MD5_memset(utils, output, value, len) memset(utils, output, value, len)
52 #define Encode(utils, output, input, len) memcpy(utils, output, input, len)
53 
54 static void wpa_MD5Transform(void *priv, UINT32 *state, unsigned int *block);
55 
56 static const unsigned char PADDING[64] = {
57 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
61 };
62 
63 /* F, G, H and I are basic MD5 functions.
64  */
65 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
66 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
67 #define H(x, y, z) ((x) ^ (y) ^ (z))
68 #define I(x, y, z) ((y) ^ ((x) | (~z)))
69 
70 /* ROTATE_LEFT rotates x left n bits.
71  */
72 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
73 
74 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
75 Rotation is separate from addition to prevent recomputation.
76  */
77 #define FF(a, b, c, d, x, s, ac) { \
78  (a) += F ((b), (c), (d)) + (x) + (UINT32)(ac); \
79  (a) = ROTATE_LEFT ((a), (s)); \
80  (a) += (b);}
81 
82 #define GG(a, b, c, d, x, s, ac) { \
83  (a) += G ((b), (c), (d)) + (x) + (UINT32)(ac); \
84  (a) = ROTATE_LEFT ((a), (s)); \
85  (a) += (b); \
86   }
87 #define HH(a, b, c, d, x, s, ac) { \
88  (a) += H ((b), (c), (d)) + (x) + (UINT32)(ac); \
89  (a) = ROTATE_LEFT ((a), (s)); \
90  (a) += (b); \
91   }
92 #define II(a, b, c, d, x, s, ac) { \
93  (a) += I ((b), (c), (d)) + (x) + (UINT32)(ac); \
94  (a) = ROTATE_LEFT ((a), (s)); \
95  (a) += (b); \
96   }
97 
98 /* MD5 initialization. Begins an MD5 operation, writing a new context.
99  */
100 void
wpa_MD5Init(Mrvl_MD5_CTX * context)101 wpa_MD5Init(Mrvl_MD5_CTX *context)
102 {
103 	context->count[0] = context->count[1] = 0;
104 	/* Load magic initialization constants. */
105 	context->state[0] = 0x67452301;
106 	context->state[1] = 0xefcdab89;
107 	context->state[2] = 0x98badcfe;
108 	context->state[3] = 0x10325476;
109 }
110 
111 /* MD5 block update operation. Continues an MD5 message-digest
112   operation, processing another message block, and updating the
113   context.
114  */
115 void
wpa_MD5Update(void * priv,Mrvl_MD5_CTX * context,UINT8 * input,UINT32 inputLen)116 wpa_MD5Update(void *priv, Mrvl_MD5_CTX *context, UINT8 *input, UINT32 inputLen)
117 {
118 	phostsa_private psapriv = (phostsa_private)priv;
119 	hostsa_util_fns *util_fns = &psapriv->util_fns;
120 	unsigned int i, index, partLen;
121 
122 	/* Compute number of bytes mod 64 */
123 	index = (unsigned int)((context->count[0] >> 3) & 0x3F);
124 
125 	/* Update number of bits */
126 	if ((context->count[0] += ((UINT32)inputLen << 3))
127 	    < ((UINT32)inputLen << 3)) {
128 		context->count[1]++;
129 	}
130 	context->count[1] += ((UINT32)inputLen >> 29);
131 
132 	partLen = 64 - index;
133 
134 	/* Transform as many times as possible. */
135 	if (inputLen >= partLen) {
136 		Mrvl_MD5_memcpy(util_fns, &context->buffer[index], input,
137 				partLen);
138 		wpa_MD5Transform((void *)priv, context->state,
139 				 (UINT32 *)context->buffer);
140 
141 		for (i = partLen; i + 63 < inputLen; i += 64) {
142 			Mrvl_MD5_memcpy(util_fns, context->scratch, (input + i),
143 					64);
144 			wpa_MD5Transform((void *)priv, context->state,
145 					 context->scratch);
146 		}
147 		index = 0;
148 	} else {
149 		i = 0;
150 	}
151 
152 	/* Buffer remaining input */
153 	Mrvl_MD5_memcpy(util_fns, &context->buffer[index], &input[i],
154 			inputLen - i);
155 }
156 
157 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
158   the message digest and zeroizing the context.
159  */
160 void
wpa_MD5Final(void * priv,unsigned char digest[16],Mrvl_MD5_CTX * context)161 wpa_MD5Final(void *priv, unsigned char digest[16], Mrvl_MD5_CTX *context)
162 {
163 	phostsa_private psapriv = (phostsa_private)priv;
164 	hostsa_util_fns *util_fns = &psapriv->util_fns;
165 	unsigned char bits[8];
166 	unsigned int index, padLen;
167 
168 	/* Save number of bits */
169 	Encode(util_fns, bits, context->count, 8);
170 
171 	/* Pad out to 56 mod 64. */
172 	index = (unsigned int)((context->count[0] >> 3) & 0x3f);
173 	padLen = (index < 56) ? (56 - index) : (120 - index);
174 	wpa_MD5Update(priv, context, (UINT8 *)PADDING, padLen);
175 
176 	/* Append length (before padding) */
177 	wpa_MD5Update(priv, context, bits, 8);
178 
179 	/* Store state in digest */
180 	Encode(util_fns, digest, context->state, 16);
181 
182 	/* Zeroize sensitive information. */
183 	Mrvl_MD5_memset(util_fns, context, 0, sizeof(*context));
184 }
185 
186 /* MD5 basic transformation. Transforms state based on block.
187  */
188 static void
wpa_MD5Transform(void * priv,UINT32 * state,unsigned int * block)189 wpa_MD5Transform(void *priv, UINT32 *state, unsigned int *block)
190 {
191 	phostsa_private psapriv = (phostsa_private)priv;
192 	hostsa_util_fns *util_fns = &psapriv->util_fns;
193 	UINT32 a = state[0], b = state[1], c = state[2], d = state[3], *x;
194 
195 	/* Decode (x, block, 64); */
196 	x = block;
197 
198 	/* Round 1 */
199 	FF(a, b, c, d, x[0], S11, 0xd76aa478);	/* 1 */
200 	FF(d, a, b, c, x[1], S12, 0xe8c7b756);	/* 2 */
201 	FF(c, d, a, b, x[2], S13, 0x242070db);	/* 3 */
202 	FF(b, c, d, a, x[3], S14, 0xc1bdceee);	/* 4 */
203 	FF(a, b, c, d, x[4], S11, 0xf57c0faf);	/* 5 */
204 	FF(d, a, b, c, x[5], S12, 0x4787c62a);	/* 6 */
205 	FF(c, d, a, b, x[6], S13, 0xa8304613);	/* 7 */
206 	FF(b, c, d, a, x[7], S14, 0xfd469501);	/* 8 */
207 	FF(a, b, c, d, x[8], S11, 0x698098d8);	/* 9 */
208 	FF(d, a, b, c, x[9], S12, 0x8b44f7af);	/* 10 */
209 	FF(c, d, a, b, x[10], S13, 0xffff5bb1);	/* 11 */
210 	FF(b, c, d, a, x[11], S14, 0x895cd7be);	/* 12 */
211 	FF(a, b, c, d, x[12], S11, 0x6b901122);	/* 13 */
212 	FF(d, a, b, c, x[13], S12, 0xfd987193);	/* 14 */
213 	FF(c, d, a, b, x[14], S13, 0xa679438e);	/* 15 */
214 	FF(b, c, d, a, x[15], S14, 0x49b40821);	/* 16 */
215 
216 	/* Round 2 */
217 	GG(a, b, c, d, x[1], S21, 0xf61e2562);	/* 17 */
218 	GG(d, a, b, c, x[6], S22, 0xc040b340);	/* 18 */
219 	GG(c, d, a, b, x[11], S23, 0x265e5a51);	/* 19 */
220 	GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);	/* 20 */
221 	GG(a, b, c, d, x[5], S21, 0xd62f105d);	/* 21 */
222 	GG(d, a, b, c, x[10], S22, 0x2441453);	/* 22 */
223 	GG(c, d, a, b, x[15], S23, 0xd8a1e681);	/* 23 */
224 	GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);	/* 24 */
225 	GG(a, b, c, d, x[9], S21, 0x21e1cde6);	/* 25 */
226 	GG(d, a, b, c, x[14], S22, 0xc33707d6);	/* 26 */
227 	GG(c, d, a, b, x[3], S23, 0xf4d50d87);	/* 27 */
228 
229 	GG(b, c, d, a, x[8], S24, 0x455a14ed);	/* 28 */
230 	GG(a, b, c, d, x[13], S21, 0xa9e3e905);	/* 29 */
231 	GG(d, a, b, c, x[2], S22, 0xfcefa3f8);	/* 30 */
232 	GG(c, d, a, b, x[7], S23, 0x676f02d9);	/* 31 */
233 	GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);	/* 32 */
234 
235 	/* Round 3 */
236 	HH(a, b, c, d, x[5], S31, 0xfffa3942);	/* 33 */
237 	HH(d, a, b, c, x[8], S32, 0x8771f681);	/* 34 */
238 	HH(c, d, a, b, x[11], S33, 0x6d9d6122);	/* 35 */
239 	HH(b, c, d, a, x[14], S34, 0xfde5380c);	/* 36 */
240 	HH(a, b, c, d, x[1], S31, 0xa4beea44);	/* 37 */
241 	HH(d, a, b, c, x[4], S32, 0x4bdecfa9);	/* 38 */
242 	HH(c, d, a, b, x[7], S33, 0xf6bb4b60);	/* 39 */
243 	HH(b, c, d, a, x[10], S34, 0xbebfbc70);	/* 40 */
244 	HH(a, b, c, d, x[13], S31, 0x289b7ec6);	/* 41 */
245 	HH(d, a, b, c, x[0], S32, 0xeaa127fa);	/* 42 */
246 	HH(c, d, a, b, x[3], S33, 0xd4ef3085);	/* 43 */
247 	HH(b, c, d, a, x[6], S34, 0x4881d05);	/* 44 */
248 	HH(a, b, c, d, x[9], S31, 0xd9d4d039);	/* 45 */
249 	HH(d, a, b, c, x[12], S32, 0xe6db99e5);	/* 46 */
250 	HH(c, d, a, b, x[15], S33, 0x1fa27cf8);	/* 47 */
251 	HH(b, c, d, a, x[2], S34, 0xc4ac5665);	/* 48 */
252 
253 	/* Round 4 */
254 	II(a, b, c, d, x[0], S41, 0xf4292244);	/* 49 */
255 	II(d, a, b, c, x[7], S42, 0x432aff97);	/* 50 */
256 	II(c, d, a, b, x[14], S43, 0xab9423a7);	/* 51 */
257 	II(b, c, d, a, x[5], S44, 0xfc93a039);	/* 52 */
258 	II(a, b, c, d, x[12], S41, 0x655b59c3);	/* 53 */
259 	II(d, a, b, c, x[3], S42, 0x8f0ccc92);	/* 54 */
260 	II(c, d, a, b, x[10], S43, 0xffeff47d);	/* 55 */
261 	II(b, c, d, a, x[1], S44, 0x85845dd1);	/* 56 */
262 	II(a, b, c, d, x[8], S41, 0x6fa87e4f);	/* 57 */
263 	II(d, a, b, c, x[15], S42, 0xfe2ce6e0);	/* 58 */
264 	II(c, d, a, b, x[6], S43, 0xa3014314);	/* 59 */
265 	II(b, c, d, a, x[13], S44, 0x4e0811a1);	/* 60 */
266 	II(a, b, c, d, x[4], S41, 0xf7537e82);	/* 61 */
267 	II(d, a, b, c, x[11], S42, 0xbd3af235);	/* 62 */
268 	II(c, d, a, b, x[2], S43, 0x2ad7d2bb);	/* 63 */
269 	II(b, c, d, a, x[9], S44, 0xeb86d391);	/* 64 */
270 	state[0] += a;
271 	state[1] += b;
272 	state[2] += c;
273 	state[3] += d;
274 
275 	/* Zeroize sensitive information. */
276 	Mrvl_MD5_memset(util_fns, x, 0, sizeof(x));
277 }
278