md5.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * This code implements the MD5 message-digest algorithm.
  3. * The algorithm is due to Ron Rivest. This code was
  4. * written by Colin Plumb in 1993, no copyright is claimed.
  5. * This code is in the public domain; do with it what you wish.
  6. *
  7. * Equivalent code is available from RSA Data Security, Inc.
  8. * This code has been tested against that, and is equivalent,
  9. * except that you don't need to include two pages of legalese
  10. * with every copy.
  11. *
  12. * To compute the message digest of a chunk of bytes, declare an
  13. * MD5Context structure, pass it to MD5Init, call MD5Update as
  14. * needed on buffers full of bytes, and then call MD5Final, which
  15. * will fill a supplied 16-byte array with the digest.
  16. */
  17. /* This code was modified in 1997 by Jim Kingdon of Cyclic Software to
  18. not require an integer type which is exactly 32 bits. This work
  19. draws on the changes for the same purpose by Tatu Ylonen
  20. <ylo@cs.hut.fi> as part of SSH, but since I didn't actually use
  21. that code, there is no copyright issue. I hereby disclaim
  22. copyright in any changes I have made; this code remains in the
  23. public domain. */
  24. /* Note regarding cvs_* namespace: this avoids potential conflicts
  25. with libraries such as some versions of Kerberos. No particular
  26. need to worry about whether the system supplies an MD5 library, as
  27. this file is only about 3k of object code. */
  28. #include <util.h>
  29. #include "md5.h"
  30. /* Little-endian byte-swapping routines. Note that these do not
  31. depend on the size of datatypes such as cvs_uint32, nor do they require
  32. us to detect the endianness of the machine we are running on. It
  33. is possible they should be macros for speed, but I would be
  34. surprised if they were a performance bottleneck for MD5. */
  35. static unsigned long
  36. getu32(const unsigned char *addr)
  37. {
  38. return (((((unsigned long)addr[3] << 8) | addr[2]) << 8)
  39. | addr[1]) << 8 | addr[0];
  40. }
  41. static void
  42. putu32(unsigned long data, unsigned char *addr)
  43. {
  44. addr[0] = (unsigned char)data;
  45. addr[1] = (unsigned char)(data >> 8);
  46. addr[2] = (unsigned char)(data >> 16);
  47. addr[3] = (unsigned char)(data >> 24);
  48. }
  49. /*
  50. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  51. * initialization constants.
  52. */
  53. void
  54. yasm_md5_init(yasm_md5_context *ctx)
  55. {
  56. ctx->buf[0] = 0x67452301;
  57. ctx->buf[1] = 0xefcdab89;
  58. ctx->buf[2] = 0x98badcfe;
  59. ctx->buf[3] = 0x10325476;
  60. ctx->bits[0] = 0;
  61. ctx->bits[1] = 0;
  62. }
  63. /*
  64. * Update context to reflect the concatenation of another buffer full
  65. * of bytes.
  66. */
  67. void
  68. yasm_md5_update(yasm_md5_context *ctx, unsigned char const *buf,
  69. unsigned long len)
  70. {
  71. unsigned long t;
  72. /* Update bitcount */
  73. t = ctx->bits[0];
  74. if ((ctx->bits[0] = (t + ((unsigned long)len << 3)) & 0xffffffff) < t)
  75. ctx->bits[1]++; /* Carry from low to high */
  76. ctx->bits[1] += len >> 29;
  77. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  78. /* Handle any leading odd-sized chunks */
  79. if ( t ) {
  80. unsigned char *p = ctx->in + t;
  81. t = 64-t;
  82. if (len < t) {
  83. memcpy(p, buf, len);
  84. return;
  85. }
  86. memcpy(p, buf, t);
  87. yasm_md5_transform (ctx->buf, ctx->in);
  88. buf += t;
  89. len -= t;
  90. }
  91. /* Process data in 64-byte chunks */
  92. while (len >= 64) {
  93. memcpy(ctx->in, buf, 64);
  94. yasm_md5_transform (ctx->buf, ctx->in);
  95. buf += 64;
  96. len -= 64;
  97. }
  98. /* Handle any remaining bytes of data. */
  99. memcpy(ctx->in, buf, len);
  100. }
  101. /*
  102. * Final wrapup - pad to 64-byte boundary with the bit pattern
  103. * 1 0* (64-bit count of bits processed, MSB-first)
  104. */
  105. void
  106. yasm_md5_final(unsigned char digest[16], yasm_md5_context *ctx)
  107. {
  108. unsigned count;
  109. unsigned char *p;
  110. /* Compute number of bytes mod 64 */
  111. count = (ctx->bits[0] >> 3) & 0x3F;
  112. /* Set the first char of padding to 0x80. This is safe since there is
  113. always at least one byte free */
  114. p = ctx->in + count;
  115. *p++ = 0x80;
  116. /* Bytes of padding needed to make 64 bytes */
  117. count = 64 - 1 - count;
  118. /* Pad out to 56 mod 64 */
  119. if (count < 8) {
  120. /* Two lots of padding: Pad the first block to 64 bytes */
  121. memset(p, 0, count);
  122. yasm_md5_transform (ctx->buf, ctx->in);
  123. /* Now fill the next block with 56 bytes */
  124. memset(ctx->in, 0, 56);
  125. } else {
  126. /* Pad block to 56 bytes */
  127. memset(p, 0, count-8);
  128. }
  129. /* Append length in bits and transform */
  130. putu32(ctx->bits[0], ctx->in + 56);
  131. putu32(ctx->bits[1], ctx->in + 60);
  132. yasm_md5_transform (ctx->buf, ctx->in);
  133. putu32(ctx->buf[0], digest);
  134. putu32(ctx->buf[1], digest + 4);
  135. putu32(ctx->buf[2], digest + 8);
  136. putu32(ctx->buf[3], digest + 12);
  137. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  138. }
  139. #ifndef ASM_MD5
  140. /* The four core functions - F1 is optimized somewhat */
  141. /* #define F1(x, y, z) (x & y | ~x & z) */
  142. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  143. #define F2(x, y, z) F1(z, x, y)
  144. #define F3(x, y, z) (x ^ y ^ z)
  145. #define F4(x, y, z) (y ^ (x | ~z))
  146. /* This is the central step in the MD5 algorithm. */
  147. #define MD5STEP(f, w, x, y, z, data, s) \
  148. ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x )
  149. /*
  150. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  151. * reflect the addition of 16 longwords of new data. MD5Update blocks
  152. * the data and converts bytes into longwords for this routine.
  153. */
  154. void
  155. yasm_md5_transform(unsigned long buf[4], const unsigned char inraw[64])
  156. {
  157. unsigned long a, b, c, d;
  158. unsigned long in[16];
  159. int i;
  160. for (i = 0; i < 16; ++i)
  161. in[i] = getu32 (inraw + 4 * i);
  162. a = buf[0];
  163. b = buf[1];
  164. c = buf[2];
  165. d = buf[3];
  166. MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
  167. MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
  168. MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
  169. MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
  170. MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
  171. MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
  172. MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
  173. MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
  174. MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
  175. MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
  176. MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
  177. MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
  178. MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
  179. MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
  180. MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
  181. MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
  182. MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
  183. MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
  184. MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
  185. MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
  186. MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
  187. MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
  188. MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
  189. MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
  190. MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
  191. MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
  192. MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
  193. MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
  194. MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
  195. MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
  196. MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
  197. MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
  198. MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
  199. MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
  200. MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
  201. MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
  202. MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
  203. MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
  204. MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
  205. MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
  206. MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
  207. MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
  208. MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
  209. MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
  210. MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
  211. MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
  212. MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
  213. MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
  214. MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
  215. MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
  216. MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
  217. MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
  218. MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
  219. MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
  220. MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
  221. MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
  222. MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
  223. MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
  224. MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
  225. MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
  226. MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
  227. MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
  228. MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
  229. MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
  230. buf[0] += a;
  231. buf[1] += b;
  232. buf[2] += c;
  233. buf[3] += d;
  234. }
  235. #endif
  236. #ifdef TEST
  237. /* Simple test program. Can use it to manually run the tests from
  238. RFC1321 for example. */
  239. #include <stdio.h>
  240. int
  241. main (int argc, char **argv)
  242. {
  243. yasm_md5_context context;
  244. unsigned char checksum[16];
  245. int i;
  246. int j;
  247. if (argc < 2)
  248. {
  249. fprintf (stderr, "usage: %s string-to-hash\n", argv[0]);
  250. exit (1);
  251. }
  252. for (j = 1; j < argc; ++j)
  253. {
  254. printf ("MD5 (\"%s\") = ", argv[j]);
  255. yasm_md5_init (&context);
  256. yasm_md5_update (&context, argv[j], strlen (argv[j]));
  257. yasm_md5_final (checksum, &context);
  258. for (i = 0; i < 16; i++)
  259. {
  260. printf ("%02x", (unsigned int) checksum[i]);
  261. }
  262. printf ("\n");
  263. }
  264. return 0;
  265. }
  266. #endif /* TEST */