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.

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