Leaked source code of windows server 2003
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.

322 lines
12 KiB

  1. /*
  2. ***********************************************************************
  3. ** md5.c **
  4. ** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
  5. ** Created: 2/17/90 RLR **
  6. ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
  7. ***********************************************************************
  8. */
  9. /*
  10. ***********************************************************************
  11. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  12. ** **
  13. ** License to copy and use this software is granted provided that **
  14. ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
  15. ** Digest Algorithm" in all material mentioning or referencing this **
  16. ** software or this function. **
  17. ** **
  18. ** License is also granted to make and use derivative works **
  19. ** provided that such works are identified as "derived from the RSA **
  20. ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
  21. ** material mentioning or referencing the derived work. **
  22. ** **
  23. ** RSA Data Security, Inc. makes no representations concerning **
  24. ** either the merchantability of this software or the suitability **
  25. ** of this software for any particular purpose. It is provided "as **
  26. ** is" without express or implied warranty of any kind. **
  27. ** **
  28. ** These notices must be retained in any copies of any part of this **
  29. ** documentation and/or software. **
  30. ***********************************************************************
  31. */
  32. // Portions copyright (c) 1992 Microsoft Corp.
  33. // All rights reserved
  34. //
  35. // This copy of md5.c modified and adapted for my purposes, tommcg 6/28/96
  36. // Copyright (C) 1996-1999, Microsoft Corporation.
  37. //
  38. #include "md5.h"
  39. #ifndef PCUCHAR
  40. typedef const unsigned char * PCUCHAR;
  41. #endif
  42. #ifndef PCULONG
  43. typedef const unsigned long * PCULONG;
  44. #endif
  45. #include <stdlib.h> /* _rotl */
  46. #include <memory.h> /* memcpy, memset */
  47. #pragma intrinsic(memcpy, memset)
  48. /* Constants for Transform routine. */
  49. #define S11 7
  50. #define S12 12
  51. #define S13 17
  52. #define S14 22
  53. #define S21 5
  54. #define S22 9
  55. #define S23 14
  56. #define S24 20
  57. #define S31 4
  58. #define S32 11
  59. #define S33 16
  60. #define S34 23
  61. #define S41 6
  62. #define S42 10
  63. #define S43 15
  64. #define S44 21
  65. /* ROTATE_LEFT rotates x left n bits. */
  66. #define ROTATE_LEFT(x, n) ((x << n) | (x >> (32 - n)))
  67. //
  68. // Intel and PowerPC both have a hardware rotate instruction with intrinsic
  69. // (inline) function for them. Rough measurements show a 25% speed increase
  70. // for Intel and 10% speed increase for PowerPC when using the instrinsic
  71. // rotate versus the above defined shift/shift/or implemenation.
  72. //
  73. #if defined(_M_IX86) || defined(_M_PPC)
  74. #undef ROTATE_LEFT
  75. #define ROTATE_LEFT(x, n) _rotl(x, n)
  76. #pragma intrinsic(_rotl)
  77. #endif
  78. /* F, G and H are basic MD5 functions */
  79. #define F(x, y, z) ((x & y) | (~x & z))
  80. #define G(x, y, z) ((x & z) | (y & ~z))
  81. #define H(x, y, z) (x ^ y ^ z)
  82. #define I(x, y, z) (y ^ (x | ~z))
  83. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  84. /* Rotation is separate from addition to prevent recomputation */
  85. #define FF(a, b, c, d, x, s, ac) \
  86. a += F(b, c, d) + x + ac; \
  87. a = ROTATE_LEFT(a, s); \
  88. a += b;
  89. #define GG(a, b, c, d, x, s, ac) \
  90. a += G(b, c, d) + x + ac; \
  91. a = ROTATE_LEFT(a, s); \
  92. a += b;
  93. #define HH(a, b, c, d, x, s, ac) \
  94. a += H(b, c, d) + x + ac; \
  95. a = ROTATE_LEFT(a, s); \
  96. a += b;
  97. #define II(a, b, c, d, x, s, ac) \
  98. a += I(b, c, d) + x + ac; \
  99. a = ROTATE_LEFT(a, s); \
  100. a += b;
  101. VOID
  102. InitMD5(
  103. IN OUT PMD5_HASH HashValue
  104. )
  105. {
  106. HashValue->Word32[ 0 ] = 0x67452301;
  107. HashValue->Word32[ 1 ] = 0xefcdab89;
  108. HashValue->Word32[ 2 ] = 0x98badcfe;
  109. HashValue->Word32[ 3 ] = 0x10325476;
  110. }
  111. VOID
  112. UpdateMD5_64ByteChunk(
  113. IN OUT PMD5_HASH HashValue, // existing hash value
  114. IN PCVOID DataChunk // ULONG-aligned pointer to 64-byte message chunk
  115. )
  116. {
  117. PCULONG MessageWord32 = DataChunk;
  118. ULONG a = HashValue->Word32[ 0 ];
  119. ULONG b = HashValue->Word32[ 1 ];
  120. ULONG c = HashValue->Word32[ 2 ];
  121. ULONG d = HashValue->Word32[ 3 ];
  122. /* Round 1 */
  123. FF ( a, b, c, d, MessageWord32[ 0 ], S11, 0xd76aa478 ) /* 1 */
  124. FF ( d, a, b, c, MessageWord32[ 1 ], S12, 0xe8c7b756 ) /* 2 */
  125. FF ( c, d, a, b, MessageWord32[ 2 ], S13, 0x242070db ) /* 3 */
  126. FF ( b, c, d, a, MessageWord32[ 3 ], S14, 0xc1bdceee ) /* 4 */
  127. FF ( a, b, c, d, MessageWord32[ 4 ], S11, 0xf57c0faf ) /* 5 */
  128. FF ( d, a, b, c, MessageWord32[ 5 ], S12, 0x4787c62a ) /* 6 */
  129. FF ( c, d, a, b, MessageWord32[ 6 ], S13, 0xa8304613 ) /* 7 */
  130. FF ( b, c, d, a, MessageWord32[ 7 ], S14, 0xfd469501 ) /* 8 */
  131. FF ( a, b, c, d, MessageWord32[ 8 ], S11, 0x698098d8 ) /* 9 */
  132. FF ( d, a, b, c, MessageWord32[ 9 ], S12, 0x8b44f7af ) /* 10 */
  133. FF ( c, d, a, b, MessageWord32[ 10 ], S13, 0xffff5bb1 ) /* 11 */
  134. FF ( b, c, d, a, MessageWord32[ 11 ], S14, 0x895cd7be ) /* 12 */
  135. FF ( a, b, c, d, MessageWord32[ 12 ], S11, 0x6b901122 ) /* 13 */
  136. FF ( d, a, b, c, MessageWord32[ 13 ], S12, 0xfd987193 ) /* 14 */
  137. FF ( c, d, a, b, MessageWord32[ 14 ], S13, 0xa679438e ) /* 15 */
  138. FF ( b, c, d, a, MessageWord32[ 15 ], S14, 0x49b40821 ) /* 16 */
  139. /* Round 2 */
  140. GG ( a, b, c, d, MessageWord32[ 1 ], S21, 0xf61e2562 ) /* 17 */
  141. GG ( d, a, b, c, MessageWord32[ 6 ], S22, 0xc040b340 ) /* 18 */
  142. GG ( c, d, a, b, MessageWord32[ 11 ], S23, 0x265e5a51 ) /* 19 */
  143. GG ( b, c, d, a, MessageWord32[ 0 ], S24, 0xe9b6c7aa ) /* 20 */
  144. GG ( a, b, c, d, MessageWord32[ 5 ], S21, 0xd62f105d ) /* 21 */
  145. GG ( d, a, b, c, MessageWord32[ 10 ], S22, 0x02441453 ) /* 22 */
  146. GG ( c, d, a, b, MessageWord32[ 15 ], S23, 0xd8a1e681 ) /* 23 */
  147. GG ( b, c, d, a, MessageWord32[ 4 ], S24, 0xe7d3fbc8 ) /* 24 */
  148. GG ( a, b, c, d, MessageWord32[ 9 ], S21, 0x21e1cde6 ) /* 25 */
  149. GG ( d, a, b, c, MessageWord32[ 14 ], S22, 0xc33707d6 ) /* 26 */
  150. GG ( c, d, a, b, MessageWord32[ 3 ], S23, 0xf4d50d87 ) /* 27 */
  151. GG ( b, c, d, a, MessageWord32[ 8 ], S24, 0x455a14ed ) /* 28 */
  152. GG ( a, b, c, d, MessageWord32[ 13 ], S21, 0xa9e3e905 ) /* 29 */
  153. GG ( d, a, b, c, MessageWord32[ 2 ], S22, 0xfcefa3f8 ) /* 30 */
  154. GG ( c, d, a, b, MessageWord32[ 7 ], S23, 0x676f02d9 ) /* 31 */
  155. GG ( b, c, d, a, MessageWord32[ 12 ], S24, 0x8d2a4c8a ) /* 32 */
  156. /* Round 3 */
  157. HH ( a, b, c, d, MessageWord32[ 5 ], S31, 0xfffa3942 ) /* 33 */
  158. HH ( d, a, b, c, MessageWord32[ 8 ], S32, 0x8771f681 ) /* 34 */
  159. HH ( c, d, a, b, MessageWord32[ 11 ], S33, 0x6d9d6122 ) /* 35 */
  160. HH ( b, c, d, a, MessageWord32[ 14 ], S34, 0xfde5380c ) /* 36 */
  161. HH ( a, b, c, d, MessageWord32[ 1 ], S31, 0xa4beea44 ) /* 37 */
  162. HH ( d, a, b, c, MessageWord32[ 4 ], S32, 0x4bdecfa9 ) /* 38 */
  163. HH ( c, d, a, b, MessageWord32[ 7 ], S33, 0xf6bb4b60 ) /* 39 */
  164. HH ( b, c, d, a, MessageWord32[ 10 ], S34, 0xbebfbc70 ) /* 40 */
  165. HH ( a, b, c, d, MessageWord32[ 13 ], S31, 0x289b7ec6 ) /* 41 */
  166. HH ( d, a, b, c, MessageWord32[ 0 ], S32, 0xeaa127fa ) /* 42 */
  167. HH ( c, d, a, b, MessageWord32[ 3 ], S33, 0xd4ef3085 ) /* 43 */
  168. HH ( b, c, d, a, MessageWord32[ 6 ], S34, 0x04881d05 ) /* 44 */
  169. HH ( a, b, c, d, MessageWord32[ 9 ], S31, 0xd9d4d039 ) /* 45 */
  170. HH ( d, a, b, c, MessageWord32[ 12 ], S32, 0xe6db99e5 ) /* 46 */
  171. HH ( c, d, a, b, MessageWord32[ 15 ], S33, 0x1fa27cf8 ) /* 47 */
  172. HH ( b, c, d, a, MessageWord32[ 2 ], S34, 0xc4ac5665 ) /* 48 */
  173. /* Round 4 */
  174. II ( a, b, c, d, MessageWord32[ 0 ], S41, 0xf4292244 ) /* 49 */
  175. II ( d, a, b, c, MessageWord32[ 7 ], S42, 0x432aff97 ) /* 50 */
  176. II ( c, d, a, b, MessageWord32[ 14 ], S43, 0xab9423a7 ) /* 51 */
  177. II ( b, c, d, a, MessageWord32[ 5 ], S44, 0xfc93a039 ) /* 52 */
  178. II ( a, b, c, d, MessageWord32[ 12 ], S41, 0x655b59c3 ) /* 53 */
  179. II ( d, a, b, c, MessageWord32[ 3 ], S42, 0x8f0ccc92 ) /* 54 */
  180. II ( c, d, a, b, MessageWord32[ 10 ], S43, 0xffeff47d ) /* 55 */
  181. II ( b, c, d, a, MessageWord32[ 1 ], S44, 0x85845dd1 ) /* 56 */
  182. II ( a, b, c, d, MessageWord32[ 8 ], S41, 0x6fa87e4f ) /* 57 */
  183. II ( d, a, b, c, MessageWord32[ 15 ], S42, 0xfe2ce6e0 ) /* 58 */
  184. II ( c, d, a, b, MessageWord32[ 6 ], S43, 0xa3014314 ) /* 59 */
  185. II ( b, c, d, a, MessageWord32[ 13 ], S44, 0x4e0811a1 ) /* 60 */
  186. II ( a, b, c, d, MessageWord32[ 4 ], S41, 0xf7537e82 ) /* 61 */
  187. II ( d, a, b, c, MessageWord32[ 11 ], S42, 0xbd3af235 ) /* 62 */
  188. II ( c, d, a, b, MessageWord32[ 2 ], S43, 0x2ad7d2bb ) /* 63 */
  189. II ( b, c, d, a, MessageWord32[ 9 ], S44, 0xeb86d391 ) /* 64 */
  190. HashValue->Word32[ 0 ] += a;
  191. HashValue->Word32[ 1 ] += b;
  192. HashValue->Word32[ 2 ] += c;
  193. HashValue->Word32[ 3 ] += d;
  194. }
  195. VOID
  196. FinalizeMD5(
  197. IN OUT PMD5_HASH HashValue,
  198. IN PCVOID RemainingData, // remaining data to hash
  199. IN ULONG RemainingBytes, // 0 <= RemainingBytes < 64
  200. IN ULONGLONG TotalBytesHashed // total bytes hashed
  201. )
  202. {
  203. union {
  204. ULONGLONG Qword[ 8 ];
  205. UCHAR Byte [ 64 ];
  206. } LocalBuffer;
  207. //
  208. // Always append a pad byte of 0x80 to the message.
  209. //
  210. // If RemainingBytes is less than (but not equal to) 56 bytes, then
  211. // the final bits hashed count will be stored in the last 8 bytes of
  212. // this 64 byte hash chunk.
  213. //
  214. // If RemainingBytes is exactly 56 bytes, the appended 0x80 pad byte
  215. // will force an extra chunk.
  216. //
  217. // If RemainingBytes is greater than or equal to 56 bytes, then the
  218. // final bits hashed count will be stored in the last 8 bytes of the
  219. // NEXT 64 byte chunk that is otherwise zeroed, so THIS chunk needs to
  220. // be zero-padded beyond the first pad byte and then hashed, then zero
  221. // the first 56 bytes of the LocalBuffer for the NEXT chunk hash.
  222. //
  223. RemainingBytes &= 63; // only care about partial frames
  224. //
  225. // Zero init local buffer.
  226. //
  227. memset( &LocalBuffer, 0, 64 );
  228. //
  229. // Append 0x80 pad byte to message.
  230. //
  231. LocalBuffer.Byte[ RemainingBytes ] = 0x80;
  232. if ( RemainingBytes > 0 ) {
  233. //
  234. // Copy remaining data bytes (0 < RemainingBytes < 64) to LocalBuffer
  235. // (remainder of LocalBuffer already zeroed except for pad byte).
  236. //
  237. memcpy( &LocalBuffer, RemainingData, RemainingBytes );
  238. if ( RemainingBytes >= 56 ) {
  239. UpdateMD5_64ByteChunk( HashValue, &LocalBuffer );
  240. memset( &LocalBuffer, 0, 56 );
  241. }
  242. }
  243. //
  244. // Number of BITS hashed goes into last 8 bytes of last chunk. This
  245. // is a 64-bit value. Note that if the number of BITS exceeds 2^64
  246. // then this number is the low order 64 bits of the result.
  247. ///
  248. LocalBuffer.Qword[ 7 ] = ( TotalBytesHashed * 8 ); // number of BITS
  249. UpdateMD5_64ByteChunk( HashValue, &LocalBuffer );
  250. }
  251. VOID
  252. ComputeCompleteMD5(
  253. IN PCVOID DataBuffer,
  254. IN ULONGLONG DataLength,
  255. OUT PMD5_HASH HashValue
  256. )
  257. {
  258. PCUCHAR DataPointer = DataBuffer;
  259. ULONGLONG ChunkCount = DataLength / 64;
  260. ULONG OddBytes = (ULONG)DataLength & 63;
  261. InitMD5( HashValue );
  262. while ( ChunkCount-- ) {
  263. UpdateMD5_64ByteChunk( HashValue, DataPointer );
  264. DataPointer += 64;
  265. }
  266. FinalizeMD5( HashValue, DataPointer, OddBytes, DataLength );
  267. }