38 #if CHAP_SUPPORT || MD5_SUPPORT
61 static unsigned char PADDING[64] = {
62 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
73 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
74 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
75 #define H(x, y, z) ((x) ^ (y) ^ (z))
76 #define I(x, y, z) ((y) ^ ((x) | (~z)))
79 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
83 #define FF(a, b, c, d, x, s, ac) \
84 {(a) += F ((b), (c), (d)) + (x) + (u32_t)(ac); \
85 (a) = ROTATE_LEFT ((a), (s)); \
88 #define GG(a, b, c, d, x, s, ac) \
89 {(a) += G ((b), (c), (d)) + (x) + (u32_t)(ac); \
90 (a) = ROTATE_LEFT ((a), (s)); \
93 #define HH(a, b, c, d, x, s, ac) \
94 {(a) += H ((b), (c), (d)) + (x) + (u32_t)(ac); \
95 (a) = ROTATE_LEFT ((a), (s)); \
98 #define II(a, b, c, d, x, s, ac) \
99 {(a) += I ((b), (c), (d)) + (x) + (u32_t)(ac); \
100 (a) = ROTATE_LEFT ((a), (s)); \
120 mdContext->
i[0] = mdContext->
i[1] = (
u32_t)0;
123 mdContext->
buf[0] = (
u32_t)0x67452301UL;
124 mdContext->
buf[1] = (
u32_t)0xefcdab89UL;
125 mdContext->
buf[2] = (
u32_t)0x98badcfeUL;
126 mdContext->
buf[3] = (
u32_t)0x10325476UL;
146 mdi = (int)((mdContext->
i[0] >> 3) & 0x3F);
149 if ((mdContext->
i[0] + ((
u32_t)inLen << 3)) < mdContext->
i[0]) {
152 mdContext->
i[0] += ((
u32_t)inLen << 3);
153 mdContext->
i[1] += ((
u32_t)inLen >> 29);
157 mdContext->
in[mdi++] = *inBuf++;
161 for (i = 0, ii = 0; i < 16; i++, ii += 4) {
162 in[i] = (((
u32_t)mdContext->
in[ii+3]) << 24) |
163 (((
u32_t)mdContext->
in[ii+2]) << 16) |
164 (((
u32_t)mdContext->
in[ii+1]) << 8) |
167 Transform (mdContext->
buf, in);
185 in[14] = mdContext->
i[0];
186 in[15] = mdContext->
i[1];
189 mdi = (int)((mdContext->
i[0] >> 3) & 0x3F);
192 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
196 for (i = 0, ii = 0; i < 14; i++, ii += 4) {
197 in[i] = (((
u32_t)mdContext->
in[ii+3]) << 24) |
198 (((
u32_t)mdContext->
in[ii+2]) << 16) |
199 (((
u32_t)mdContext->
in[ii+1]) << 8) |
202 Transform (mdContext->
buf, in);
205 for (i = 0, ii = 0; i < 4; i++, ii += 4) {
206 mdContext->
digest[ii] = (
unsigned char)(mdContext->
buf[i] & 0xFF);
208 (
unsigned char)((mdContext->
buf[i] >> 8) & 0xFF);
210 (
unsigned char)((mdContext->
buf[i] >> 16) & 0xFF);
212 (
unsigned char)((mdContext->
buf[i] >> 24) & 0xFF);
222 u32_t a = buf[0], b = buf[1], c = buf[2], d = buf[3];
229 FF ( a, b, c, d, in[ 0], S11, UL(3614090360));
230 FF ( d, a, b, c, in[ 1], S12, UL(3905402710));
231 FF ( c, d, a, b, in[ 2], S13, UL( 606105819));
232 FF ( b, c, d, a, in[ 3], S14, UL(3250441966));
233 FF ( a, b, c, d, in[ 4], S11, UL(4118548399));
234 FF ( d, a, b, c, in[ 5], S12, UL(1200080426));
235 FF ( c, d, a, b, in[ 6], S13, UL(2821735955));
236 FF ( b, c, d, a, in[ 7], S14, UL(4249261313));
237 FF ( a, b, c, d, in[ 8], S11, UL(1770035416));
238 FF ( d, a, b, c, in[ 9], S12, UL(2336552879));
239 FF ( c, d, a, b, in[10], S13, UL(4294925233));
240 FF ( b, c, d, a, in[11], S14, UL(2304563134));
241 FF ( a, b, c, d, in[12], S11, UL(1804603682));
242 FF ( d, a, b, c, in[13], S12, UL(4254626195));
243 FF ( c, d, a, b, in[14], S13, UL(2792965006));
244 FF ( b, c, d, a, in[15], S14, UL(1236535329));
251 GG ( a, b, c, d, in[ 1], S21, UL(4129170786));
252 GG ( d, a, b, c, in[ 6], S22, UL(3225465664));
253 GG ( c, d, a, b, in[11], S23, UL( 643717713));
254 GG ( b, c, d, a, in[ 0], S24, UL(3921069994));
255 GG ( a, b, c, d, in[ 5], S21, UL(3593408605));
256 GG ( d, a, b, c, in[10], S22, UL( 38016083));
257 GG ( c, d, a, b, in[15], S23, UL(3634488961));
258 GG ( b, c, d, a, in[ 4], S24, UL(3889429448));
259 GG ( a, b, c, d, in[ 9], S21, UL( 568446438));
260 GG ( d, a, b, c, in[14], S22, UL(3275163606));
261 GG ( c, d, a, b, in[ 3], S23, UL(4107603335));
262 GG ( b, c, d, a, in[ 8], S24, UL(1163531501));
263 GG ( a, b, c, d, in[13], S21, UL(2850285829));
264 GG ( d, a, b, c, in[ 2], S22, UL(4243563512));
265 GG ( c, d, a, b, in[ 7], S23, UL(1735328473));
266 GG ( b, c, d, a, in[12], S24, UL(2368359562));
273 HH ( a, b, c, d, in[ 5], S31, UL(4294588738));
274 HH ( d, a, b, c, in[ 8], S32, UL(2272392833));
275 HH ( c, d, a, b, in[11], S33, UL(1839030562));
276 HH ( b, c, d, a, in[14], S34, UL(4259657740));
277 HH ( a, b, c, d, in[ 1], S31, UL(2763975236));
278 HH ( d, a, b, c, in[ 4], S32, UL(1272893353));
279 HH ( c, d, a, b, in[ 7], S33, UL(4139469664));
280 HH ( b, c, d, a, in[10], S34, UL(3200236656));
281 HH ( a, b, c, d, in[13], S31, UL( 681279174));
282 HH ( d, a, b, c, in[ 0], S32, UL(3936430074));
283 HH ( c, d, a, b, in[ 3], S33, UL(3572445317));
284 HH ( b, c, d, a, in[ 6], S34, UL( 76029189));
285 HH ( a, b, c, d, in[ 9], S31, UL(3654602809));
286 HH ( d, a, b, c, in[12], S32, UL(3873151461));
287 HH ( c, d, a, b, in[15], S33, UL( 530742520));
288 HH ( b, c, d, a, in[ 2], S34, UL(3299628645));
295 II ( a, b, c, d, in[ 0], S41, UL(4096336452));
296 II ( d, a, b, c, in[ 7], S42, UL(1126891415));
297 II ( c, d, a, b, in[14], S43, UL(2878612391));
298 II ( b, c, d, a, in[ 5], S44, UL(4237533241));
299 II ( a, b, c, d, in[12], S41, UL(1700485571));
300 II ( d, a, b, c, in[ 3], S42, UL(2399980690));
301 II ( c, d, a, b, in[10], S43, UL(4293915773));
302 II ( b, c, d, a, in[ 1], S44, UL(2240044497));
303 II ( a, b, c, d, in[ 8], S41, UL(1873313359));
304 II ( d, a, b, c, in[15], S42, UL(4264355552));
305 II ( c, d, a, b, in[ 6], S43, UL(2734768916));
306 II ( b, c, d, a, in[13], S44, UL(1309151649));
307 II ( a, b, c, d, in[ 4], S41, UL(4149444226));
308 II ( d, a, b, c, in[11], S42, UL(3174756917));
309 II ( c, d, a, b, in[ 2], S43, UL( 718787259));
310 II ( b, c, d, a, in[ 9], S44, UL(3951481745));