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.

393 lines
9.4 KiB

  1. /*
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. crypt.h
  5. Abstract:
  6. This module contains the public data structures and API definitions
  7. needed to utilize the encryption library
  8. Author:
  9. David Chalmers (Davidc) 21-October-1991
  10. Revision History:
  11. --*/
  12. #ifndef _NTCRYPT_
  13. #define _NTCRYPT_
  14. #define IN
  15. #define OUT
  16. /////////////////////////////////////////////////////////////////////////
  17. // //
  18. // Core encryption types //
  19. // //
  20. /////////////////////////////////////////////////////////////////////////
  21. #define CLEAR_BLOCK_LENGTH 8
  22. typedef struct _CLEAR_BLOCK {
  23. char data[CLEAR_BLOCK_LENGTH];
  24. } CLEAR_BLOCK;
  25. typedef CLEAR_BLOCK * PCLEAR_BLOCK;
  26. #define CYPHER_BLOCK_LENGTH 8
  27. typedef struct _CYPHER_BLOCK {
  28. char data[CYPHER_BLOCK_LENGTH];
  29. } CYPHER_BLOCK;
  30. typedef CYPHER_BLOCK * PCYPHER_BLOCK;
  31. #define BLOCK_KEY_LENGTH 7
  32. typedef struct _BLOCK_KEY {
  33. char data[BLOCK_KEY_LENGTH];
  34. } BLOCK_KEY;
  35. typedef BLOCK_KEY * PBLOCK_KEY;
  36. /////////////////////////////////////////////////////////////////////////
  37. // //
  38. // Arbitrary length data encryption types //
  39. // //
  40. /////////////////////////////////////////////////////////////////////////
  41. typedef struct _CRYPT_BUFFER {
  42. unsigned long Length; // Number of valid bytes in buffer
  43. unsigned long MaximumLength; // Number of bytes pointed to by Buffer
  44. void * Buffer;
  45. } CRYPT_BUFFER;
  46. typedef CRYPT_BUFFER * PCRYPT_BUFFER;
  47. typedef CRYPT_BUFFER CLEAR_DATA;
  48. typedef CLEAR_DATA * PCLEAR_DATA;
  49. typedef CRYPT_BUFFER DATA_KEY;
  50. typedef DATA_KEY * PDATA_KEY;
  51. typedef CRYPT_BUFFER CYPHER_DATA;
  52. typedef CYPHER_DATA * PCYPHER_DATA;
  53. /////////////////////////////////////////////////////////////////////////
  54. // //
  55. // Lan Manager data types //
  56. // //
  57. /////////////////////////////////////////////////////////////////////////
  58. //
  59. // Define a LanManager compatible password
  60. //
  61. // A LanManager password is a null-terminated ansi string consisting of a
  62. // maximum of 14 characters (not including terminator)
  63. //
  64. typedef char * PLM_PASSWORD;
  65. //
  66. // Define the result of the 'One Way Function' (OWF) on a LM password
  67. //
  68. #define LM_OWF_PASSWORD_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  69. typedef struct _LM_OWF_PASSWORD {
  70. CYPHER_BLOCK data[2];
  71. } LM_OWF_PASSWORD;
  72. typedef LM_OWF_PASSWORD * PLM_OWF_PASSWORD;
  73. //
  74. // NT password types.
  75. //
  76. typedef struct _UNICODE_STRING {
  77. USHORT Length;
  78. USHORT MaximumLength;
  79. PWSTR Buffer;
  80. } UNICODE_STRING;
  81. typedef UNICODE_STRING *PUNICODE_STRING;
  82. typedef UNICODE_STRING NT_PASSWORD;
  83. typedef NT_PASSWORD * PNT_PASSWORD;
  84. #define NT_OWF_PASSWORD_LENGTH LM_OWF_PASSWORD_LENGTH
  85. typedef LM_OWF_PASSWORD NT_OWF_PASSWORD;
  86. typedef NT_OWF_PASSWORD * PNT_OWF_PASSWORD;
  87. //
  88. // Define the challenge sent by the Lanman server during logon
  89. //
  90. #define LM_CHALLENGE_LENGTH CLEAR_BLOCK_LENGTH
  91. typedef CLEAR_BLOCK LM_CHALLENGE;
  92. typedef LM_CHALLENGE * PLM_CHALLENGE;
  93. typedef LM_CHALLENGE NT_CHALLENGE;
  94. typedef NT_CHALLENGE * PNT_CHALLENGE;
  95. #define USER_SESSION_KEY_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  96. typedef struct _USER_SESSION_KEY {
  97. CYPHER_BLOCK data[2];
  98. } USER_SESSION_KEY;
  99. typedef USER_SESSION_KEY * PUSER_SESSION_KEY;
  100. //
  101. // Define the response sent by redirector in response to challenge from server
  102. //
  103. #define LM_RESPONSE_LENGTH (CYPHER_BLOCK_LENGTH * 3)
  104. typedef struct _LM_RESPONSE {
  105. CYPHER_BLOCK data[3];
  106. } LM_RESPONSE;
  107. typedef LM_RESPONSE * PLM_RESPONSE;
  108. #define NT_RESPONSE_LENGTH LM_RESPONSE_LENGTH
  109. typedef LM_RESPONSE NT_RESPONSE;
  110. typedef NT_RESPONSE * PNT_RESPONSE;
  111. //
  112. // Define the result of the reversible encryption of an OWF'ed password.
  113. //
  114. #define ENCRYPTED_LM_OWF_PASSWORD_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  115. typedef struct _ENCRYPTED_LM_OWF_PASSWORD {
  116. CYPHER_BLOCK data[2];
  117. } ENCRYPTED_LM_OWF_PASSWORD;
  118. typedef ENCRYPTED_LM_OWF_PASSWORD * PENCRYPTED_LM_OWF_PASSWORD;
  119. //
  120. // Define the session key maintained by the redirector and server
  121. //
  122. #define LM_SESSION_KEY_LENGTH LM_CHALLENGE_LENGTH
  123. typedef LM_CHALLENGE LM_SESSION_KEY;
  124. typedef LM_SESSION_KEY * PLM_SESSION_KEY;
  125. #define NT_SESSION_KEY_LENGTH (2 * CLEAR_BLOCK_LENGTH)
  126. //
  127. // Define the index type used to encrypt OWF Passwords
  128. //
  129. typedef long CRYPT_INDEX;
  130. typedef CRYPT_INDEX * PCRYPT_INDEX;
  131. ////////////////////////////////////////////////////////////////////////////
  132. // //
  133. // Encryption library API macros //
  134. // //
  135. // To conceal the purpose of these functions to someone dumping out the //
  136. // encryption dll they have been purposefully given unhelpful names. //
  137. // Each has an associated macro that should be used by system components //
  138. // to access these routines in a readable way. //
  139. // //
  140. ////////////////////////////////////////////////////////////////////////////
  141. ////////////////////////////////////////////////////////////////////////////
  142. // //
  143. // Encryption library API function prototypes //
  144. // //
  145. ////////////////////////////////////////////////////////////////////////////
  146. //
  147. // Core block encryption functions
  148. //
  149. BOOL
  150. EncryptBlock(
  151. IN PCLEAR_BLOCK ClearBlock,
  152. IN PBLOCK_KEY BlockKey,
  153. OUT PCYPHER_BLOCK CypherBlock
  154. );
  155. BOOL
  156. DecryptBlock(
  157. IN PCYPHER_BLOCK CypherBlock,
  158. IN PBLOCK_KEY BlockKey,
  159. OUT PCLEAR_BLOCK ClearBlock
  160. );
  161. BOOL
  162. EncryptStdBlock(
  163. IN PBLOCK_KEY BlockKey,
  164. OUT PCYPHER_BLOCK CypherBlock
  165. );
  166. //
  167. // Arbitrary length data encryption functions
  168. //
  169. BOOL
  170. EncryptData(
  171. IN PCLEAR_DATA ClearData,
  172. IN PDATA_KEY DataKey,
  173. OUT PCYPHER_DATA CypherData
  174. );
  175. BOOL
  176. DecryptData(
  177. IN PCYPHER_DATA CypherData,
  178. IN PDATA_KEY DataKey,
  179. OUT PCLEAR_DATA ClearData
  180. );
  181. //
  182. // Password hashing functions (One Way Function)
  183. //
  184. BOOL
  185. CalculateLmOwfPassword(
  186. IN PLM_PASSWORD LmPassword,
  187. OUT PLM_OWF_PASSWORD LmOwfPassword
  188. );
  189. BOOL
  190. CalculateNtOwfPassword(
  191. IN PNT_PASSWORD NtPassword,
  192. OUT PNT_OWF_PASSWORD NtOwfPassword
  193. );
  194. //
  195. // OWF password comparison functions
  196. //
  197. BOOL
  198. EqualLmOwfPassword(
  199. IN PLM_OWF_PASSWORD LmOwfPassword1,
  200. IN PLM_OWF_PASSWORD LmOwfPassword2
  201. );
  202. //
  203. // Functions for calculating response to server challenge
  204. //
  205. BOOL
  206. CalculateLmResponse(
  207. IN PLM_CHALLENGE LmChallenge,
  208. IN PLM_OWF_PASSWORD LmOwfPassword,
  209. OUT PLM_RESPONSE LmResponse
  210. );
  211. BOOL
  212. CalculateNtResponse(
  213. IN PNT_CHALLENGE NtChallenge,
  214. IN PNT_OWF_PASSWORD NtOwfPassword,
  215. OUT PNT_RESPONSE NtResponse
  216. );
  217. BOOL
  218. CalculateUserSessionKeyLm(
  219. IN PLM_RESPONSE LmResponse,
  220. IN PLM_OWF_PASSWORD LmOwfPassword,
  221. OUT PUSER_SESSION_KEY UserSessionKey
  222. );
  223. BOOL
  224. CalculateUserSessionKeyNt(
  225. IN PNT_RESPONSE NtResponse,
  226. IN PNT_OWF_PASSWORD NtOwfPassword,
  227. OUT PUSER_SESSION_KEY UserSessionKey
  228. );
  229. //
  230. // OwfPassword encryption functions
  231. //
  232. //
  233. // Encrypt OwfPassword using OwfPassword as the key
  234. //
  235. BOOL
  236. EncryptLmOwfPwdWithLmOwfPwd(
  237. IN PLM_OWF_PASSWORD DataLmOwfPassword,
  238. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  239. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  240. );
  241. BOOL
  242. DecryptLmOwfPwdWithLmOwfPwd(
  243. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  244. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  245. OUT PLM_OWF_PASSWORD DataLmOwfPassword
  246. );
  247. //
  248. // Encrypt OwfPassword using SessionKey as the key
  249. //
  250. BOOL
  251. EncryptLmOwfPwdWithLmSesKey(
  252. IN PLM_OWF_PASSWORD LmOwfPassword,
  253. IN PLM_SESSION_KEY LmSessionKey,
  254. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  255. );
  256. BOOL
  257. DecryptLmOwfPwdWithLmSesKey(
  258. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  259. IN PLM_SESSION_KEY LmSessionKey,
  260. OUT PLM_OWF_PASSWORD LmOwfPassword
  261. );
  262. //
  263. // Encrypt OwfPassword using an index as the key
  264. //
  265. BOOL
  266. EncryptLmOwfPwdWithIndex(
  267. IN PLM_OWF_PASSWORD LmOwfPassword,
  268. IN PCRYPT_INDEX Index,
  269. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  270. );
  271. BOOL
  272. DecryptLmOwfPwdWithIndex(
  273. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  274. IN PCRYPT_INDEX Index,
  275. OUT PLM_OWF_PASSWORD LmOwfPassword
  276. );
  277. #endif // _NTCRYPT_