Debian Codegraph @

csrc/md5.c

raw symbols SCIP
1 /*
2 * $Id: md5.c,v 1.3 2008-11-12 09:30:52 mgouy Exp $
3 *
4 * This code implements the MD5 message-digest algorithm.
5 * The algorithm is due to Ron Rivest. This code was
6 * written by Colin Plumb in 1993, no copyright is claimed.
7 * This code is in the public domain; do with it what you wish.
8 *
9 * Equivalent code is available from RSA Data Security, Inc.
10 * This code has been tested against that, and is equivalent,
11 * except that you don't need to include two pages of legalese
12 * with every copy.
13 *
14 * To compute the message digest of a chunk of bytes, declare an
15 * MD5Context structure, pass it to MD5_Init, call MD5_Update as
16 * needed on buffers full of bytes, and then call MD5_Final, which
17 * will fill a supplied 16-byte array with the digest.
18 *
19 * Modified 12 June 2003 Jeremy Katz <[email protected]> to handle
20 * endianness better
21 *
22 */
23 // taken from DEBIAN package isomd5sum
24 #include <string.h>
25 #include <stdio.h>
26 #ifdef _WIN32
27 #define IS_BIG_ENDIAN 0
28 #else
29 #include <sys/types.h>
30 #if !(defined(_BIG_ENDIAN) || defined(BIG_ENDIAN))
31 #define IS_BIG_ENDIAN 0
32 #else
33 #define IS_BIG_ENDIAN 1
34 #endif
35 #endif
36
37
38 typedef unsigned int uint32;
39
40 struct MD5Context {
41 uint32 buf[4];
42 uint32 bits[2];
43 unsigned char in[64];
44 int doByteReverse;
45 };
46
47 static void MD5_Transform(uint32 *buf, uint32 const *in);
48
49 static void byteReverse(unsigned char *buf, unsigned longs);
50
51 #ifndef ASM_MD5
52 /*
53 * Note: this code is harmless on little-endian machines.
54 */
55 static void byteReverse(unsigned char *buf, unsigned longs)
56 {
57 uint32 t;
58 do {
59 t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
60 ((unsigned) buf[1] << 8 | buf[0]);
61 *(uint32 *) buf = t;
62 buf += 4;
63 } while (--longs);
64 }
65 #endif
66
67 /*
68 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
69 * initialization constants.
70 */
71 static void MD5_Init(struct MD5Context *ctx)
72 {
73 ctx->buf[0] = 0x67452301U;
74 ctx->buf[1] = 0xefcdab89U;
75 ctx->buf[2] = 0x98badcfeU;
76 ctx->buf[3] = 0x10325476U;
77
78 ctx->bits[0] = 0;
79 ctx->bits[1] = 0;
80
81
82 if (IS_BIG_ENDIAN)
83 ctx->doByteReverse = 1;
84 else
85 ctx->doByteReverse = 0;
86 }
87
88 /*
89 * Update context to reflect the concatenation of another buffer full
90 * of bytes.
91 */
92 static void MD5_Update(struct MD5Context *ctx, unsigned const char *buf, unsigned len)
93 {
94 uint32 t;
95
96 /* Update bitcount */
97
98 t = ctx->bits[0];
99 if ((ctx->bits[0] = t + ((uint32) len << 3)) < t)
100 ctx->bits[1]++; /* Carry from low to high */
101 ctx->bits[1] += len >> 29;
102
103 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
104
105 /* Handle any leading odd-sized chunks */
106
107 if (t) {
108 unsigned char *p = (unsigned char *) ctx->in + t;
109
110 t = 64 - t;
111 if (len < t) {
112 memcpy(p, buf, len);
113 return;
114 }
115 memcpy(p, buf, t);
116 if (ctx->doByteReverse) byteReverse(ctx->in, 16);
117 MD5_Transform(ctx->buf, (uint32 *) ctx->in);
118 buf += t;
119 len -= t;
120 }
121 /* Process data in 64-byte chunks */
122
123 while (len >= 64) {
124 memcpy(ctx->in, buf, 64);
125 if (ctx->doByteReverse) byteReverse(ctx->in, 16);
126 MD5_Transform(ctx->buf, (uint32 *) ctx->in);
127 buf += 64;
128 len -= 64;
129 }
130
131 /* Handle any remaining bytes of data. */
132
133 memcpy(ctx->in, buf, len);
134 }
135
136 /*
137 * Final wrapup - pad to 64-byte boundary with the bit pattern
138 * 1 0* (64-bit count of bits processed, MSB-first)
139 */
140 static void MD5_Final(unsigned char digest[16], struct MD5Context *ctx)
141 {
142 unsigned count;
143 unsigned char *p;
144
145 /* Compute number of bytes mod 64 */
146 count = (ctx->bits[0] >> 3) & 0x3F;
147
148 /* Set the first char of padding to 0x80. This is safe since there is
149 always at least one byte free */
150 p = ctx->in + count;
151 *p++ = 0x80;
152
153 /* Bytes of padding needed to make 64 bytes */
154 count = 64 - 1 - count;
155
156 /* Pad out to 56 mod 64 */
157 if (count < 8) {
158 /* Two lots of padding: Pad the first block to 64 bytes */
159 memset(p, 0, count);
160 if (ctx->doByteReverse) byteReverse(ctx->in, 16);
161 MD5_Transform(ctx->buf, (uint32 *) ctx->in);
162
163 /* Now fill the next block with 56 bytes */
164 memset(ctx->in, 0, 56);
165 } else {
166 /* Pad block to 56 bytes */
167 memset(p, 0, count - 8);
168 }
169 if (ctx->doByteReverse) byteReverse(ctx->in, 14);
170
171 /* Append length in bits and transform */
172 ((uint32 *) ctx->in)[14] = ctx->bits[0];
173 ((uint32 *) ctx->in)[15] = ctx->bits[1];
174
175 MD5_Transform(ctx->buf, (uint32 *) ctx->in);
176 if (ctx->doByteReverse) byteReverse((unsigned char *) ctx->buf, 4);
177 memcpy(digest, ctx->buf, 16);
178 memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */
179 }
180
181 #ifndef ASM_MD5
182
183 /* The four core functions - F1 is optimized somewhat */
184
185 /* #define F1(x, y, z) (x & y | ~x & z) */
186 #define F1(x, y, z) (z ^ (x & (y ^ z)))
187 #define F2(x, y, z) F1(z, x, y)
188 #define F3(x, y, z) (x ^ y ^ z)
189 #define F4(x, y, z) (y ^ (x | ~z))
190
191 /* This is the central step in the MD5 algorithm. */
192 #define MD5STEP(f, w, x, y, z, data, s) \
193 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
194
195 /*
196 * The core of the MD5 algorithm, this alters an existing MD5 hash to
197 * reflect the addition of 16 longwords of new data. MD5_Update blocks
198 * the data and converts bytes into longwords for this routine.
199 */
200 static void MD5_Transform(uint32 buf[4], uint32 const in[16])
201 {
202 register uint32 a, b, c, d;
203
204 a = buf[0];
205 b = buf[1];
206 c = buf[2];
207 d = buf[3];
208
209 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478U, 7);
210 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756U, 12);
211 MD5STEP(F1, c, d, a, b, in[2] + 0x242070dbU, 17);
212 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceeeU, 22);
213 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0fafU, 7);
214 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62aU, 12);
215 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613U, 17);
216 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501U, 22);
217 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8U, 7);
218 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7afU, 12);
219 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1U, 17);
220 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7beU, 22);
221 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122U, 7);
222 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193U, 12);
223 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438eU, 17);
224 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821U, 22);
225
226 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562U, 5);
227 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340U, 9);
228 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51U, 14);
229 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aaU, 20);
230 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105dU, 5);
231 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453U, 9);
232 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681U, 14);
233 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8U, 20);
234 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6U, 5);
235 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6U, 9);
236 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87U, 14);
237 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14edU, 20);
238 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905U, 5);
239 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8U, 9);
240 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9U, 14);
241 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8aU, 20);
242
243 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942U, 4);
244 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681U, 11);
245 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122U, 16);
246 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380cU, 23);
247 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44U, 4);
248 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9U, 11);
249 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60U, 16);
250 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70U, 23);
251 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6U, 4);
252 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127faU, 11);
253 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085U, 16);
254 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05U, 23);
255 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039U, 4);
256 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5U, 11);
257 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8U, 16);
258 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665U, 23);
259
260 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244U, 6);
261 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97U, 10);
262 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7U, 15);
263 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039U, 21);
264 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3U, 6);
265 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92U, 10);
266 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47dU, 15);
267 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1U, 21);
268 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4fU, 6);
269 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0U, 10);
270 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314U, 15);
271 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1U, 21);
272 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82U, 6);
273 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235U, 10);
274 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bbU, 15);
275 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391U, 21);
276
277 buf[0] += a;
278 buf[1] += b;
279 buf[2] += c;
280 buf[3] += d;
281 }
282
283 #endif
284
285
286 /* function for very simple interface */
287 void raa_MD5String (char *in_string, char out_digest[33])
288 /*
289 in_string: string to be MD5-digested
290 out_digest: resulting digest as 32 hexadecimal digits plus final NULL
291 */
292 {
293 struct MD5Context context;
294 unsigned int len = strlen (in_string);
295 char *p;
296 int i;
297 unsigned char digest[16];
298
299 MD5_Init (&context);
300 MD5_Update (&context, (unsigned const char *)in_string, len);
301 MD5_Final (digest, &context);
302
303 p = out_digest;
304 for (i = 0; i < 16; i++) {
305 snprintf (p, 3, "%02x", digest[i]);
306 p += 2;
307 }
308 }