1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
////////////////////////////////////////////////////////////////////////////////
/// Name:         MD4 Class
///
/// Purpose:      aMule ed2k link creator
///
/// Last modified by: ThePolish <thepolish@vipmail.ru>
///
/// Copyright (c) 2004-2011 ThePolish ( thepolish@vipmail.ru )
///
/// Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
///
/// Copyright (c) 2004-2011 Alo Sarv ( madcat_@users.sourceforge.net )
///
/// Copyright (c) 2002-2011 Michael Buesch
/// Email: mbuesch@freenet.de
///
/// The algorithm is due to Ron Rivest.  This code is based on code
/// written by Colin Plumb in 1993.
///
/// This code implements the MD4 message-digest algorithm.
///
/// This program is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the
/// Free Software Foundation, Inc.,
/// 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
////////////////////////////////////////////////////////////////////////////////


#include <wx/ffile.h>
#include <wx/intl.h>

#include "md4.h"
#include "bithelp.h"


/// BIG ENDIAN byte reversing
#if wxBYTE_ORDER == wxBIG_ENDIAN
// Note: this code is harmless on little-endian machines.
void MD4::byteReverse(unsigned char *buf, unsigned longs)
{
  do
    {
      uint32_t t;
      t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
          ((unsigned) buf[1] << 8 | buf[0]);
      *(uint32_t *) buf = t;
      buf += 4;
    }
  while (--longs);
}
#else
#define byteReverse(buf, len)	do { } while (0)
#endif

/// Start MD4 accumulation.
/// Set bit count to 0 and buffer to mysteriousinitialization constants.
void MD4::MD4Init(struct MD4Context *ctx)
{
  ctx->buf[0] = 0x67452301;
  ctx->buf[1] = 0xefcdab89;
  ctx->buf[2] = 0x98badcfe;
  ctx->buf[3] = 0x10325476;
  ctx->bits[0] = 0;
  ctx->bits[1] = 0;
}

/// Update context to reflect the concatenation of another buffer full of bytes.
void MD4::MD4Update(struct MD4Context *ctx, unsigned char const *buf,
                    size_t len)
{
  register uint32_t t;

  // Update bitcount
  t = ctx->bits[0];
  if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
    ctx->bits[1]++;	// Carry from low to high
  ctx->bits[1] += len >> 29;

  t = (t >> 3) & 0x3f;	// Bytes already in shsInfo->data

  // Handle any leading odd-sized chunks
  if (t)
    {
      unsigned char *p = (unsigned char *) ctx->in + t;

      t = 64 - t;
      if (len < t)
        {
          memcpy(p, buf, len);
          return;
        }
      memcpy(p, buf, t);
      byteReverse(ctx->in, 16);
      MD4Transform(ctx->buf, (uint32_t *) ctx->in);
      buf += t;
      len -= t;
    }

  // Process data in 64-byte chunks
  while (len >= 64)
    {
      memcpy(ctx->in, buf, 64);
      byteReverse(ctx->in, 16);
      MD4Transform(ctx->buf, (uint32_t *) ctx->in);
      buf += 64;
      len -= 64;
    }

  //Handle any remaining bytes of data.
  memcpy(ctx->in, buf, len);
}


///  Final wrapup - pad to 64-byte boundary with the bit pattern
///  1 0* (64-bit count of bits processed, MSB-first)
void MD4::MD4Final(struct MD4Context *ctx, unsigned char* digest)
{
  unsigned int count;
  unsigned char *p;

  // Compute number of bytes mod 64
  count = (ctx->bits[0] >> 3) & 0x3F;

  // Set the first char of padding to 0x80.
  //This is safe since there is always at least one byte free
  p = ctx->in + count;
  *p++ = 0x80;

  // Bytes of padding needed to make 64 bytes
  count = 64 - 1 - count;

  // Pad out to 56 mod 64
  if (count < 8)
    {
      // Two lots of padding:  Pad the first block to 64 bytes
      memset(p, 0, count);
      byteReverse(ctx->in, 16);
      MD4Transform(ctx->buf, (uint32_t *) ctx->in);

      // Now fill the next block with 56 bytes
      memset(ctx->in, 0, 56);
    }
  else
    {
      // Pad block to 56 bytes
      memset(p, 0, count - 8);
    }
  byteReverse(ctx->in, 14);

  // Append length in bits and transform
  uint32_t * in32 = (uint32_t *) ctx->in;
  in32[14] = ctx->bits[0];
  in32[15] = ctx->bits[1];

  MD4Transform(ctx->buf, (uint32_t *) ctx->in);
  byteReverse((unsigned char *) ctx->buf, 4);

  if (digest!=NULL)
    {
      memcpy(digest, ctx->buf, 16);
    }
  memset(ctx, 0, sizeof(*ctx));	// In case it's sensitive
}

