You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

356 lines
12 KiB

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