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