llmd5.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * @file llmd5.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewergpl$
  5. *
  6. * Copyright (c) 2001-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. *
  31. * llMD5.CC - source code for the C++/object oriented translation and
  32. * modification of MD5.
  33. *
  34. * Adapted to Linden Lab by Frank Filipanits, 6/25/2002
  35. * Fixed potential memory leak, James Cook, 6/27/2002
  36. *
  37. * Translation and modification (c) 1995 by Mordechai T. Abzug
  38. *
  39. * This translation/ modification is provided "as is," without express or
  40. * implied warranty of any kind.
  41. *
  42. * The translator/ modifier does not claim (1) that MD5 will do what you think
  43. * it does; (2) that this translation/ modification is accurate; or (3) that
  44. * this software is "merchantible." (Language for this disclaimer partially
  45. * copied from the disclaimer below).
  46. *
  47. * Based on:
  48. *
  49. * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm MDDRIVER.C -
  50. * test driver for MD2, MD4 and MD5
  51. *
  52. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
  53. * reserved.
  54. *
  55. * License to copy and use this software is granted provided that it
  56. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  57. * Algorithm" in all material mentioning or referencing this software
  58. * or this function.
  59. *
  60. * License is also granted to make and use derivative works provided
  61. * that such works are identified as "derived from the RSA Data
  62. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  63. * mentioning or referencing the derived work.
  64. *
  65. * RSA Data Security, Inc. makes no representations concerning either
  66. * the merchantability of this software or the suitability of this
  67. * software for any particular purpose. It is provided "as is"
  68. * without express or implied warranty of any kind.
  69. *
  70. * These notices must be retained in any copies of any part of this
  71. * documentation and/or software.
  72. */
  73. #include "linden_common.h"
  74. #include "llmd5.h"
  75. // How many bytes to grab at a time when checking files
  76. constexpr size_t BLOCK_LEN = 4096;
  77. LLMD5::LLMD5()
  78. {
  79. init();
  80. }
  81. LLMD5::LLMD5(FILE* file)
  82. {
  83. init(); // Must be called be all constructors
  84. update(file);
  85. finalize();
  86. }
  87. LLMD5::LLMD5(std::istream& stream)
  88. {
  89. init(); // Must called by all constructors
  90. update(stream);
  91. finalize();
  92. }
  93. // Digest a string of the format ("%s:%i" % (s, number))
  94. LLMD5::LLMD5(const unsigned char* string, U32 number)
  95. {
  96. init();
  97. update(string, (U32)strlen((const char*)string));
  98. const char* colon = ":";
  99. update((unsigned char*)colon, 1);
  100. char tbuf[MD5RAW_BYTES];
  101. snprintf(tbuf, sizeof(tbuf), "%i", number);
  102. update((unsigned char*)tbuf, (U32)strlen(tbuf));
  103. finalize();
  104. }
  105. // Digest a string
  106. LLMD5::LLMD5(const unsigned char* s)
  107. {
  108. init();
  109. update(s, (U32)strlen((const char*)s));
  110. finalize();
  111. }
  112. void LLMD5::init()
  113. {
  114. mFinalized = false; // We just started !
  115. // Nothing counted, so mCount=0
  116. mCount[0] = mCount[1] = 0;
  117. // Load magic initialization constants.
  118. mState[0] = 0x67452301;
  119. mState[1] = 0xefcdab89;
  120. mState[2] = 0x98badcfe;
  121. mState[3] = 0x10325476;
  122. }
  123. // MD5 block update operation. Continues an MD5 message-digest operation,
  124. // processing another message block, and updating the context.
  125. void LLMD5::update(const unsigned char* input, U32 input_length)
  126. {
  127. if (mFinalized)
  128. {
  129. // So we cannot update !
  130. llwarns << "Cannot update a finalized digest !" << llendl;
  131. return;
  132. }
  133. // Compute number of bytes mod 64
  134. U32 buffer_index = (U32)((mCount[0] >> 3) & 0x3F);
  135. // Update number of bits
  136. if ((mCount[0] += ((U32)input_length << 3)) < ((U32)input_length << 3))
  137. {
  138. ++mCount[1];
  139. }
  140. mCount[1] += (U32)input_length >> 29;
  141. U32 input_index = 0; // So we can buffer the whole input
  142. U32 buffer_space = 64 - buffer_index; // How much space is left in buffer
  143. // Transform as many times as possible.
  144. if (input_length >= buffer_space) // Have we enough to fill the buffer ?
  145. {
  146. // Fill the rest of the buffer and transform
  147. memcpy((void*)(mBuffer + buffer_index), (void*)input, buffer_space);
  148. transform(mBuffer);
  149. // Now, transform each 64-byte piece of the input, bypassing the buffer
  150. if (!input || input_length == 0)
  151. {
  152. llwarns << "Invalid input" << llendl;
  153. return;
  154. }
  155. for (input_index = buffer_space; input_index + 63 < input_length;
  156. input_index += 64)
  157. {
  158. transform(input + input_index);
  159. }
  160. buffer_index = 0; // So we can buffer remaining
  161. }
  162. // And here we do the buffering:
  163. memcpy((void*)(mBuffer + buffer_index), (void*)(input + input_index),
  164. input_length - input_index);
  165. }
  166. // MD5 update for files. Like above, except that it works on files (and uses
  167. // above as a primitive).
  168. void LLMD5::update(FILE* file)
  169. {
  170. unsigned char buffer[BLOCK_LEN];
  171. U32 len;
  172. while ((len = fread((void*)buffer, 1, BLOCK_LEN, file)))
  173. {
  174. update(buffer, len);
  175. }
  176. fclose(file);
  177. }
  178. // MD5 update for istreams. Like update for files; see above.
  179. void LLMD5::update(std::istream& stream)
  180. {
  181. unsigned char buffer[BLOCK_LEN];
  182. U32 len;
  183. while (stream.good())
  184. {
  185. // Note that return value of read is unusable.
  186. stream.read((char*)buffer, BLOCK_LEN);
  187. len = stream.gcount();
  188. update(buffer, len);
  189. }
  190. }
  191. void LLMD5::update(const std::string& s)
  192. {
  193. update((unsigned char*)s.c_str(), s.length());
  194. }
  195. // MD5 finalization. Ends an MD5 message-digest operation, writing the message
  196. // digest and zeroizing the context.
  197. void LLMD5::finalize()
  198. {
  199. static unsigned char PADDING[64]= {
  200. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  201. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  202. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  203. };
  204. if (mFinalized)
  205. {
  206. llwarns << "Already finalized this digest !" << llendl;
  207. return;
  208. }
  209. // Save number of bits
  210. unsigned char bits[8];
  211. encode(bits, mCount, 8);
  212. // Pad out to 56 mod 64.
  213. U32 index = (U32)((mCount[0] >> 3) & 0x3f);
  214. U32 pad_len = index < 56 ? 56 - index : 120 - index;
  215. update(PADDING, pad_len);
  216. // Append length (before padding)
  217. update(bits, 8);
  218. // Store mState in digest
  219. encode(mDigest, mState, MD5RAW_BYTES);
  220. // Zeroize sensitive information
  221. memset((void*)mBuffer, 0, sizeof(mBuffer));
  222. mFinalized = true;
  223. }
  224. void LLMD5::raw_digest(unsigned char* s) const
  225. {
  226. if (!mFinalized)
  227. {
  228. llwarns << "Cannot get digest if you have not finalized the digest !"
  229. << llendl;
  230. s[0] = '\0';
  231. return;
  232. }
  233. memcpy((void*)s, (void*)mDigest, MD5RAW_BYTES);
  234. }
  235. void LLMD5::hex_digest(char* s) const
  236. {
  237. if (!mFinalized)
  238. {
  239. llwarns << "Cannot get digest if you have not finalized the digest !"
  240. << llendl;
  241. s[0] = '\0';
  242. return;
  243. }
  244. for (S32 i = 0; i < MD5RAW_BYTES; ++i)
  245. {
  246. sprintf(s + i * 2, "%02x", mDigest[i]);
  247. }
  248. s[32] = '\0';
  249. }
  250. std::ostream& operator<<(std::ostream& stream, LLMD5 context)
  251. {
  252. char s[33];
  253. context.hex_digest(s);
  254. stream << s;
  255. return stream;
  256. }
  257. bool operator==(const LLMD5& a, const LLMD5& b)
  258. {
  259. unsigned char a_guts[MD5RAW_BYTES];
  260. unsigned char b_guts[MD5RAW_BYTES];
  261. a.raw_digest(a_guts);
  262. b.raw_digest(b_guts);
  263. return memcmp(a_guts, b_guts, MD5RAW_BYTES) == 0;
  264. }
  265. bool operator!=(const LLMD5& a, const LLMD5& b)
  266. {
  267. unsigned char a_guts[MD5RAW_BYTES];
  268. unsigned char b_guts[MD5RAW_BYTES];
  269. a.raw_digest(a_guts);
  270. b.raw_digest(b_guts);
  271. return memcmp(a_guts, b_guts, MD5RAW_BYTES) != 0;
  272. }
  273. // Constants for MD5Transform routine.
  274. // Although we could use C++ style constants, defines are actually better,
  275. // since they let us easily evade scope clashes.
  276. #define s11 7
  277. #define s12 12
  278. #define s13 17
  279. #define s14 22
  280. #define s21 5
  281. #define s22 9
  282. #define s23 14
  283. #define s24 20
  284. #define s31 4
  285. #define s32 11
  286. #define s33 16
  287. #define s34 23
  288. #define s41 6
  289. #define s42 10
  290. #define s43 15
  291. #define s44 21
  292. // #defines are faster than inlines. Timing tests prove that this works ~40%
  293. // faster on win with msvc++2k3 over using static inline.
  294. /* F, G, H and I are basic MD5 functions.
  295. */
  296. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  297. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  298. #define H(x, y, z) ((x) ^ (y) ^ (z))
  299. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  300. /* ROTATE_LEFT rotates x left n bits.
  301. */
  302. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  303. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  304. Rotation is separate from addition to prevent recomputation.
  305. */
  306. #define FF(a, b, c, d, x, s, ac) { \
  307. (a) += F ((b), (c), (d)) + (x) + (U32)(ac); \
  308. (a) = ROTATE_LEFT ((a), (s)); \
  309. (a) += (b); \
  310. }
  311. #define GG(a, b, c, d, x, s, ac) { \
  312. (a) += G ((b), (c), (d)) + (x) + (U32)(ac); \
  313. (a) = ROTATE_LEFT ((a), (s)); \
  314. (a) += (b); \
  315. }
  316. #define HH(a, b, c, d, x, s, ac) { \
  317. (a) += H ((b), (c), (d)) + (x) + (U32)(ac); \
  318. (a) = ROTATE_LEFT ((a), (s)); \
  319. (a) += (b); \
  320. }
  321. #define II(a, b, c, d, x, s, ac) { \
  322. (a) += I ((b), (c), (d)) + (x) + (U32)(ac); \
  323. (a) = ROTATE_LEFT ((a), (s)); \
  324. (a) += (b); \
  325. }
  326. // LLMD5 basic transformation. Transforms mState based on block.
  327. void LLMD5::transform(const unsigned char block[64])
  328. {
  329. U32 a = mState[0];
  330. U32 b = mState[1];
  331. U32 c = mState[2];
  332. U32 d = mState[3];
  333. U32 x[MD5RAW_BYTES];
  334. decode(x, block, 64);
  335. // Not just a user error, since the method is private
  336. llassert(!mFinalized);
  337. // Round 1
  338. FF(a, b, c, d, x[ 0], s11, 0xd76aa478); /* 1 */
  339. FF(d, a, b, c, x[ 1], s12, 0xe8c7b756); /* 2 */
  340. FF(c, d, a, b, x[ 2], s13, 0x242070db); /* 3 */
  341. FF(b, c, d, a, x[ 3], s14, 0xc1bdceee); /* 4 */
  342. FF(a, b, c, d, x[ 4], s11, 0xf57c0faf); /* 5 */
  343. FF(d, a, b, c, x[ 5], s12, 0x4787c62a); /* 6 */
  344. FF(c, d, a, b, x[ 6], s13, 0xa8304613); /* 7 */
  345. FF(b, c, d, a, x[ 7], s14, 0xfd469501); /* 8 */
  346. FF(a, b, c, d, x[ 8], s11, 0x698098d8); /* 9 */
  347. FF(d, a, b, c, x[ 9], s12, 0x8b44f7af); /* 10 */
  348. FF(c, d, a, b, x[10], s13, 0xffff5bb1); /* 11 */
  349. FF(b, c, d, a, x[11], s14, 0x895cd7be); /* 12 */
  350. FF(a, b, c, d, x[12], s11, 0x6b901122); /* 13 */
  351. FF(d, a, b, c, x[13], s12, 0xfd987193); /* 14 */
  352. FF(c, d, a, b, x[14], s13, 0xa679438e); /* 15 */
  353. FF(b, c, d, a, x[15], s14, 0x49b40821); /* 16 */
  354. // Round 2
  355. GG(a, b, c, d, x[ 1], s21, 0xf61e2562); /* 17 */
  356. GG(d, a, b, c, x[ 6], s22, 0xc040b340); /* 18 */
  357. GG(c, d, a, b, x[11], s23, 0x265e5a51); /* 19 */
  358. GG(b, c, d, a, x[ 0], s24, 0xe9b6c7aa); /* 20 */
  359. GG(a, b, c, d, x[ 5], s21, 0xd62f105d); /* 21 */
  360. GG(d, a, b, c, x[10], s22, 0x2441453); /* 22 */
  361. GG(c, d, a, b, x[15], s23, 0xd8a1e681); /* 23 */
  362. GG(b, c, d, a, x[ 4], s24, 0xe7d3fbc8); /* 24 */
  363. GG(a, b, c, d, x[ 9], s21, 0x21e1cde6); /* 25 */
  364. GG(d, a, b, c, x[14], s22, 0xc33707d6); /* 26 */
  365. GG(c, d, a, b, x[ 3], s23, 0xf4d50d87); /* 27 */
  366. GG(b, c, d, a, x[ 8], s24, 0x455a14ed); /* 28 */
  367. GG(a, b, c, d, x[13], s21, 0xa9e3e905); /* 29 */
  368. GG(d, a, b, c, x[ 2], s22, 0xfcefa3f8); /* 30 */
  369. GG(c, d, a, b, x[ 7], s23, 0x676f02d9); /* 31 */
  370. GG(b, c, d, a, x[12], s24, 0x8d2a4c8a); /* 32 */
  371. // Round 3
  372. HH(a, b, c, d, x[ 5], s31, 0xfffa3942); /* 33 */
  373. HH(d, a, b, c, x[ 8], s32, 0x8771f681); /* 34 */
  374. HH(c, d, a, b, x[11], s33, 0x6d9d6122); /* 35 */
  375. HH(b, c, d, a, x[14], s34, 0xfde5380c); /* 36 */
  376. HH(a, b, c, d, x[ 1], s31, 0xa4beea44); /* 37 */
  377. HH(d, a, b, c, x[ 4], s32, 0x4bdecfa9); /* 38 */
  378. HH(c, d, a, b, x[ 7], s33, 0xf6bb4b60); /* 39 */
  379. HH(b, c, d, a, x[10], s34, 0xbebfbc70); /* 40 */
  380. HH(a, b, c, d, x[13], s31, 0x289b7ec6); /* 41 */
  381. HH(d, a, b, c, x[ 0], s32, 0xeaa127fa); /* 42 */
  382. HH(c, d, a, b, x[ 3], s33, 0xd4ef3085); /* 43 */
  383. HH(b, c, d, a, x[ 6], s34, 0x4881d05); /* 44 */
  384. HH(a, b, c, d, x[ 9], s31, 0xd9d4d039); /* 45 */
  385. HH(d, a, b, c, x[12], s32, 0xe6db99e5); /* 46 */
  386. HH(c, d, a, b, x[15], s33, 0x1fa27cf8); /* 47 */
  387. HH(b, c, d, a, x[ 2], s34, 0xc4ac5665); /* 48 */
  388. // Round 4
  389. II(a, b, c, d, x[ 0], s41, 0xf4292244); /* 49 */
  390. II(d, a, b, c, x[ 7], s42, 0x432aff97); /* 50 */
  391. II(c, d, a, b, x[14], s43, 0xab9423a7); /* 51 */
  392. II(b, c, d, a, x[ 5], s44, 0xfc93a039); /* 52 */
  393. II(a, b, c, d, x[12], s41, 0x655b59c3); /* 53 */
  394. II(d, a, b, c, x[ 3], s42, 0x8f0ccc92); /* 54 */
  395. II(c, d, a, b, x[10], s43, 0xffeff47d); /* 55 */
  396. II(b, c, d, a, x[ 1], s44, 0x85845dd1); /* 56 */
  397. II(a, b, c, d, x[ 8], s41, 0x6fa87e4f); /* 57 */
  398. II(d, a, b, c, x[15], s42, 0xfe2ce6e0); /* 58 */
  399. II(c, d, a, b, x[ 6], s43, 0xa3014314); /* 59 */
  400. II(b, c, d, a, x[13], s44, 0x4e0811a1); /* 60 */
  401. II(a, b, c, d, x[ 4], s41, 0xf7537e82); /* 61 */
  402. II(d, a, b, c, x[11], s42, 0xbd3af235); /* 62 */
  403. II(c, d, a, b, x[ 2], s43, 0x2ad7d2bb); /* 63 */
  404. II(b, c, d, a, x[ 9], s44, 0xeb86d391); /* 64 */
  405. mState[0] += a;
  406. mState[1] += b;
  407. mState[2] += c;
  408. mState[3] += d;
  409. // Zeroize sensitive information.
  410. memset((void*)x, 0, sizeof(x));
  411. }
  412. // Encodes input (U32) into output (unsigned char). Assumes len is a multiple
  413. // of 4.
  414. void LLMD5::encode(unsigned char* output, const U32* input, U32 len)
  415. {
  416. for (U32 i = 0, j = 0; j < len; ++i, j += 4)
  417. {
  418. output[j] = (unsigned char)(input[i] & 0xff);
  419. output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
  420. output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
  421. output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
  422. }
  423. }
  424. // Decodes input (unsigned char) into output (U32). Assumes len is a multiple
  425. // of 4.
  426. void LLMD5::decode(U32* output, const unsigned char* input, U32 len)
  427. {
  428. for (U32 i = 0, j = 0; j < len; ++i, j += 4)
  429. {
  430. output[i] = ((U32)input[j]) |
  431. (((U32)input[j + 1]) << 8) |
  432. (((U32)input[j + 2]) << 16) |
  433. (((U32)input[j + 3]) << 24);
  434. }
  435. }