PageRenderTime 181ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llmd5.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 538 lines | 298 code | 127 blank | 113 comment | 17 complexity | caaf0bb1de09e8d43e68fca180154dce MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmd5.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. // llMD5.CC - source code for the C++/object oriented translation and
  26. // modification of MD5.
  27. //
  28. // Adapted to Linden Lab by Frank Filipanits, 6/25/2002
  29. // Fixed potential memory leak, James Cook, 6/27/2002
  30. // Translation and modification (c) 1995 by Mordechai T. Abzug
  31. // This translation/ modification is provided "as is," without express or
  32. // implied warranty of any kind.
  33. // The translator/ modifier does not claim (1) that MD5 will do what you think
  34. // it does; (2) that this translation/ modification is accurate; or (3) that
  35. // this software is "merchantible." (Language for this disclaimer partially
  36. // copied from the disclaimer below).
  37. /* based on:
  38. MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  39. MDDRIVER.C - test driver for MD2, MD4 and MD5
  40. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  41. rights reserved.
  42. License to copy and use this software is granted provided that it
  43. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  44. Algorithm" in all material mentioning or referencing this software
  45. or this function.
  46. License is also granted to make and use derivative works provided
  47. that such works are identified as "derived from the RSA Data
  48. Security, Inc. MD5 Message-Digest Algorithm" in all material
  49. mentioning or referencing the derived work.
  50. RSA Data Security, Inc. makes no representations concerning either
  51. the merchantability of this software or the suitability of this
  52. software for any particular purpose. It is provided "as is"
  53. without express or implied warranty of any kind.
  54. These notices must be retained in any copies of any part of this
  55. documentation and/or software.
  56. */
  57. #include "linden_common.h"
  58. #include "llmd5.h"
  59. #include <cassert>
  60. #include <iostream> // cerr
  61. // how many bytes to grab at a time when checking files
  62. const int LLMD5::BLOCK_LEN = 4096;
  63. // LLMD5 simple initialization method
  64. LLMD5::LLMD5()
  65. {
  66. init();
  67. }
  68. // MD5 block update operation. Continues an MD5 message-digest
  69. // operation, processing another message block, and updating the
  70. // context.
  71. void LLMD5::update (const uint1 *input, const uint4 input_length) {
  72. uint4 input_index, buffer_index;
  73. uint4 buffer_space; // how much space is left in buffer
  74. if (finalized){ // so we can't update!
  75. std::cerr << "LLMD5::update: Can't update a finalized digest!" << std::endl;
  76. return;
  77. }
  78. // Compute number of bytes mod 64
  79. buffer_index = (unsigned int)((count[0] >> 3) & 0x3F);
  80. // Update number of bits
  81. if ( (count[0] += ((uint4) input_length << 3))<((uint4) input_length << 3) )
  82. count[1]++;
  83. count[1] += ((uint4)input_length >> 29);
  84. buffer_space = 64 - buffer_index; // how much space is left in buffer
  85. // Transform as many times as possible.
  86. if (input_length >= buffer_space) { // ie. we have enough to fill the buffer
  87. // fill the rest of the buffer and transform
  88. memcpy( /* Flawfinder: ignore */
  89. buffer + buffer_index,
  90. input,
  91. buffer_space);
  92. transform (buffer);
  93. // now, transform each 64-byte piece of the input, bypassing the buffer
  94. if (input == NULL || input_length == 0){
  95. std::cerr << "LLMD5::update: Invalid input!" << std::endl;
  96. return;
  97. }
  98. for (input_index = buffer_space; input_index + 63 < input_length;
  99. input_index += 64)
  100. transform (input+input_index);
  101. buffer_index = 0; // so we can buffer remaining
  102. }
  103. else
  104. input_index=0; // so we can buffer the whole input
  105. // and here we do the buffering:
  106. memcpy(buffer+buffer_index, input+input_index, input_length-input_index); /* Flawfinder: ignore */
  107. }
  108. // MD5 update for files.
  109. // Like above, except that it works on files (and uses above as a primitive.)
  110. void LLMD5::update(FILE* file){
  111. unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */
  112. int len;
  113. while ( (len=(int)fread(buffer, 1, BLOCK_LEN, file)) )
  114. update(buffer, len);
  115. fclose (file);
  116. }
  117. // MD5 update for istreams.
  118. // Like update for files; see above.
  119. void LLMD5::update(std::istream& stream){
  120. unsigned char buffer[BLOCK_LEN]; /* Flawfinder: ignore */
  121. int len;
  122. while (stream.good()){
  123. stream.read( (char*)buffer, BLOCK_LEN); /* Flawfinder: ignore */ // note that return value of read is unusable.
  124. len=stream.gcount();
  125. update(buffer, len);
  126. }
  127. }
  128. void LLMD5::update(const std::string& s)
  129. {
  130. update((unsigned char *)s.c_str(),s.length());
  131. }
  132. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  133. // the message digest and zeroizing the context.
  134. void LLMD5::finalize (){
  135. unsigned char bits[8]; /* Flawfinder: ignore */
  136. unsigned int index, padLen;
  137. static uint1 PADDING[64]={
  138. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  139. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  140. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  141. };
  142. if (finalized){
  143. std::cerr << "LLMD5::finalize: Already finalized this digest!" << std::endl;
  144. return;
  145. }
  146. // Save number of bits
  147. encode (bits, count, 8);
  148. // Pad out to 56 mod 64.
  149. index = (uint4) ((count[0] >> 3) & 0x3f);
  150. padLen = (index < 56) ? (56 - index) : (120 - index);
  151. update (PADDING, padLen);
  152. // Append length (before padding)
  153. update (bits, 8);
  154. // Store state in digest
  155. encode (digest, state, 16);
  156. // Zeroize sensitive information
  157. memset (buffer, 0, sizeof(*buffer));
  158. finalized=1;
  159. }
  160. LLMD5::LLMD5(FILE *file){
  161. init(); // must be called be all constructors
  162. update(file);
  163. finalize ();
  164. }
  165. LLMD5::LLMD5(std::istream& stream){
  166. init(); // must called by all constructors
  167. update (stream);
  168. finalize();
  169. }
  170. // Digest a string of the format ("%s:%i" % (s, number))
  171. LLMD5::LLMD5(const unsigned char *string, const unsigned int number)
  172. {
  173. const char *colon = ":";
  174. char tbuf[16]; /* Flawfinder: ignore */
  175. init();
  176. update(string, (U32)strlen((const char *) string)); /* Flawfinder: ignore */
  177. update((const unsigned char *) colon, (U32)strlen(colon)); /* Flawfinder: ignore */
  178. snprintf(tbuf, sizeof(tbuf), "%i", number); /* Flawfinder: ignore */
  179. update((const unsigned char *) tbuf, (U32)strlen(tbuf)); /* Flawfinder: ignore */
  180. finalize();
  181. }
  182. // Digest a string
  183. LLMD5::LLMD5(const unsigned char *s)
  184. {
  185. init();
  186. update(s, (U32)strlen((const char *) s)); /* Flawfinder: ignore */
  187. finalize();
  188. }
  189. void LLMD5::raw_digest(unsigned char *s) const
  190. {
  191. if (!finalized)
  192. {
  193. std::cerr << "LLMD5::raw_digest: Can't get digest if you haven't "<<
  194. "finalized the digest!" << std::endl;
  195. s[0] = '\0';
  196. return;
  197. }
  198. memcpy(s, digest, 16); /* Flawfinder: ignore */
  199. return;
  200. }
  201. void LLMD5::hex_digest(char *s) const
  202. {
  203. int i;
  204. if (!finalized)
  205. {
  206. std::cerr << "LLMD5::hex_digest: Can't get digest if you haven't "<<
  207. "finalized the digest!" <<std::endl;
  208. s[0] = '\0';
  209. return;
  210. }
  211. for (i=0; i<16; i++)
  212. {
  213. sprintf(s+i*2, "%02x", digest[i]); /* Flawfinder: ignore */
  214. }
  215. s[32]='\0';
  216. return;
  217. }
  218. std::ostream& operator<<(std::ostream &stream, LLMD5 context)
  219. {
  220. char s[33]; /* Flawfinder: ignore */
  221. context.hex_digest(s);
  222. stream << s;
  223. return stream;
  224. }
  225. bool operator==(const LLMD5& a, const LLMD5& b)
  226. {
  227. unsigned char a_guts[16];
  228. unsigned char b_guts[16];
  229. a.raw_digest(a_guts);
  230. b.raw_digest(b_guts);
  231. if (memcmp(a_guts,b_guts,16)==0)
  232. return true;
  233. else
  234. return false;
  235. }
  236. bool operator!=(const LLMD5& a, const LLMD5& b)
  237. {
  238. return !(a==b);
  239. }
  240. // PRIVATE METHODS:
  241. void LLMD5::init(){
  242. finalized=0; // we just started!
  243. // Nothing counted, so count=0
  244. count[0] = 0;
  245. count[1] = 0;
  246. // Load magic initialization constants.
  247. state[0] = 0x67452301;
  248. state[1] = 0xefcdab89;
  249. state[2] = 0x98badcfe;
  250. state[3] = 0x10325476;
  251. }
  252. // Constants for MD5Transform routine.
  253. // Although we could use C++ style constants, defines are actually better,
  254. // since they let us easily evade scope clashes.
  255. #define S11 7
  256. #define S12 12
  257. #define S13 17
  258. #define S14 22
  259. #define S21 5
  260. #define S22 9
  261. #define S23 14
  262. #define S24 20
  263. #define S31 4
  264. #define S32 11
  265. #define S33 16
  266. #define S34 23
  267. #define S41 6
  268. #define S42 10
  269. #define S43 15
  270. #define S44 21
  271. // #defines are faster then inline, etc because the compiler is not required to inline.
  272. // Timing tests prove that this works ~40% faster on win with msvc++2k3 over using static inline.
  273. /* F, G, H and I are basic MD5 functions.
  274. */
  275. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  276. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  277. #define H(x, y, z) ((x) ^ (y) ^ (z))
  278. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  279. /* ROTATE_LEFT rotates x left n bits.
  280. */
  281. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  282. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  283. Rotation is separate from addition to prevent recomputation.
  284. */
  285. #define FF(a, b, c, d, x, s, ac) { \
  286. (a) += F ((b), (c), (d)) + (x) + (U32)(ac); \
  287. (a) = ROTATE_LEFT ((a), (s)); \
  288. (a) += (b); \
  289. }
  290. #define GG(a, b, c, d, x, s, ac) { \
  291. (a) += G ((b), (c), (d)) + (x) + (U32)(ac); \
  292. (a) = ROTATE_LEFT ((a), (s)); \
  293. (a) += (b); \
  294. }
  295. #define HH(a, b, c, d, x, s, ac) { \
  296. (a) += H ((b), (c), (d)) + (x) + (U32)(ac); \
  297. (a) = ROTATE_LEFT ((a), (s)); \
  298. (a) += (b); \
  299. }
  300. #define II(a, b, c, d, x, s, ac) { \
  301. (a) += I ((b), (c), (d)) + (x) + (U32)(ac); \
  302. (a) = ROTATE_LEFT ((a), (s)); \
  303. (a) += (b); \
  304. }
  305. // LLMD5 basic transformation. Transforms state based on block.
  306. void LLMD5::transform (const U8 block[64]){
  307. uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  308. decode (x, block, 64);
  309. assert(!finalized); // not just a user error, since the method is private
  310. /* Round 1 */
  311. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  312. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  313. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  314. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  315. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  316. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  317. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  318. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  319. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  320. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  321. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  322. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  323. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  324. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  325. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  326. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  327. /* Round 2 */
  328. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  329. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  330. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  331. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  332. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  333. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  334. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  335. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  336. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  337. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  338. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  339. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  340. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  341. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  342. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  343. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  344. /* Round 3 */
  345. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  346. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  347. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  348. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  349. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  350. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  351. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  352. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  353. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  354. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  355. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  356. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  357. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  358. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  359. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  360. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  361. /* Round 4 */
  362. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  363. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  364. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  365. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  366. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  367. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  368. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  369. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  370. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  371. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  372. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  373. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  374. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  375. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  376. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  377. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  378. state[0] += a;
  379. state[1] += b;
  380. state[2] += c;
  381. state[3] += d;
  382. // Zeroize sensitive information.
  383. memset ( (uint1 *) x, 0, sizeof(x));
  384. }
  385. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  386. // a multiple of 4.
  387. void LLMD5::encode (uint1 *output, const uint4 *input, const uint4 len) {
  388. unsigned int i, j;
  389. for (i = 0, j = 0; j < len; i++, j += 4) {
  390. output[j] = (uint1) (input[i] & 0xff);
  391. output[j+1] = (uint1) ((input[i] >> 8) & 0xff);
  392. output[j+2] = (uint1) ((input[i] >> 16) & 0xff);
  393. output[j+3] = (uint1) ((input[i] >> 24) & 0xff);
  394. }
  395. }
  396. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  397. // a multiple of 4.
  398. void LLMD5::decode (uint4 *output, const uint1 *input, const uint4 len){
  399. unsigned int i, j;
  400. for (i = 0, j = 0; j < len; i++, j += 4)
  401. output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
  402. (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
  403. }