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.

360 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
7 years ago
​
6 years ago
​
7 years ago
6 years ago
7 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /////////////////////////////////////////////////////////////////////////
  2. // MD5.cpp
  3. // Implementation file for MD5 class
  4. //
  5. // This C++ Class implementation of the original RSA Data Security, Inc.
  6. // MD5 Message-Digest Algorithm is copyright (c) 2002, Gary McNickle.
  7. // All rights reserved. This software is a derivative of the "RSA Data
  8. // Security, Inc. MD5 Message-Digest Algorithm"
  9. //
  10. // You may use this software free of any charge, but without any
  11. // warranty or implied warranty, provided that you follow the terms
  12. // of the original RSA copyright, listed below.
  13. //
  14. // Original RSA Data Security, Inc. Copyright notice
  15. /////////////////////////////////////////////////////////////////////////
  16. //
  17. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  18. // rights reserved.
  19. //
  20. // License to copy and use this software is granted provided that it
  21. // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  22. // Algorithm" in all material mentioning or referencing this software
  23. // or this function.
  24. // License is also granted to make and use derivative works provided
  25. // that such works are identified as "derived from the RSA Data
  26. // Security, Inc. MD5 Message-Digest Algorithm" in all material
  27. // mentioning or referencing the derived work.
  28. // RSA Data Security, Inc. makes no representations concerning either
  29. // the merchantability of this software or the suitability of this
  30. // software for any particular purpose. It is provided "as is"
  31. // without express or implied warranty of any kind.
  32. // These notices must be retained in any copies of any part of this
  33. // documentation and/or software.
  34. /////////////////////////////////////////////////////////////////////////
  35. #include <assert.h>
  36. #include <memory.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <malloc.h>
  40. #include "md5.h"
  41. static unsigned char PADDING[64] =
  42. {
  43. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  46. };
  47. #define S11 7
  48. #define S12 12
  49. #define S13 17
  50. #define S14 22
  51. #define S21 5
  52. #define S22 9
  53. #define S23 14
  54. #define S24 20
  55. #define S31 4
  56. #define S32 11
  57. #define S33 16
  58. #define S34 23
  59. #define S41 6
  60. #define S42 10
  61. #define S43 15
  62. #define S44 21
  63. // PrintMD5: Converts a completed md5 digest into a char* string.
  64. char* PrintMD5(u_char md5Digest[16])
  65. {
  66. char chBuffer[33];
  67. char chEach[3];
  68. int nCount;
  69. memset(chBuffer,0,33);
  70. memset(chEach, 0, 3);
  71. for (nCount = 0; nCount < 16; nCount++){
  72. sprintf(chEach, "%02x", md5Digest[nCount]);
  73. strncat(chBuffer, chEach, sizeof(chEach));
  74. };
  75. #ifdef __linux__
  76. /** @todo Valgrind tells us this @p strdup() called from @ref videoAVLIBSource is never freed.
  77. *
  78. * ~~~~
  79. = =00:00:04:04.006 26021== 33 bytes in 1 blocks are definitely* lost in loss record 2,412 of 3,972
  80. ==00:00:04:04.006 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  81. ==00:00:04:04.006 26021== by 0x7802C3A: strdup (strdup.c:42) [now line 102 since I've inserted this comment]
  82. ==00:00:04:04.006 26021== by 0x1D20BD: PrintMD5(unsigned char*) (md5.cpp:84)
  83. ==00:00:04:04.006 26021== by 0x1D212A: MD5String(char*) (md5.cpp:100)
  84. ==00:00:04:04.006 26021== by 0x2476EA: videoAVLIBSource::_open(char*, bool, float, int, int, int, int, int) (videoAVLIBSource.cpp:388)
  85. ==00:00:04:04.006 26021== by 0x1AE949: streamHandler::create(dataBinder*, unsigned int) (kernelModuleEmbedVideo.cpp:1949)
  86. ==00:00:04:04.006 26021== by 0x1ADA24: kernelModuleEmbedVideo::registerStream(unsigned int, unsigned char) (kernelModuleEmbedVideo.cpp:1731)
  87. ==00:00:04:04.006 26021== by 0x1AC2A9: kernelModuleEmbedVideo::validate() (kernelModuleEmbedVideo.cpp:1347)
  88. ==00:00:04:04.006 26021== by 0x1BB1E7: kernelModuleFactory::m_fThreadProc() (kernelModuleFactory.cpp:397)
  89. ==00:00:04:04.006 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  90. ==00:00:04:04.006 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  91. ==00:00:04:04.006 26021== by 0x7874ADE: clone (clone.S:95)
  92. * ~~~~
  93. */
  94. return strdup(chBuffer);
  95. #else
  96. return _strdup(chBuffer);
  97. #endif
  98. }
  99. // MD5String: Performs the MD5 algorithm on a char* string, returning
  100. // the results as a char*.
  101. char* MD5String(char* szString)
  102. {
  103. size_t nLen = strlen(szString);
  104. md5 alg;
  105. alg.Update((unsigned char*)szString, nLen );
  106. alg.Finalize();
  107. return PrintMD5(alg.Digest());
  108. }
  109. // MD5Binary: Performs the MD5 algorithm on a byte array, returning
  110. // the results as a char*.
  111. char * MD5Binary( u_char *bin, size_t len ){
  112. md5 alg;
  113. alg.Update( bin, len );
  114. alg.Finalize();
  115. return PrintMD5(alg.Digest());
  116. };
  117. // MD5File: Performs the MD5 algorithm on a file (binar or text),
  118. // returning the results as a char*. Returns NULL if it fails.
  119. char* MD5File(char* szFilename)
  120. {
  121. FILE * file;
  122. md5 alg;
  123. size_t nLen;
  124. unsigned char chBuffer[1024];
  125. try
  126. {
  127. memset(chBuffer, 0, 1024);
  128. if ((file = fopen (szFilename, "rb")) != NULL)
  129. {
  130. while (nLen = fread (chBuffer, 1, 1024, file))
  131. alg.Update(chBuffer, nLen);
  132. alg.Finalize();
  133. fclose (file);
  134. return PrintMD5(alg.Digest());
  135. }
  136. }
  137. catch(...)
  138. {
  139. }
  140. return NULL; // failed
  141. }
  142. // md5::Init
  143. // Initializes a new context.
  144. void md5::Init()
  145. {
  146. memset(m_Count, 0, 2 * sizeof(u_int4));
  147. m_State[0] = 0x67452301;
  148. m_State[1] = 0xefcdab89;
  149. m_State[2] = 0x98badcfe;
  150. m_State[3] = 0x10325476;
  151. }
  152. // md5::Update
  153. // MD5 block update operation. Continues an MD5 message-digest
  154. // operation, processing another message block, and updating the
  155. // context.
  156. void md5::Update(u_char* chInput, size_t IL)
  157. {
  158. u_int4 i, index, partLen;
  159. u_int4 nInputLen = (u_int4)IL;
  160. // Compute number of bytes mod 64
  161. index = (unsigned int)((m_Count[0] >> 3) & 0x3F);
  162. // Update number of bits
  163. if ((m_Count[0] += (nInputLen << 3)) < (nInputLen << 3))
  164. m_Count[1]++;
  165. m_Count[1] += (nInputLen >> 29);
  166. partLen = 64 - index;
  167. // Transform as many times as possible.
  168. if (nInputLen >= partLen)
  169. {
  170. memcpy( &m_Buffer[index], chInput, partLen );
  171. Transform(m_Buffer);
  172. for (i = partLen; i + 63 < nInputLen; i += 64)
  173. Transform(&chInput[i]);
  174. index = 0;
  175. }
  176. else
  177. i = 0;
  178. // Buffer remaining input
  179. memcpy( &m_Buffer[index], &chInput[i], nInputLen-i );
  180. }
  181. // md5::Finalize
  182. // MD5 finalization. Ends an MD5 message-digest operation, writing
  183. // the message digest and zeroizing the context.
  184. void md5::Finalize()
  185. {
  186. u_char bits[8];
  187. u_int4 index, padLen;
  188. // Save number of bits
  189. Encode (bits, m_Count, 8);
  190. // Pad out to 56 mod 64
  191. index = (unsigned int)((m_Count[0] >> 3) & 0x3f);
  192. padLen = (index < 56) ? (56 - index) : (120 - index);
  193. Update(PADDING, padLen);
  194. // Append length (before padding)
  195. Update (bits, 8);
  196. // Store state in digest
  197. Encode (m_Digest, m_State, 16);
  198. memset(m_Count, 0, 2 * sizeof(u_int4));
  199. memset(m_State, 0, 4 * sizeof(u_int4));
  200. memset(m_Buffer,0, 64 * sizeof(u_char));
  201. }
  202. // md5::Transform
  203. // MD5 basic transformation. Transforms state based on block.
  204. void md5::Transform (u_char* block)
  205. {
  206. u_int4 a = m_State[0], b = m_State[1], c = m_State[2], d = m_State[3], x[16];
  207. Decode (x, block, 64);
  208. // Round 1
  209. FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
  210. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
  211. FF (c, d, a, b, x[ 2], S13, 0x242070db);
  212. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
  213. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
  214. FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
  215. FF (c, d, a, b, x[ 6], S13, 0xa8304613);
  216. FF (b, c, d, a, x[ 7], S14, 0xfd469501);
  217. FF (a, b, c, d, x[ 8], S11, 0x698098d8);
  218. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
  219. FF (c, d, a, b, x[10], S13, 0xffff5bb1);
  220. FF (b, c, d, a, x[11], S14, 0x895cd7be);
  221. FF (a, b, c, d, x[12], S11, 0x6b901122);
  222. FF (d, a, b, c, x[13], S12, 0xfd987193);
  223. FF (c, d, a, b, x[14], S13, 0xa679438e);
  224. FF (b, c, d, a, x[15], S14, 0x49b40821);
  225. // Round 2
  226. GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
  227. GG (d, a, b, c, x[ 6], S22, 0xc040b340);
  228. GG (c, d, a, b, x[11], S23, 0x265e5a51);
  229. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
  230. GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
  231. GG (d, a, b, c, x[10], S22, 0x2441453);
  232. GG (c, d, a, b, x[15], S23, 0xd8a1e681);
  233. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
  234. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
  235. GG (d, a, b, c, x[14], S22, 0xc33707d6);
  236. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
  237. GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
  238. GG (a, b, c, d, x[13], S21, 0xa9e3e905);
  239. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
  240. GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
  241. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
  242. // Round 3
  243. HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
  244. HH (d, a, b, c, x[ 8], S32, 0x8771f681);
  245. HH (c, d, a, b, x[11], S33, 0x6d9d6122);
  246. HH (b, c, d, a, x[14], S34, 0xfde5380c);
  247. HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
  248. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
  249. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
  250. HH (b, c, d, a, x[10], S34, 0xbebfbc70);
  251. HH (a, b, c, d, x[13], S31, 0x289b7ec6);
  252. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
  253. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
  254. HH (b, c, d, a, x[ 6], S34, 0x4881d05);
  255. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
  256. HH (d, a, b, c, x[12], S32, 0xe6db99e5);
  257. HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
  258. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
  259. // Round 4
  260. II (a, b, c, d, x[ 0], S41, 0xf4292244);
  261. II (d, a, b, c, x[ 7], S42, 0x432aff97);
  262. II (c, d, a, b, x[14], S43, 0xab9423a7);
  263. II (b, c, d, a, x[ 5], S44, 0xfc93a039);
  264. II (a, b, c, d, x[12], S41, 0x655b59c3);
  265. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
  266. II (c, d, a, b, x[10], S43, 0xffeff47d);
  267. II (b, c, d, a, x[ 1], S44, 0x85845dd1);
  268. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
  269. II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
  270. II (c, d, a, b, x[ 6], S43, 0xa3014314);
  271. II (b, c, d, a, x[13], S44, 0x4e0811a1);
  272. II (a, b, c, d, x[ 4], S41, 0xf7537e82);
  273. II (d, a, b, c, x[11], S42, 0xbd3af235);
  274. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
  275. II (b, c, d, a, x[ 9], S44, 0xeb86d391);
  276. m_State[0] += a;
  277. m_State[1] += b;
  278. m_State[2] += c;
  279. m_State[3] += d;
  280. memset(x, 0, sizeof(x));
  281. }
  282. // md5::Encode
  283. // Encodes input (uint4) into output (uchar). Assumes nLength is
  284. // a multiple of 4.
  285. void md5::Encode(u_char* dest, u_int4* src, u_int4 nLength)
  286. {
  287. u_int4 i, j;
  288. assert(nLength % 4 == 0);
  289. for (i = 0, j = 0; j < nLength; i++, j += 4)
  290. {
  291. dest[j] = (u_char)(src[i] & 0xff);
  292. dest[j+1] = (u_char)((src[i] >> 8) & 0xff);
  293. dest[j+2] = (u_char)((src[i] >> 16) & 0xff);
  294. dest[j+3] = (u_char)((src[i] >> 24) & 0xff);
  295. }
  296. }
  297. // md5::Decode
  298. // Decodes input (uchar) into output (uint4). Assumes nLength is
  299. // a multiple of 4.
  300. void md5::Decode(u_int4* dest, u_char* src, u_int4 nLength)
  301. {
  302. u_int4 i, j;
  303. assert(nLength % 4 == 0);
  304. for (i = 0, j = 0; j < nLength; i++, j += 4)
  305. {
  306. dest[i] = ((u_int4)src[j]) | (((u_int4)src[j+1])<<8) |
  307. (((u_int4)src[j+2])<<16) | (((u_int4)src[j+3])<<24);
  308. }
  309. }