comparison g23m/condat/com/src/comlib/cl_md5.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /*
2 +-----------------------------------------------------------------------------
3 | Project : COMLIB
4 | Modul : cl_md5.c
5 +-----------------------------------------------------------------------------
6 | Copyright 2002 Texas Instruments Berlin, AG
7 | All rights reserved.
8 |
9 | This file is confidential and a trade secret of Texas
10 | Instruments Berlin, AG
11 | The receipt of or possession of this file does not convey
12 | any rights to reproduce or disclose its contents or to
13 | manufacture, use, or sell anything it may describe, in
14 | whole, or in part, without the specific written consent of
15 | Texas Instruments Berlin, AG.
16 +-----------------------------------------------------------------------------
17 | Purpose : Definitions of common library functions: MD5 algorithm
18 +-----------------------------------------------------------------------------
19 */
20 /*
21 * Version 1.0
22 */
23
24 /**********************************************************************************/
25
26 /*
27 NOTE:
28 */
29
30 /**********************************************************************************/
31
32 #ifndef CL_MD5_C
33 #define CL_MD5_C
34
35 #include "typedefs.h"
36 #include <string.h>
37 #include "vsi.h" /* to get a lot of macros */
38 #include "cl_md5.h"
39 #include "stdio.h"
40
41
42 /*==== FUNCTIONS ==================================================*/
43
44
45 /*
46 * Constants for MD5 routine.
47 */
48
49 #define S11 7
50 #define S12 12
51 #define S13 17
52 #define S14 22
53 #define S21 5
54 #define S22 9
55 #define S23 14
56 #define S24 20
57 #define S31 4
58 #define S32 11
59 #define S33 16
60 #define S34 23
61 #define S41 6
62 #define S42 10
63 #define S43 15
64 #define S44 21
65
66 /*
67 * F, G, H and I are basic MD5 functions.
68 */
69 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
70 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
71 #define H(x, y, z) ((x) ^ (y) ^ (z))
72 #define I(x, y, z) ((y) ^ ((x) | (~z)))
73
74 /*
75 * ROTATE_LEFT rotates x left n bits.
76 */
77 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
78
79 /*
80 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
81 * Rotation is separate from addition to prevent recomputation.
82 */
83 #define FF(a, b, c, d, x, s, ac) { \
84 (a) += F ((b), (c), (d)) + (x) + (UINT)(ac); \
85 (a) = ROTATE_LEFT ((a), (s)); \
86 (a) += (b); \
87 }
88 #define GG(a, b, c, d, x, s, ac) { \
89 (a) += G ((b), (c), (d)) + (x) + (UINT)(ac); \
90 (a) = ROTATE_LEFT ((a), (s)); \
91 (a) += (b); \
92 }
93 #define HH(a, b, c, d, x, s, ac) { \
94 (a) += H ((b), (c), (d)) + (x) + (UINT)(ac); \
95 (a) = ROTATE_LEFT ((a), (s)); \
96 (a) += (b); \
97 }
98 #define II(a, b, c, d, x, s, ac) { \
99 (a) += I ((b), (c), (d)) + (x) + (UINT)(ac); \
100 (a) = ROTATE_LEFT ((a), (s)); \
101 (a) += (b); \
102 }
103 /*
104 * 1. Append the length in bits
105 * 2. Process message
106 * 3. Store state in digest
107 */
108 #define PROCESS_MSG() {\
109 memcpy(&context.buffer[56], bits, 8);\
110 cl_md5_transform(context.state, context.buffer);\
111 cl_md5_enc (digest, context.state, 16);\
112 }
113
114 #ifdef _SIMULATION_
115 /*
116 +------------------------------------------------------------------------------
117 | Function : MDPrint
118 +------------------------------------------------------------------------------
119 | Description : Prints a message digest in hexadecimal.
120 |
121 | Parameters : UBYTE digest[16]
122 +------------------------------------------------------------------------------
123 */
124 GLOBAL void MDPrint (UBYTE *digest, UINT len)
125 {
126 UINT i;
127 for (i = 0; i < len; i+=8)
128 TRACE_EVENT_P8 ("0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, ",
129 digest[i], digest[i+1], digest[i+2], digest[i+3],
130 digest[i+4], digest[i+5], digest[i+6], digest[i+7]);
131 }
132 #endif
133
134 /*
135 +------------------------------------------------------------------------------
136 | Function : cl_md5_enc
137 +------------------------------------------------------------------------------
138 | Description : Encodes input (UINT) into output (UBYTE). Assumes len
139 | is a multiple of 4.
140 |
141 | Parameters : UBYTE *output
142 | UINT *input
143 | UINT len
144 +------------------------------------------------------------------------------
145 */
146 void cl_md5_enc (UBYTE *output, UINT *input, UINT len)
147 {
148 UINT i, j;
149
150 for (i = 0, j = 0; j < len; i++, j += 4) {
151 output[j] = (UBYTE)(input[i] & 0xff);
152 output[j+1] = (UBYTE)((input[i] >> 8) & 0xff);
153 output[j+2] = (UBYTE)((input[i] >> 16) & 0xff);
154 output[j+3] = (UBYTE)((input[i] >> 24) & 0xff);
155 }
156 }
157
158 /*
159 +------------------------------------------------------------------------------
160 | Function : cl_md5_dec
161 +------------------------------------------------------------------------------
162 | Description : Decodes input (UBYTE) into output (UINT). Assumes len
163 | is a multiple of 4.
164 |
165 | Parameters : UINT *output
166 | UBYTE *input
167 | UINT len
168 +------------------------------------------------------------------------------
169 */
170 void cl_md5_dec (UINT *output, UBYTE *input, UINT len)
171 {
172 UINT i, j;
173
174 for (i = 0, j = 0; j < len; i++, j += 4)
175 output[i] = ((UINT)input[j]) | (((UINT)input[j+1]) << 8) |
176 (((UINT)input[j+2]) << 16) | (((UINT)input[j+3]) << 24);
177 }
178
179
180 /*
181 +------------------------------------------------------------------------------
182 | Function : cl_md5_transform
183 +------------------------------------------------------------------------------
184 | Description : MD5 basic transformation. Transforms state based on block.
185 | For more information see RFC 1321 "MD5 Message-Digest Algorithm".
186 | This routine is derived from the RSA Data Security, Inc. MD5
187 | Message-Digest Algorithm.
188 |
189 | Parameters : UINT state[4]
190 | UBYTE block[64]
191 |
192 +------------------------------------------------------------------------------
193 */
194 void cl_md5_transform (UINT state[4], UBYTE block[64])
195 {
196 UINT a = state[0], b = state[1], c = state[2], d = state[3], x[16];
197
198 cl_md5_dec (x, block, 64);
199
200 /* Round 1 */
201 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
202 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
203 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
204 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
205 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
206 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
207 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
208 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
209 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
210 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
211 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
212 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
213 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
214 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
215 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
216 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
217
218 /* Round 2 */
219 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
220 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
221 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
222 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
223 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
224 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
225 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
226 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
227 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
228 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
229 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
230
231 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
232 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
233 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
234 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
235 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
236
237 /* Round 3 */
238 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
239 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
240 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
241 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
242 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
243 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
244 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
245 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
246 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
247 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
248 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
249 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
250 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
251 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
252 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
253 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
254
255 /* Round 4 */
256 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
257 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
258 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
259 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
260 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
261 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
262 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
263 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
264 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
265 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
266 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
267 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
268 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
269 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
270 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
271 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
272
273 state[0] += a;
274 state[1] += b;
275 state[2] += c;
276 state[3] += d;
277
278 /* Zeroize sensitive information.*/
279 memset((UBYTE *)x, 0, sizeof (x));
280 }
281
282 /*
283 +------------------------------------------------------------------------------
284 | Function : cl_md5
285 +------------------------------------------------------------------------------
286 | Description : Digests a string and prints the result. For more information see
287 | RFC 1321 "MD5 Message-Digest Algorithm".
288 |
289 | Parameters : UBYTE *input - input challenge string
290 | UINT len - length of input string
291 | UBYTE *digest - output digest message
292 +------------------------------------------------------------------------------
293 */
294 GLOBAL void cl_md5 (UBYTE *input, UINT len, UBYTE *digest)
295 {
296 MD5_CTX context;
297 T_TIME endTime, startTime;
298 UBYTE bits[8];
299 UINT i, ind, len_bits;
300
301 TRACE_EVENT("cl_md5");
302
303 if(len==0)
304 len = strlen ((char *)input);
305
306 #ifdef _SIMULATION_
307 TRACE_EVENT("Input Message");
308 MDPrint (input, len);
309 #endif
310
311 /*
312 * MD5 initialization. Begins an MD5 operation, writing a new context.
313 */
314 context.count[0] = context.count[1] = 0;
315 /*
316 * Load magic initialization constants according to RFC 1321
317 */
318 context.state[0] = 0x67452301; /* word A */
319 context.state[1] = 0xefcdab89; /* word B */
320 context.state[2] = 0x98badcfe; /* word C */
321 context.state[3] = 0x10325476; /* word D */
322 /*
323 * Update number of bits (64-bit representation of message length)
324 */
325 if ((context.count[0] += ((UINT)len << 3)) < ((UINT)len << 3))
326 context.count[1]++;
327 context.count[1] += ((UINT)len >> 29);
328 /*
329 * Save number of bits
330 */
331 cl_md5_enc (bits, context.count, 8);
332
333 /*
334 * Step 1. Append Padding Bits
335 * The message is extended so that its length is congruent to 56 bytes
336 * (448 bits), modulo 64 (512). Extending is always performed, even if the
337 * length is already congruent to 56 bytes, modulo 64.
338 *
339 * Extending is performed as follows, a single "1" bit is append to the
340 * message, and then "0" bits are appended so that the legth in bits of the
341 * message becomes congruent to 56, modulo 64. In all, at least one byte and
342 * at most 64 bytes are appended.
343 *
344 * Step 2. Append Length.
345 * A 64-bit representation of the message length before the padding is
346 * appended to the result of previous step.
347 *
348 * Step 3. Process Message in 16-word blocks
349 */
350 if(len < 56)
351 {
352 /*
353 * copy message
354 */
355 memcpy(&context.buffer[0], &input[0], len);
356 /*
357 * Append length to 56 bytes
358 */
359 context.buffer[len] = 0x80; /* append a single "1" bit */
360 memset(&context.buffer[len+1], 0, 55-len); /* append "0" bits */
361 /*
362 * Append length in bits and process message
363 */
364 PROCESS_MSG();
365 }
366 else if(len >= 56 && len < 64)
367 {
368 /*
369 * copy message
370 */
371 memcpy(&context.buffer[0], &input[0], len);
372 /*
373 * Append length to 64 bytes
374 */
375 context.buffer[len] = 0x80;
376 memset(&context.buffer[len+1], 0, 63-len);
377 /*
378 * Process message
379 */
380 cl_md5_transform (context.state, context.buffer);
381 /*
382 * Append length to 56 bytes
383 */
384 memset(&context.buffer[0], 0, 56);
385 /*
386 * Append the length in bits and process message
387 */
388 PROCESS_MSG();
389 }
390 else if(len >= 64)
391 {
392 /*
393 * Copy first 64 bytes
394 */
395 memcpy(&context.buffer[0], &input[0], 64);
396 /*
397 * Process message
398 */
399 cl_md5_transform (context.state, context.buffer);
400 if(len >= 120)
401 {
402 /*
403 * Process message in 64-byte blocks
404 */
405 for (ind = 64; ind + 63 < len; ind += 64)
406 cl_md5_transform (context.state, &input[ind]);
407 }
408 else
409 ind = 64;
410 /*
411 * Copy the rest
412 */
413 memcpy(&context.buffer[0], &input[ind], len-ind);
414 /*
415 * Append length to 56 bytes
416 */
417 context.buffer[len-ind] = 0x80;
418 memset(&context.buffer[len-ind+1], 0, 55-(len-ind));
419 /*
420 * Append the length in bits and process message
421 */
422 PROCESS_MSG();
423 }
424
425 #ifdef _SIMULATION_
426 TRACE_EVENT("Digest Message");
427 MDPrint (digest, 16);
428 #endif
429 }
430
431
432 #ifdef _SIMULATION_
433 /*
434 +------------------------------------------------------------------------------
435 | Function : MDTestSuite
436 +------------------------------------------------------------------------------
437 | Description : Digests a reference suite of strings and prints the results.
438 |
439 | Parameters : void
440 +------------------------------------------------------------------------------
441 */
442 GLOBAL void cl_md5TestSuite ()
443 {
444 UBYTE digest[16];
445 UBYTE test_digest0[16] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
446 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
447 UBYTE test_digest1[16] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
448 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61};
449 UBYTE test_digest3[16] = {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
450 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72};
451 UBYTE test_digest14[16] = {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
452 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0};
453 UBYTE test_digest26[16] = {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
454 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b};
455 UBYTE test_digest62[16] = {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
456 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f};
457 UBYTE test_digest80[16] = {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
458 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a};
459 UBYTE test_digest160[16] = {0x26, 0x8c, 0x79, 0x19, 0x18, 0x9d, 0x85, 0xe2,
460 0x76, 0xd7, 0x4b, 0x8c, 0x60, 0xb2, 0xf8, 0x4f};
461
462 TRACE_EVENT("MD5 test suite:");
463
464 /*
465 * Test 1. Lenght := 0
466 */
467 cl_md5("", 0, digest);
468 if(memcmp(digest, test_digest0, 16))
469 {
470 TRACE_EVENT("CHAP MD5: Test 1 failed!!");
471 }
472 else
473 {
474 TRACE_EVENT("CHAP MD5: Test 1 passed.");
475 }
476
477 /*
478 * Test 2. Lenght := 1
479 */
480 cl_md5("a", 0, digest);
481 if(memcmp(digest, test_digest1, 16))
482 {
483 TRACE_EVENT("CHAP MD5: Test 2 failed!!");
484 }
485 else
486 {
487 TRACE_EVENT("CHAP MD5: Test 2 passed.");
488 }
489
490 /*
491 * Test 3. Lenght := 3
492 */
493 cl_md5("abc", 0, digest);
494 if(memcmp(digest, test_digest3, 16))
495 {
496 TRACE_EVENT("CHAP MD5: Test 3 failed!!");
497 }
498 else
499 {
500 TRACE_EVENT("CHAP MD5: Test 3 passed.");
501 }
502
503 /*
504 * Test 4. Lenght := 14
505 */
506 cl_md5("message digest", 0, digest);
507 if(memcmp(digest, test_digest14, 16))
508 {
509 TRACE_EVENT("CHAP MD5: Test 4 failed!!");
510 }
511 else
512 {
513 TRACE_EVENT("CHAP MD5: Test 4 passed.");
514 }
515
516 /*
517 * Test 5. Lenght := 26
518 */
519 cl_md5("abcdefghijklmnopqrstuvwxyz", 0, digest);
520 if(memcmp(digest, test_digest26, 16))
521 {
522 TRACE_EVENT("CHAP MD5: Test5 failed!!");
523 }
524 else
525 {
526 TRACE_EVENT("CHAP MD5: Test 5 passed.");
527 }
528
529 /*
530 * Test 6. Lenght := 62
531 */
532 cl_md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 0, digest);/*62*/
533 if(memcmp(digest, test_digest62, 16))
534 {
535 TRACE_EVENT("CHAP MD5: Test 6 failed!!");
536 }
537 else
538 {
539 TRACE_EVENT("CHAP MD5: Test 6 passed.");
540 }
541
542 /*
543 * Test 7. Lenght := 80
544 */
545 cl_md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, digest);/*80*/
546 if(memcmp(digest, test_digest80, 16))
547 {
548 TRACE_EVENT("CHAP MD5: Test 7 failed!!");
549 }
550 else
551 {
552 TRACE_EVENT("CHAP MD5: Test 7 passed.");
553 }
554
555 /*
556 * Test 8. Lenght := 160
557 */
558 cl_md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890\
559 12345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, digest);
560 if(memcmp(digest, test_digest160, 16))
561 {
562 TRACE_EVENT("CHAP MD5: Test 8 failed!!");
563 }
564 else
565 {
566 TRACE_EVENT("CHAP MD5: Test 8 passed.");
567 }
568
569 }
570 #endif/*_SIMULATION_*/
571
572
573 #endif /* CL_MD5_C */