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.

228 lines
6.0 KiB

  1. /*++
  2. Copyright(c) 2000 Microsoft Corporation
  3. Module Name:
  4. license.cpp
  5. Abstract:
  6. Windows Load Balancing Service (WLBS)
  7. Code to encrypt/decrypt passwords and port rules.
  8. Author:
  9. kyrilf
  10. History:
  11. JosephJ 11/22/00 Gutted this file and folded in three constants from
  12. the now defunct license.h. Basically the functions in this
  13. file used to do lots of things but now only encrypt/decrypt
  14. port rules and passwords. The port rules stuff is only used
  15. for upgrading from olde versions of wlbs so that may go away
  16. as well.
  17. This file is located in two places:
  18. WLBS netconfig code -- net\config\netcfg\wlbscfg
  19. WLBS API code -- net\wlbs\api
  20. Because this involves password encryption, we don't want to make
  21. the functions callable via a DLL entrypoint, and setting up
  22. a static library to be shared between netconfig and api stuff is
  23. not trivial and overkill because the two trees are far apart.
  24. --*/
  25. #include <precomp.h>
  26. /* CONSTANTS */
  27. static UCHAR data_key [] =
  28. { 0x3f, 0xba, 0x6e, 0xf0, 0xe1, 0x44, 0x1b, 0x45,
  29. 0x41, 0xc4, 0x9f, 0xfb, 0x46, 0x54, 0xbc, 0x43 };
  30. static UCHAR str_key [] =
  31. { 0xdb, 0x1b, 0xac, 0x1a, 0xb9, 0xb1, 0x18, 0x03,
  32. 0x55, 0x57, 0x4a, 0x62, 0x36, 0x21, 0x7c, 0xa6 };
  33. /* Encryption and decryption routines are based on a public-domain Tiny
  34. Encryption Algorithm (TEA) by David Wheeler and Roger Needham at the
  35. Computer Laboratory of Cambridge University. For reference, please
  36. consult http://vader.brad.ac.uk/tea/tea.shtml */
  37. static VOID License_decipher (
  38. PULONG v,
  39. PULONG k)
  40. {
  41. ULONG y = v [0],
  42. z = v [1],
  43. a = k [0],
  44. b = k [1],
  45. c = k [2],
  46. d = k [3],
  47. n = 32,
  48. sum = 0xC6EF3720,
  49. delta = 0x9E3779B9;
  50. /* sum = delta<<5, in general sum = delta * n */
  51. while (n-- > 0)
  52. {
  53. z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  54. y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  55. sum -= delta;
  56. }
  57. v [0] = y; v [1] = z;
  58. } /* end License_decipher */
  59. static VOID License_encipher (
  60. PULONG v,
  61. PULONG k)
  62. {
  63. ULONG y = v [0],
  64. z = v [1],
  65. a = k [0],
  66. b = k [1],
  67. c = k [2],
  68. d = k [3],
  69. n = 32,
  70. sum = 0,
  71. delta = 0x9E3779B9;
  72. while (n-- > 0)
  73. {
  74. sum += delta;
  75. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  76. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  77. }
  78. v [0] = y; v [1] = z;
  79. } /* end License_encipher */
  80. BOOL License_data_decode (
  81. PCHAR data,
  82. ULONG len)
  83. {
  84. ULONG i;
  85. if (len % LICENSE_DATA_GRANULARITY != 0)
  86. return FALSE;
  87. for (i = 0; i < len; i += LICENSE_DATA_GRANULARITY)
  88. License_decipher ((PULONG) (data + i), (PULONG) data_key);
  89. return TRUE;
  90. } /* License_data_decode */
  91. ULONG License_string_encode (
  92. PCHAR str)
  93. {
  94. CHAR buf [LICENSE_STR_IMPORTANT_CHARS + 1];
  95. ULONG code, i;
  96. PULONG nibp;
  97. for (i = 0; i < LICENSE_STR_IMPORTANT_CHARS; i++)
  98. {
  99. if (str[i] == 0)
  100. break;
  101. buf[i] = str[i];
  102. }
  103. for (; i < LICENSE_STR_IMPORTANT_CHARS + 1; i ++)
  104. buf[i] = 0;
  105. for (i = 0; i < LICENSE_STR_NIBBLES; i ++)
  106. License_encipher ((PULONG) (buf + i * LICENSE_DATA_GRANULARITY),
  107. (PULONG) str_key);
  108. for (i = 0, code = 0; i < LICENSE_STR_NIBBLES; i ++)
  109. {
  110. nibp = (PULONG) (buf + (i * LICENSE_DATA_GRANULARITY));
  111. code ^= nibp [0] ^ nibp [1];
  112. }
  113. /* V2.2 - if password consists of the same characters - XORing nibbles
  114. above makes it go to 0 - put some recovery for that special case since
  115. we cannot modify the algorithm due to legacy issues */
  116. if (code == 0 && str [0] != 0)
  117. code = * ((PULONG) buf);
  118. return code;
  119. } /* License_string_encode */
  120. ULONG License_wstring_encode (
  121. PWCHAR str)
  122. {
  123. CHAR buf [LICENSE_STR_IMPORTANT_CHARS + 1];
  124. ULONG code, i;
  125. PULONG nibp;
  126. for (i = 0; i < LICENSE_STR_IMPORTANT_CHARS; i++)
  127. {
  128. if (str[i] == 0)
  129. break;
  130. buf[i] = (UCHAR)str[i];
  131. }
  132. for (; i < LICENSE_STR_IMPORTANT_CHARS + 1; i ++)
  133. buf[i] = 0;
  134. for (i = 0; i < LICENSE_STR_NIBBLES; i ++)
  135. License_encipher ((PULONG) (buf + i * LICENSE_DATA_GRANULARITY),
  136. (PULONG) str_key);
  137. for (i = 0, code = 0; i < LICENSE_STR_NIBBLES; i ++)
  138. {
  139. nibp = (PULONG) (buf + (i * LICENSE_DATA_GRANULARITY));
  140. code ^= nibp [0] ^ nibp [1];
  141. }
  142. /* V2.2 - if password consists of the same characters - XORing nibbles
  143. above makes it go to 0 - put some recovery for that special case since
  144. we cannot modify the algorithm due to legacy issues */
  145. if (code == 0 && str [0] != 0)
  146. code = * ((PULONG) buf);
  147. return code;
  148. } /* License_wstring_encode */
  149. BOOL License_data_encode (
  150. PCHAR data,
  151. ULONG len)
  152. {
  153. ULONG i;
  154. if (len % LICENSE_DATA_GRANULARITY != 0)
  155. return FALSE;
  156. for (i = 0; i < len; i += LICENSE_DATA_GRANULARITY)
  157. License_encipher ((PULONG) (data + i), (PULONG) data_key);
  158. return TRUE;
  159. } /* License_data_encode */