Debian Codegraph @

libs/pplib/pplib-src/src/util/utilmd5.c

raw
1
2 /* md5 implementation excerpted from code by Peter Deutsch */
3
4 /* begin of md5.c */
5
6 /*
7 Copyright (C) 1999, 2000, 2002 Aladdin Enterprises. All rights reserved.
8
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any damages
11 arising from the use of this software.
12
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it
15 freely, subject to the following restrictions:
16
17 1. The origin of this software must not be misrepresented; you must not
18 claim that you wrote the original software. If you use this software
19 in a product, an acknowledgment in the product documentation would be
20 appreciated but is not required.
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original software.
23 3. This notice may not be removed or altered from any source distribution.
24
25 L. Peter Deutsch
26 [email protected]
27
28 */
29 /* $Id: md5.c,v 1.6 2002/04/13 19:20:28 lpd Exp $ */
30 /*
31 Independent implementation of MD5 (RFC 1321).
32
33 This code implements the MD5 Algorithm defined in RFC 1321, whose
34 text is available at
35 http://www.ietf.org/rfc/rfc1321.txt
36 The code is derived from the text of the RFC, including the test suite
37 (section A.5) but excluding the rest of Appendix A. It does not include
38 any code or documentation that is identified in the RFC as being
39 copyrighted.
40
41 The original and principal author of md5.c is L. Peter Deutsch
42 <[email protected]>. Other authors are noted in the change history
43 that follows (in reverse chronological order):
44
45 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
46 either statically or dynamically; added missing #include <string.h>
47 in library.
48 2002-03-11 lpd Corrected argument list for main(), and added int return
49 type, in test program and T value program.
50 2002-02-21 lpd Added missing #include <stdio.h> in test program.
51 2000-07-03 lpd Patched to eliminate warnings about "constant is
52 unsigned in ANSI C, signed in traditional"; made test program
53 self-checking.
54 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
55 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
56 1999-05-03 lpd Original version.
57 */
58
59 #include <string.h> // memcpy
60 #include <stdio.h> // FILE
61
62 #include "utilmd5.h"
63
64 #undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
65 #ifdef ARCH_IS_BIG_ENDIAN
66 # define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
67 #else
68 # define BYTE_ORDER 0
69 #endif
70
71 #define T_MASK ((uint32_t)~0)
72 #define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
73 #define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
74 #define T3 0x242070db
75 #define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
76 #define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
77 #define T6 0x4787c62a
78 #define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
79 #define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
80 #define T9 0x698098d8
81 #define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
82 #define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
83 #define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
84 #define T13 0x6b901122
85 #define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
86 #define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
87 #define T16 0x49b40821
88 #define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
89 #define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
90 #define T19 0x265e5a51
91 #define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
92 #define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
93 #define T22 0x02441453
94 #define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
95 #define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
96 #define T25 0x21e1cde6
97 #define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
98 #define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
99 #define T28 0x455a14ed
100 #define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
101 #define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
102 #define T31 0x676f02d9
103 #define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
104 #define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
105 #define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
106 #define T35 0x6d9d6122
107 #define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
108 #define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
109 #define T38 0x4bdecfa9
110 #define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
111 #define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
112 #define T41 0x289b7ec6
113 #define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
114 #define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
115 #define T44 0x04881d05
116 #define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
117 #define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
118 #define T47 0x1fa27cf8
119 #define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
120 #define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
121 #define T50 0x432aff97
122 #define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
123 #define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
124 #define T53 0x655b59c3
125 #define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
126 #define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
127 #define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
128 #define T57 0x6fa87e4f
129 #define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
130 #define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
131 #define T60 0x4e0811a1
132 #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
133 #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
134 #define T63 0x2ad7d2bb
135 #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
136
137 static void md5_process (md5_state *state, const uint8_t *data /*[64]*/)
138 {
139 uint32_t
140 a = state->words[0], b = state->words[1],
141 c = state->words[2], d = state->words[3];
142 uint32_t t;
143 #if BYTE_ORDER > 0
144 /* Define storage only for big-endian CPUs. */
145 uint32_t X[16];
146 #else
147 /* Define storage for little-endian or both types of CPUs. */
148 uint32_t xbuf[16];
149 const uint32_t *X;
150 #endif
151
152 {
153 #if BYTE_ORDER == 0
154 /*
155 * Determine dynamically whether this is a big-endian or
156 * little-endian machine, since we can use a more efficient
157 * algorithm on the latter.
158 */
159 static const int w = 1;
160
161 if (*((const uint8_t *)&w)) /* dynamic little-endian */
162 #endif
163 #if BYTE_ORDER <= 0 /* little-endian */
164 {
165 /*
166 * On little-endian machines, we can process properly aligned
167 * data without copying it.
168 */
169 if (!((data - (const uint8_t *)0) & 3)) {
170 /* data are properly aligned */
171 X = (const uint32_t *)((const void *)data); // avoid compiler warning
172 } else {
173 /* not aligned */
174 memcpy(xbuf, data, 64);
175 X = xbuf;
176 }
177 }
178 #endif
179 #if BYTE_ORDER == 0
180 else /* dynamic big-endian */
181 #endif
182 #if BYTE_ORDER >= 0 /* big-endian */
183 {
184 /*
185 * On big-endian machines, we must arrange the bytes in the
186 * right order.
187 */
188 const uint8_t *xp = data;
189 int i;
190 # if BYTE_ORDER == 0
191 X = xbuf; /* (dynamic only) */
192 # else
193 # define xbuf X /* (static only) */
194 # endif
195 for (i = 0; i < 16; ++i, xp += 4)
196 xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
197 }
198 #endif
199 }
200
201 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
202
203 /* Round 1. */
204 /* Let [abcd k s i] denote the operation
205 a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
206 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
207 #define SET(a, b, c, d, k, s, Ti)\
208 t = a + F(b,c,d) + X[k] + Ti;\
209 a = ROTATE_LEFT(t, s) + b
210 /* Do the following 16 operations. */
211 SET(a, b, c, d, 0, 7, T1);
212 SET(d, a, b, c, 1, 12, T2);
213 SET(c, d, a, b, 2, 17, T3);
214 SET(b, c, d, a, 3, 22, T4);
215 SET(a, b, c, d, 4, 7, T5);
216 SET(d, a, b, c, 5, 12, T6);
217 SET(c, d, a, b, 6, 17, T7);
218 SET(b, c, d, a, 7, 22, T8);
219 SET(a, b, c, d, 8, 7, T9);
220 SET(d, a, b, c, 9, 12, T10);
221 SET(c, d, a, b, 10, 17, T11);
222 SET(b, c, d, a, 11, 22, T12);
223 SET(a, b, c, d, 12, 7, T13);
224 SET(d, a, b, c, 13, 12, T14);
225 SET(c, d, a, b, 14, 17, T15);
226 SET(b, c, d, a, 15, 22, T16);
227 #undef SET
228
229 /* Round 2. */
230 /* Let [abcd k s i] denote the operation
231 a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
232 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
233 #define SET(a, b, c, d, k, s, Ti)\
234 t = a + G(b,c,d) + X[k] + Ti;\
235 a = ROTATE_LEFT(t, s) + b
236 /* Do the following 16 operations. */
237 SET(a, b, c, d, 1, 5, T17);
238 SET(d, a, b, c, 6, 9, T18);
239 SET(c, d, a, b, 11, 14, T19);
240 SET(b, c, d, a, 0, 20, T20);
241 SET(a, b, c, d, 5, 5, T21);
242 SET(d, a, b, c, 10, 9, T22);
243 SET(c, d, a, b, 15, 14, T23);
244 SET(b, c, d, a, 4, 20, T24);
245 SET(a, b, c, d, 9, 5, T25);
246 SET(d, a, b, c, 14, 9, T26);
247 SET(c, d, a, b, 3, 14, T27);
248 SET(b, c, d, a, 8, 20, T28);
249 SET(a, b, c, d, 13, 5, T29);
250 SET(d, a, b, c, 2, 9, T30);
251 SET(c, d, a, b, 7, 14, T31);
252 SET(b, c, d, a, 12, 20, T32);
253 #undef SET
254
255 /* Round 3. */
256 /* Let [abcd k s t] denote the operation
257 a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
258 #define H(x, y, z) ((x) ^ (y) ^ (z))
259 #define SET(a, b, c, d, k, s, Ti)\
260 t = a + H(b,c,d) + X[k] + Ti;\
261 a = ROTATE_LEFT(t, s) + b
262 /* Do the following 16 operations. */
263 SET(a, b, c, d, 5, 4, T33);
264 SET(d, a, b, c, 8, 11, T34);
265 SET(c, d, a, b, 11, 16, T35);
266 SET(b, c, d, a, 14, 23, T36);
267 SET(a, b, c, d, 1, 4, T37);
268 SET(d, a, b, c, 4, 11, T38);
269 SET(c, d, a, b, 7, 16, T39);
270 SET(b, c, d, a, 10, 23, T40);
271 SET(a, b, c, d, 13, 4, T41);
272 SET(d, a, b, c, 0, 11, T42);
273 SET(c, d, a, b, 3, 16, T43);
274 SET(b, c, d, a, 6, 23, T44);
275 SET(a, b, c, d, 9, 4, T45);
276 SET(d, a, b, c, 12, 11, T46);
277 SET(c, d, a, b, 15, 16, T47);
278 SET(b, c, d, a, 2, 23, T48);
279 #undef SET
280
281 /* Round 4. */
282 /* Let [abcd k s t] denote the operation
283 a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
284 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
285 #define SET(a, b, c, d, k, s, Ti)\
286 t = a + I(b,c,d) + X[k] + Ti;\
287 a = ROTATE_LEFT(t, s) + b
288 /* Do the following 16 operations. */
289 SET(a, b, c, d, 0, 6, T49);
290 SET(d, a, b, c, 7, 10, T50);
291 SET(c, d, a, b, 14, 15, T51);
292 SET(b, c, d, a, 5, 21, T52);
293 SET(a, b, c, d, 12, 6, T53);
294 SET(d, a, b, c, 3, 10, T54);
295 SET(c, d, a, b, 10, 15, T55);
296 SET(b, c, d, a, 1, 21, T56);
297 SET(a, b, c, d, 8, 6, T57);
298 SET(d, a, b, c, 15, 10, T58);
299 SET(c, d, a, b, 6, 15, T59);
300 SET(b, c, d, a, 13, 21, T60);
301 SET(a, b, c, d, 4, 6, T61);
302 SET(d, a, b, c, 11, 10, T62);
303 SET(c, d, a, b, 2, 15, T63);
304 SET(b, c, d, a, 9, 21, T64);
305 #undef SET
306
307 /* Then perform the following additions. (That is increment each
308 of the four registers by the value it had before this block
309 was started.) */
310 state->words[0] += a;
311 state->words[1] += b;
312 state->words[2] += c;
313 state->words[3] += d;
314 }
315
316 /* api */
317
318 md5_state * md5_digest_init (md5_state *state)
319 {
320 state->bitcount[0] = state->bitcount[1] = 0;
321 state->words[0] = 0x67452301;
322 state->words[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
323 state->words[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
324 state->words[3] = 0x10325476;
325 return state;
326 }
327
328 void md5_digest_add (md5_state *state, const void *input, size_t size)
329 {
330 const uint8_t *p = (const uint8_t *)input;
331 int nbytes = (int)size; // PJ
332 int left = nbytes;
333 int offset = (state->bitcount[0] >> 3) & 63;
334 uint32_t nbits = (uint32_t)(nbytes << 3);
335
336 if (nbytes <= 0)
337 return;
338
339 /* Update the message length. */
340 state->bitcount[1] += nbytes >> 29;
341 state->bitcount[0] += nbits;
342 if (state->bitcount[0] < nbits)
343 state->bitcount[1]++;
344
345 /* Process an initial partial block. */
346 if (offset) {
347 int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
348
349 memcpy(state->buffer + offset, p, copy);
350 if (offset + copy < 64)
351 return;
352 p += copy;
353 left -= copy;
354 md5_process(state, state->buffer);
355 }
356
357 /* Process full blocks. */
358 for (; left >= 64; p += 64, left -= 64)
359 md5_process(state, p);
360
361 /* Process a final partial block. */
362 if (left)
363 memcpy(state->buffer, p, left);
364 }
365
366 #define md5_digest_byte(state, i) (uint8_t)(state->words[i >> 2] >> ((i & 3) << 3))
367
368 void md5_digest_get (md5_state *state, uint8_t digest[], int flags)
369 {
370 static const uint8_t pad[64] = {
371 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
372 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
373 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
374 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
375 };
376 uint8_t data[8];
377 int i;
378
379 /* Save the length before padding. */
380 for (i = 0; i < 8; ++i)
381 data[i] = (uint8_t)(state->bitcount[i >> 2] >> ((i & 3) << 3));
382 /* Pad to 56 bytes mod 64. */
383 md5_digest_add(state, pad, ((55 - (state->bitcount[0] >> 3)) & 63) + 1);
384 /* Append the length. */
385 md5_digest_add(state, data, 8);
386
387 /* Output */
388 if (flags & MD5_HEX)
389 { // expected digest buffer size MD5_STRING_LENGTH
390 uint8_t byte;
391 const char *alphabet;
392 alphabet = (flags & MD5_LCHEX) ? "0123456789abcdef" : "0123456789ABCDEF";
393 for (i = 0; i < MD5_DIGEST_LENGTH; ++i)
394 {
395 byte = md5_digest_byte(state, i);
396 *digest++ = (uint8_t)alphabet[byte >> 4];
397 *digest++ = (uint8_t)alphabet[byte & 15];
398 }
399 *digest = 0;
400 }
401 else
402 { // expected digest buffer size MD5_DIGEST_LENGTH
403 for (i = 0; i < MD5_DIGEST_LENGTH; ++i)
404 *digest++ = md5_digest_byte(state, i);
405 }
406 }
407
408 void md5_digest (const void *input, size_t length, uint8_t digest[], int flags)
409 {
410 md5_state md5;
411 md5_digest_init(&md5);
412 md5_digest_add(&md5, input, length);
413 md5_digest_get(&md5, digest, flags);
414 }
415
416 /* file checksum */
417
418 #define DIGEST_BUFFER_SIZE 4096
419
420 int md5_digest_add_file (md5_state *state, const char *filename)
421 {
422 FILE *fh;
423 uint8_t buffer[DIGEST_BUFFER_SIZE];
424 size_t read;
425
426 if ((fh = fopen(filename, "rb")) == NULL)
427 return 0;
428 do {
429 read = fread(buffer, 1, DIGEST_BUFFER_SIZE, fh);
430 md5_digest_add(state, buffer, read);
431 } while (read == DIGEST_BUFFER_SIZE);
432 fclose(fh);
433 return 1;
434 }
435
436 int md5_digest_file (const char *filename, uint8_t digest[], int flags)
437 {
438 md5_state state;
439
440 md5_digest_init(&state);
441 if (md5_digest_add_file(&state, filename))
442 {
443 md5_digest_get(&state, digest, flags);
444 return 1;
445 }
446 return 0;
447 }