md5.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //-----------------------------------------------------------------------------
  2. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  3. // rights reserved.
  4. //
  5. // License to copy and use this software is granted provided that it
  6. // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  7. // Algorithm" in all material mentioning or referencing this software
  8. // or this function.
  9. //
  10. // License is also granted to make and use derivative works provided
  11. // that such works are identified as "derived from the RSA Data
  12. // Security, Inc. MD5 Message-Digest Algorithm" in all material
  13. // mentioning or referencing the derived work.
  14. //
  15. // RSA Data Security, Inc. makes no representations concerning either
  16. // the merchantability of this software or the suitability of this
  17. // software for any particular purpose. It is provided "as is"
  18. // without express or implied warranty of any kind.
  19. //
  20. // These notices must be retained in any copies of any part of this
  21. // documentation and/or software.
  22. //-----------------------------------------------------------------------------
  23. #ifndef _MD5_H_
  24. #define _MD5_H_
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _FILESTREAM_H_
  29. #include "io/fileStream.h"
  30. #endif
  31. #ifndef _CONSOLE_H_
  32. #include "console/console.h"
  33. #endif
  34. //-----------------------------------------------------------------------------
  35. // Constants for MD5Transform routine.
  36. #define _S11 7
  37. #define _S12 12
  38. #define _S13 17
  39. #define _S14 22
  40. #define _S21 5
  41. #define _S22 9
  42. #define _S23 14
  43. #define _S24 20
  44. #define _S31 4
  45. #define _S32 11
  46. #define _S33 16
  47. #define _S34 23
  48. #define _S41 6
  49. #define _S42 10
  50. #define _S43 15
  51. #define _S44 21
  52. // F, G, H and I are basic MD5 functions.
  53. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  54. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  55. #define H(x, y, z) ((x) ^ (y) ^ (z))
  56. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  57. // ROTATE_LEFT rotates x left n bits.
  58. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  59. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  60. // Rotation is separate from addition to prevent recomputation.
  61. #define FF(a, b, c, d, x, s, ac) { \
  62. (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  63. (a) = ROTATE_LEFT ((a), (s)); \
  64. (a) += (b); \
  65. }
  66. #define GG(a, b, c, d, x, s, ac) { \
  67. (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  68. (a) = ROTATE_LEFT ((a), (s)); \
  69. (a) += (b); \
  70. }
  71. #define HH(a, b, c, d, x, s, ac) { \
  72. (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  73. (a) = ROTATE_LEFT ((a), (s)); \
  74. (a) += (b); \
  75. }
  76. #define II(a, b, c, d, x, s, ac) { \
  77. (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  78. (a) = ROTATE_LEFT ((a), (s)); \
  79. (a) += (b); \
  80. }
  81. //-----------------------------------------------------------------------------
  82. typedef unsigned char BYTE ;
  83. // POINTER defines a generic pointer type
  84. typedef unsigned char *POINTER;
  85. // UINT2 defines a two byte word
  86. typedef unsigned short int UINT2;
  87. // UINT4 defines a four byte word
  88. typedef unsigned long int UINT4;
  89. static unsigned char PADDING[64] = {
  90. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  93. };
  94. //-----------------------------------------------------------------------------
  95. // convenient object that wraps
  96. // the C-functions for use in C++ only
  97. class MD5
  98. {
  99. private:
  100. struct __context_t {
  101. UINT4 state[4]; /* state (ABCD) */
  102. UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  103. unsigned char buffer[64]; /* input buffer */
  104. } context ;
  105. #pragma region static helper functions
  106. // The core of the MD5 algorithm is here.
  107. // MD5 basic transformation. Transforms state based on block.
  108. static void MD5Transform( UINT4 state[4], unsigned char block[64] )
  109. {
  110. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  111. Decode (x, block, 64);
  112. /* Round 1 */
  113. FF (a, b, c, d, x[ 0], _S11, 0xd76aa478); /* 1 */
  114. FF (d, a, b, c, x[ 1], _S12, 0xe8c7b756); /* 2 */
  115. FF (c, d, a, b, x[ 2], _S13, 0x242070db); /* 3 */
  116. FF (b, c, d, a, x[ 3], _S14, 0xc1bdceee); /* 4 */
  117. FF (a, b, c, d, x[ 4], _S11, 0xf57c0faf); /* 5 */
  118. FF (d, a, b, c, x[ 5], _S12, 0x4787c62a); /* 6 */
  119. FF (c, d, a, b, x[ 6], _S13, 0xa8304613); /* 7 */
  120. FF (b, c, d, a, x[ 7], _S14, 0xfd469501); /* 8 */
  121. FF (a, b, c, d, x[ 8], _S11, 0x698098d8); /* 9 */
  122. FF (d, a, b, c, x[ 9], _S12, 0x8b44f7af); /* 10 */
  123. FF (c, d, a, b, x[10], _S13, 0xffff5bb1); /* 11 */
  124. FF (b, c, d, a, x[11], _S14, 0x895cd7be); /* 12 */
  125. FF (a, b, c, d, x[12], _S11, 0x6b901122); /* 13 */
  126. FF (d, a, b, c, x[13], _S12, 0xfd987193); /* 14 */
  127. FF (c, d, a, b, x[14], _S13, 0xa679438e); /* 15 */
  128. FF (b, c, d, a, x[15], _S14, 0x49b40821); /* 16 */
  129. /* Round 2 */
  130. GG (a, b, c, d, x[ 1], _S21, 0xf61e2562); /* 17 */
  131. GG (d, a, b, c, x[ 6], _S22, 0xc040b340); /* 18 */
  132. GG (c, d, a, b, x[11], _S23, 0x265e5a51); /* 19 */
  133. GG (b, c, d, a, x[ 0], _S24, 0xe9b6c7aa); /* 20 */
  134. GG (a, b, c, d, x[ 5], _S21, 0xd62f105d); /* 21 */
  135. GG (d, a, b, c, x[10], _S22, 0x2441453); /* 22 */
  136. GG (c, d, a, b, x[15], _S23, 0xd8a1e681); /* 23 */
  137. GG (b, c, d, a, x[ 4], _S24, 0xe7d3fbc8); /* 24 */
  138. GG (a, b, c, d, x[ 9], _S21, 0x21e1cde6); /* 25 */
  139. GG (d, a, b, c, x[14], _S22, 0xc33707d6); /* 26 */
  140. GG (c, d, a, b, x[ 3], _S23, 0xf4d50d87); /* 27 */
  141. GG (b, c, d, a, x[ 8], _S24, 0x455a14ed); /* 28 */
  142. GG (a, b, c, d, x[13], _S21, 0xa9e3e905); /* 29 */
  143. GG (d, a, b, c, x[ 2], _S22, 0xfcefa3f8); /* 30 */
  144. GG (c, d, a, b, x[ 7], _S23, 0x676f02d9); /* 31 */
  145. GG (b, c, d, a, x[12], _S24, 0x8d2a4c8a); /* 32 */
  146. /* Round 3 */
  147. HH (a, b, c, d, x[ 5], _S31, 0xfffa3942); /* 33 */
  148. HH (d, a, b, c, x[ 8], _S32, 0x8771f681); /* 34 */
  149. HH (c, d, a, b, x[11], _S33, 0x6d9d6122); /* 35 */
  150. HH (b, c, d, a, x[14], _S34, 0xfde5380c); /* 36 */
  151. HH (a, b, c, d, x[ 1], _S31, 0xa4beea44); /* 37 */
  152. HH (d, a, b, c, x[ 4], _S32, 0x4bdecfa9); /* 38 */
  153. HH (c, d, a, b, x[ 7], _S33, 0xf6bb4b60); /* 39 */
  154. HH (b, c, d, a, x[10], _S34, 0xbebfbc70); /* 40 */
  155. HH (a, b, c, d, x[13], _S31, 0x289b7ec6); /* 41 */
  156. HH (d, a, b, c, x[ 0], _S32, 0xeaa127fa); /* 42 */
  157. HH (c, d, a, b, x[ 3], _S33, 0xd4ef3085); /* 43 */
  158. HH (b, c, d, a, x[ 6], _S34, 0x4881d05); /* 44 */
  159. HH (a, b, c, d, x[ 9], _S31, 0xd9d4d039); /* 45 */
  160. HH (d, a, b, c, x[12], _S32, 0xe6db99e5); /* 46 */
  161. HH (c, d, a, b, x[15], _S33, 0x1fa27cf8); /* 47 */
  162. HH (b, c, d, a, x[ 2], _S34, 0xc4ac5665); /* 48 */
  163. /* Round 4 */
  164. II (a, b, c, d, x[ 0], _S41, 0xf4292244); /* 49 */
  165. II (d, a, b, c, x[ 7], _S42, 0x432aff97); /* 50 */
  166. II (c, d, a, b, x[14], _S43, 0xab9423a7); /* 51 */
  167. II (b, c, d, a, x[ 5], _S44, 0xfc93a039); /* 52 */
  168. II (a, b, c, d, x[12], _S41, 0x655b59c3); /* 53 */
  169. II (d, a, b, c, x[ 3], _S42, 0x8f0ccc92); /* 54 */
  170. II (c, d, a, b, x[10], _S43, 0xffeff47d); /* 55 */
  171. II (b, c, d, a, x[ 1], _S44, 0x85845dd1); /* 56 */
  172. II (a, b, c, d, x[ 8], _S41, 0x6fa87e4f); /* 57 */
  173. II (d, a, b, c, x[15], _S42, 0xfe2ce6e0); /* 58 */
  174. II (c, d, a, b, x[ 6], _S43, 0xa3014314); /* 59 */
  175. II (b, c, d, a, x[13], _S44, 0x4e0811a1); /* 60 */
  176. II (a, b, c, d, x[ 4], _S41, 0xf7537e82); /* 61 */
  177. II (d, a, b, c, x[11], _S42, 0xbd3af235); /* 62 */
  178. II (c, d, a, b, x[ 2], _S43, 0x2ad7d2bb); /* 63 */
  179. II (b, c, d, a, x[ 9], _S44, 0xeb86d391); /* 64 */
  180. state[0] += a;
  181. state[1] += b;
  182. state[2] += c;
  183. state[3] += d;
  184. // Zeroize sensitive information.
  185. dMemset((POINTER)x, 0, sizeof (x));
  186. }
  187. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  188. // a multiple of 4.
  189. static void Encode( unsigned char *output, UINT4 *input, unsigned int len )
  190. {
  191. unsigned int i, j;
  192. for (i = 0, j = 0; j < len; i++, j += 4) {
  193. output[j] = (unsigned char)(input[i] & 0xff);
  194. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  195. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  196. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  197. }
  198. }
  199. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  200. // a multiple of 4.
  201. static void Decode( UINT4 *output, unsigned char *input, unsigned int len )
  202. {
  203. unsigned int i, j;
  204. for (i = 0, j = 0; j < len; i++, j += 4)
  205. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  206. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  207. }
  208. #pragma endregion
  209. public:
  210. // MAIN FUNCTIONS
  211. MD5()
  212. {
  213. Init() ;
  214. }
  215. // MD5 initialization. Begins an MD5 operation, writing a new context.
  216. void Init()
  217. {
  218. context.count[0] = context.count[1] = 0;
  219. // Load magic initialization constants.
  220. context.state[0] = 0x67452301;
  221. context.state[1] = 0xefcdab89;
  222. context.state[2] = 0x98badcfe;
  223. context.state[3] = 0x10325476;
  224. }
  225. // MD5 block update operation. Continues an MD5 message-digest
  226. // operation, processing another message block, and updating the
  227. // context.
  228. void Update(
  229. unsigned char *input, // input block
  230. unsigned int inputLen ) // length of input block
  231. {
  232. unsigned int i, index, partLen;
  233. // Compute number of bytes mod 64
  234. index = (unsigned int)((context.count[0] >> 3) & 0x3F);
  235. // Update number of bits
  236. if ((context.count[0] += ((UINT4)inputLen << 3))
  237. < ((UINT4)inputLen << 3))
  238. context.count[1]++;
  239. context.count[1] += ((UINT4)inputLen >> 29);
  240. partLen = 64 - index;
  241. // Transform as many times as possible.
  242. if (inputLen >= partLen) {
  243. dMemcpy((POINTER)&context.buffer[index], (POINTER)input, partLen);
  244. MD5Transform (context.state, context.buffer);
  245. for (i = partLen; i + 63 < inputLen; i += 64)
  246. MD5Transform (context.state, &input[i]);
  247. index = 0;
  248. }
  249. else
  250. i = 0;
  251. /* Buffer remaining input */
  252. dMemcpy((POINTER)&context.buffer[index], (POINTER)&input[i], inputLen-i);
  253. }
  254. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  255. // the message digest and zeroizing the context.
  256. // Writes to digestRaw
  257. void Final()
  258. {
  259. unsigned char bits[8];
  260. unsigned int index, padLen;
  261. // Save number of bits
  262. Encode( bits, context.count, 8 );
  263. // Pad out to 56 mod 64.
  264. index = (unsigned int)((context.count[0] >> 3) & 0x3f);
  265. padLen = (index < 56) ? (56 - index) : (120 - index);
  266. Update( PADDING, padLen );
  267. // Append length (before padding)
  268. Update( bits, 8 );
  269. // Store state in digest
  270. Encode( digestRaw, context.state, 16);
  271. // Zeroize sensitive information.
  272. dMemset((POINTER)&context, 0, sizeof (context));
  273. writeToString() ;
  274. }
  275. /// Buffer must be 32+1 (nul) = 33 chars long at least
  276. void writeToString()
  277. {
  278. int pos ;
  279. for( pos = 0 ; pos < 16 ; pos++ )
  280. dSprintf( digestChars+(pos*2), sizeof(digestChars), "%02x", digestRaw[pos] ) ;
  281. }
  282. public:
  283. // an MD5 digest is a 16-byte number (32 hex digits)
  284. BYTE digestRaw[ 16 ] ;
  285. // This version of the digest is actually
  286. // a "printf'd" version of the digest.
  287. char digestChars[ 33 ] ;
  288. /// Load a file from disk and digest it
  289. // Digests a file and returns the result.
  290. char* digestFile( char *filename )
  291. {
  292. Init() ;
  293. FileStream stream;
  294. U8 buffer[1024] ;
  295. if ( !stream.open( filename, FileStream::Read ) )
  296. {
  297. Con::warnf( "MD5: Cannot open file '%s'.", filename );
  298. }
  299. else
  300. {
  301. const U32 streamSize = stream.getStreamSize();
  302. while( stream.getStatus() != Stream::EOS )
  303. {
  304. const U32 streamPosition = stream.getPosition();
  305. const S32 streamRemaining = streamSize-streamPosition;
  306. const S32 readLength = streamRemaining >= sizeof(buffer) ? sizeof(buffer) : streamRemaining;
  307. stream.read( readLength, buffer );
  308. Update( buffer, readLength ) ;
  309. }
  310. Final();
  311. stream.close();
  312. }
  313. return digestChars ;
  314. }
  315. /// Digests a byte-array already in memory
  316. char* digestMemory( BYTE *memchunk, int len )
  317. {
  318. Init() ;
  319. Update( memchunk, len ) ;
  320. Final() ;
  321. return digestChars ;
  322. }
  323. // Digests a string and prints the result.
  324. char* digestString( char *string )
  325. {
  326. Init() ;
  327. Update( (unsigned char*)string, strlen(string) ) ;
  328. Final() ;
  329. return digestChars ;
  330. }
  331. } ;
  332. #endif // _MD5_H_