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.

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