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.

174 lines
4.6 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. #include <windows.h>
  27. #include "wlbsparm.h"
  28. #include "license.h"
  29. /* CONSTANTS */
  30. static UCHAR data_key [] =
  31. { 0x3f, 0xba, 0x6e, 0xf0, 0xe1, 0x44, 0x1b, 0x45,
  32. 0x41, 0xc4, 0x9f, 0xfb, 0x46, 0x54, 0xbc, 0x43 };
  33. static UCHAR str_key [] =
  34. { 0xdb, 0x1b, 0xac, 0x1a, 0xb9, 0xb1, 0x18, 0x03,
  35. 0x55, 0x57, 0x4a, 0x62, 0x36, 0x21, 0x7c, 0xa6 };
  36. /* Encryption and decryption routines are based on a public-domain Tiny
  37. Encryption Algorithm (TEA) by David Wheeler and Roger Needham at the
  38. Computer Laboratory of Cambridge University. For reference, please
  39. consult http://vader.brad.ac.uk/tea/tea.shtml */
  40. static VOID License_decipher (
  41. PULONG v,
  42. PULONG k)
  43. {
  44. ULONG y = v [0],
  45. z = v [1],
  46. a = k [0],
  47. b = k [1],
  48. c = k [2],
  49. d = k [3],
  50. n = 32,
  51. sum = 0xC6EF3720,
  52. delta = 0x9E3779B9;
  53. /* sum = delta<<5, in general sum = delta * n */
  54. while (n-- > 0)
  55. {
  56. z -= (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  57. y -= (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  58. sum -= delta;
  59. }
  60. v [0] = y; v [1] = z;
  61. } /* end License_decipher */
  62. static VOID License_encipher (
  63. PULONG v,
  64. PULONG k)
  65. {
  66. ULONG y = v [0],
  67. z = v [1],
  68. a = k [0],
  69. b = k [1],
  70. c = k [2],
  71. d = k [3],
  72. n = 32,
  73. sum = 0,
  74. delta = 0x9E3779B9;
  75. while (n-- > 0)
  76. {
  77. sum += delta;
  78. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  79. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  80. }
  81. v [0] = y; v [1] = z;
  82. } /* end License_encipher */
  83. BOOL License_data_decode (
  84. PCHAR data,
  85. ULONG len)
  86. {
  87. ULONG i;
  88. if (len % LICENSE_DATA_GRANULARITY != 0)
  89. return FALSE;
  90. for (i = 0; i < len; i += LICENSE_DATA_GRANULARITY)
  91. License_decipher ((PULONG) (data + i), (PULONG) data_key);
  92. return TRUE;
  93. } /* License_data_decode */
  94. ULONG License_wstring_encode (
  95. PWCHAR str)
  96. {
  97. CHAR buf [LICENSE_STR_IMPORTANT_CHARS + 1];
  98. ULONG code, i;
  99. PULONG nibp;
  100. for (i = 0; i < LICENSE_STR_IMPORTANT_CHARS; i++)
  101. {
  102. if (str[i] == 0)
  103. break;
  104. buf[i] = (UCHAR)str[i];
  105. }
  106. for (; i < LICENSE_STR_IMPORTANT_CHARS + 1; i ++)
  107. buf[i] = 0;
  108. for (i = 0; i < LICENSE_STR_NIBBLES; i ++)
  109. License_encipher ((PULONG) (buf + i * LICENSE_DATA_GRANULARITY),
  110. (PULONG) str_key);
  111. for (i = 0, code = 0; i < LICENSE_STR_NIBBLES; i ++)
  112. {
  113. nibp = (PULONG) (buf + (i * LICENSE_DATA_GRANULARITY));
  114. code ^= nibp [0] ^ nibp [1];
  115. }
  116. /* V2.2 - if password consists of the same characters - XORing nibbles
  117. above makes it go to 0 - put some recovery for that special case since
  118. we cannot modify the algorithm due to legacy issues */
  119. if (code == 0 && str [0] != 0)
  120. code = * ((PULONG) buf);
  121. return code;
  122. } /* License_wstring_encode */