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.

404 lines
9.9 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. #ifdef MAC
  83. typedef UNICODE_STRING *PCUNICODE_STRING;
  84. #endif
  85. typedef UNICODE_STRING NT_PASSWORD;
  86. typedef NT_PASSWORD * PNT_PASSWORD;
  87. #define NT_OWF_PASSWORD_LENGTH LM_OWF_PASSWORD_LENGTH
  88. typedef LM_OWF_PASSWORD NT_OWF_PASSWORD;
  89. typedef NT_OWF_PASSWORD * PNT_OWF_PASSWORD;
  90. //
  91. // Define the challenge sent by the Lanman server during logon
  92. //
  93. #define LM_CHALLENGE_LENGTH CLEAR_BLOCK_LENGTH
  94. typedef CLEAR_BLOCK LM_CHALLENGE;
  95. typedef LM_CHALLENGE * PLM_CHALLENGE;
  96. typedef LM_CHALLENGE NT_CHALLENGE;
  97. typedef NT_CHALLENGE * PNT_CHALLENGE;
  98. #define USER_SESSION_KEY_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  99. typedef struct _USER_SESSION_KEY {
  100. CYPHER_BLOCK data[2];
  101. } USER_SESSION_KEY;
  102. typedef USER_SESSION_KEY * PUSER_SESSION_KEY;
  103. //
  104. // Define the response sent by redirector in response to challenge from server
  105. //
  106. #define LM_RESPONSE_LENGTH (CYPHER_BLOCK_LENGTH * 3)
  107. typedef struct _LM_RESPONSE {
  108. CYPHER_BLOCK data[3];
  109. } LM_RESPONSE;
  110. typedef LM_RESPONSE * PLM_RESPONSE;
  111. #define NT_RESPONSE_LENGTH LM_RESPONSE_LENGTH
  112. typedef LM_RESPONSE NT_RESPONSE;
  113. typedef NT_RESPONSE * PNT_RESPONSE;
  114. //
  115. // Define the result of the reversible encryption of an OWF'ed password.
  116. //
  117. #define ENCRYPTED_LM_OWF_PASSWORD_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  118. typedef struct _ENCRYPTED_LM_OWF_PASSWORD {
  119. CYPHER_BLOCK data[2];
  120. } ENCRYPTED_LM_OWF_PASSWORD;
  121. typedef ENCRYPTED_LM_OWF_PASSWORD * PENCRYPTED_LM_OWF_PASSWORD;
  122. //
  123. // Define the session key maintained by the redirector and server
  124. //
  125. #define LM_SESSION_KEY_LENGTH LM_CHALLENGE_LENGTH
  126. typedef LM_CHALLENGE LM_SESSION_KEY;
  127. typedef LM_SESSION_KEY * PLM_SESSION_KEY;
  128. #define NT_SESSION_KEY_LENGTH (2 * CLEAR_BLOCK_LENGTH)
  129. //
  130. // Define the index type used to encrypt OWF Passwords
  131. //
  132. typedef long CRYPT_INDEX;
  133. typedef CRYPT_INDEX * PCRYPT_INDEX;
  134. ////////////////////////////////////////////////////////////////////////////
  135. // //
  136. // Encryption library API macros //
  137. // //
  138. // To conceal the purpose of these functions to someone dumping out the //
  139. // encryption dll they have been purposefully given unhelpful names. //
  140. // Each has an associated macro that should be used by system components //
  141. // to access these routines in a readable way. //
  142. // //
  143. ////////////////////////////////////////////////////////////////////////////
  144. ////////////////////////////////////////////////////////////////////////////
  145. // //
  146. // Encryption library API function prototypes //
  147. // //
  148. ////////////////////////////////////////////////////////////////////////////
  149. //
  150. // Core block encryption functions
  151. //
  152. #ifdef __cplusplus
  153. extern "C" {
  154. #endif
  155. BOOL
  156. EncryptBlock(
  157. IN PCLEAR_BLOCK ClearBlock,
  158. IN PBLOCK_KEY BlockKey,
  159. OUT PCYPHER_BLOCK CypherBlock
  160. );
  161. BOOL
  162. DecryptBlock(
  163. IN PCYPHER_BLOCK CypherBlock,
  164. IN PBLOCK_KEY BlockKey,
  165. OUT PCLEAR_BLOCK ClearBlock
  166. );
  167. BOOL
  168. EncryptStdBlock(
  169. IN PBLOCK_KEY BlockKey,
  170. OUT PCYPHER_BLOCK CypherBlock
  171. );
  172. //
  173. // Arbitrary length data encryption functions
  174. //
  175. BOOL
  176. EncryptData(
  177. IN PCLEAR_DATA ClearData,
  178. IN PDATA_KEY DataKey,
  179. OUT PCYPHER_DATA CypherData
  180. );
  181. BOOL
  182. DecryptData(
  183. IN PCYPHER_DATA CypherData,
  184. IN PDATA_KEY DataKey,
  185. OUT PCLEAR_DATA ClearData
  186. );
  187. //
  188. // Password hashing functions (One Way Function)
  189. //
  190. BOOL
  191. CalculateLmOwfPassword(
  192. IN PLM_PASSWORD LmPassword,
  193. OUT PLM_OWF_PASSWORD LmOwfPassword
  194. );
  195. BOOL
  196. CalculateNtOwfPassword(
  197. IN PNT_PASSWORD NtPassword,
  198. OUT PNT_OWF_PASSWORD NtOwfPassword
  199. );
  200. //
  201. // OWF password comparison functions
  202. //
  203. BOOL
  204. EqualLmOwfPassword(
  205. IN PLM_OWF_PASSWORD LmOwfPassword1,
  206. IN PLM_OWF_PASSWORD LmOwfPassword2
  207. );
  208. //
  209. // Functions for calculating response to server challenge
  210. //
  211. BOOL
  212. CalculateLmResponse(
  213. IN PLM_CHALLENGE LmChallenge,
  214. IN PLM_OWF_PASSWORD LmOwfPassword,
  215. OUT PLM_RESPONSE LmResponse
  216. );
  217. BOOL
  218. CalculateNtResponse(
  219. IN PNT_CHALLENGE NtChallenge,
  220. IN PNT_OWF_PASSWORD NtOwfPassword,
  221. OUT PNT_RESPONSE NtResponse
  222. );
  223. BOOL
  224. CalculateUserSessionKeyLm(
  225. IN PLM_RESPONSE LmResponse,
  226. IN PLM_OWF_PASSWORD LmOwfPassword,
  227. OUT PUSER_SESSION_KEY UserSessionKey
  228. );
  229. BOOL
  230. CalculateUserSessionKeyNt(
  231. IN PNT_RESPONSE NtResponse,
  232. IN PNT_OWF_PASSWORD NtOwfPassword,
  233. OUT PUSER_SESSION_KEY UserSessionKey
  234. );
  235. //
  236. // OwfPassword encryption functions
  237. //
  238. //
  239. // Encrypt OwfPassword using OwfPassword as the key
  240. //
  241. BOOL
  242. EncryptLmOwfPwdWithLmOwfPwd(
  243. IN PLM_OWF_PASSWORD DataLmOwfPassword,
  244. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  245. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  246. );
  247. BOOL
  248. DecryptLmOwfPwdWithLmOwfPwd(
  249. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  250. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  251. OUT PLM_OWF_PASSWORD DataLmOwfPassword
  252. );
  253. //
  254. // Encrypt OwfPassword using SessionKey as the key
  255. //
  256. BOOL
  257. EncryptLmOwfPwdWithLmSesKey(
  258. IN PLM_OWF_PASSWORD LmOwfPassword,
  259. IN PLM_SESSION_KEY LmSessionKey,
  260. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  261. );
  262. BOOL
  263. DecryptLmOwfPwdWithLmSesKey(
  264. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  265. IN PLM_SESSION_KEY LmSessionKey,
  266. OUT PLM_OWF_PASSWORD LmOwfPassword
  267. );
  268. //
  269. // Encrypt OwfPassword using an index as the key
  270. //
  271. BOOL
  272. EncryptLmOwfPwdWithIndex(
  273. IN PLM_OWF_PASSWORD LmOwfPassword,
  274. IN PCRYPT_INDEX Index,
  275. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  276. );
  277. BOOL
  278. DecryptLmOwfPwdWithIndex(
  279. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  280. IN PCRYPT_INDEX Index,
  281. OUT PLM_OWF_PASSWORD LmOwfPassword
  282. );
  283. #ifdef __cplusplus
  284. }
  285. #endif //__cplusplus
  286. #endif // _NTCRYPT_