md5.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: md5.cc,v 1.5 1999/11/17 04:13:49 jgg Exp $
  4. /* ######################################################################
  5. MD5Sum - MD5 Message Digest Algorithm.
  6. This code implements the MD5 message-digest algorithm. The algorithm is
  7. due to Ron Rivest. This code was written by Colin Plumb in 1993, no
  8. copyright is claimed. This code is in the public domain; do with it what
  9. you wish.
  10. Equivalent code is available from RSA Data Security, Inc. This code has
  11. been tested against that, and is equivalent, except that you don't need to
  12. include two pages of legalese with every copy.
  13. To compute the message digest of a chunk of bytes, instantiate the class,
  14. and repeatedly call one of the Add() members. When finished the Result
  15. method will return the Hash and finalize the value.
  16. Changed so as no longer to depend on Colin Plumb's `usual.h' header
  17. definitions; now uses stuff from dpkg's config.h.
  18. - Ian Jackson <ijackson@nyx.cs.du.edu>.
  19. Changed into a C++ interface and made work with APT's config.h.
  20. - Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
  21. Still in the public domain.
  22. The classes use arrays of char that are a specific size. We cast those
  23. arrays to uint8_t's and go from there. This allows us to advoid using
  24. the uncommon inttypes.h in a public header or internally newing memory.
  25. In theory if C9x becomes nicely accepted
  26. ##################################################################### */
  27. /*}}}*/
  28. // Include Files /*{{{*/
  29. #ifdef __GNUG__
  30. #pragma implementation "dsync/md5.h"
  31. #endif
  32. #include <dsync/md5.h>
  33. #include <dsync/strutl.h>
  34. #include <string.h>
  35. #include <system.h>
  36. #include <unistd.h>
  37. #include <inttypes.h>
  38. #include <config.h>
  39. /*}}}*/
  40. // byteSwap - Swap bytes in a buffer /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* Swap n 32 bit longs in given buffer */
  43. #ifdef WORDS_BIGENDIAN
  44. static void byteSwap(uint32_t *buf, unsigned words)
  45. {
  46. uint8_t *p = (uint8_t *)buf;
  47. do
  48. {
  49. *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
  50. ((unsigned)p[1] << 8 | p[0]);
  51. p += 4;
  52. } while (--words);
  53. }
  54. #else
  55. #define byteSwap(buf,words)
  56. #endif
  57. /*}}}*/
  58. // MD5Transform - Alters an existing MD5 hash /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* The core of the MD5 algorithm, this alters an existing MD5 hash to
  61. reflect the addition of 16 longwords of new data. Add blocks
  62. the data and converts bytes into longwords for this routine. */
  63. // The four core functions - F1 is optimized somewhat
  64. // #define F1(x, y, z) (x & y | ~x & z)
  65. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  66. #define F2(x, y, z) F1(z, x, y)
  67. #define F3(x, y, z) (x ^ y ^ z)
  68. #define F4(x, y, z) (y ^ (x | ~z))
  69. // This is the central step in the MD5 algorithm.
  70. #define MD5STEP(f,w,x,y,z,in,s) \
  71. (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
  72. static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
  73. {
  74. register uint32_t a, b, c, d;
  75. a = buf[0];
  76. b = buf[1];
  77. c = buf[2];
  78. d = buf[3];
  79. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  80. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  81. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  82. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  83. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  84. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  85. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  86. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  87. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  88. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  89. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  90. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  91. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  92. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  93. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  94. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  95. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  96. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  97. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  98. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  99. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  100. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  101. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  102. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  103. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  104. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  105. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  106. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  107. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  108. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  109. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  110. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  111. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  112. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  113. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  114. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  115. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  116. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  117. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  118. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  119. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  120. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  121. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  122. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  123. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  124. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  125. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  126. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  127. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  128. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  129. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  130. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  131. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  132. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  133. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  134. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  135. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  136. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  137. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  138. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  139. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  140. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  141. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  142. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  143. buf[0] += a;
  144. buf[1] += b;
  145. buf[2] += c;
  146. buf[3] += d;
  147. }
  148. /*}}}*/
  149. // MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* The string form of a MD5 is a 32 character hex number */
  152. MD5SumValue::MD5SumValue(string Str)
  153. {
  154. memset(Sum,0,sizeof(Sum));
  155. Set(Str);
  156. }
  157. /*}}}*/
  158. // MD5SumValue::MD5SumValue - Default constructor /*{{{*/
  159. // ---------------------------------------------------------------------
  160. /* Sets the value to 0 */
  161. MD5SumValue::MD5SumValue()
  162. {
  163. memset(Sum,0,sizeof(Sum));
  164. }
  165. /*}}}*/
  166. // MD5SumValue::Set - Set the sum from a string /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* Converts the hex string into a set of chars */
  169. bool MD5SumValue::Set(string Str)
  170. {
  171. return Hex2Num(Str.c_str(),Str.c_str()+strlen(Str.c_str()),Sum,sizeof(Sum));
  172. }
  173. /*}}}*/
  174. // MD5SumValue::Value - Convert the number into a string /*{{{*/
  175. // ---------------------------------------------------------------------
  176. /* Converts the set of chars into a hex string in lower case */
  177. string MD5SumValue::Value() const
  178. {
  179. char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
  180. 'c','d','e','f'};
  181. char Result[33];
  182. Result[32] = 0;
  183. // Convert each char into two letters
  184. int J = 0;
  185. int I = 0;
  186. for (; I != 32; J++, I += 2)
  187. {
  188. Result[I] = Conv[Sum[J] >> 4];
  189. Result[I + 1] = Conv[Sum[J] & 0xF];
  190. }
  191. return string(Result);
  192. }
  193. /*}}}*/
  194. // MD5SumValue::operator == - Comparitor /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* Call memcmp on the buffer */
  197. bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
  198. {
  199. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  200. }
  201. /*}}}*/
  202. // MD5Summation::MD5Summation - Initialize the summer /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* This assigns the deep magic initial values */
  205. MD5Summation::MD5Summation()
  206. {
  207. uint32_t *buf = (uint32_t *)Buf;
  208. uint32_t *bytes = (uint32_t *)Bytes;
  209. buf[0] = 0x67452301;
  210. buf[1] = 0xefcdab89;
  211. buf[2] = 0x98badcfe;
  212. buf[3] = 0x10325476;
  213. bytes[0] = 0;
  214. bytes[1] = 0;
  215. Done = false;
  216. }
  217. /*}}}*/
  218. // MD5Summation::Add - 'Add' a data set to the hash /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* */
  221. bool MD5Summation::Add(const unsigned char *data,unsigned long len)
  222. {
  223. if (Done == true)
  224. return false;
  225. uint32_t *buf = (uint32_t *)Buf;
  226. uint32_t *bytes = (uint32_t *)Bytes;
  227. uint32_t *in = (uint32_t *)In;
  228. // Update byte count and carry (this could be done with a long long?)
  229. uint32_t t = bytes[0];
  230. if ((bytes[0] = t + len) < t)
  231. bytes[1]++;
  232. // Space available (at least 1)
  233. t = 64 - (t & 0x3f);
  234. if (t > len)
  235. {
  236. memcpy((unsigned char *)in + 64 - t,data,len);
  237. return true;
  238. }
  239. // First chunk is an odd size
  240. memcpy((unsigned char *)in + 64 - t,data,t);
  241. byteSwap(in, 16);
  242. MD5Transform(buf,in);
  243. data += t;
  244. len -= t;
  245. // Process data in 64-byte chunks
  246. while (len >= 64)
  247. {
  248. memcpy(in,data,64);
  249. byteSwap(in,16);
  250. MD5Transform(buf,in);
  251. data += 64;
  252. len -= 64;
  253. }
  254. // Handle any remaining bytes of data.
  255. memcpy(in,data,len);
  256. return true;
  257. }
  258. /*}}}*/
  259. // MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
  260. // ---------------------------------------------------------------------
  261. /* */
  262. bool MD5Summation::AddFD(int Fd,unsigned long Size)
  263. {
  264. unsigned char Buf[64*64];
  265. int Res = 0;
  266. while (Size != 0)
  267. {
  268. Res = read(Fd,Buf,MIN(Size,sizeof(Buf)));
  269. if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf)))
  270. return false;
  271. Size -= Res;
  272. Add(Buf,Res);
  273. }
  274. return true;
  275. }
  276. /*}}}*/
  277. // MD5Summation::Result - Returns the value of the sum /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* Because this must add in the last bytes of the series it prevents anyone
  280. from calling add after. */
  281. MD5SumValue MD5Summation::Result()
  282. {
  283. uint32_t *buf = (uint32_t *)Buf;
  284. uint32_t *bytes = (uint32_t *)Bytes;
  285. uint32_t *in = (uint32_t *)In;
  286. if (Done == false)
  287. {
  288. // Number of bytes in In
  289. int count = bytes[0] & 0x3f;
  290. unsigned char *p = (unsigned char *)in + count;
  291. // Set the first char of padding to 0x80. There is always room.
  292. *p++ = 0x80;
  293. // Bytes of padding needed to make 56 bytes (-8..55)
  294. count = 56 - 1 - count;
  295. // Padding forces an extra block
  296. if (count < 0)
  297. {
  298. memset(p,0,count + 8);
  299. byteSwap(in, 16);
  300. MD5Transform(buf,in);
  301. p = (unsigned char *)in;
  302. count = 56;
  303. }
  304. memset(p, 0, count);
  305. byteSwap(in, 14);
  306. // Append length in bits and transform
  307. in[14] = bytes[0] << 3;
  308. in[15] = bytes[1] << 3 | bytes[0] >> 29;
  309. MD5Transform(buf,in);
  310. byteSwap(buf,4);
  311. Done = true;
  312. }
  313. MD5SumValue V;
  314. memcpy(V.Sum,buf,16);
  315. return V;
  316. }
  317. /*}}}*/