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.

428 lines
11 KiB

  1. //
  2. // Copyright (c) 1998-1999, Microsoft Corporation, all rights reserved
  3. //
  4. // md5.c
  5. //
  6. // IEEE1394 mini-port/call-manager driver
  7. //
  8. // Mini-port routines
  9. //
  10. // 08/08/2000 ADube created.
  11. //
  12. // Purpose: Create a unique MAC address from 1394 EUID
  13. //
  14. // Derived from derived from the RSA Data Security,
  15. // Inc. MD5 Message-Digest Algorithm
  16. //
  17. #include <precomp.h>
  18. #pragma hdrstop
  19. #include "md5.h"
  20. // Constants for MD5Transform routine.
  21. #define S11 7
  22. #define S12 12
  23. #define S13 17
  24. #define S14 22
  25. #define S21 5
  26. #define S22 9
  27. #define S23 14
  28. #define S24 20
  29. #define S31 4
  30. #define S32 11
  31. #define S33 16
  32. #define S34 23
  33. #define S41 6
  34. #define S42 10
  35. #define S43 15
  36. #define S44 21
  37. static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  38. static void Encode PROTO_LIST
  39. ((unsigned char *, UINT4 *, unsigned int));
  40. static void Decode PROTO_LIST
  41. ((UINT4 *, unsigned char *, unsigned int));
  42. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  43. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  44. static unsigned char PADDING[64] = {
  45. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  48. };
  49. // F, G, H and I are basic MD5 functions.
  50. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  51. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  52. #define H(x, y, z) ((x) ^ (y) ^ (z))
  53. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  54. // ROTATE_LEFT rotates x left n bits.
  55. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  56. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  57. // Rotation is separate from addition to prevent recomputation.
  58. #define FF(a, b, c, d, x, s, ac) { \
  59. (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  60. (a) = ROTATE_LEFT ((a), (s)); \
  61. (a) += (b); \
  62. }
  63. #define GG(a, b, c, d, x, s, ac) { \
  64. (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  65. (a) = ROTATE_LEFT ((a), (s)); \
  66. (a) += (b); \
  67. }
  68. #define HH(a, b, c, d, x, s, ac) { \
  69. (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  70. (a) = ROTATE_LEFT ((a), (s)); \
  71. (a) += (b); \
  72. }
  73. #define II(a, b, c, d, x, s, ac) { \
  74. (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  75. (a) = ROTATE_LEFT ((a), (s)); \
  76. (a) += (b); \
  77. }
  78. // MD5 initialization. Begins an MD5 operation, writing a new context.
  79. void
  80. MD5Init (
  81. MD5_CTX *context// context
  82. )
  83. {
  84. context->count[0] = context->count[1] = 0;
  85. // Load magic initialization constants.
  86. context->state[0] = 0x67452301;
  87. context->state[1] = 0xefcdab89;
  88. context->state[2] = 0x98badcfe;
  89. context->state[3] = 0x10325476;
  90. }
  91. // MD5 block update operation. Continues an MD5 message-digest
  92. // operation, processing another message block, and updating the
  93. // context.
  94. void
  95. MD5Update (
  96. MD5_CTX *context, // context
  97. unsigned char *input, // input block
  98. unsigned int inputLen // length of input block
  99. )
  100. {
  101. unsigned int i, index, partLen;
  102. // Compute number of bytes mod 64
  103. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  104. // Update number of bits
  105. if ((context->count[0] += ((UINT4)inputLen << 3))
  106. < ((UINT4)inputLen << 3))
  107. {
  108. context->count[1]++;
  109. }
  110. context->count[1] += ((UINT4)inputLen >> 29);
  111. partLen = 64 - index;
  112. // Transform as many times as possible.
  113. if (inputLen >= partLen)
  114. {
  115. MD5_memcpy ((POINTER)&context->buffer[index],
  116. (POINTER)input,
  117. partLen);
  118. MD5Transform (context->state, context->buffer);
  119. for (i = partLen; i + 63 < inputLen; i += 64)
  120. {
  121. MD5Transform (context->state, &input[i]);
  122. }
  123. index = 0;
  124. }
  125. else
  126. {
  127. i = 0;
  128. }
  129. // Buffer remaining input
  130. MD5_memcpy
  131. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  132. inputLen-i);
  133. }
  134. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  135. // the message digest and zeroizing the context.
  136. void
  137. MD5Final (
  138. unsigned char digest[16], // message digest
  139. MD5_CTX *context
  140. ) // context
  141. {
  142. unsigned char bits[8];
  143. unsigned int index, padLen;
  144. // Save number of bits
  145. Encode (bits, context->count, 8);
  146. // Pad out to 56 mod 64.
  147. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  148. padLen = (index < 56) ? (56 - index) : (120 - index);
  149. MD5Update (context, PADDING, padLen);
  150. // Append length (before padding)
  151. MD5Update (context, bits, 8);
  152. // Store state in digest
  153. Encode (digest, context->state, 6);
  154. // Zeroize sensitive information.
  155. MD5_memset ((POINTER)context, 0, sizeof (*context));
  156. }
  157. // MD5 basic transformation. Transforms state based on block.
  158. static
  159. void
  160. MD5Transform (
  161. UINT4 state[4],
  162. unsigned char block[64]
  163. )
  164. {
  165. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  166. Decode (x, block, 64);
  167. // Round 1
  168. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); // 1
  169. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); // 2
  170. FF (c, d, a, b, x[ 2], S13, 0x242070db); // 3
  171. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); // 4
  172. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); // 5
  173. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); // 6
  174. FF (c, d, a, b, x[ 6], S13, 0xa8304613); // 7
  175. FF (b, c, d, a, x[ 7], S14, 0xfd469501); // 8
  176. FF (a, b, c, d, x[ 8], S11, 0x698098d8); // 9
  177. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); // 10
  178. FF (c, d, a, b, x[10], S13, 0xffff5bb1); // 11
  179. FF (b, c, d, a, x[11], S14, 0x895cd7be); // 12
  180. FF (a, b, c, d, x[12], S11, 0x6b901122); // 13
  181. FF (d, a, b, c, x[13], S12, 0xfd987193); // 14
  182. FF (c, d, a, b, x[14], S13, 0xa679438e); // 15
  183. FF (b, c, d, a, x[15], S14, 0x49b40821); // 16
  184. // Round 2
  185. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); // 17
  186. GG (d, a, b, c, x[ 6], S22, 0xc040b340); // 18
  187. GG (c, d, a, b, x[11], S23, 0x265e5a51); // 19
  188. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); // 20
  189. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); // 21
  190. GG (d, a, b, c, x[10], S22, 0x2441453); // 22
  191. GG (c, d, a, b, x[15], S23, 0xd8a1e681); // 23
  192. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); // 24
  193. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); // 25
  194. GG (d, a, b, c, x[14], S22, 0xc33707d6); // 26
  195. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); // 27
  196. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); // 28
  197. GG (a, b, c, d, x[13], S21, 0xa9e3e905); // 29
  198. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); // 30
  199. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); // 31
  200. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); // 32
  201. // Round 3
  202. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); // 33
  203. HH (d, a, b, c, x[ 8], S32, 0x8771f681); // 34
  204. HH (c, d, a, b, x[11], S33, 0x6d9d6122); // 35
  205. HH (b, c, d, a, x[14], S34, 0xfde5380c); // 36
  206. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); // 37
  207. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); // 38
  208. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); // 39
  209. HH (b, c, d, a, x[10], S34, 0xbebfbc70); // 40
  210. HH (a, b, c, d, x[13], S31, 0x289b7ec6); // 41
  211. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); // 42
  212. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); // 43
  213. HH (b, c, d, a, x[ 6], S34, 0x4881d05); // 44
  214. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); // 45
  215. HH (d, a, b, c, x[12], S32, 0xe6db99e5); // 46
  216. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); // 47
  217. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); // 48
  218. // Round 4
  219. II (a, b, c, d, x[ 0], S41, 0xf4292244); // 49
  220. II (d, a, b, c, x[ 7], S42, 0x432aff97); // 50
  221. II (c, d, a, b, x[14], S43, 0xab9423a7); // 51
  222. II (b, c, d, a, x[ 5], S44, 0xfc93a039); // 52
  223. II (a, b, c, d, x[12], S41, 0x655b59c3); // 53
  224. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); // 54
  225. II (c, d, a, b, x[10], S43, 0xffeff47d); // 55
  226. II (b, c, d, a, x[ 1], S44, 0x85845dd1); // 56
  227. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); // 57
  228. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); // 58
  229. II (c, d, a, b, x[ 6], S43, 0xa3014314); // 59
  230. II (b, c, d, a, x[13], S44, 0x4e0811a1); // 60
  231. II (a, b, c, d, x[ 4], S41, 0xf7537e82); // 61
  232. II (d, a, b, c, x[11], S42, 0xbd3af235); // 62
  233. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); // 63
  234. II (b, c, d, a, x[ 9], S44, 0xeb86d391); // 64
  235. state[0] += a;
  236. state[1] += b;
  237. state[2] += c;
  238. state[3] += d;
  239. // Zeroize sensitive information.
  240. MD5_memset ((POINTER)x, 0, sizeof (x));
  241. }
  242. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  243. // a multiple of 4.
  244. static
  245. void
  246. Encode (
  247. unsigned char *output,
  248. UINT4 *input,
  249. unsigned int len
  250. )
  251. {
  252. unsigned int i, j;
  253. for (i = 0, j = 0; j < len; i++, j += 4)
  254. {
  255. output[j] = (unsigned char)(input[i] & 0xff);
  256. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  257. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  258. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  259. }
  260. }
  261. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  262. // a multiple of 4.
  263. static
  264. void
  265. Decode (
  266. UINT4 *output,
  267. unsigned char *input,
  268. unsigned int len
  269. )
  270. {
  271. unsigned int i, j;
  272. for (i = 0, j = 0; j < len; i++, j += 4)
  273. {
  274. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  275. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  276. }
  277. }
  278. // Note: Replace "for loop" with standard memcpy if possible.
  279. static
  280. void
  281. MD5_memcpy (
  282. POINTER output,
  283. POINTER input,
  284. unsigned int len
  285. )
  286. {
  287. unsigned int i;
  288. for (i = 0; i < len; i++)
  289. {
  290. output[i] = input[i];
  291. }
  292. }
  293. // Note: Replace "for loop" with standard memset if possible.
  294. static
  295. void
  296. MD5_memset (
  297. POINTER output,
  298. int value,
  299. unsigned int len
  300. )
  301. {
  302. unsigned int i;
  303. for (i = 0; i < len; i++)
  304. {
  305. ((char *)output)[i] = (char)value;
  306. }
  307. }
  308. #define ETH_IS_MULTICAST(Address) \
  309. (BOOLEAN)(((PUCHAR)(Address))[0] & ((UCHAR)0x01))
  310. VOID
  311. nicGetMacAddressFromEuid (
  312. UINT64 *pEuid,
  313. MAC_ADDRESS *pMacAddr
  314. )
  315. {
  316. MD_CTX context;
  317. unsigned char digest[6];
  318. unsigned int len = 8;
  319. MD5Init (&context);
  320. MD5Update (&context, (unsigned char*)pEuid, len);
  321. MD5Final (digest, &context);
  322. NdisMoveMemory (pMacAddr, digest, 6);
  323. // Set the locally administered bit
  324. // and clear the multicast bit.
  325. //
  326. // randomize the returned Mac Address
  327. // by xor ing the address with a random
  328. // 0xf22f617c91e0 (a random number)
  329. //
  330. //pMacAddr->addr[0] ^= 0x00;
  331. pMacAddr->addr[0] |= 0x2;
  332. pMacAddr->addr[0] &= 0xf2;
  333. pMacAddr->addr[1] ^= 0x2f;
  334. pMacAddr->addr[2] ^= 0x61;
  335. pMacAddr->addr[3] ^= 0x7c;
  336. pMacAddr->addr[4] ^= 0x91;
  337. pMacAddr->addr[5] ^= 0x30;
  338. }
  339. // Digests a string and prints the result.
  340. VOID
  341. nicGetFakeMacAddress(
  342. UINT64 *pEuid,
  343. MAC_ADDRESS *pMacAddr
  344. )
  345. {
  346. nicGetMacAddressFromEuid (pEuid, pMacAddr);
  347. }