1 |
2 |
kfleming |
/*
|
2 |
|
|
**********************************************************************
|
3 |
|
|
** md5.h -- Header file for implementation of MD5 **
|
4 |
|
|
** RSA Data Security, Inc. MD5 Message Digest Algorithm **
|
5 |
|
|
** Created: 2/17/90 RLR **
|
6 |
|
|
** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version **
|
7 |
|
|
** Revised (for MD5): RLR 4/27/91 **
|
8 |
|
|
** -- G modified to have y&~z instead of y&z **
|
9 |
|
|
** -- FF, GG, HH modified to add in last register done **
|
10 |
|
|
** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
|
11 |
|
|
** -- distinct additive constant for each step **
|
12 |
|
|
** -- round 4 added, working mod 7 **
|
13 |
|
|
**********************************************************************
|
14 |
|
|
*/
|
15 |
|
|
|
16 |
|
|
/*
|
17 |
|
|
**********************************************************************
|
18 |
|
|
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
|
19 |
|
|
** **
|
20 |
|
|
** License to copy and use this software is granted provided that **
|
21 |
|
|
** it is identified as the "RSA Data Security, Inc. MD5 Message **
|
22 |
|
|
** Digest Algorithm" in all material mentioning or referencing this **
|
23 |
|
|
** software or this function. **
|
24 |
|
|
** **
|
25 |
|
|
** License is also granted to make and use derivative works **
|
26 |
|
|
** provided that such works are identified as "derived from the RSA **
|
27 |
|
|
** Data Security, Inc. MD5 Message Digest Algorithm" in all **
|
28 |
|
|
** material mentioning or referencing the derived work. **
|
29 |
|
|
** **
|
30 |
|
|
** RSA Data Security, Inc. makes no representations concerning **
|
31 |
|
|
** either the merchantability of this software or the suitability **
|
32 |
|
|
** of this software for any particular purpose. It is provided "as **
|
33 |
|
|
** is" without express or implied warranty of any kind. **
|
34 |
|
|
** **
|
35 |
|
|
** These notices must be retained in any copies of any part of this **
|
36 |
|
|
** documentation and/or software. **
|
37 |
|
|
**********************************************************************
|
38 |
|
|
*/
|
39 |
|
|
|
40 |
|
|
/* typedef a 32 bit type */
|
41 |
|
|
typedef unsigned long int UINT4;
|
42 |
|
|
|
43 |
|
|
/* Data structure for MD5 (Message Digest) computation */
|
44 |
|
|
typedef struct {
|
45 |
|
|
UINT4 i[2]; /* number of _bits_ handled mod 2^64 */
|
46 |
|
|
UINT4 buf[4]; /* scratch buffer */
|
47 |
|
|
unsigned char in[64]; /* input buffer */
|
48 |
|
|
unsigned char digest[16]; /* actual digest after MD5Final call */
|
49 |
|
|
} MD5_CTX;
|
50 |
|
|
|
51 |
|
|
void MD5Init ();
|
52 |
|
|
void MD5Update ();
|
53 |
|
|
void MD5Final ();
|
54 |
|
|
|
55 |
|
|
/*
|
56 |
|
|
**********************************************************************
|
57 |
|
|
** End of md5.h **
|
58 |
|
|
******************************* (cut) ********************************
|
59 |
|
|
*/
|
60 |
|
|
|
61 |
|
|
/*
|
62 |
|
|
**********************************************************************
|
63 |
|
|
** md5.c **
|
64 |
|
|
** RSA Data Security, Inc. MD5 Message Digest Algorithm **
|
65 |
|
|
** Created: 2/17/90 RLR **
|
66 |
|
|
** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
|
67 |
|
|
**********************************************************************
|
68 |
|
|
*/
|
69 |
|
|
|
70 |
|
|
/*
|
71 |
|
|
**********************************************************************
|
72 |
|
|
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
|
73 |
|
|
** **
|
74 |
|
|
** License to copy and use this software is granted provided that **
|
75 |
|
|
** it is identified as the "RSA Data Security, Inc. MD5 Message **
|
76 |
|
|
** Digest Algorithm" in all material mentioning or referencing this **
|
77 |
|
|
** software or this function. **
|
78 |
|
|
** **
|
79 |
|
|
** License is also granted to make and use derivative works **
|
80 |
|
|
** provided that such works are identified as "derived from the RSA **
|
81 |
|
|
** Data Security, Inc. MD5 Message Digest Algorithm" in all **
|
82 |
|
|
** material mentioning or referencing the derived work. **
|
83 |
|
|
** **
|
84 |
|
|
** RSA Data Security, Inc. makes no representations concerning **
|
85 |
|
|
** either the merchantability of this software or the suitability **
|
86 |
|
|
** of this software for any particular purpose. It is provided "as **
|
87 |
|
|
** is" without express or implied warranty of any kind. **
|
88 |
|
|
** **
|
89 |
|
|
** These notices must be retained in any copies of any part of this **
|
90 |
|
|
** documentation and/or software. **
|
91 |
|
|
**********************************************************************
|
92 |
|
|
*/
|
93 |
|
|
|
94 |
|
|
/* -- include the following line if the md5.h header file is separate -- */
|
95 |
|
|
/* #include "md5.h" */
|
96 |
|
|
|
97 |
|
|
/* forward declaration */
|
98 |
|
|
static void Transform ();
|
99 |
|
|
|
100 |
|
|
static unsigned char PADDING[64] = {
|
101 |
|
|
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
102 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
103 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
104 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
105 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
106 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
107 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
108 |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
109 |
|
|
};
|
110 |
|
|
|
111 |
|
|
/* F, G and H are basic MD5 functions: selection, majority, parity */
|
112 |
|
|
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
113 |
|
|
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
114 |
|
|
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
115 |
|
|
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
116 |
|
|
|
117 |
|
|
/* ROTATE_LEFT rotates x left n bits */
|
118 |
|
|
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
119 |
|
|
|
120 |
|
|
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
|
121 |
|
|
/* Rotation is separate from addition to prevent recomputation */
|
122 |
|
|
#define FF(a, b, c, d, x, s, ac) \
|
123 |
|
|
{(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
124 |
|
|
(a) = ROTATE_LEFT ((a), (s)); \
|
125 |
|
|
(a) += (b); \
|
126 |
|
|
}
|
127 |
|
|
#define GG(a, b, c, d, x, s, ac) \
|
128 |
|
|
{(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
129 |
|
|
(a) = ROTATE_LEFT ((a), (s)); \
|
130 |
|
|
(a) += (b); \
|
131 |
|
|
}
|
132 |
|
|
#define HH(a, b, c, d, x, s, ac) \
|
133 |
|
|
{(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
134 |
|
|
(a) = ROTATE_LEFT ((a), (s)); \
|
135 |
|
|
(a) += (b); \
|
136 |
|
|
}
|
137 |
|
|
#define II(a, b, c, d, x, s, ac) \
|
138 |
|
|
{(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
139 |
|
|
(a) = ROTATE_LEFT ((a), (s)); \
|
140 |
|
|
(a) += (b); \
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
void MD5Init (mdContext)
|
144 |
|
|
MD5_CTX *mdContext;
|
145 |
|
|
{
|
146 |
|
|
mdContext->i[0] = mdContext->i[1] = (UINT4)0;
|
147 |
|
|
|
148 |
|
|
/* Load magic initialization constants.
|
149 |
|
|
*/
|
150 |
|
|
mdContext->buf[0] = (UINT4)0x67452301;
|
151 |
|
|
mdContext->buf[1] = (UINT4)0xefcdab89;
|
152 |
|
|
mdContext->buf[2] = (UINT4)0x98badcfe;
|
153 |
|
|
mdContext->buf[3] = (UINT4)0x10325476;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
void MD5Update (mdContext, inBuf, inLen)
|
157 |
|
|
MD5_CTX *mdContext;
|
158 |
|
|
unsigned char *inBuf;
|
159 |
|
|
unsigned int inLen;
|
160 |
|
|
{
|
161 |
|
|
UINT4 in[16];
|
162 |
|
|
int mdi;
|
163 |
|
|
unsigned int i, ii;
|
164 |
|
|
|
165 |
|
|
/* compute number of bytes mod 64 */
|
166 |
|
|
mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
|
167 |
|
|
|
168 |
|
|
/* update number of bits */
|
169 |
|
|
if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
|
170 |
|
|
mdContext->i[1]++;
|
171 |
|
|
mdContext->i[0] += ((UINT4)inLen << 3);
|
172 |
|
|
mdContext->i[1] += ((UINT4)inLen >> 29);
|
173 |
|
|
|
174 |
|
|
while (inLen--) {
|
175 |
|
|
/* add new character to buffer, increment mdi */
|
176 |
|
|
mdContext->in[mdi++] = *inBuf++;
|
177 |
|
|
|
178 |
|
|
/* transform if necessary */
|
179 |
|
|
if (mdi == 0x40) {
|
180 |
|
|
for (i = 0, ii = 0; i < 16; i++, ii += 4)
|
181 |
|
|
in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
|
182 |
|
|
(((UINT4)mdContext->in[ii+2]) << 16) |
|
183 |
|
|
(((UINT4)mdContext->in[ii+1]) << 8) |
|
184 |
|
|
((UINT4)mdContext->in[ii]);
|
185 |
|
|
Transform (mdContext->buf, in);
|
186 |
|
|
mdi = 0;
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
void MD5Final (mdContext)
|
192 |
|
|
MD5_CTX *mdContext;
|
193 |
|
|
{
|
194 |
|
|
UINT4 in[16];
|
195 |
|
|
int mdi;
|
196 |
|
|
unsigned int i, ii;
|
197 |
|
|
unsigned int padLen;
|
198 |
|
|
|
199 |
|
|
/* save number of bits */
|
200 |
|
|
in[14] = mdContext->i[0];
|
201 |
|
|
in[15] = mdContext->i[1];
|
202 |
|
|
|
203 |
|
|
/* compute number of bytes mod 64 */
|
204 |
|
|
mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
|
205 |
|
|
|
206 |
|
|
/* pad out to 56 mod 64 */
|
207 |
|
|
padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
|
208 |
|
|
MD5Update (mdContext, PADDING, padLen);
|
209 |
|
|
|
210 |
|
|
/* append length in bits and transform */
|
211 |
|
|
for (i = 0, ii = 0; i < 14; i++, ii += 4)
|
212 |
|
|
in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
|
213 |
|
|
(((UINT4)mdContext->in[ii+2]) << 16) |
|
214 |
|
|
(((UINT4)mdContext->in[ii+1]) << 8) |
|
215 |
|
|
((UINT4)mdContext->in[ii]);
|
216 |
|
|
Transform (mdContext->buf, in);
|
217 |
|
|
|
218 |
|
|
/* store buffer in digest */
|
219 |
|
|
for (i = 0, ii = 0; i < 4; i++, ii += 4) {
|
220 |
|
|
mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
|
221 |
|
|
mdContext->digest[ii+1] =
|
222 |
|
|
(unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
|
223 |
|
|
mdContext->digest[ii+2] =
|
224 |
|
|
(unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
|
225 |
|
|
mdContext->digest[ii+3] =
|
226 |
|
|
(unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
/* Basic MD5 step. Transform buf based on in.
|
231 |
|
|
*/
|
232 |
|
|
static void Transform (buf, in)
|
233 |
|
|
UINT4 *buf;
|
234 |
|
|
UINT4 *in;
|
235 |
|
|
{
|
236 |
|
|
UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
|
237 |
|
|
|
238 |
|
|
/* Round 1 */
|
239 |
|
|
#define S11 7
|
240 |
|
|
#define S12 12
|
241 |
|
|
#define S13 17
|
242 |
|
|
#define S14 22
|
243 |
|
|
FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */
|
244 |
|
|
FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */
|
245 |
|
|
FF ( c, d, a, b, in[ 2], S13, 606105819); /* 3 */
|
246 |
|
|
FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */
|
247 |
|
|
FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */
|
248 |
|
|
FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */
|
249 |
|
|
FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */
|
250 |
|
|
FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */
|
251 |
|
|
FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */
|
252 |
|
|
FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */
|
253 |
|
|
FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 */
|
254 |
|
|
FF ( b, c, d, a, in[11], S14, 2304563134); /* 12 */
|
255 |
|
|
FF ( a, b, c, d, in[12], S11, 1804603682); /* 13 */
|
256 |
|
|
FF ( d, a, b, c, in[13], S12, 4254626195); /* 14 */
|
257 |
|
|
FF ( c, d, a, b, in[14], S13, 2792965006); /* 15 */
|
258 |
|
|
FF ( b, c, d, a, in[15], S14, 1236535329); /* 16 */
|
259 |
|
|
|
260 |
|
|
/* Round 2 */
|
261 |
|
|
#define S21 5
|
262 |
|
|
#define S22 9
|
263 |
|
|
#define S23 14
|
264 |
|
|
#define S24 20
|
265 |
|
|
GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */
|
266 |
|
|
GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */
|
267 |
|
|
GG ( c, d, a, b, in[11], S23, 643717713); /* 19 */
|
268 |
|
|
GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */
|
269 |
|
|
GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */
|
270 |
|
|
GG ( d, a, b, c, in[10], S22, 38016083); /* 22 */
|
271 |
|
|
GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */
|
272 |
|
|
GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */
|
273 |
|
|
GG ( a, b, c, d, in[ 9], S21, 568446438); /* 25 */
|
274 |
|
|
GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */
|
275 |
|
|
GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */
|
276 |
|
|
GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */
|
277 |
|
|
GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */
|
278 |
|
|
GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */
|
279 |
|
|
GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */
|
280 |
|
|
GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */
|
281 |
|
|
|
282 |
|
|
/* Round 3 */
|
283 |
|
|
#define S31 4
|
284 |
|
|
#define S32 11
|
285 |
|
|
#define S33 16
|
286 |
|
|
#define S34 23
|
287 |
|
|
HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */
|
288 |
|
|
HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */
|
289 |
|
|
HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */
|
290 |
|
|
HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */
|
291 |
|
|
HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */
|
292 |
|
|
HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */
|
293 |
|
|
HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */
|
294 |
|
|
HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */
|
295 |
|
|
HH ( a, b, c, d, in[13], S31, 681279174); /* 41 */
|
296 |
|
|
HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */
|
297 |
|
|
HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */
|
298 |
|
|
HH ( b, c, d, a, in[ 6], S34, 76029189); /* 44 */
|
299 |
|
|
HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */
|
300 |
|
|
HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */
|
301 |
|
|
HH ( c, d, a, b, in[15], S33, 530742520); /* 47 */
|
302 |
|
|
HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */
|
303 |
|
|
|
304 |
|
|
/* Round 4 */
|
305 |
|
|
#define S41 6
|
306 |
|
|
#define S42 10
|
307 |
|
|
#define S43 15
|
308 |
|
|
#define S44 21
|
309 |
|
|
II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */
|
310 |
|
|
II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */
|
311 |
|
|
II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */
|
312 |
|
|
II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */
|
313 |
|
|
II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */
|
314 |
|
|
II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */
|
315 |
|
|
II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */
|
316 |
|
|
II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */
|
317 |
|
|
II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */
|
318 |
|
|
II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */
|
319 |
|
|
II ( c, d, a, b, in[ 6], S43, 2734768916); /* 59 */
|
320 |
|
|
II ( b, c, d, a, in[13], S44, 1309151649); /* 60 */
|
321 |
|
|
II ( a, b, c, d, in[ 4], S41, 4149444226); /* 61 */
|
322 |
|
|
II ( d, a, b, c, in[11], S42, 3174756917); /* 62 */
|
323 |
|
|
II ( c, d, a, b, in[ 2], S43, 718787259); /* 63 */
|
324 |
|
|
II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */
|
325 |
|
|
|
326 |
|
|
buf[0] += a;
|
327 |
|
|
buf[1] += b;
|
328 |
|
|
buf[2] += c;
|
329 |
|
|
buf[3] += d;
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
/*
|
333 |
|
|
**********************************************************************
|
334 |
|
|
** End of md5.c **
|
335 |
|
|
******************************* (cut) ********************************
|
336 |
|
|
*/
|
337 |
|
|
|
338 |
|
|
/*
|
339 |
|
|
**********************************************************************
|
340 |
|
|
** md5driver.c -- sample routines to test **
|
341 |
|
|
** RSA Data Security, Inc. MD5 message digest algorithm. **
|
342 |
|
|
** Created: 2/16/90 RLR **
|
343 |
|
|
** Updated: 1/91 SRD **
|
344 |
|
|
**********************************************************************
|
345 |
|
|
*/
|
346 |
|
|
|
347 |
|
|
/*
|
348 |
|
|
**********************************************************************
|
349 |
|
|
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
|
350 |
|
|
** **
|
351 |
|
|
** RSA Data Security, Inc. makes no representations concerning **
|
352 |
|
|
** either the merchantability of this software or the suitability **
|
353 |
|
|
** of this software for any particular purpose. It is provided "as **
|
354 |
|
|
** is" without express or implied warranty of any kind. **
|
355 |
|
|
** **
|
356 |
|
|
** These notices must be retained in any copies of any part of this **
|
357 |
|
|
** documentation and/or software. **
|
358 |
|
|
**********************************************************************
|
359 |
|
|
*/
|
360 |
|
|
|
361 |
|
|
#include <stdio.h>
|
362 |
|
|
#include <string.h>
|
363 |
|
|
#include <sys/types.h>
|
364 |
|
|
#include <time.h>
|
365 |
|
|
|
366 |
|
|
/* -- include the following file if the file md5.h is separate -- */
|
367 |
|
|
/* #include "md5.h" */
|
368 |
|
|
|
369 |
|
|
/* Prints message digest buffer in mdContext as 32 hexadecimal digits.
|
370 |
|
|
Order is from low-order byte to high-order byte of digest.
|
371 |
|
|
Each byte is printed with high-order hexadecimal digit first.
|
372 |
|
|
*/
|
373 |
|
|
static void MDPrint (mdContext)
|
374 |
|
|
MD5_CTX *mdContext;
|
375 |
|
|
{
|
376 |
|
|
int i;
|
377 |
|
|
|
378 |
|
|
for (i = 0; i < 16; i++)
|
379 |
|
|
printf ("%02x", mdContext->digest[i]);
|
380 |
|
|
}
|
381 |
|
|
|
382 |
|
|
/* size of test block */
|
383 |
|
|
#define TEST_BLOCK_SIZE 1000
|
384 |
|
|
|
385 |
|
|
/* number of blocks to process */
|
386 |
|
|
#define TEST_BLOCKS 100000
|
387 |
|
|
|
388 |
|
|
/* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */
|
389 |
|
|
static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS;
|
390 |
|
|
|
391 |
|
|
/* A time trial routine, to measure the speed of MD5.
|
392 |
|
|
Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
|
393 |
|
|
characters.
|
394 |
|
|
*/
|
395 |
|
|
static void MDTimeTrial ()
|
396 |
|
|
{
|
397 |
|
|
MD5_CTX mdContext;
|
398 |
|
|
time_t endTime, startTime;
|
399 |
|
|
unsigned char data[TEST_BLOCK_SIZE];
|
400 |
|
|
unsigned int i;
|
401 |
|
|
|
402 |
|
|
/* initialize test data */
|
403 |
|
|
for (i = 0; i < TEST_BLOCK_SIZE; i++)
|
404 |
|
|
data[i] = (unsigned char)(i & 0xFF);
|
405 |
|
|
|
406 |
|
|
/* start timer */
|
407 |
|
|
printf ("MD5 time trial. Processing %ld characters...\n", TEST_BYTES);
|
408 |
|
|
time (&startTime);
|
409 |
|
|
|
410 |
|
|
/* digest data in TEST_BLOCK_SIZE byte blocks */
|
411 |
|
|
MD5Init (&mdContext);
|
412 |
|
|
for (i = TEST_BLOCKS; i > 0; i--)
|
413 |
|
|
MD5Update (&mdContext, data, TEST_BLOCK_SIZE);
|
414 |
|
|
MD5Final (&mdContext);
|
415 |
|
|
|
416 |
|
|
/* stop timer, get time difference */
|
417 |
|
|
time (&endTime);
|
418 |
|
|
MDPrint (&mdContext);
|
419 |
|
|
printf (" is digest of test input.\n");
|
420 |
|
|
printf
|
421 |
|
|
("Seconds to process test input: %ld\n", (long)(endTime-startTime));
|
422 |
|
|
printf
|
423 |
|
|
("Characters processed per second: %ld\n",
|
424 |
|
|
TEST_BYTES/(endTime-startTime));
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
/* Computes the message digest for string inString.
|
428 |
|
|
Prints out message digest, a space, the string (in quotes) and a
|
429 |
|
|
carriage return.
|
430 |
|
|
*/
|
431 |
|
|
static void MDString (inString)
|
432 |
|
|
char *inString;
|
433 |
|
|
{
|
434 |
|
|
MD5_CTX mdContext;
|
435 |
|
|
unsigned int len = strlen (inString);
|
436 |
|
|
|
437 |
|
|
MD5Init (&mdContext);
|
438 |
|
|
MD5Update (&mdContext, inString, len);
|
439 |
|
|
MD5Final (&mdContext);
|
440 |
|
|
MDPrint (&mdContext);
|
441 |
|
|
printf (" \"%s\"\n\n", inString);
|
442 |
|
|
}
|
443 |
|
|
|
444 |
|
|
/* Computes the message digest for a specified file.
|
445 |
|
|
Prints out message digest, a space, the file name, and a carriage
|
446 |
|
|
return.
|
447 |
|
|
*/
|
448 |
|
|
static void MDFile (filename)
|
449 |
|
|
char *filename;
|
450 |
|
|
{
|
451 |
|
|
FILE *inFile = fopen (filename, "rb");
|
452 |
|
|
MD5_CTX mdContext;
|
453 |
|
|
int bytes;
|
454 |
|
|
unsigned char data[1024];
|
455 |
|
|
|
456 |
|
|
if (inFile == NULL) {
|
457 |
|
|
printf ("%s can't be opened.\n", filename);
|
458 |
|
|
return;
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
MD5Init (&mdContext);
|
462 |
|
|
while ((bytes = fread (data, 1, 1024, inFile)) != 0)
|
463 |
|
|
MD5Update (&mdContext, data, bytes);
|
464 |
|
|
MD5Final (&mdContext);
|
465 |
|
|
MDPrint (&mdContext);
|
466 |
|
|
printf (" %s\n", filename);
|
467 |
|
|
fclose (inFile);
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
/* Writes the message digest of the data from stdin onto stdout,
|
471 |
|
|
followed by a carriage return.
|
472 |
|
|
*/
|
473 |
|
|
static void MDFilter ()
|
474 |
|
|
{
|
475 |
|
|
MD5_CTX mdContext;
|
476 |
|
|
int bytes;
|
477 |
|
|
unsigned char data[16];
|
478 |
|
|
|
479 |
|
|
MD5Init (&mdContext);
|
480 |
|
|
while ((bytes = fread (data, 1, 16, stdin)) != 0)
|
481 |
|
|
MD5Update (&mdContext, data, bytes);
|
482 |
|
|
MD5Final (&mdContext);
|
483 |
|
|
MDPrint (&mdContext);
|
484 |
|
|
printf ("\n");
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
/* Runs a standard suite of test data.
|
488 |
|
|
*/
|
489 |
|
|
static void MDTestSuite ()
|
490 |
|
|
{
|
491 |
|
|
printf ("MD5 test suite results:\n\n");
|
492 |
|
|
MDString ("");
|
493 |
|
|
MDString ("a");
|
494 |
|
|
MDString ("abc");
|
495 |
|
|
MDString ("message digest");
|
496 |
|
|
MDString ("abcdefghijklmnopqrstuvwxyz");
|
497 |
|
|
MDString
|
498 |
|
|
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
499 |
|
|
MDString
|
500 |
|
|
("1234567890123456789012345678901234567890\
|
501 |
|
|
1234567890123456789012345678901234567890");
|
502 |
|
|
/* Contents of file foo are "abc" */
|
503 |
|
|
MDFile ("foo");
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
/* ******************************************************************** */
|
507 |
|
|
/* MD6 stuff here */
|
508 |
|
|
/* ******************************************************************** */
|
509 |
|
|
|
510 |
|
|
#define W 64 /* number of bits in a word */
|
511 |
|
|
#define Db 512 /* number of bits of output */
|
512 |
|
|
#define Dw (Db/W) /* number of words of output */
|
513 |
|
|
#define N 4096 /* number of bits of message input per block */
|
514 |
|
|
#define Nw (N/W) /* number of words of message input per block */
|
515 |
|
|
|
516 |
|
|
#define K 89 /* length of nonlinear feedback shift register */
|
517 |
|
|
#define T (32*89) /* number of computation steps */
|
518 |
|
|
|
519 |
|
|
#define A0 17 /* index for linear feedback */
|
520 |
|
|
#define A1 18 /* index for first input to first and */
|
521 |
|
|
#define A2 21 /* index for second input to first and */
|
522 |
|
|
#define A3 31 /* index for first input to second and */
|
523 |
|
|
#define A4 67 /* index for second input to second and */
|
524 |
|
|
|
525 |
|
|
/* A time trial routine, to measure the speed of MD6.
|
526 |
|
|
Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
|
527 |
|
|
characters.
|
528 |
|
|
*/
|
529 |
|
|
|
530 |
|
|
static void MD6TimeTrial ()
|
531 |
|
|
{
|
532 |
|
|
time_t endTime, startTime;
|
533 |
|
|
long int i,k;
|
534 |
|
|
unsigned long long int temp;
|
535 |
|
|
unsigned long long int Z[7000]; /* buffer for computation */
|
536 |
|
|
unsigned long long S[7000]; /* stage constants */
|
537 |
|
|
unsigned int R[7000]; /* shift right amounts */
|
538 |
|
|
unsigned int L[7000]; /* shift left amounts */
|
539 |
|
|
|
540 |
|
|
for (i = 0;i<T;i++) /* these are dummy values for speed test */
|
541 |
|
|
{
|
542 |
|
|
S[i] = i;
|
543 |
|
|
R[i] = i % 29;
|
544 |
|
|
L[i] = i % 23;
|
545 |
|
|
}
|
546 |
|
|
/* start timer */
|
547 |
|
|
printf ("MD6 time trial. Processing %ld characters...\n", TEST_BYTES);
|
548 |
|
|
time (&startTime);
|
549 |
|
|
|
550 |
|
|
/* main computation loop */
|
551 |
|
|
for (k = TEST_BYTES; k > 0; k = k - N/(W/8))
|
552 |
|
|
{
|
553 |
|
|
for (i = T-1; i>=0; i = i - 1)
|
554 |
|
|
{
|
555 |
|
|
temp = Z[i+K] ^ Z[i+A0] ^ ( Z[i+A1] & Z[i+A2] ) ^ ( Z[i+A3] & ~Z[i+A4] ) ;
|
556 |
|
|
temp = temp ^ S[i];
|
557 |
|
|
temp = temp ^ (temp >> R[i]);
|
558 |
|
|
temp = temp ^ (temp << L[i]);
|
559 |
|
|
Z[i] = temp;
|
560 |
|
|
}
|
561 |
|
|
}
|
562 |
|
|
/* concentration phase */
|
563 |
|
|
for (i = 78;i>=0;i--)
|
564 |
|
|
{ long long int Z1, Z2, Z3;
|
565 |
|
|
Z1 = Z[i+8];
|
566 |
|
|
Z2 = Z[i+9];
|
567 |
|
|
Z3 = Z[i+10];
|
568 |
|
|
Z[i] = Z[i] ^ (Z1&Z2) ^ ((~Z1)&Z3);
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
/* stop timer, get time difference */
|
572 |
|
|
time (&endTime);
|
573 |
|
|
printf
|
574 |
|
|
("Seconds to process test input: %ld\n", (long)(endTime-startTime));
|
575 |
|
|
printf
|
576 |
|
|
("Characters processed per second: %ld\n",
|
577 |
|
|
TEST_BYTES/(endTime-startTime));
|
578 |
|
|
}
|
579 |
|
|
|
580 |
|
|
int main (argc, argv)
|
581 |
|
|
int argc;
|
582 |
|
|
char *argv[];
|
583 |
|
|
{
|
584 |
|
|
int i;
|
585 |
|
|
|
586 |
|
|
/* For each command line argument in turn:
|
587 |
|
|
** filename -- prints message digest and name of file
|
588 |
|
|
** -sstring -- prints message digest and contents of string
|
589 |
|
|
** -t -- prints time trial statistics for 1M characters
|
590 |
|
|
** -u -- prints MD6 time trial stats for 1M chars
|
591 |
|
|
** -x -- execute a standard suite of test data
|
592 |
|
|
** (no args) -- writes messages digest of stdin onto stdout
|
593 |
|
|
*/
|
594 |
|
|
if (argc == 1)
|
595 |
|
|
MDFilter ();
|
596 |
|
|
else
|
597 |
|
|
for (i = 1; i < argc; i++)
|
598 |
|
|
if (argv[i][0] == '-' && argv[i][1] == 's')
|
599 |
|
|
MDString (argv[i] + 2);
|
600 |
|
|
else if (strcmp (argv[i], "-t") == 0)
|
601 |
|
|
MDTimeTrial ();
|
602 |
|
|
else if (strcmp (argv[i], "-u") == 0)
|
603 |
|
|
MD6TimeTrial ();
|
604 |
|
|
else if (strcmp (argv[i], "-x") == 0)
|
605 |
|
|
MDTestSuite ();
|
606 |
|
|
else MDFile (argv[i]);
|
607 |
|
|
}
|
608 |
|
|
|
609 |
|
|
/*
|
610 |
|
|
**********************************************************************
|
611 |
|
|
** End of md5driver.c **
|
612 |
|
|
******************************* (cut) ********************************
|
613 |
|
|
*/
|