md5c.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * This is an OpenSSL-compatible implementation of the RSA Data Security,
  3. * Inc. MD5 Message-Digest Algorithm.
  4. *
  5. * Written by Solar Designer <solar@openwall.com> in 2001, and placed in
  6. * the public domain.
  7. *
  8. * This differs from Colin Plumb's older public domain implementation in
  9. * that no 32-bit integer data type is required, there's no compile-time
  10. * endianness configuration, and the function prototypes match OpenSSL's.
  11. * The primary goals are portability and ease of use.
  12. *
  13. * This implementation is meant to be fast, but not as fast as possible.
  14. * Some known optimizations are not included to reduce source code size
  15. * and avoid compile-time configuration.
  16. */
  17. #include <string.h>
  18. #include "md5.h"
  19. //#include "global.h"
  20. /*
  21. * The basic MD5 functions.
  22. *
  23. * F is optimized compared to its RFC 1321 definition just like in Colin
  24. * Plumb's implementation.
  25. */
  26. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  27. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  28. #define H(x, y, z) ((x) ^ (y) ^ (z))
  29. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  30. /*
  31. * The MD5 transformation for all four rounds.
  32. */
  33. #define STEP(f, a, b, c, d, x, t, s) \
  34. (a) += f((b), (c), (d)) + (x) + (t); \
  35. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  36. (a) += (b);
  37. /*
  38. * SET reads 4 input bytes in little-endian byte order and stores them
  39. * in a properly aligned word in host byte order.
  40. *
  41. * The check for little-endian architectures which tolerate unaligned
  42. * memory accesses is just an optimization. Nothing will break if it
  43. * doesn't work.
  44. */
  45. #if defined(__i386__) || defined(__vax__)
  46. #define SET(n) \
  47. (*(MD5_u32plus *)&ptr[(n) * 4])
  48. #define GET(n) \
  49. SET(n)
  50. #else
  51. #define SET(n) \
  52. (ctx->block[(n)] = \
  53. (MD5_u32plus)ptr[(n) * 4] | \
  54. ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
  55. ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
  56. ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
  57. #define GET(n) \
  58. (ctx->block[(n)])
  59. #endif
  60. /*
  61. * This processes one or more 64-byte data blocks, but does NOT update
  62. * the bit counters. There're no alignment requirements.
  63. */
  64. static void *body(MD5_CTX *ctx, void *data, unsigned long size)
  65. {
  66. unsigned char *ptr;
  67. MD5_u32plus a, b, c, d;
  68. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  69. ptr = data;
  70. a = ctx->a;
  71. b = ctx->b;
  72. c = ctx->c;
  73. d = ctx->d;
  74. do {
  75. saved_a = a;
  76. saved_b = b;
  77. saved_c = c;
  78. saved_d = d;
  79. /* Round 1 */
  80. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  81. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  82. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  83. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  84. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  85. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  86. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  87. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  88. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  89. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  90. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  91. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  92. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  93. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  94. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  95. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  96. /* Round 2 */
  97. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  98. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  99. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  100. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  101. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  102. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  103. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  104. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  105. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  106. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  107. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  108. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  109. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  110. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  111. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  112. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  113. /* Round 3 */
  114. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  115. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  116. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  117. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  118. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  119. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  120. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  121. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  122. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  123. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  124. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  125. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  126. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  127. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  128. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  129. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  130. /* Round 4 */
  131. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  132. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  133. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  134. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  135. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  136. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  137. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  138. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  139. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  140. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  141. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  142. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  143. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  144. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  145. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  146. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  147. a += saved_a;
  148. b += saved_b;
  149. c += saved_c;
  150. d += saved_d;
  151. ptr += 64;
  152. } while (size -= 64);
  153. ctx->a = a;
  154. ctx->b = b;
  155. ctx->c = c;
  156. ctx->d = d;
  157. return ptr;
  158. }
  159. void MD5_Init(MD5_CTX *ctx)
  160. {
  161. ctx->a = 0x67452301;
  162. ctx->b = 0xefcdab89;
  163. ctx->c = 0x98badcfe;
  164. ctx->d = 0x10325476;
  165. ctx->lo = 0;
  166. ctx->hi = 0;
  167. }
  168. void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
  169. {
  170. MD5_u32plus saved_lo;
  171. unsigned long used, free;
  172. saved_lo = ctx->lo;
  173. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  174. ctx->hi++;
  175. ctx->hi += size >> 29;
  176. used = saved_lo & 0x3f;
  177. if (used) {
  178. free = 64 - used;
  179. if (size < free) {
  180. memcpy(&ctx->buffer[used], data, size);
  181. return;
  182. }
  183. memcpy(&ctx->buffer[used], data, free);
  184. (unsigned char *)data += free;
  185. size -= free;
  186. body(ctx, ctx->buffer, 64);
  187. }
  188. if (size >= 64) {
  189. data = body(ctx, data, size & ~(unsigned long)0x3f);
  190. size &= 0x3f;
  191. }
  192. memcpy(ctx->buffer, data, size);
  193. }
  194. void MD5_Final(unsigned char *result, MD5_CTX *ctx)
  195. {
  196. unsigned long used, free;
  197. used = ctx->lo & 0x3f;
  198. ctx->buffer[used++] = 0x80;
  199. free = 64 - used;
  200. if (free < 8) {
  201. memset(&ctx->buffer[used], 0, free);
  202. body(ctx, ctx->buffer, 64);
  203. used = 0;
  204. free = 64;
  205. }
  206. memset(&ctx->buffer[used], 0, free - 8);
  207. ctx->lo <<= 3;
  208. ctx->buffer[56] = ctx->lo;
  209. ctx->buffer[57] = ctx->lo >> 8;
  210. ctx->buffer[58] = ctx->lo >> 16;
  211. ctx->buffer[59] = ctx->lo >> 24;
  212. ctx->buffer[60] = ctx->hi;
  213. ctx->buffer[61] = ctx->hi >> 8;
  214. ctx->buffer[62] = ctx->hi >> 16;
  215. ctx->buffer[63] = ctx->hi >> 24;
  216. body(ctx, ctx->buffer, 64);
  217. result[0] = ctx->a;
  218. result[1] = ctx->a >> 8;
  219. result[2] = ctx->a >> 16;
  220. result[3] = ctx->a >> 24;
  221. result[4] = ctx->b;
  222. result[5] = ctx->b >> 8;
  223. result[6] = ctx->b >> 16;
  224. result[7] = ctx->b >> 24;
  225. result[8] = ctx->c;
  226. result[9] = ctx->c >> 8;
  227. result[10] = ctx->c >> 16;
  228. result[11] = ctx->c >> 24;
  229. result[12] = ctx->d;
  230. result[13] = ctx->d >> 8;
  231. result[14] = ctx->d >> 16;
  232. result[15] = ctx->d >> 24;
  233. memset(ctx, 0, sizeof(ctx));
  234. }
  235. char * MD5Data (const unsigned char *data, unsigned int len, char *buf)
  236. {
  237. MD5_CTX ctx;
  238. char *ret = NULL;
  239. MD5_Init(&ctx);
  240. MD5_Update(&ctx, &data, len);
  241. MD5_Final(ret, &ctx);
  242. return ret;
  243. // MD5COUNT(&ctx,len);
  244. // return MD5End(&ctx, buf);
  245. }
  246. /* DIE
  247. char *md5sum (char *filename)
  248. {
  249. unsigned char buffer[BUFSIZ];
  250. unsigned char md5out[33];
  251. char md5string[33];
  252. MD5_CTX ctx;
  253. int f,i,j;
  254. MD5_Init(&ctx);
  255. f = open(filename,O_RDONLY);
  256. if (f < 0) return 0;
  257. while ((i = read(f,buffer,sizeof buffer)) > 0) {
  258. MD5_Update(&ctx,buffer,i);
  259. MD5COUNT(&ctx, (unsigned int)i);
  260. }
  261. j = errno;
  262. close(f);
  263. errno = j;
  264. if (i < 0) return 0;
  265. MD5_final(md5out, &ctx);
  266. for(i=0; i<16; i++)
  267. sprintf(md5string + (i*2), "%.2x", md5out[i]);
  268. return md5string;
  269. }
  270. */