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.

169 lines
2.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: pwutil.cpp
  4. //
  5. // Module: Common Source
  6. //
  7. // Synopsis: Simple encryption funcs - borrowed from RAS
  8. //
  9. // Copyright (c) 1994-1999 Microsoft Corporation
  10. //
  11. // Author: nickball Created 08/03/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #define PASSWORDMAGIC 0xA5
  15. VOID
  16. ReverseSzA(
  17. CHAR* psz )
  18. /* Reverses order of characters in 'psz'.
  19. */
  20. {
  21. CHAR* pszBegin;
  22. CHAR* pszEnd;
  23. for (pszBegin = psz, pszEnd = psz + lstrlenA( psz ) - 1;
  24. pszBegin < pszEnd;
  25. ++pszBegin, --pszEnd)
  26. {
  27. CHAR ch = *pszBegin;
  28. *pszBegin = *pszEnd;
  29. *pszEnd = ch;
  30. }
  31. }
  32. VOID
  33. ReverseSzW(
  34. WCHAR* psz )
  35. /* Reverses order of characters in 'psz'.
  36. */
  37. {
  38. WCHAR* pszBegin;
  39. WCHAR* pszEnd;
  40. for (pszBegin = psz, pszEnd = psz + lstrlenW( psz ) - 1;
  41. pszBegin < pszEnd;
  42. ++pszBegin, --pszEnd)
  43. {
  44. WCHAR ch = *pszBegin;
  45. *pszBegin = *pszEnd;
  46. *pszEnd = ch;
  47. }
  48. }
  49. VOID
  50. CmDecodePasswordA(
  51. IN OUT CHAR* pszPassword )
  52. /* Un-obfuscate 'pszPassword' in place.
  53. **
  54. ** Returns Nothing
  55. */
  56. {
  57. CmEncodePasswordA( pszPassword );
  58. }
  59. VOID
  60. CmDecodePasswordW(
  61. IN OUT WCHAR* pszPassword )
  62. /* Un-obfuscate 'pszPassword' in place.
  63. **
  64. ** Returns the address of 'pszPassword'.
  65. */
  66. {
  67. CmEncodePasswordW( pszPassword );
  68. }
  69. VOID
  70. CmEncodePasswordA(
  71. IN OUT CHAR* pszPassword )
  72. /* Obfuscate 'pszPassword' in place to foil memory scans for passwords.
  73. **
  74. ** Returns Nothing
  75. */
  76. {
  77. if (pszPassword)
  78. {
  79. CHAR* psz;
  80. ReverseSzA( pszPassword );
  81. for (psz = pszPassword; *psz != '\0'; ++psz)
  82. {
  83. if (*psz != PASSWORDMAGIC)
  84. *psz ^= PASSWORDMAGIC;
  85. }
  86. }
  87. }
  88. VOID
  89. CmEncodePasswordW(
  90. IN OUT WCHAR* pszPassword )
  91. /* Obfuscate 'pszPassword' in place to foil memory scans for passwords.
  92. **
  93. ** Returns Nothing
  94. */
  95. {
  96. if (pszPassword)
  97. {
  98. WCHAR* psz;
  99. ReverseSzW( pszPassword );
  100. for (psz = pszPassword; *psz != L'\0'; ++psz)
  101. {
  102. if (*psz != PASSWORDMAGIC)
  103. *psz ^= PASSWORDMAGIC;
  104. }
  105. }
  106. }
  107. VOID
  108. CmWipePasswordA(
  109. IN OUT CHAR* pszPassword )
  110. /* Zero out the memory occupied by a password.
  111. **
  112. ** Returns Nothing
  113. */
  114. {
  115. if (pszPassword)
  116. {
  117. CHAR* psz = pszPassword;
  118. while (*psz != '\0')
  119. *psz++ = '\0';
  120. }
  121. }
  122. VOID
  123. CmWipePasswordW(
  124. IN OUT WCHAR* pszPassword )
  125. /* Zero out the memory occupied by a password.
  126. **
  127. ** Returns Nothing
  128. */
  129. {
  130. if (pszPassword)
  131. {
  132. WCHAR* psz = pszPassword;
  133. while (*psz != L'\0')
  134. *psz++ = L'\0';
  135. }
  136. }