Resync with broadcom drivers 5.100.138.20 and utilities.
[tomato.git] / release / src-rt / bcmcrypto / md5.c
blobdd3f444bf254dd1feac07eca6537f7bd068b07c0
1 /*
2 * md5.c, copied from src/router/ppp/pppd to src/bcmcrypto for general use,
3 * with a few casts added to make it usable with a fussy compiler.
5 * md5.c -- the source code for MD5 routines
6 * RSA Data Security, Inc. MD5 Message-Digest Algorithm
7 * Created: 2/17/90 RLR
8 * Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr.
9 */
12 * Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.
14 * License to copy and use this software is granted provided that
15 * it is identified as the "RSA Data Security, Inc. MD5 Message-
16 * Digest Algorithm" in all material mentioning or referencing this
17 * software or this function.
19 * License is also granted to make and use derivative works
20 * provided that such works are identified as "derived from the RSA
21 * Data Security, Inc. MD5 Message-Digest Algorithm" in all
22 * material mentioning or referencing the derived work.
24 * RSA Data Security, Inc. makes no representations concerning
25 * either the merchantability of this software or the suitability
26 * of this software for any particular purpose. It is provided "as
27 * is" without express or implied warranty of any kind.
29 * These notices must be retained in any copies of any part of this
30 * documentation and/or software.
32 * Copyright (C) 2010, Broadcom Corporation
33 * All Rights Reserved.
35 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
36 * the contents of this file may not be disclosed to third parties, copied
37 * or duplicated in any form, in whole or in part, without the prior
38 * written permission of Broadcom Corporation.
40 * $Id: md5.c,v 1.12 2007-03-26 17:28:56 Exp $
43 #ifdef BCMDRIVER
44 #include <typedefs.h>
45 #include <osl.h>
46 #else
47 #if defined(__GNUC__)
48 #include <string.h>
49 #endif
50 #endif /* BCMDRIVER */
52 #include <bcmcrypto/md5.h>
54 typedef uint32 UINT4;
57 * Message-digest routines:
58 * To form the message digest for a message M
59 * (1) Initialize a context buffer mdContext using MD5Init
60 * (2) Call MD5Update on mdContext and M
61 * (3) Call MD5Final on mdContext
62 * The message digest is now in mdContext->digest[0...15]
66 /* forward declaration */
67 static void Transform(UINT4 *buf, UINT4 *in);
69 static const unsigned char PADDING[64] = {
70 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80 /* F, G, H and I are basic MD5 functions */
81 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
82 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
83 #define H(x, y, z) ((x) ^ (y) ^ (z))
84 #define I(x, y, z) ((y) ^ ((x) | (~z)))
86 /* ROTATE_LEFT rotates x left n bits */
87 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
89 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
90 /* Rotation is separate from addition to prevent recomputation */
91 #define FF(a, b, c, d, x, s, ac) \
92 { \
93 (a) += F((b), (c), (d)) + (x) + (UINT4)(ac); \
94 (a) = ROTATE_LEFT((a), (s)); \
95 (a) += (b); \
97 #define GG(a, b, c, d, x, s, ac) \
98 { \
99 (a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \
100 (a) = ROTATE_LEFT((a), (s)); \
101 (a) += (b); \
103 #define HH(a, b, c, d, x, s, ac) \
105 (a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \
106 (a) = ROTATE_LEFT((a), (s)); \
107 (a) += (b); \
109 #define II(a, b, c, d, x, s, ac) \
111 (a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \
112 (a) = ROTATE_LEFT((a), (s)); \
113 (a) += (b); \
116 #ifndef UL
117 #ifdef __STDC__
118 #define UL(x) x##U
119 #else
120 #define UL(x) x
121 #endif
122 #endif /* !UL */
124 /* The routine MD5Init initializes the message-digest context
125 * mdContext. All fields are set to zero.
127 void
128 BCMROMFN(MD5Init)(MD5_CTX *mdContext)
130 mdContext->i[0] = mdContext->i[1] = (UINT4)0;
132 /* Load magic initialization constants.
134 mdContext->buf[0] = (UINT4)0x67452301;
135 mdContext->buf[1] = (UINT4)0xefcdab89;
136 mdContext->buf[2] = (UINT4)0x98badcfe;
137 mdContext->buf[3] = (UINT4)0x10325476;
140 /* The routine MD5Update updates the message-digest context to
141 * account for the presence of each of the characters inBuf[0..inLen-1]
142 * in the message whose digest is being computed.
144 void
145 BCMROMFN(MD5Update)(MD5_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
147 UINT4 in[16];
148 int mdi;
149 unsigned int i, ii;
151 /* compute number of bytes mod 64 */
152 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
154 /* update number of bits */
155 if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
156 mdContext->i[1]++;
157 mdContext->i[0] += ((UINT4)inLen << 3);
158 mdContext->i[1] += ((UINT4)inLen >> 29);
160 while (inLen--) {
161 /* add new character to buffer, increment mdi */
162 mdContext->in[mdi++] = *inBuf++;
164 /* transform if necessary */
165 if (mdi == 0x40) {
166 for (i = 0, ii = 0; i < 16; i++, ii += 4)
167 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
168 (((UINT4)mdContext->in[ii+2]) << 16) |
169 (((UINT4)mdContext->in[ii+1]) << 8) |
170 ((UINT4)mdContext->in[ii]);
171 Transform(mdContext->buf, in);
172 mdi = 0;
177 /* The routine MD5Final terminates the message-digest computation and
178 * ends with the desired message digest in mdContext->digest[0...15].
180 void
181 BCMROMFN(MD5Final)(unsigned char *hash, MD5_CTX *mdContext)
183 UINT4 in[16];
184 int mdi;
185 unsigned int i, ii;
186 unsigned int padLen;
188 /* save number of bits */
189 in[14] = mdContext->i[0];
190 in[15] = mdContext->i[1];
192 /* compute number of bytes mod 64 */
193 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
195 /* pad out to 56 mod 64 */
196 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
197 MD5Update(mdContext, PADDING, padLen);
199 /* append length in bits and transform */
200 for (i = 0, ii = 0; i < 14; i++, ii += 4)
201 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
202 (((UINT4)mdContext->in[ii+2]) << 16) |
203 (((UINT4)mdContext->in[ii+1]) << 8) |
204 ((UINT4)mdContext->in[ii]);
205 Transform(mdContext->buf, in);
207 /* store buffer in digest */
208 for (i = 0, ii = 0; i < 4; i++, ii += 4) {
209 mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
210 mdContext->digest[ii+1] =
211 (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
212 mdContext->digest[ii+2] =
213 (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
214 mdContext->digest[ii+3] =
215 (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
217 memcpy((void *)hash, mdContext->digest, 16);
220 /* Basic MD5 step. Transforms buf based on in.
222 static void
223 Transform(UINT4 *buf, UINT4 *in)
225 UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
227 /* Round 1 */
228 #define S11 7
229 #define S12 12
230 #define S13 17
231 #define S14 22
232 FF(a, b, c, d, in[ 0], S11, UL(3614090360)); /* 1 */
233 FF(d, a, b, c, in[ 1], S12, UL(3905402710)); /* 2 */
234 FF(c, d, a, b, in[ 2], S13, UL(606105819)); /* 3 */
235 FF(b, c, d, a, in[ 3], S14, UL(3250441966)); /* 4 */
236 FF(a, b, c, d, in[ 4], S11, UL(4118548399)); /* 5 */
237 FF(d, a, b, c, in[ 5], S12, UL(1200080426)); /* 6 */
238 FF(c, d, a, b, in[ 6], S13, UL(2821735955)); /* 7 */
239 FF(b, c, d, a, in[ 7], S14, UL(4249261313)); /* 8 */
240 FF(a, b, c, d, in[ 8], S11, UL(1770035416)); /* 9 */
241 FF(d, a, b, c, in[ 9], S12, UL(2336552879)); /* 10 */
242 FF(c, d, a, b, in[10], S13, UL(4294925233)); /* 11 */
243 FF(b, c, d, a, in[11], S14, UL(2304563134)); /* 12 */
244 FF(a, b, c, d, in[12], S11, UL(1804603682)); /* 13 */
245 FF(d, a, b, c, in[13], S12, UL(4254626195)); /* 14 */
246 FF(c, d, a, b, in[14], S13, UL(2792965006)); /* 15 */
247 FF(b, c, d, a, in[15], S14, UL(1236535329)); /* 16 */
249 /* Round 2 */
250 #define S21 5
251 #define S22 9
252 #define S23 14
253 #define S24 20
254 GG(a, b, c, d, in[ 1], S21, UL(4129170786)); /* 17 */
255 GG(d, a, b, c, in[ 6], S22, UL(3225465664)); /* 18 */
256 GG(c, d, a, b, in[11], S23, UL(643717713)); /* 19 */
257 GG(b, c, d, a, in[ 0], S24, UL(3921069994)); /* 20 */
258 GG(a, b, c, d, in[ 5], S21, UL(3593408605)); /* 21 */
259 GG(d, a, b, c, in[10], S22, UL( 38016083)); /* 22 */
260 GG(c, d, a, b, in[15], S23, UL(3634488961)); /* 23 */
261 GG(b, c, d, a, in[ 4], S24, UL(3889429448)); /* 24 */
262 GG(a, b, c, d, in[ 9], S21, UL(568446438)); /* 25 */
263 GG(d, a, b, c, in[14], S22, UL(3275163606)); /* 26 */
264 GG(c, d, a, b, in[ 3], S23, UL(4107603335)); /* 27 */
265 GG(b, c, d, a, in[ 8], S24, UL(1163531501)); /* 28 */
266 GG(a, b, c, d, in[13], S21, UL(2850285829)); /* 29 */
267 GG(d, a, b, c, in[ 2], S22, UL(4243563512)); /* 30 */
268 GG(c, d, a, b, in[ 7], S23, UL(1735328473)); /* 31 */
269 GG(b, c, d, a, in[12], S24, UL(2368359562)); /* 32 */
271 /* Round 3 */
272 #define S31 4
273 #define S32 11
274 #define S33 16
275 #define S34 23
276 HH(a, b, c, d, in[ 5], S31, UL(4294588738)); /* 33 */
277 HH(d, a, b, c, in[ 8], S32, UL(2272392833)); /* 34 */
278 HH(c, d, a, b, in[11], S33, UL(1839030562)); /* 35 */
279 HH(b, c, d, a, in[14], S34, UL(4259657740)); /* 36 */
280 HH(a, b, c, d, in[ 1], S31, UL(2763975236)); /* 37 */
281 HH(d, a, b, c, in[ 4], S32, UL(1272893353)); /* 38 */
282 HH(c, d, a, b, in[ 7], S33, UL(4139469664)); /* 39 */
283 HH(b, c, d, a, in[10], S34, UL(3200236656)); /* 40 */
284 HH(a, b, c, d, in[13], S31, UL(681279174)); /* 41 */
285 HH(d, a, b, c, in[ 0], S32, UL(3936430074)); /* 42 */
286 HH(c, d, a, b, in[ 3], S33, UL(3572445317)); /* 43 */
287 HH(b, c, d, a, in[ 6], S34, UL( 76029189)); /* 44 */
288 HH(a, b, c, d, in[ 9], S31, UL(3654602809)); /* 45 */
289 HH(d, a, b, c, in[12], S32, UL(3873151461)); /* 46 */
290 HH(c, d, a, b, in[15], S33, UL(530742520)); /* 47 */
291 HH(b, c, d, a, in[ 2], S34, UL(3299628645)); /* 48 */
293 /* Round 4 */
294 #define S41 6
295 #define S42 10
296 #define S43 15
297 #define S44 21
298 II(a, b, c, d, in[ 0], S41, UL(4096336452)); /* 49 */
299 II(d, a, b, c, in[ 7], S42, UL(1126891415)); /* 50 */
300 II(c, d, a, b, in[14], S43, UL(2878612391)); /* 51 */
301 II(b, c, d, a, in[ 5], S44, UL(4237533241)); /* 52 */
302 II(a, b, c, d, in[12], S41, UL(1700485571)); /* 53 */
303 II(d, a, b, c, in[ 3], S42, UL(2399980690)); /* 54 */
304 II(c, d, a, b, in[10], S43, UL(4293915773)); /* 55 */
305 II(b, c, d, a, in[ 1], S44, UL(2240044497)); /* 56 */
306 II(a, b, c, d, in[ 8], S41, UL(1873313359)); /* 57 */
307 II(d, a, b, c, in[15], S42, UL(4264355552)); /* 58 */
308 II(c, d, a, b, in[ 6], S43, UL(2734768916)); /* 59 */
309 II(b, c, d, a, in[13], S44, UL(1309151649)); /* 60 */
310 II(a, b, c, d, in[ 4], S41, UL(4149444226)); /* 61 */
311 II(d, a, b, c, in[11], S42, UL(3174756917)); /* 62 */
312 II(c, d, a, b, in[ 2], S43, UL(718787259)); /* 63 */
313 II(b, c, d, a, in[ 9], S44, UL(3951481745)); /* 64 */
315 buf[0] += a;
316 buf[1] += b;
317 buf[2] += c;
318 buf[3] += d;
322 * End of md5.c