iainnitro

PM Full C Source

Oct 31st, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.58 KB | None | 0 0
  1. #include <sys/types.h>
  2.  
  3. #include <err.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <string.h> /* for memcpy() */
  8.  
  9. struct MD5Context {
  10.         u_int32_t buf[4];
  11.         u_int32_t bytes[2];
  12.         u_int32_t in[16];
  13. };
  14.  
  15. static void MD5Init(struct MD5Context *context);
  16. static void MD5Update(struct MD5Context *context,
  17.                       u_int8_t const *buf, unsigned len);
  18. static void MD5Final(unsigned char digest[16], struct MD5Context *context);
  19. static void MD5Transform(u_int32_t buf[4], u_int32_t const in[16]);
  20.  
  21. #ifdef BIG_ENDIAN
  22. void
  23. byteSwap(u_int32_t *buf, unsigned words)
  24. {
  25.         u_int8_t *p = (u_int8_t *)buf;
  26.  
  27.         do {
  28.                 *buf++ = (u_int32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
  29.                         ((unsigned)p[1] << 8 | p[0]);
  30.                 p += 4;
  31.         } while (--words);
  32. }
  33. #else
  34. #define byteSwap(buf,words)
  35. #endif
  36.  
  37. /*
  38.  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
  39.  * initialization constants.
  40.  */
  41. void
  42. MD5Init(struct MD5Context *ctx)
  43. {
  44.         ctx->buf[0] = 0x67452301;
  45.         ctx->buf[1] = 0xefcdab89;
  46.         ctx->buf[2] = 0x98badcfe;
  47.         ctx->buf[3] = 0x10325476;
  48.  
  49.         ctx->bytes[0] = 0;
  50.         ctx->bytes[1] = 0;
  51. }
  52.  
  53. /*
  54.  * Update context to reflect the concatenation of another buffer full
  55.  * of bytes.
  56.  */
  57. void
  58. MD5Update(struct MD5Context *ctx, u_int8_t const *buf, unsigned len)
  59. {
  60.         u_int32_t t;
  61.  
  62.         /* Update byte count */
  63.  
  64.         t = ctx->bytes[0];
  65.         if ((ctx->bytes[0] = t + len) < t)
  66.                 ctx->bytes[1]++;        /* Carry from low to high */
  67.  
  68.         t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
  69.         if (t > len) {
  70.                 memcpy((u_int8_t *)ctx->in + 64 - t, buf, len);
  71.                 return;
  72.         }
  73.         /* First chunk is an odd size */
  74.         memcpy((u_int8_t *)ctx->in + 64 - t, buf, t);
  75.         byteSwap(ctx->in, 16);
  76.         MD5Transform(ctx->buf, ctx->in);
  77.         buf += t;
  78.         len -= t;
  79.  
  80.         /* Process data in 64-byte chunks */
  81.         while (len >= 64) {
  82.                 memcpy(ctx->in, buf, 64);
  83.                 byteSwap(ctx->in, 16);
  84.                 MD5Transform(ctx->buf, ctx->in);
  85.                 buf += 64;
  86.                 len -= 64;
  87.         }
  88.  
  89.         /* Handle any remaining bytes of data. */
  90.         memcpy(ctx->in, buf, len);
  91. }
  92.  
  93. /*
  94.  * Final wrapup - pad to 64-byte boundary with the bit pattern
  95.  * 1 0* (64-bit count of bits processed, MSB-first)
  96.  */
  97. void
  98. MD5Final(u_int8_t digest[16], struct MD5Context *ctx)
  99. {
  100.         int count = ctx->bytes[0] & 0x3f;       /* Number of bytes in ctx->in */
  101.         u_int8_t *p = (u_int8_t *)ctx->in + count;
  102.  
  103.         /* Set the first char of padding to 0x80.  There is always room. */
  104.         *p++ = 0x80;
  105.  
  106.         /* Bytes of padding needed to make 56 bytes (-8..55) */
  107.         count = 56 - 1 - count;
  108.  
  109.         if (count < 0) {        /* Padding forces an extra block */
  110.                 memset(p, 0, count + 8);
  111.                 byteSwap(ctx->in, 16);
  112.                 MD5Transform(ctx->buf, ctx->in);
  113.                 p = (u_int8_t *)ctx->in;
  114.                 count = 56;
  115.         }
  116.         memset(p, 0, count);
  117.         byteSwap(ctx->in, 14);
  118.  
  119.         /* Append length in bits and transform */
  120.         ctx->in[14] = ctx->bytes[0] << 3;
  121.         ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
  122.         MD5Transform(ctx->buf, ctx->in);
  123.  
  124.         byteSwap(ctx->buf, 4);
  125.         memcpy(digest, ctx->buf, 16);
  126.         memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
  127. }
  128.  
  129. /* The four core functions - F1 is optimized somewhat */
  130.  
  131. /* #define F1(x, y, z) (x & y | ~x & z) */
  132. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  133. #define F2(x, y, z) F1(z, x, y)
  134. #define F3(x, y, z) (x ^ y ^ z)
  135. #define F4(x, y, z) (y ^ (x | ~z))
  136.  
  137. /* This is the central step in the MD5 algorithm. */
  138. #define MD5STEP(f,w,x,y,z,in,s) \
  139.          (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
  140.  
  141. /*
  142.  * The core of the MD5 algorithm, this alters an existing MD5 hash to
  143.  * reflect the addition of 16 longwords of new data.  MD5Update blocks
  144.  * the data and converts bytes into longwords for this routine.
  145.  */
  146. void
  147. MD5Transform(u_int32_t buf[4], u_int32_t const in[16])
  148. {
  149.         register u_int32_t a, b, c, d;
  150.  
  151.         a = buf[0];
  152.         b = buf[1];
  153.         c = buf[2];
  154.         d = buf[3];
  155.  
  156.         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  157.         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  158.         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  159.         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  160.         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  161.         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  162.         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  163.         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  164.         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  165.         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  166.         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  167.         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  168.         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  169.         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  170.         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  171.         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  172.  
  173.         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  174.         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  175.         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  176.         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  177.         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  178.         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  179.         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  180.         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  181.         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  182.         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  183.         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  184.         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  185.         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  186.         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  187.         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  188.         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  189.  
  190.         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  191.         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  192.         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  193.         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  194.         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  195.         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  196.         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  197.         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  198.         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  199.         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  200.         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  201.         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  202.         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  203.         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  204.         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  205.         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  206.  
  207.         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  208.         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  209.         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  210.         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  211.         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  212.         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  213.         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  214.         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  215.         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  216.         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  217.         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  218.         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  219.         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  220.         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  221.         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  222.         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  223.  
  224.         buf[0] += a;
  225.         buf[1] += b;
  226.         buf[2] += c;
  227.         buf[3] += d;
  228. }
  229.  
  230. #define DIGLEN 16
  231. #define BUFLEN 56
  232.  
  233. int main(int argc, char *argv[]) {
  234. char *usage = "Usage: mz 16_CHARS_OF_CHALLENGE\n2001, [email protected]\nThere once was a Livingston RABU...\n";
  235. struct MD5Context ctx;
  236. unsigned char digest[16];
  237. char response[DIGLEN+1];
  238. char buf[BUFLEN] = {
  239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  240. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  241. 0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x48, 0x6f,
  242. 0x73, 0x74, 0x20, 0x41, 0x64, 0x64, 0x72, 0x65,
  243. 0x73, 0x73, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20,
  244. 0x62, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68,
  245. 0x65, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74
  246. };
  247.  
  248. int i;
  249.  
  250.  if (argc != 2) {
  251.   printf("%s", usage);
  252.   return 1;
  253.  }
  254.  
  255.  if (strlen(argv[1]) != DIGLEN) {
  256.   printf("%s", usage);
  257.   return 2;
  258.  }
  259.  
  260.  for (i=0; i<DIGLEN; i++) {
  261.   if ((argv[1][i] < 0x30) || (argv[1][i] > (0x30+0x2B-1))) {
  262.    printf("%s", usage);
  263.    return 3;
  264.   }
  265.  }
  266.  
  267.  printf("CHALLENGE: %s\n", argv[1]);
  268.  
  269.  bcopy(argv[1], buf, DIGLEN);
  270.  MD5Init(&ctx);
  271.  MD5Update(&ctx, buf, BUFLEN);
  272.  MD5Final(digest, &ctx);
  273.  
  274.  sprintf(response, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", digest[0]%0x2b+0x30, digest[1]%0x2b+0x30, digest[2]%0x2b+0x30, digest[3]%0x2b+0x30, digest[4]%0x2b+0x30, digest[5]%0x2b+0x30, digest[6]%0x2b+0x30, digest[7]%0x2b+0x30, digest[8]%0x2b+0x30, digest[9]%0x2b+0x30, digest[10]%0x2b+0x30, digest[11]%0x2b+0x30, digest[12]%0x2b+0x30, digest[13]%0x2b+0x30, digest[14]%0x2b+0x30, digest[15]%0x2b+0x30);
  275.  
  276.  printf("RESPONSE: %s\n", response);
  277.  return 0;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment