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.

314 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name:
  4. local.c
  5. Abstract:
  6. Contains functions that encrypt and decrypt data to be stored locally
  7. Author:
  8. Adam Overton (adamo) 08-Feb-1998
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. #include <seccom.h>
  14. #include <tchar.h>
  15. #include <extypes.h>
  16. #include <license.h>
  17. #include <cryptkey.h>
  18. #if defined(OS_WINCE)
  19. BOOL GetUserName(
  20. LPTSTR lpBuffer, // address of name buffer
  21. LPDWORD pdwSize // address of size of name buffer
  22. )
  23. /*++
  24. Routine Description:
  25. Provides the GetUserName API on platforms that don't have it
  26. Arguments:
  27. lpBuffer - pointer to a buffer for the username
  28. nSize - size of name buffer
  29. Return Value:
  30. TRUE - successfully retrieved UserName
  31. FALSE - otherwise
  32. --*/
  33. {
  34. DWORD dwT;
  35. memset(lpBuffer, 0, *pdwSize);
  36. //
  37. // There doesn't appear to be user name available, just
  38. // use a default and rely on the machine UUID for security
  39. //
  40. dwT = *pdwSize;
  41. #define USER_RANDOM "eefdbcf0001255b4009c9e1800f73774"
  42. if (dwT > sizeof(USER_RANDOM))
  43. dwT = sizeof(USER_RANDOM);
  44. memcpy(lpBuffer, USER_RANDOM, (size_t)dwT);
  45. return TRUE;
  46. }
  47. #endif // defined(OS_WINCE)
  48. BOOL GetLocalKey(
  49. struct RC4_KEYSTRUCT *prc4Key
  50. )
  51. /*++
  52. Routine Description:
  53. This function creates and caches a rc4 key which can be used to store
  54. private information locally
  55. Arguments:
  56. prc4Key - pointer to a buffer to hold the RC4 key
  57. Return Value:
  58. TRUE - successfully generated key
  59. FALSE - otherwise
  60. --*/
  61. {
  62. A_SHA_CTX SHAHash;
  63. BYTE abSHADigest[A_SHA_DIGEST_LEN];
  64. static BOOL fCreatedKey = FALSE;
  65. static struct RC4_KEYSTRUCT rc4Key;
  66. TCHAR szUserName[SEC_MAX_USERNAME];
  67. DWORD dwSize;
  68. HWID hwid;
  69. if (!fCreatedKey) {
  70. A_SHAInit(&SHAHash);
  71. //
  72. // Get the user name
  73. //
  74. dwSize = (DWORD)sizeof(szUserName);
  75. memset(szUserName, 0, (size_t)dwSize);
  76. if (!GetUserName(szUserName, &dwSize))
  77. return FALSE;
  78. A_SHAUpdate(&SHAHash, (unsigned char *)szUserName, dwSize);
  79. //
  80. // Get unique machine identifier
  81. //
  82. if (LICENSE_STATUS_OK == GenerateClientHWID(&hwid)) {
  83. A_SHAUpdate(&SHAHash, (unsigned char *)&hwid, sizeof(HWID));
  84. }
  85. //
  86. // Update the Hash with something less guessable
  87. // but known to our apps
  88. //
  89. #define RANDOM_CONSTANT "deed047e-a3cb-11d1-b96c-00c04fb15601"
  90. A_SHAUpdate(&SHAHash, RANDOM_CONSTANT, sizeof(RANDOM_CONSTANT));
  91. //
  92. // Finalize the hash
  93. //
  94. A_SHAFinal(&SHAHash, abSHADigest);
  95. //
  96. // Generate a key based on this hash
  97. //
  98. msrc4_key(&rc4Key, (UINT)MAX_SESSION_KEY_SIZE, abSHADigest);
  99. fCreatedKey = TRUE;
  100. }
  101. memcpy(prc4Key, &rc4Key, sizeof(rc4Key));
  102. return TRUE;
  103. }
  104. BOOL GetLocalKey50(
  105. struct RC4_KEYSTRUCT *prc4Key,
  106. LPBYTE pbSalt,
  107. DWORD dwSaltLength
  108. )
  109. /*++
  110. Routine Description:
  111. This function creates and caches a rc4 key which can be used to store
  112. private information locally
  113. Arguments:
  114. prc4Key - pointer to a buffer to hold the RC4 key
  115. Return Value:
  116. TRUE - successfully generated key
  117. FALSE - otherwise
  118. --*/
  119. {
  120. A_SHA_CTX SHAHash;
  121. BYTE abSHADigest[A_SHA_DIGEST_LEN];
  122. struct RC4_KEYSTRUCT rc4Key;
  123. TCHAR szUserName[SEC_MAX_USERNAME];
  124. DWORD dwSize;
  125. HWID hwid;
  126. DWORD dw;
  127. A_SHAInit(&SHAHash);
  128. //
  129. // Get the user name
  130. //
  131. dwSize = (DWORD)sizeof(szUserName);
  132. memset(szUserName, 0, (size_t)dwSize);
  133. if (!GetUserName(szUserName, &dwSize))
  134. return FALSE;
  135. A_SHAUpdate(&SHAHash, (unsigned char *)szUserName, dwSize);
  136. //
  137. // Get unique machine identifier
  138. //
  139. if (LICENSE_STATUS_OK == GenerateClientHWID(&hwid)) {
  140. A_SHAUpdate(&SHAHash, (unsigned char *)&hwid, sizeof(HWID));
  141. }
  142. //
  143. // Update the Hash with something less guessable
  144. // but known to our apps
  145. //
  146. #define RANDOM_CONSTANT "deed047e-a3cb-11d1-b96c-00c04fb15601"
  147. A_SHAUpdate(&SHAHash, RANDOM_CONSTANT, sizeof(RANDOM_CONSTANT));
  148. //
  149. // Finalize the hash
  150. //
  151. A_SHAFinal(&SHAHash, abSHADigest);
  152. //
  153. // Add salt and stir gently
  154. //
  155. for (dw = 0; dw < 256; dw++) {
  156. A_SHAInit(&SHAHash);
  157. A_SHAUpdate(&SHAHash, pbSalt, dwSaltLength);
  158. A_SHAUpdate(&SHAHash, abSHADigest, A_SHA_DIGEST_LEN);
  159. A_SHAFinal(&SHAHash, abSHADigest);
  160. }
  161. //
  162. // Generate a key based on this hash
  163. //
  164. msrc4_key(&rc4Key, (UINT)MAX_SESSION_KEY_SIZE, abSHADigest);
  165. memcpy(prc4Key, &rc4Key, sizeof(rc4Key));
  166. return TRUE;
  167. }
  168. BOOL EncryptDecryptLocalData(
  169. LPBYTE pbData,
  170. DWORD dwDataLen
  171. )
  172. /*++
  173. Routine Description:
  174. This function encrypts/decrypts data to be stored locally, but usable
  175. only by the current user on the this machine
  176. Arguments:
  177. pbData - pointer to a data buffer.
  178. dwDataLen - length of the above data.
  179. Return Value:
  180. TRUE - successfully encrypted data
  181. FALSE - otherwise
  182. --*/
  183. {
  184. struct RC4_KEYSTRUCT rc4Key;
  185. if (!GetLocalKey(&rc4Key))
  186. return FALSE;
  187. msrc4(&rc4Key, (UINT)dwDataLen, pbData);
  188. return TRUE;
  189. }
  190. BOOL EncryptDecryptLocalData50(
  191. LPBYTE pbData,
  192. DWORD dwDataLen,
  193. LPBYTE pbSalt,
  194. DWORD dwSaltLen
  195. )
  196. /*++
  197. Routine Description:
  198. This function encrypts/decrypts data to be stored locally, but usable
  199. only by the current user on the this machine
  200. Arguments:
  201. pbData - pointer to a data buffer.
  202. dwDataLen - length of the above data.
  203. Return Value:
  204. TRUE - successfully encrypted data
  205. FALSE - otherwise
  206. --*/
  207. {
  208. struct RC4_KEYSTRUCT rc4Key;
  209. if (!GetLocalKey50(&rc4Key, pbSalt, dwSaltLen))
  210. return FALSE;
  211. msrc4(&rc4Key, (UINT)dwDataLen, pbData);
  212. return TRUE;
  213. }