md5.cc 12 KB

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