comparison gsm-fw/comlib/cl_md5.c @ 664:d36f647c2432

gsm-fw/comlib: initial import of TI's source cl_des.c and cl_imei.c are from the Leonardo semi-src; others are from LoCosto
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 28 Sep 2014 05:09:53 +0000
parents
children
comparison
equal deleted inserted replaced
663:643379e7e141 664:d36f647c2432
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 UBYTE bits[8];
298 UINT ind;
299 int chk_len;
300 TRACE_EVENT("cl_md5");
301
302 if(len==0)
303 len = strlen ((char *)input);
304
305 #ifdef _SIMULATION_
306 TRACE_EVENT("Input Message");
307 MDPrint (input, len);
308 #endif
309
310 /*
311 * MD5 initialization. Begins an MD5 operation, writing a new context.
312 */
313 context.count[0] = context.count[1] = 0;
314 /*
315 * Load magic initialization constants according to RFC 1321
316 */
317 context.state[0] = 0x67452301; /* word A */
318 context.state[1] = 0xefcdab89; /* word B */
319 context.state[2] = 0x98badcfe; /* word C */
320 context.state[3] = 0x10325476; /* word D */
321 /*
322 * Update number of bits (64-bit representation of message length)
323 */
324 if ((context.count[0] += ((UINT)len << 3)) < ((UINT)len << 3))
325 context.count[1]++;
326 context.count[1] += ((UINT)len >> 29);
327 /*
328 * Save number of bits
329 */
330 cl_md5_enc (bits, context.count, 8);
331
332 /*
333 * Step 1. Append Padding Bits
334 * The message is extended so that its length is congruent to 56 bytes
335 * (448 bits), modulo 64 (512). Extending is always performed, even if the
336 * length is already congruent to 56 bytes, modulo 64.
337 *
338 * Extending is performed as follows, a single "1" bit is append to the
339 * message, and then "0" bits are appended so that the legth in bits of the
340 * message becomes congruent to 56, modulo 64. In all, at least one byte and
341 * at most 64 bytes are appended.
342 *
343 * Step 2. Append Length.
344 * A 64-bit representation of the message length before the padding is
345 * appended to the result of previous step.
346 *
347 * Step 3. Process Message in 16-word blocks
348 */
349 if(len < 56)
350 {
351 /*
352 * copy message
353 */
354 memcpy(&context.buffer[0], &input[0], len);
355 /*
356 * Append length to 56 bytes
357 */
358 context.buffer[len] = 0x80; /* append a single "1" bit */
359 memset(&context.buffer[len+1], 0, 55-len); /* append "0" bits */
360 /*
361 * Append length in bits and process message
362 */
363 PROCESS_MSG();
364 }
365 else if(len >= 56 && len < 64)
366 {
367 /*
368 * copy message
369 */
370 memcpy(&context.buffer[0], &input[0], len);
371 /*
372 * Append length to 64 bytes
373 */
374 context.buffer[len] = 0x80;
375 memset(&context.buffer[len+1], 0, 63-len);
376 /*
377 * Process message
378 */
379 cl_md5_transform (context.state, context.buffer);
380 /*
381 * Append length to 56 bytes
382 */
383 memset(&context.buffer[0], 0, 56);
384 /*
385 * Append the length in bits and process message
386 */
387 PROCESS_MSG();
388 }
389 else if(len >= 64)
390 {
391 /*
392 * Copy first 64 bytes
393 */
394 memcpy(&context.buffer[0], &input[0], 64);
395 /*
396 * Process message
397 */
398 cl_md5_transform (context.state, context.buffer);
399 if(len >= 120)
400 {
401 /*
402 * Process message in 64-byte blocks
403 */
404 for (ind = 64; ind + 63 < len; ind += 64)
405 cl_md5_transform (context.state, &input[ind]);
406 }
407 else
408 ind = 64;
409 /*
410 * Copy the rest
411 */
412 memcpy(&context.buffer[0], &input[ind], len-ind);
413 /*
414 * Append length to 56 bytes
415 */
416 /*lint -e661 -e662 -e669 possible access or creation of bount ptr or data overrun*/
417 context.buffer[len-ind] = 0x80;
418 chk_len=55-(len-ind);
419 if(chk_len >=0)
420 memset(&context.buffer[len-ind+1], 0, chk_len);
421 /*lint +e661 +e662 +e669 possible access or creation of bount ptr or data overun*/
422 /*
423 * Append the length in bits and process message
424 */
425 PROCESS_MSG();
426 }
427
428 #ifdef _SIMULATION_
429 TRACE_EVENT("Digest Message");
430 MDPrint (digest, 16);
431 #endif
432 }
433
434
435 #ifdef _SIMULATION_
436 /*
437 +------------------------------------------------------------------------------
438 | Function : MDTestSuite
439 +------------------------------------------------------------------------------
440 | Description : Digests a reference suite of strings and prints the results.
441 |
442 | Parameters : void
443 +------------------------------------------------------------------------------
444 */
445 GLOBAL void cl_md5TestSuite ()
446 {
447 UBYTE digest[16];
448 UBYTE test_digest0[16] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
449 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
450 UBYTE test_digest1[16] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
451 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61};
452 UBYTE test_digest3[16] = {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
453 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72};
454 UBYTE test_digest14[16] = {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
455 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0};
456 UBYTE test_digest26[16] = {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
457 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b};
458 UBYTE test_digest62[16] = {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
459 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f};
460 UBYTE test_digest80[16] = {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
461 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a};
462 UBYTE test_digest160[16] = {0x26, 0x8c, 0x79, 0x19, 0x18, 0x9d, 0x85, 0xe2,
463 0x76, 0xd7, 0x4b, 0x8c, 0x60, 0xb2, 0xf8, 0x4f};
464
465 TRACE_EVENT("MD5 test suite:");
466
467 /*
468 * Test 1. Lenght := 0
469 */
470 cl_md5("", 0, digest);
471 if(memcmp(digest, test_digest0, 16))
472 {
473 TRACE_EVENT("CHAP MD5: Test 1 failed!!");
474 }
475 else
476 {
477 TRACE_EVENT("CHAP MD5: Test 1 passed.");
478 }
479
480 /*
481 * Test 2. Lenght := 1
482 */
483 cl_md5("a", 0, digest);
484 if(memcmp(digest, test_digest1, 16))
485 {
486 TRACE_EVENT("CHAP MD5: Test 2 failed!!");
487 }
488 else
489 {
490 TRACE_EVENT("CHAP MD5: Test 2 passed.");
491 }
492
493 /*
494 * Test 3. Lenght := 3
495 */
496 cl_md5("abc", 0, digest);
497 if(memcmp(digest, test_digest3, 16))
498 {
499 TRACE_EVENT("CHAP MD5: Test 3 failed!!");
500 }
501 else
502 {
503 TRACE_EVENT("CHAP MD5: Test 3 passed.");
504 }
505
506 /*
507 * Test 4. Lenght := 14
508 */
509 cl_md5("message digest", 0, digest);
510 if(memcmp(digest, test_digest14, 16))
511 {
512 TRACE_EVENT("CHAP MD5: Test 4 failed!!");
513 }
514 else
515 {
516 TRACE_EVENT("CHAP MD5: Test 4 passed.");
517 }
518
519 /*
520 * Test 5. Lenght := 26
521 */
522 cl_md5("abcdefghijklmnopqrstuvwxyz", 0, digest);
523 if(memcmp(digest, test_digest26, 16))
524 {
525 TRACE_EVENT("CHAP MD5: Test5 failed!!");
526 }
527 else
528 {
529 TRACE_EVENT("CHAP MD5: Test 5 passed.");
530 }
531
532 /*
533 * Test 6. Lenght := 62
534 */
535 cl_md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 0, digest);/*62*/
536 if(memcmp(digest, test_digest62, 16))
537 {
538 TRACE_EVENT("CHAP MD5: Test 6 failed!!");
539 }
540 else
541 {
542 TRACE_EVENT("CHAP MD5: Test 6 passed.");
543 }
544
545 /*
546 * Test 7. Lenght := 80
547 */
548 cl_md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, digest);/*80*/
549 if(memcmp(digest, test_digest80, 16))
550 {
551 TRACE_EVENT("CHAP MD5: Test 7 failed!!");
552 }
553 else
554 {
555 TRACE_EVENT("CHAP MD5: Test 7 passed.");
556 }
557
558 /*
559 * Test 8. Lenght := 160
560 */
561 cl_md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890\
562 12345678901234567890123456789012345678901234567890123456789012345678901234567890", 0, digest);
563 if(memcmp(digest, test_digest160, 16))
564 {
565 TRACE_EVENT("CHAP MD5: Test 8 failed!!");
566 }
567 else
568 {
569 TRACE_EVENT("CHAP MD5: Test 8 passed.");
570 }
571
572 }
573 #endif/*_SIMULATION_*/
574
575
576 #endif /* CL_MD5_C */