Process Hacker
md5.c
Go to the documentation of this file.
1 /*
2  * MD5 hash implementation and interface functions
3  * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 /* This code was modified for Process Hacker. */
11 
12 #include <phbase.h>
13 #include <md5.h>
14 
15 void MD5Transform(ULONG buf[4], ULONG in[16]);
16 
17 /*
18  * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
19  * initialization constants.
20  */
22  _Out_ MD5_CTX *Context
23  )
24 {
25  Context->buf[0] = 0x67452301;
26  Context->buf[1] = 0xefcdab89;
27  Context->buf[2] = 0x98badcfe;
28  Context->buf[3] = 0x10325476;
29 
30  Context->i[0] = 0;
31  Context->i[1] = 0;
32 }
33 
34 /*
35  * Update context to reflect the concatenation of another buffer full
36  * of bytes.
37  */
39  _Inout_ MD5_CTX *Context,
40  _In_reads_bytes_(Length) UCHAR *Input,
41  _In_ ULONG Length
42  )
43 {
44  ULONG t;
45 
46  /* Update bitcount */
47 
48  t = Context->i[0];
49  if ((Context->i[0] = t + ((ULONG) Length << 3)) < t)
50  Context->i[1]++; /* Carry from low to high */
51  Context->i[1] += Length >> 29;
52 
53  t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
54 
55  /* Handle any leading odd-sized chunks */
56 
57  if (t) {
58  unsigned char *p = (unsigned char *) Context->in + t;
59 
60  t = 64 - t;
61  if (Length < t) {
62  memcpy(p, Input, Length);
63  return;
64  }
65  memcpy(p, Input, t);
66  MD5Transform(Context->buf, (ULONG *) Context->in);
67  Input += t;
68  Length -= t;
69  }
70  /* Process data in 64-byte chunks */
71 
72  while (Length >= 64) {
73  memcpy(Context->in, Input, 64);
74  MD5Transform(Context->buf, (ULONG *) Context->in);
75  Input += 64;
76  Length -= 64;
77  }
78 
79  /* Handle any remaining bytes of data. */
80 
81  memcpy(Context->in, Input, Length);
82 }
83 
84 /*
85  * Final wrapup - pad to 64-byte boundary with the bit pattern
86  * 1 0* (64-bit count of bits processed, MSB-first)
87  */
89  _Inout_ MD5_CTX *Context
90  )
91 {
92  unsigned int count;
93  unsigned char *p;
94 
95  /* Compute number of bytes mod 64 */
96  count = (Context->i[0] >> 3) & 0x3F;
97 
98  /* Set the first char of padding to 0x80. This is safe since there is
99  always at least one byte free */
100  p = Context->in + count;
101  *p++ = 0x80;
102 
103  /* Bytes of padding needed to make 64 bytes */
104  count = 64 - 1 - count;
105 
106  /* Pad out to 56 mod 64 */
107  if (count < 8) {
108  /* Two lots of padding: Pad the first block to 64 bytes */
109  memset(p, 0, count);
110  MD5Transform(Context->buf, (ULONG *) Context->in);
111 
112  /* Now fill the next block with 56 bytes */
113  memset(Context->in, 0, 56);
114  } else {
115  /* Pad block to 56 bytes */
116  memset(p, 0, count - 8);
117  }
118 
119  /* Append length in bits and transform */
120  ((ULONG *) Context->in)[14] = Context->i[0];
121  ((ULONG *) Context->in)[15] = Context->i[1];
122 
123  MD5Transform(Context->buf, (ULONG *) Context->in);
124  memcpy(Context->digest, Context->buf, 16);
125 }
126 
127 /* The four core functions - F1 is optimized somewhat */
128 
129 /* #define F1(x, y, z) (x & y | ~x & z) */
130 #define F1(x, y, z) (z ^ (x & (y ^ z)))
131 #define F2(x, y, z) F1(z, x, y)
132 #define F3(x, y, z) (x ^ y ^ z)
133 #define F4(x, y, z) (y ^ (x | ~z))
134 
135 /* This is the central step in the MD5 algorithm. */
136 #define MD5STEP(f, w, x, y, z, data, s) \
137  ( w += f(x, y, z) + data, w = _rotl(w, s), w += x )
138 
139 /*
140  * The core of the MD5 algorithm, this alters an existing MD5 hash to
141  * reflect the addition of 16 longwords of new data. MD5Update blocks
142  * the data and converts bytes into longwords for this routine.
143  */
144 void MD5Transform(ULONG buf[4], ULONG in[16])
145 {
146  register ULONG a, b, c, d;
147 
148  a = buf[0];
149  b = buf[1];
150  c = buf[2];
151  d = buf[3];
152 
153  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
154  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
155  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
156  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
157  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
158  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
159  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
160  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
161  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
162  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
163  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
164  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
165  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
166  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
167  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
168  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
169 
170  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
171  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
172  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
173  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
174  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
175  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
176  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
177  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
178  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
179  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
180  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
181  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
182  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
183  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
184  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
185  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
186 
187  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
188  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
189  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
190  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
191  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
192  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
193  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
194  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
195  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
196  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
197  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
198  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
199  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
200  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
201  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
202  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
203 
204  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
205  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
206  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
207  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
208  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
209  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
210  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
211  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
212  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
213  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
214  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
215  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
216  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
217  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
218  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
219  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
220 
221  buf[0] += a;
222  buf[1] += b;
223  buf[2] += c;
224  buf[3] += d;
225 }