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.

286 lines
10 KiB

  1. /*++
  2. Copyright (c) 2001-2010 Microsoft Corporation
  3. Module Name:
  4. Md5.c
  5. Abstract:
  6. MD5 functions implementations.
  7. Author:
  8. [ported by] Sanjay Kaniyar (sanjayka) 20-Oct-2001
  9. Revision History:
  10. /***********************************************************************
  11. ** md5.h -- Header file for implementation of MD5 **
  12. ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
  13. ** Created: 2/17/90 RLR **
  14. ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version **
  15. ** Revised (for MD5): RLR 4/27/91 **
  16. ** -- G modified to have y&~z instead of y&z **
  17. ** -- FF, GG, HH modified to add in last register done **
  18. ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
  19. ** -- distinct additive constant for each step **
  20. ** -- round 4 added, working mod 7 **
  21. **********************************************************************
  22. */
  23. /*
  24. **********************************************************************
  25. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  26. ** **
  27. ** License to copy and use this software is granted provided that **
  28. ** it is identified as the "RSA Data Security, Inc. MD5 Message **
  29. ** Digest Algorithm" in all material mentioning or referencing this **
  30. ** software or this function. **
  31. ** **
  32. ** License is also granted to make and use derivative works **
  33. ** provided that such works are identified as "derived from the RSA **
  34. ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
  35. ** material mentioning or referencing the derived work. **
  36. ** **
  37. ** RSA Data Security, Inc. makes no representations concerning **
  38. ** either the merchantability of this software or the suitability **
  39. ** of this software for any particular purpose. It is provided "as **
  40. ** is" without express or implied warranty of any kind. **
  41. ** **
  42. ** These notices must be retained in any copies of any part of this **
  43. ** documentation and/or software. **
  44. **********************************************************************
  45. */
  46. #include "precomp.h"
  47. #include "md5.h"
  48. //
  49. // Definitions required for each round of operation.
  50. //
  51. #define S11 7
  52. #define S12 12
  53. #define S13 17
  54. #define S14 22
  55. #define S21 5
  56. #define S22 9
  57. #define S23 14
  58. #define S24 20
  59. #define S31 4
  60. #define S32 11
  61. #define S33 16
  62. #define S34 23
  63. #define S41 6
  64. #define S42 10
  65. #define S43 15
  66. #define S44 21
  67. //
  68. // F, G and H are basic MD5 functions: selection, majority, parity.
  69. //
  70. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  71. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  72. #define H(x, y, z) ((x) ^ (y) ^ (z))
  73. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  74. //
  75. // ROTATE_LEFT rotates x left n bits.
  76. //
  77. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  78. //
  79. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  80. // Rotation is separate from addition to prevent recomputation.
  81. //
  82. #define FF(a, b, c, d, x, s, ac) \
  83. {(a) += F ((b), (c), (d)) + (x) + (UINT32)(ac); \
  84. (a) = ROTATE_LEFT ((a), (s)); \
  85. (a) += (b); \
  86. }
  87. #define GG(a, b, c, d, x, s, ac) \
  88. {(a) += G ((b), (c), (d)) + (x) + (UINT32)(ac); \
  89. (a) = ROTATE_LEFT ((a), (s)); \
  90. (a) += (b); \
  91. }
  92. #define HH(a, b, c, d, x, s, ac) \
  93. {(a) += H ((b), (c), (d)) + (x) + (UINT32)(ac); \
  94. (a) = ROTATE_LEFT ((a), (s)); \
  95. (a) += (b); \
  96. }
  97. #define II(a, b, c, d, x, s, ac) \
  98. {(a) += I ((b), (c), (d)) + (x) + (UINT32)(ac); \
  99. (a) = ROTATE_LEFT ((a), (s)); \
  100. (a) += (b); \
  101. }
  102. VOID
  103. MD5Init (
  104. PMD5_CONTEXT Md5Context,
  105. PULONG InitialRandomNumberList
  106. )
  107. /*++
  108. Routine Description:
  109. Initialization function for MD5 computations. Needs to be called only once
  110. to initiazlie the scratch memory. Also, this initializes the unused space in
  111. the data portion (over which MD5 transform is computed) to random values
  112. rather than leaving it 0.
  113. Arguments:
  114. Md5Context - Md5 context that will be initialized by this function.
  115. InitialRandomNumberList - List of 16 random numbers generated at boot.
  116. Return Value:
  117. None.
  118. --*/
  119. {
  120. //
  121. // Load magic initialization constants.
  122. //
  123. Md5Context->Scratch[0] = (UINT32)0x67452301;
  124. Md5Context->Scratch[1] = (UINT32)0xefcdab89;
  125. Md5Context->Scratch[2] = (UINT32)0x98badcfe;
  126. Md5Context->Scratch[3] = (UINT32)0x10325476;
  127. //
  128. // Load the Initial Random Numbers.
  129. //
  130. RtlCopyMemory(&Md5Context->Data, InitialRandomNumberList,
  131. sizeof(ULONG)*MD5_DATA_LENGTH);
  132. //
  133. // Last 2 ULONGs should store the length in bits. Since we are using 14
  134. // ULONGs, it is (14*4*8) bits.
  135. //
  136. Md5Context->Data[MD5_DATA_LENGTH-2] = 14*4*8;
  137. Md5Context->Data[MD5_DATA_LENGTH-1] = 0;
  138. }
  139. ULONG
  140. ComputeMd5Transform(
  141. PMD5_CONTEXT MD5Context
  142. )
  143. /*++
  144. Routine Description:
  145. Initialization function for MD5 computations. Note that this has been
  146. changed somewhat from the original code to not change the scratch
  147. values after each invocation to save re-initialization.
  148. Arguments:
  149. Md5Context - Md5 context. Scratch space stores the MD5 initialization
  150. values and the buffer contains the data (invariants of a TCP
  151. connection) over which the hash has to be computed.
  152. Return Value:
  153. 32 bits of hash value.
  154. --*/
  155. {
  156. UINT32 a = MD5Context->Scratch[0], b = MD5Context->Scratch[1],
  157. c = MD5Context->Scratch[2], d = MD5Context->Scratch[3];
  158. //
  159. // Round 1
  160. //
  161. FF ( a, b, c, d, MD5Context->Data[ 0], S11, 3614090360);
  162. FF ( d, a, b, c, MD5Context->Data[ 1], S12, 3905402710);
  163. FF ( c, d, a, b, MD5Context->Data[ 2], S13, 606105819);
  164. FF ( b, c, d, a, MD5Context->Data[ 3], S14, 3250441966);
  165. FF ( a, b, c, d, MD5Context->Data[ 4], S11, 4118548399);
  166. FF ( d, a, b, c, MD5Context->Data[ 5], S12, 1200080426);
  167. FF ( c, d, a, b, MD5Context->Data[ 6], S13, 2821735955);
  168. FF ( b, c, d, a, MD5Context->Data[ 7], S14, 4249261313);
  169. FF ( a, b, c, d, MD5Context->Data[ 8], S11, 1770035416);
  170. FF ( d, a, b, c, MD5Context->Data[ 9], S12, 2336552879);
  171. FF ( c, d, a, b, MD5Context->Data[10], S13, 4294925233);
  172. FF ( b, c, d, a, MD5Context->Data[11], S14, 2304563134);
  173. FF ( a, b, c, d, MD5Context->Data[12], S11, 1804603682);
  174. FF ( d, a, b, c, MD5Context->Data[13], S12, 4254626195);
  175. FF ( c, d, a, b, MD5Context->Data[14], S13, 2792965006);
  176. FF ( b, c, d, a, MD5Context->Data[15], S14, 1236535329);
  177. //
  178. // Round 2
  179. //
  180. GG ( a, b, c, d, MD5Context->Data[ 1], S21, 4129170786);
  181. GG ( d, a, b, c, MD5Context->Data[ 6], S22, 3225465664);
  182. GG ( c, d, a, b, MD5Context->Data[11], S23, 643717713);
  183. GG ( b, c, d, a, MD5Context->Data[ 0], S24, 3921069994);
  184. GG ( a, b, c, d, MD5Context->Data[ 5], S21, 3593408605);
  185. GG ( d, a, b, c, MD5Context->Data[10], S22, 38016083);
  186. GG ( c, d, a, b, MD5Context->Data[15], S23, 3634488961);
  187. GG ( b, c, d, a, MD5Context->Data[ 4], S24, 3889429448);
  188. GG ( a, b, c, d, MD5Context->Data[ 9], S21, 568446438);
  189. GG ( d, a, b, c, MD5Context->Data[14], S22, 3275163606);
  190. GG ( c, d, a, b, MD5Context->Data[ 3], S23, 4107603335);
  191. GG ( b, c, d, a, MD5Context->Data[ 8], S24, 1163531501);
  192. GG ( a, b, c, d, MD5Context->Data[13], S21, 2850285829);
  193. GG ( d, a, b, c, MD5Context->Data[ 2], S22, 4243563512);
  194. GG ( c, d, a, b, MD5Context->Data[ 7], S23, 1735328473);
  195. GG ( b, c, d, a, MD5Context->Data[12], S24, 2368359562);
  196. //
  197. // Round 3
  198. //
  199. HH ( a, b, c, d, MD5Context->Data[ 5], S31, 4294588738);
  200. HH ( d, a, b, c, MD5Context->Data[ 8], S32, 2272392833);
  201. HH ( c, d, a, b, MD5Context->Data[11], S33, 1839030562);
  202. HH ( b, c, d, a, MD5Context->Data[14], S34, 4259657740);
  203. HH ( a, b, c, d, MD5Context->Data[ 1], S31, 2763975236);
  204. HH ( d, a, b, c, MD5Context->Data[ 4], S32, 1272893353);
  205. HH ( c, d, a, b, MD5Context->Data[ 7], S33, 4139469664);
  206. HH ( b, c, d, a, MD5Context->Data[10], S34, 3200236656);
  207. HH ( a, b, c, d, MD5Context->Data[13], S31, 681279174);
  208. HH ( d, a, b, c, MD5Context->Data[ 0], S32, 3936430074);
  209. HH ( c, d, a, b, MD5Context->Data[ 3], S33, 3572445317);
  210. HH ( b, c, d, a, MD5Context->Data[ 6], S34, 76029189);
  211. HH ( a, b, c, d, MD5Context->Data[ 9], S31, 3654602809);
  212. HH ( d, a, b, c, MD5Context->Data[12], S32, 3873151461);
  213. HH ( c, d, a, b, MD5Context->Data[15], S33, 530742520);
  214. HH ( b, c, d, a, MD5Context->Data[ 2], S34, 3299628645);
  215. //
  216. // Round 4
  217. //
  218. II ( a, b, c, d, MD5Context->Data[ 0], S41, 4096336452);
  219. II ( d, a, b, c, MD5Context->Data[ 7], S42, 1126891415);
  220. II ( c, d, a, b, MD5Context->Data[14], S43, 2878612391);
  221. II ( b, c, d, a, MD5Context->Data[ 5], S44, 4237533241);
  222. II ( a, b, c, d, MD5Context->Data[12], S41, 1700485571);
  223. II ( d, a, b, c, MD5Context->Data[ 3], S42, 2399980690);
  224. II ( c, d, a, b, MD5Context->Data[10], S43, 4293915773);
  225. II ( b, c, d, a, MD5Context->Data[ 1], S44, 2240044497);
  226. II ( a, b, c, d, MD5Context->Data[ 8], S41, 1873313359);
  227. II ( d, a, b, c, MD5Context->Data[15], S42, 4264355552);
  228. II ( c, d, a, b, MD5Context->Data[ 6], S43, 2734768916);
  229. II ( b, c, d, a, MD5Context->Data[13], S44, 1309151649);
  230. II ( a, b, c, d, MD5Context->Data[ 4], S41, 4149444226);
  231. II ( d, a, b, c, MD5Context->Data[11], S42, 3174756917);
  232. II ( c, d, a, b, MD5Context->Data[ 2], S43, 718787259);
  233. II ( b, c, d, a, MD5Context->Data[ 9], S44, 3951481745);
  234. return (MD5Context->Scratch[0] + a);
  235. }