Source code of Windows XP (NT5)
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.

302 lines
10 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. #include <nt.h>
  35. #define UINT4 ULONG
  36. #include "md5.h"
  37. /* Constants for Transform routine.
  38. */
  39. #define S11 7
  40. #define S12 12
  41. #define S13 17
  42. #define S14 22
  43. #define S21 5
  44. #define S22 9
  45. #define S23 14
  46. #define S24 20
  47. #define S31 4
  48. #define S32 11
  49. #define S33 16
  50. #define S34 23
  51. #define S41 6
  52. #define S42 10
  53. #define S43 15
  54. #define S44 21
  55. static void TransformMD5 PROTO_LIST ((UINT4 *, UINT4 *));
  56. static unsigned char PADDING[64] = {
  57. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  65. };
  66. /* F, G and H are basic MD5 functions */
  67. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  68. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  69. #define H(x, y, z) ((x) ^ (y) ^ (z))
  70. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  71. /* ROTATE_LEFT rotates x left n bits.
  72. */
  73. #if CT_COMPILER == CT_MPW
  74. /* MPW doesn't compile the macro correctly, so use a procedure. */
  75. static UINT4 RotateLeft (UINT4, unsigned int);
  76. #define ROTATE_LEFT RotateLeft
  77. #else
  78. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  79. #endif
  80. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  81. /* Rotation is separate from addition to prevent recomputation */
  82. #define FF(a, b, c, d, x, s, ac) \
  83. {(a) += F ((b), (c), (d)) + (x) + (UINT4)(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) + (UINT4)(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) + (UINT4)(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) + (UINT4)(ac); \
  99. (a) = ROTATE_LEFT ((a), (s)); \
  100. (a) += (b); \
  101. }
  102. __declspec(dllexport)
  103. void IISMD5Init (mdContext)
  104. IISMD5_CTX *mdContext;
  105. {
  106. mdContext->i[0] = mdContext->i[1] = (UINT4)0;
  107. /* Load magic initialization constants.
  108. */
  109. mdContext->buf[0] = (UINT4)0x67452301;
  110. mdContext->buf[1] = (UINT4)0xefcdab89;
  111. mdContext->buf[2] = (UINT4)0x98badcfe;
  112. mdContext->buf[3] = (UINT4)0x10325476;
  113. }
  114. __declspec(dllexport)
  115. void IISMD5Update (mdContext, inBuf, inLen)
  116. IISMD5_CTX *mdContext;
  117. unsigned char *inBuf;
  118. unsigned int inLen;
  119. {
  120. UINT4 in[16];
  121. int mdi;
  122. unsigned int i, ii;
  123. /* compute number of bytes mod 64 */
  124. mdi = (int)((mdContext->i[0] >> 3) & 0x3f);
  125. /* update number of bits */
  126. if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
  127. mdContext->i[1]++;
  128. mdContext->i[0] += ((UINT4)inLen << 3);
  129. mdContext->i[1] += ((UINT4)inLen >> 29);
  130. while (inLen--) {
  131. /* add new character to buffer, increment mdi */
  132. mdContext->in[mdi++] = *inBuf++;
  133. /* transform if necessary */
  134. if (mdi == 0x40) {
  135. for (i = 0, ii = 0; i < 16; i++, ii += 4)
  136. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  137. (((UINT4)mdContext->in[ii+2]) << 16) |
  138. (((UINT4)mdContext->in[ii+1]) << 8) |
  139. ((UINT4)mdContext->in[ii]);
  140. TransformMD5 (mdContext->buf, in);
  141. mdi = 0;
  142. }
  143. }
  144. }
  145. __declspec(dllexport)
  146. void IISMD5Final (mdContext)
  147. IISMD5_CTX *mdContext;
  148. {
  149. UINT4 in[16];
  150. int mdi;
  151. unsigned int i, ii;
  152. unsigned int padLen;
  153. /* save number of bits */
  154. in[14] = mdContext->i[0];
  155. in[15] = mdContext->i[1];
  156. /* compute number of bytes mod 64 */
  157. mdi = (int)((mdContext->i[0] >> 3) & 0x3f);
  158. /* pad out to 56 mod 64 */
  159. padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  160. IISMD5Update (mdContext, PADDING, padLen);
  161. /* append length in bits and transform */
  162. for (i = 0, ii = 0; i < 14; i++, ii += 4)
  163. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  164. (((UINT4)mdContext->in[ii+2]) << 16) |
  165. (((UINT4)mdContext->in[ii+1]) << 8) |
  166. ((UINT4)mdContext->in[ii]);
  167. TransformMD5 (mdContext->buf, in);
  168. /* store buffer in digest */
  169. for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  170. mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xff);
  171. mdContext->digest[ii+1] =
  172. (unsigned char)((mdContext->buf[i] >> 8) & 0xff);
  173. mdContext->digest[ii+2] =
  174. (unsigned char)((mdContext->buf[i] >> 16) & 0xff);
  175. mdContext->digest[ii+3] =
  176. (unsigned char)((mdContext->buf[i] >> 24) & 0xff);
  177. }
  178. }
  179. /* Basic MD5 step. Transforms buf based on in.
  180. */
  181. static void TransformMD5 (buf, in)
  182. UINT4 *buf;
  183. UINT4 *in;
  184. {
  185. UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  186. /* Round 1 */
  187. FF ( a, b, c, d, in[ 0], S11, 0xd76aa478); /* 1 */
  188. FF ( d, a, b, c, in[ 1], S12, 0xe8c7b756); /* 2 */
  189. FF ( c, d, a, b, in[ 2], S13, 0x242070db); /* 3 */
  190. FF ( b, c, d, a, in[ 3], S14, 0xc1bdceee); /* 4 */
  191. FF ( a, b, c, d, in[ 4], S11, 0xf57c0faf); /* 5 */
  192. FF ( d, a, b, c, in[ 5], S12, 0x4787c62a); /* 6 */
  193. FF ( c, d, a, b, in[ 6], S13, 0xa8304613); /* 7 */
  194. FF ( b, c, d, a, in[ 7], S14, 0xfd469501); /* 8 */
  195. FF ( a, b, c, d, in[ 8], S11, 0x698098d8); /* 9 */
  196. FF ( d, a, b, c, in[ 9], S12, 0x8b44f7af); /* 10 */
  197. FF ( c, d, a, b, in[10], S13, 0xffff5bb1); /* 11 */
  198. FF ( b, c, d, a, in[11], S14, 0x895cd7be); /* 12 */
  199. FF ( a, b, c, d, in[12], S11, 0x6b901122); /* 13 */
  200. FF ( d, a, b, c, in[13], S12, 0xfd987193); /* 14 */
  201. FF ( c, d, a, b, in[14], S13, 0xa679438e); /* 15 */
  202. FF ( b, c, d, a, in[15], S14, 0x49b40821); /* 16 */
  203. /* Round 2 */
  204. GG ( a, b, c, d, in[ 1], S21, 0xf61e2562); /* 17 */
  205. GG ( d, a, b, c, in[ 6], S22, 0xc040b340); /* 18 */
  206. GG ( c, d, a, b, in[11], S23, 0x265e5a51); /* 19 */
  207. GG ( b, c, d, a, in[ 0], S24, 0xe9b6c7aa); /* 20 */
  208. GG ( a, b, c, d, in[ 5], S21, 0xd62f105d); /* 21 */
  209. GG ( d, a, b, c, in[10], S22, 0x2441453); /* 22 */
  210. GG ( c, d, a, b, in[15], S23, 0xd8a1e681); /* 23 */
  211. GG ( b, c, d, a, in[ 4], S24, 0xe7d3fbc8); /* 24 */
  212. GG ( a, b, c, d, in[ 9], S21, 0x21e1cde6); /* 25 */
  213. GG ( d, a, b, c, in[14], S22, 0xc33707d6); /* 26 */
  214. GG ( c, d, a, b, in[ 3], S23, 0xf4d50d87); /* 27 */
  215. GG ( b, c, d, a, in[ 8], S24, 0x455a14ed); /* 28 */
  216. GG ( a, b, c, d, in[13], S21, 0xa9e3e905); /* 29 */
  217. GG ( d, a, b, c, in[ 2], S22, 0xfcefa3f8); /* 30 */
  218. GG ( c, d, a, b, in[ 7], S23, 0x676f02d9); /* 31 */
  219. GG ( b, c, d, a, in[12], S24, 0x8d2a4c8a); /* 32 */
  220. /* Round 3 */
  221. HH ( a, b, c, d, in[ 5], S31, 0xfffa3942); /* 33 */
  222. HH ( d, a, b, c, in[ 8], S32, 0x8771f681); /* 34 */
  223. HH ( c, d, a, b, in[11], S33, 0x6d9d6122); /* 35 */
  224. HH ( b, c, d, a, in[14], S34, 0xfde5380c); /* 36 */
  225. HH ( a, b, c, d, in[ 1], S31, 0xa4beea44); /* 37 */
  226. HH ( d, a, b, c, in[ 4], S32, 0x4bdecfa9); /* 38 */
  227. HH ( c, d, a, b, in[ 7], S33, 0xf6bb4b60); /* 39 */
  228. HH ( b, c, d, a, in[10], S34, 0xbebfbc70); /* 40 */
  229. HH ( a, b, c, d, in[13], S31, 0x289b7ec6); /* 41 */
  230. HH ( d, a, b, c, in[ 0], S32, 0xeaa127fa); /* 42 */
  231. HH ( c, d, a, b, in[ 3], S33, 0xd4ef3085); /* 43 */
  232. HH ( b, c, d, a, in[ 6], S34, 0x4881d05); /* 44 */
  233. HH ( a, b, c, d, in[ 9], S31, 0xd9d4d039); /* 45 */
  234. HH ( d, a, b, c, in[12], S32, 0xe6db99e5); /* 46 */
  235. HH ( c, d, a, b, in[15], S33, 0x1fa27cf8); /* 47 */
  236. HH ( b, c, d, a, in[ 2], S34, 0xc4ac5665); /* 48 */
  237. /* Round 4 */
  238. II ( a, b, c, d, in[ 0], S41, 0xf4292244); /* 49 */
  239. II ( d, a, b, c, in[ 7], S42, 0x432aff97); /* 50 */
  240. II ( c, d, a, b, in[14], S43, 0xab9423a7); /* 51 */
  241. II ( b, c, d, a, in[ 5], S44, 0xfc93a039); /* 52 */
  242. II ( a, b, c, d, in[12], S41, 0x655b59c3); /* 53 */
  243. II ( d, a, b, c, in[ 3], S42, 0x8f0ccc92); /* 54 */
  244. II ( c, d, a, b, in[10], S43, 0xffeff47d); /* 55 */
  245. II ( b, c, d, a, in[ 1], S44, 0x85845dd1); /* 56 */
  246. II ( a, b, c, d, in[ 8], S41, 0x6fa87e4f); /* 57 */
  247. II ( d, a, b, c, in[15], S42, 0xfe2ce6e0); /* 58 */
  248. II ( c, d, a, b, in[ 6], S43, 0xa3014314); /* 59 */
  249. II ( b, c, d, a, in[13], S44, 0x4e0811a1); /* 60 */
  250. II ( a, b, c, d, in[ 4], S41, 0xf7537e82); /* 61 */
  251. II ( d, a, b, c, in[11], S42, 0xbd3af235); /* 62 */
  252. II ( c, d, a, b, in[ 2], S43, 0x2ad7d2bb); /* 63 */
  253. II ( b, c, d, a, in[ 9], S44, 0xeb86d391); /* 64 */
  254. buf[0] += a;
  255. buf[1] += b;
  256. buf[2] += c;
  257. buf[3] += d;
  258. }
  259. #if CT_COMPILER == CT_MPW
  260. static UINT4 RotateLeft (x, n)
  261. UINT4 x;
  262. unsigned int n;
  263. {
  264. return (((x) << (n)) | ((x) >> (32-(n))));
  265. }
  266. #endif