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.

682 lines
18 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  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. Scott Field (SField) 09-October-2000
  12. Add RNG and Memory encryption interfaces
  13. --*/
  14. #ifndef _NTCRYPT_
  15. #define _NTCRYPT_
  16. #ifndef MIDL_PASS // Don't confuse MIDL
  17. #ifndef RPC_NO_WINDOWS_H // Don't let rpc.h include windows.h
  18. #define RPC_NO_WINDOWS_H
  19. #endif // RPC_NO_WINDOWS_H
  20. #include <rpc.h>
  21. #endif // MIDL_PASS
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /////////////////////////////////////////////////////////////////////////
  26. // //
  27. // Core encryption types //
  28. // //
  29. /////////////////////////////////////////////////////////////////////////
  30. // begin_ntsubauth
  31. #define CLEAR_BLOCK_LENGTH 8
  32. typedef struct _CLEAR_BLOCK {
  33. CHAR data[CLEAR_BLOCK_LENGTH];
  34. } CLEAR_BLOCK;
  35. typedef CLEAR_BLOCK * PCLEAR_BLOCK;
  36. #define CYPHER_BLOCK_LENGTH 8
  37. typedef struct _CYPHER_BLOCK {
  38. CHAR data[CYPHER_BLOCK_LENGTH];
  39. } CYPHER_BLOCK;
  40. typedef CYPHER_BLOCK * PCYPHER_BLOCK;
  41. // end_ntsubauth
  42. #define BLOCK_KEY_LENGTH 7
  43. typedef struct _BLOCK_KEY {
  44. CHAR data[BLOCK_KEY_LENGTH];
  45. } BLOCK_KEY;
  46. typedef BLOCK_KEY * PBLOCK_KEY;
  47. /////////////////////////////////////////////////////////////////////////
  48. // //
  49. // Arbitrary length data encryption types //
  50. // //
  51. /////////////////////////////////////////////////////////////////////////
  52. typedef struct _CRYPT_BUFFER {
  53. ULONG Length; // Number of valid bytes in buffer
  54. ULONG MaximumLength; // Number of bytes pointed to by Buffer
  55. PVOID Buffer;
  56. } CRYPT_BUFFER;
  57. typedef CRYPT_BUFFER * PCRYPT_BUFFER;
  58. typedef CRYPT_BUFFER CLEAR_DATA;
  59. typedef CLEAR_DATA * PCLEAR_DATA;
  60. typedef CRYPT_BUFFER DATA_KEY;
  61. typedef DATA_KEY * PDATA_KEY;
  62. typedef CRYPT_BUFFER CYPHER_DATA;
  63. typedef CYPHER_DATA * PCYPHER_DATA;
  64. /////////////////////////////////////////////////////////////////////////
  65. // //
  66. // Lan Manager data types //
  67. // //
  68. /////////////////////////////////////////////////////////////////////////
  69. //
  70. // Define a LanManager compatible password
  71. //
  72. // A LanManager password is a null-terminated ansi string consisting of a
  73. // maximum of 14 characters (not including terminator)
  74. //
  75. typedef CHAR * PLM_PASSWORD;
  76. //
  77. // Define the result of the 'One Way Function' (OWF) on a LM password
  78. //
  79. #define LM_OWF_PASSWORD_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  80. // begin_ntsubauth
  81. typedef struct _LM_OWF_PASSWORD {
  82. CYPHER_BLOCK data[2];
  83. } LM_OWF_PASSWORD;
  84. typedef LM_OWF_PASSWORD * PLM_OWF_PASSWORD;
  85. // end_ntsubauth
  86. //
  87. // Define the challenge sent by the Lanman server during logon
  88. //
  89. #define LM_CHALLENGE_LENGTH CLEAR_BLOCK_LENGTH
  90. // begin_ntsubauth
  91. typedef CLEAR_BLOCK LM_CHALLENGE;
  92. typedef LM_CHALLENGE * PLM_CHALLENGE;
  93. // end_ntsubauth
  94. //
  95. // Define the response sent by redirector in response to challenge from server
  96. //
  97. #define LM_RESPONSE_LENGTH (CYPHER_BLOCK_LENGTH * 3)
  98. typedef struct _LM_RESPONSE {
  99. CYPHER_BLOCK data[3];
  100. } LM_RESPONSE;
  101. typedef LM_RESPONSE * PLM_RESPONSE;
  102. //
  103. // Define the result of the reversible encryption of an OWF'ed password.
  104. //
  105. #define ENCRYPTED_LM_OWF_PASSWORD_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  106. typedef struct _ENCRYPTED_LM_OWF_PASSWORD {
  107. CYPHER_BLOCK data[2];
  108. } ENCRYPTED_LM_OWF_PASSWORD;
  109. typedef ENCRYPTED_LM_OWF_PASSWORD * PENCRYPTED_LM_OWF_PASSWORD;
  110. //
  111. // Define the session key maintained by the redirector and server
  112. //
  113. #define LM_SESSION_KEY_LENGTH LM_CHALLENGE_LENGTH
  114. typedef LM_CHALLENGE LM_SESSION_KEY;
  115. typedef LM_SESSION_KEY * PLM_SESSION_KEY;
  116. //
  117. // Define the index type used to encrypt OWF Passwords
  118. //
  119. typedef LONG CRYPT_INDEX;
  120. typedef CRYPT_INDEX * PCRYPT_INDEX;
  121. /////////////////////////////////////////////////////////////////////////
  122. // //
  123. // 'NT' encryption types that are used to duplicate existing LM //
  124. // functionality with improved algorithms. //
  125. // //
  126. /////////////////////////////////////////////////////////////////////////
  127. typedef UNICODE_STRING NT_PASSWORD;
  128. typedef NT_PASSWORD * PNT_PASSWORD;
  129. #define NT_OWF_PASSWORD_LENGTH LM_OWF_PASSWORD_LENGTH
  130. // begin_ntsubauth
  131. typedef LM_OWF_PASSWORD NT_OWF_PASSWORD;
  132. typedef NT_OWF_PASSWORD * PNT_OWF_PASSWORD;
  133. // end_ntsubauth
  134. #define NT_CHALLENGE_LENGTH LM_CHALLENGE_LENGTH
  135. // begin_ntsubauth
  136. typedef LM_CHALLENGE NT_CHALLENGE;
  137. typedef NT_CHALLENGE * PNT_CHALLENGE;
  138. // end_ntsubauth
  139. #define NT_RESPONSE_LENGTH LM_RESPONSE_LENGTH
  140. typedef LM_RESPONSE NT_RESPONSE;
  141. typedef NT_RESPONSE * PNT_RESPONSE;
  142. #define ENCRYPTED_NT_OWF_PASSWORD_LENGTH ENCRYPTED_LM_OWF_PASSWORD_LENGTH
  143. typedef ENCRYPTED_LM_OWF_PASSWORD ENCRYPTED_NT_OWF_PASSWORD;
  144. typedef ENCRYPTED_NT_OWF_PASSWORD * PENCRYPTED_NT_OWF_PASSWORD;
  145. #define NT_SESSION_KEY_LENGTH LM_SESSION_KEY_LENGTH
  146. typedef LM_SESSION_KEY NT_SESSION_KEY;
  147. typedef NT_SESSION_KEY * PNT_SESSION_KEY;
  148. /////////////////////////////////////////////////////////////////////////
  149. // //
  150. // 'NT' encryption types for new functionality not present in LM //
  151. // //
  152. /////////////////////////////////////////////////////////////////////////
  153. //
  154. // The user session key is similar to the LM and NT session key except it
  155. // is different for each user on the system. This allows it to be used
  156. // for secure user communication with a server.
  157. //
  158. // begin_ntsubauth
  159. #define USER_SESSION_KEY_LENGTH (CYPHER_BLOCK_LENGTH * 2)
  160. typedef struct _USER_SESSION_KEY {
  161. CYPHER_BLOCK data[2];
  162. } USER_SESSION_KEY;
  163. typedef USER_SESSION_KEY * PUSER_SESSION_KEY;
  164. // end_ntsubauth
  165. ////////////////////////////////////////////////////////////////////////////
  166. // //
  167. // Encryption library API macros //
  168. // //
  169. // To conceal the purpose of these functions to someone dumping out the //
  170. // encryption dll they have been purposefully given unhelpful names. //
  171. // Each has an associated macro that should be used by system components //
  172. // to access these routines in a readable way. //
  173. // //
  174. ////////////////////////////////////////////////////////////////////////////
  175. #define RtlEncryptBlock SystemFunction001
  176. #define RtlDecryptBlock SystemFunction002
  177. #define RtlEncryptStdBlock SystemFunction003
  178. #define RtlEncryptData SystemFunction004
  179. #define RtlDecryptData SystemFunction005
  180. #define RtlCalculateLmOwfPassword SystemFunction006
  181. #define RtlCalculateNtOwfPassword SystemFunction007
  182. #define RtlCalculateLmResponse SystemFunction008
  183. #define RtlCalculateNtResponse SystemFunction009
  184. #define RtlCalculateUserSessionKeyLm SystemFunction010
  185. #define RtlCalculateUserSessionKeyNt SystemFunction011
  186. #define RtlEncryptLmOwfPwdWithLmOwfPwd SystemFunction012
  187. #define RtlDecryptLmOwfPwdWithLmOwfPwd SystemFunction013
  188. #define RtlEncryptNtOwfPwdWithNtOwfPwd SystemFunction014
  189. #define RtlDecryptNtOwfPwdWithNtOwfPwd SystemFunction015
  190. #define RtlEncryptLmOwfPwdWithLmSesKey SystemFunction016
  191. #define RtlDecryptLmOwfPwdWithLmSesKey SystemFunction017
  192. #define RtlEncryptNtOwfPwdWithNtSesKey SystemFunction018
  193. #define RtlDecryptNtOwfPwdWithNtSesKey SystemFunction019
  194. #define RtlEncryptLmOwfPwdWithUserKey SystemFunction020
  195. #define RtlDecryptLmOwfPwdWithUserKey SystemFunction021
  196. #define RtlEncryptNtOwfPwdWithUserKey SystemFunction022
  197. #define RtlDecryptNtOwfPwdWithUserKey SystemFunction023
  198. #define RtlEncryptLmOwfPwdWithIndex SystemFunction024
  199. #define RtlDecryptLmOwfPwdWithIndex SystemFunction025
  200. #define RtlEncryptNtOwfPwdWithIndex SystemFunction026
  201. #define RtlDecryptNtOwfPwdWithIndex SystemFunction027
  202. #define RtlGetUserSessionKeyClient SystemFunction028
  203. #define RtlGetUserSessionKeyServer SystemFunction029
  204. #define RtlEqualLmOwfPassword SystemFunction030
  205. #define RtlEqualNtOwfPassword SystemFunction031
  206. #define RtlEncryptData2 SystemFunction032
  207. #define RtlDecryptData2 SystemFunction033
  208. #define RtlGetUserSessionKeyClientBinding SystemFunction034
  209. #define RtlCheckSignatureInFile SystemFunction035
  210. #define RtlGenRandom SystemFunction036
  211. // begin_ntsecapi
  212. #define RtlEncryptMemory SystemFunction040
  213. #define RtlDecryptMemory SystemFunction041
  214. // end_ntsecapi
  215. ////////////////////////////////////////////////////////////////////////////
  216. // //
  217. // Encryption library API function prototypes //
  218. // //
  219. ////////////////////////////////////////////////////////////////////////////
  220. //
  221. // Core block encryption functions
  222. //
  223. NTSTATUS
  224. RtlEncryptBlock(
  225. IN PCLEAR_BLOCK ClearBlock,
  226. IN PBLOCK_KEY BlockKey,
  227. OUT PCYPHER_BLOCK CypherBlock
  228. );
  229. NTSTATUS
  230. RtlDecryptBlock(
  231. IN PCYPHER_BLOCK CypherBlock,
  232. IN PBLOCK_KEY BlockKey,
  233. OUT PCLEAR_BLOCK ClearBlock
  234. );
  235. NTSTATUS
  236. RtlEncryptStdBlock(
  237. IN PBLOCK_KEY BlockKey,
  238. OUT PCYPHER_BLOCK CypherBlock
  239. );
  240. //
  241. // Arbitrary length data encryption functions
  242. //
  243. NTSTATUS
  244. RtlEncryptData(
  245. IN PCLEAR_DATA ClearData,
  246. IN PDATA_KEY DataKey,
  247. OUT PCYPHER_DATA CypherData
  248. );
  249. NTSTATUS
  250. RtlDecryptData(
  251. IN PCYPHER_DATA CypherData,
  252. IN PDATA_KEY DataKey,
  253. OUT PCLEAR_DATA ClearData
  254. );
  255. //
  256. // Faster arbitrary length data encryption functions (using RC4)
  257. //
  258. NTSTATUS
  259. RtlEncryptData2(
  260. IN OUT PCRYPT_BUFFER pData,
  261. IN PDATA_KEY pKey
  262. );
  263. NTSTATUS
  264. RtlDecryptData2(
  265. IN OUT PCRYPT_BUFFER pData,
  266. IN PDATA_KEY pKey
  267. );
  268. //
  269. // Password hashing functions (One Way Function)
  270. //
  271. NTSTATUS
  272. RtlCalculateLmOwfPassword(
  273. IN PLM_PASSWORD LmPassword,
  274. OUT PLM_OWF_PASSWORD LmOwfPassword
  275. );
  276. NTSTATUS
  277. RtlCalculateNtOwfPassword(
  278. IN PNT_PASSWORD NtPassword,
  279. OUT PNT_OWF_PASSWORD NtOwfPassword
  280. );
  281. //
  282. // OWF password comparison functions
  283. //
  284. BOOLEAN
  285. RtlEqualLmOwfPassword(
  286. IN PLM_OWF_PASSWORD LmOwfPassword1,
  287. IN PLM_OWF_PASSWORD LmOwfPassword2
  288. );
  289. BOOLEAN
  290. RtlEqualNtOwfPassword(
  291. IN PNT_OWF_PASSWORD NtOwfPassword1,
  292. IN PNT_OWF_PASSWORD NtOwfPassword2
  293. );
  294. //
  295. // Functions for calculating response to server challenge
  296. //
  297. NTSTATUS
  298. RtlCalculateLmResponse(
  299. IN PLM_CHALLENGE LmChallenge,
  300. IN PLM_OWF_PASSWORD LmOwfPassword,
  301. OUT PLM_RESPONSE LmResponse
  302. );
  303. NTSTATUS
  304. RtlCalculateNtResponse(
  305. IN PNT_CHALLENGE NtChallenge,
  306. IN PNT_OWF_PASSWORD NtOwfPassword,
  307. OUT PNT_RESPONSE NtResponse
  308. );
  309. //
  310. // Functions for calculating User Session Key.
  311. //
  312. //
  313. // Calculate a User Session Key from LM data
  314. //
  315. NTSTATUS
  316. RtlCalculateUserSessionKeyLm(
  317. IN PLM_RESPONSE LmResponse,
  318. IN PLM_OWF_PASSWORD LmOwfPassword,
  319. OUT PUSER_SESSION_KEY UserSessionKey
  320. );
  321. //
  322. // Calculate a User Session Key from NT data
  323. //
  324. NTSTATUS
  325. RtlCalculateUserSessionKeyNt(
  326. IN PNT_RESPONSE NtResponse,
  327. IN PNT_OWF_PASSWORD NtOwfPassword,
  328. OUT PUSER_SESSION_KEY UserSessionKey
  329. );
  330. //
  331. // OwfPassword encryption functions
  332. //
  333. //
  334. // Encrypt OwfPassword using OwfPassword as the key
  335. //
  336. NTSTATUS
  337. RtlEncryptLmOwfPwdWithLmOwfPwd(
  338. IN PLM_OWF_PASSWORD DataLmOwfPassword,
  339. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  340. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  341. );
  342. NTSTATUS
  343. RtlDecryptLmOwfPwdWithLmOwfPwd(
  344. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  345. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  346. OUT PLM_OWF_PASSWORD DataLmOwfPassword
  347. );
  348. NTSTATUS
  349. RtlEncryptNtOwfPwdWithNtOwfPwd(
  350. IN PNT_OWF_PASSWORD DataNtOwfPassword,
  351. IN PNT_OWF_PASSWORD KeyNtOwfPassword,
  352. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  353. );
  354. NTSTATUS
  355. RtlDecryptNtOwfPwdWithNtOwfPwd(
  356. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  357. IN PNT_OWF_PASSWORD KeyNtOwfPassword,
  358. OUT PNT_OWF_PASSWORD DataNtOwfPassword
  359. );
  360. //
  361. // Encrypt OwfPassword using SessionKey as the key
  362. //
  363. NTSTATUS
  364. RtlEncryptLmOwfPwdWithLmSesKey(
  365. IN PLM_OWF_PASSWORD LmOwfPassword,
  366. IN PLM_SESSION_KEY LmSessionKey,
  367. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  368. );
  369. NTSTATUS
  370. RtlDecryptLmOwfPwdWithLmSesKey(
  371. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  372. IN PLM_SESSION_KEY LmSessionKey,
  373. OUT PLM_OWF_PASSWORD LmOwfPassword
  374. );
  375. NTSTATUS
  376. RtlEncryptNtOwfPwdWithNtSesKey(
  377. IN PNT_OWF_PASSWORD NtOwfPassword,
  378. IN PNT_SESSION_KEY NtSessionKey,
  379. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  380. );
  381. NTSTATUS
  382. RtlDecryptNtOwfPwdWithNtSesKey(
  383. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  384. IN PNT_SESSION_KEY NtSessionKey,
  385. OUT PNT_OWF_PASSWORD NtOwfPassword
  386. );
  387. //
  388. // Encrypt OwfPassword using UserSessionKey as the key
  389. //
  390. NTSTATUS
  391. RtlEncryptLmOwfPwdWithUserKey(
  392. IN PLM_OWF_PASSWORD LmOwfPassword,
  393. IN PUSER_SESSION_KEY UserSessionKey,
  394. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  395. );
  396. NTSTATUS
  397. RtlDecryptLmOwfPwdWithUserKey(
  398. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  399. IN PUSER_SESSION_KEY UserSessionKey,
  400. OUT PLM_OWF_PASSWORD LmOwfPassword
  401. );
  402. NTSTATUS
  403. RtlEncryptNtOwfPwdWithUserKey(
  404. IN PNT_OWF_PASSWORD NtOwfPassword,
  405. IN PUSER_SESSION_KEY UserSessionKey,
  406. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  407. );
  408. NTSTATUS
  409. RtlDecryptNtOwfPwdWithUserKey(
  410. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  411. IN PUSER_SESSION_KEY UserSessionKey,
  412. OUT PNT_OWF_PASSWORD NtOwfPassword
  413. );
  414. //
  415. // Encrypt OwfPassword using an index as the key
  416. //
  417. NTSTATUS
  418. RtlEncryptLmOwfPwdWithIndex(
  419. IN PLM_OWF_PASSWORD LmOwfPassword,
  420. IN PCRYPT_INDEX Index,
  421. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  422. );
  423. NTSTATUS
  424. RtlDecryptLmOwfPwdWithIndex(
  425. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  426. IN PCRYPT_INDEX Index,
  427. OUT PLM_OWF_PASSWORD LmOwfPassword
  428. );
  429. NTSTATUS
  430. RtlEncryptNtOwfPwdWithIndex(
  431. IN PNT_OWF_PASSWORD NtOwfPassword,
  432. IN PCRYPT_INDEX Index,
  433. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  434. );
  435. NTSTATUS
  436. RtlDecryptNtOwfPwdWithIndex(
  437. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  438. IN PCRYPT_INDEX Index,
  439. OUT PNT_OWF_PASSWORD NtOwfPassword
  440. );
  441. ULONG
  442. RtlCheckSignatureInFile(
  443. IN PWSTR File
  444. );
  445. BOOLEAN
  446. RtlGenRandom(
  447. OUT PVOID RandomBuffer,
  448. IN ULONG RandomBufferLength
  449. );
  450. // begin_ntsecapi
  451. //
  452. // The buffer passed into RtlEncryptMemory and RtlDecryptMemory
  453. // must be a multiple of this length.
  454. //
  455. #define RTL_ENCRYPT_MEMORY_SIZE 8
  456. //
  457. // Allow Encrypt/Decrypt across process boundaries.
  458. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory.
  459. //
  460. #define RTL_ENCRYPT_OPTION_CROSS_PROCESS 0x01
  461. //
  462. // Allow Encrypt/Decrypt across callers with same LogonId.
  463. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory whilst impersonating.
  464. //
  465. #define RTL_ENCRYPT_OPTION_SAME_LOGON 0x02
  466. NTSTATUS
  467. RtlEncryptMemory(
  468. IN OUT PVOID Memory,
  469. IN ULONG MemoryLength,
  470. IN ULONG OptionFlags
  471. );
  472. NTSTATUS
  473. RtlDecryptMemory(
  474. IN OUT PVOID Memory,
  475. IN ULONG MemoryLength,
  476. IN ULONG OptionFlags
  477. );
  478. // end_ntsecapi
  479. //
  480. // Get the user session key for an RPC connection
  481. //
  482. #ifndef MIDL_PASS // Don't confuse MIDL
  483. NTSTATUS
  484. RtlGetUserSessionKeyClient(
  485. IN PVOID RpcContextHandle,
  486. OUT PUSER_SESSION_KEY UserSessionKey
  487. );
  488. NTSTATUS
  489. RtlGetUserSessionKeyClientBinding(
  490. IN PVOID RpcBindingHandle,
  491. OUT HANDLE *RedirHandle,
  492. OUT PUSER_SESSION_KEY UserSessionKey
  493. );
  494. NTSTATUS
  495. RtlGetUserSessionKeyServer(
  496. IN PVOID RpcContextHandle OPTIONAL,
  497. OUT PUSER_SESSION_KEY UserSessionKey
  498. );
  499. #endif // MIDL_PASS
  500. #ifdef __cplusplus
  501. } // extern "C"
  502. #endif
  503. #endif // _NTCRYPT_