/// The three core functions
#define MD4_F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define MD4_G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define MD4__H(x, y, z) ((x) ^ (y) ^ (z))

#define MD4_FF(a, b, c, d, x, s) { \
    (a) += MD4_F ((b), (c), (d)) + (x); \
    (a) = rol ((a), (s)); \
  }
#define MD4_GG(a, b, c, d, x, s) { \
    (a) += MD4_G ((b), (c), (d)) + (x) + (uint32_t)0x5a827999; \
    (a) = rol ((a), (s)); \
  }
#define MD4_HH(a, b, c, d, x, s) { \
    (a) += MD4__H ((b), (c), (d)) + (x) + (uint32_t)0x6ed9eba1; \
    (a) = rol ((a), (s)); \
  }

/// The core of the MD4 algorithm
void MD4::MD4Transform(uint32_t buf[4], uint32_t const in[16])
{
  register uint32_t a, b, c, d;

  a = buf[0];
  b = buf[1];
  c = buf[2];
  d = buf[3];

  MD4_FF(a, b, c, d, in[0], 3);	    /* 1 */
  MD4_FF(d, a, b, c, in[1], 7);	    /* 2 */
  MD4_FF(c, d, a, b, in[2], 11);	/* 3 */
  MD4_FF(b, c, d, a, in[3], 19);	/* 4 */
  MD4_FF(a, b, c, d, in[4], 3);		/* 5 */
  MD4_FF(d, a, b, c, in[5], 7);		/* 6 */
  MD4_FF(c, d, a, b, in[6], 11);	/* 7 */
  MD4_FF(b, c, d, a, in[7], 19);	/* 8 */
  MD4_FF(a, b, c, d, in[8], 3);		/* 9 */
  MD4_FF(d, a, b, c, in[9], 7);		/* 10 */
  MD4_FF(c, d, a, b, in[10], 11);	/* 11 */
  MD4_FF(b, c, d, a, in[11], 19);	/* 12 */
  MD4_FF(a, b, c, d, in[12], 3);	/* 13 */
  MD4_FF(d, a, b, c, in[13], 7);	/* 14 */
  MD4_FF(c, d, a, b, in[14], 11);	/* 15 */
  MD4_FF(b, c, d, a, in[15], 19);	/* 16 */

  MD4_GG(a, b, c, d, in[0], 3);		/* 17 */
  MD4_GG(d, a, b, c, in[4], 5);		/* 18 */
  MD4_GG(c, d, a, b, in[8], 9);		/* 19 */
  MD4_GG(b, c, d, a, in[12], 13);	/* 20 */
  MD4_GG(a, b, c, d, in[1], 3);		/* 21 */
  MD4_GG(d, a, b, c, in[5], 5);		/* 22 */
  MD4_GG(c, d, a, b, in[9], 9);		/* 23 */
  MD4_GG(b, c, d, a, in[13], 13);	/* 24 */
  MD4_GG(a, b, c, d, in[2], 3);		/* 25 */
  MD4_GG(d, a, b, c, in[6], 5);		/* 26 */
  MD4_GG(c, d, a, b, in[10], 9);	/* 27 */
  MD4_GG(b, c, d, a, in[14], 13);	/* 28 */
  MD4_GG(a, b, c, d, in[3], 3);		/* 29 */
  MD4_GG(d, a, b, c, in[7], 5);		/* 30 */
  MD4_GG(c, d, a, b, in[11], 9);	/* 31 */
  MD4_GG(b, c, d, a, in[15], 13);	/* 32 */

  MD4_HH(a, b, c, d, in[0], 3);		/* 33 */
  MD4_HH(d, a, b, c, in[8], 9);		/* 34 */
  MD4_HH(c, d, a, b, in[4], 11);	/* 35 */
  MD4_HH(b, c, d, a, in[12], 15);	/* 36 */
  MD4_HH(a, b, c, d, in[2], 3);		/* 37 */
  MD4_HH(d, a, b, c, in[10], 9);	/* 38 */
  MD4_HH(c, d, a, b, in[6], 11);	/* 39 */
  MD4_HH(b, c, d, a, in[14], 15);	/* 40 */
  MD4_HH(a, b, c, d, in[1], 3);		/* 41 */
  MD4_HH(d, a, b, c, in[9], 9);		/* 42 */
  MD4_HH(c, d, a, b, in[5], 11);	/* 43 */
  MD4_HH(b, c, d, a, in[13], 15);	/* 44 */
  MD4_HH(a, b, c, d, in[3], 3);		/* 45 */
  MD4_HH(d, a, b, c, in[11], 9);	/* 46 */
  MD4_HH(c, d, a, b, in[7], 11);	/* 47 */
  MD4_HH(b, c, d, a, in[15], 15);	/* 48 */

  buf[0] += a;
  buf[1] += b;
  buf[2] += c;
  buf[3] += d;
}

/// Algorithm verification
bool MD4::selfTest()
{
  wxString test1(wxEmptyString);
  wxString test1_md(wxT("31D6CFE0D16AE931B73C59D7E0C089C0"));
  wxString test2(wxT("a"));
  wxString test2_md(wxT("BDE52CB31DE33E46245E05FBDBD6FB24"));
  wxString test3(wxT("abc"));
  wxString test3_md(wxT("A448017AAF21D8525FC10AE87AA6729D"));
  wxString test4(wxT("message digest"));
  wxString test4_md(wxT("D9130A8164549FE818874806E1C7014B"));
  wxString test5(wxT("abcdefghijklmnopqrstuvwxyz"));
  wxString test5_md(wxT("D79E1C308AA5BBCDEEA8ED63DF412DA9"));
  wxString test6(wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
  wxString test6_md(wxT("043F8582F241DB351CE627E153E7F0E4"));
  wxString test7(wxT("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
  wxString test7_md(wxT("E33B4DDC9C38F2199C3E7B164FCC0536"));

  MD4 md4;

  if (md4.calcMd4FromString(test1) != test1_md)
    return false;
  if (md4.calcMd4FromString(test2) != test2_md)
    return false;
  if (md4.calcMd4FromString(test3) != test3_md)
    return false;
  if (md4.calcMd4FromString(test4) != test4_md)
    return false;
  if (md4.calcMd4FromString(test5) != test5_md)
    return false;
  if (md4.calcMd4FromString(test6) != test6_md)
    return false;
  if (md4.calcMd4FromString(test7) != test7_md)
    return false;

  return true;
}

/// Get Md4 hash from a string
wxString MD4::calcMd4FromString(const wxString &buf)
{
  MD4Context hdc;
  unsigned char ret[MD4_HASHLEN_BYTE];

  MD4Init(&hdc);
  MD4Update(&hdc, (const unsigned char*)buf.c_str(), buf.length());
  MD4Final(&hdc, ret);

  return charToHex((const char*)ret, MD4_HASHLEN_BYTE);
}

/// Get Md4 hash from a file
wxString MD4::calcMd4FromFile(const wxString &filename, MD4Hook hook)
{
  MD4Context hdc;

  // Open file and let wxFFile destructor close the file
  // Closing it explicitly may crash on Win32 ...
  wxFFile file(filename, wxS("rbS"));
  if (! file.IsOpened())
    {
      return wxEmptyString;
    }
  else
    {
      unsigned char ret[MD4_HASHLEN_BYTE];
      unsigned int bufSize = calcBufSize(file.Length());
      char *buf = new char[bufSize];

      bool keep_going = true;
      size_t totalread = 0;

      bool goAhead = true;

      MD4Init(&hdc);
      while (!file.Eof() && keep_going)
        {
          if (hook)
            {
              goAhead = hook( (int)((double)(100.0 * totalread) / file.Length()));
            }
          if (goAhead)
            {
              size_t read = file.Read(buf, bufSize);
              MD4Update(&hdc, reinterpret_cast<unsigned char const *>(buf),
                        read );
              totalread += read;
            }
          else
            {
              delete [] buf;
              return (_("Cancelled !"));
            }
        }
      MD4Final(&hdc, ret);

      delete [] buf;

      return charToHex(reinterpret_cast<const char *>(ret),
                       MD4_HASHLEN_BYTE);
    }
}

/// Convert hash to hexa string
wxString MD4::charToHex(const char *buf, size_t len)
{
  size_t i;
  wxString hexString;

  for (i = 0; i < len; ++i)
    {
      hexString += wxString::Format(wxS("%02x"), 0xFF & *(buf + i));
    }

  // Reduce memory usage
  hexString.Shrink();

  return (hexString);
}

/// Compute Md4 buffsize
size_t MD4::calcBufSize(size_t filesize)
{
  if (filesize < 100000)
    {
      filesize = 100000;
    }
  else if (filesize > 200000)
    {
      filesize = 200000;
    }

  return (filesize);
}
// File_checked_for_headers