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.

677 lines
17 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. #define RtlEncryptMemory SystemFunction040
  212. #define RtlDecryptMemory SystemFunction041
  213. ////////////////////////////////////////////////////////////////////////////
  214. // //
  215. // Encryption library API function prototypes //
  216. // //
  217. ////////////////////////////////////////////////////////////////////////////
  218. //
  219. // Core block encryption functions
  220. //
  221. NTSTATUS
  222. RtlEncryptBlock(
  223. IN PCLEAR_BLOCK ClearBlock,
  224. IN PBLOCK_KEY BlockKey,
  225. OUT PCYPHER_BLOCK CypherBlock
  226. );
  227. NTSTATUS
  228. RtlDecryptBlock(
  229. IN PCYPHER_BLOCK CypherBlock,
  230. IN PBLOCK_KEY BlockKey,
  231. OUT PCLEAR_BLOCK ClearBlock
  232. );
  233. NTSTATUS
  234. RtlEncryptStdBlock(
  235. IN PBLOCK_KEY BlockKey,
  236. OUT PCYPHER_BLOCK CypherBlock
  237. );
  238. //
  239. // Arbitrary length data encryption functions
  240. //
  241. NTSTATUS
  242. RtlEncryptData(
  243. IN PCLEAR_DATA ClearData,
  244. IN PDATA_KEY DataKey,
  245. OUT PCYPHER_DATA CypherData
  246. );
  247. NTSTATUS
  248. RtlDecryptData(
  249. IN PCYPHER_DATA CypherData,
  250. IN PDATA_KEY DataKey,
  251. OUT PCLEAR_DATA ClearData
  252. );
  253. //
  254. // Faster arbitrary length data encryption functions (using RC4)
  255. //
  256. NTSTATUS
  257. RtlEncryptData2(
  258. IN OUT PCRYPT_BUFFER pData,
  259. IN PDATA_KEY pKey
  260. );
  261. NTSTATUS
  262. RtlDecryptData2(
  263. IN OUT PCRYPT_BUFFER pData,
  264. IN PDATA_KEY pKey
  265. );
  266. //
  267. // Password hashing functions (One Way Function)
  268. //
  269. NTSTATUS
  270. RtlCalculateLmOwfPassword(
  271. IN PLM_PASSWORD LmPassword,
  272. OUT PLM_OWF_PASSWORD LmOwfPassword
  273. );
  274. NTSTATUS
  275. RtlCalculateNtOwfPassword(
  276. IN PNT_PASSWORD NtPassword,
  277. OUT PNT_OWF_PASSWORD NtOwfPassword
  278. );
  279. //
  280. // OWF password comparison functions
  281. //
  282. BOOLEAN
  283. RtlEqualLmOwfPassword(
  284. IN PLM_OWF_PASSWORD LmOwfPassword1,
  285. IN PLM_OWF_PASSWORD LmOwfPassword2
  286. );
  287. BOOLEAN
  288. RtlEqualNtOwfPassword(
  289. IN PNT_OWF_PASSWORD NtOwfPassword1,
  290. IN PNT_OWF_PASSWORD NtOwfPassword2
  291. );
  292. //
  293. // Functions for calculating response to server challenge
  294. //
  295. NTSTATUS
  296. RtlCalculateLmResponse(
  297. IN PLM_CHALLENGE LmChallenge,
  298. IN PLM_OWF_PASSWORD LmOwfPassword,
  299. OUT PLM_RESPONSE LmResponse
  300. );
  301. NTSTATUS
  302. RtlCalculateNtResponse(
  303. IN PNT_CHALLENGE NtChallenge,
  304. IN PNT_OWF_PASSWORD NtOwfPassword,
  305. OUT PNT_RESPONSE NtResponse
  306. );
  307. //
  308. // Functions for calculating User Session Key.
  309. //
  310. //
  311. // Calculate a User Session Key from LM data
  312. //
  313. NTSTATUS
  314. RtlCalculateUserSessionKeyLm(
  315. IN PLM_RESPONSE LmResponse,
  316. IN PLM_OWF_PASSWORD LmOwfPassword,
  317. OUT PUSER_SESSION_KEY UserSessionKey
  318. );
  319. //
  320. // Calculate a User Session Key from NT data
  321. //
  322. NTSTATUS
  323. RtlCalculateUserSessionKeyNt(
  324. IN PNT_RESPONSE NtResponse,
  325. IN PNT_OWF_PASSWORD NtOwfPassword,
  326. OUT PUSER_SESSION_KEY UserSessionKey
  327. );
  328. //
  329. // OwfPassword encryption functions
  330. //
  331. //
  332. // Encrypt OwfPassword using OwfPassword as the key
  333. //
  334. NTSTATUS
  335. RtlEncryptLmOwfPwdWithLmOwfPwd(
  336. IN PLM_OWF_PASSWORD DataLmOwfPassword,
  337. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  338. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  339. );
  340. NTSTATUS
  341. RtlDecryptLmOwfPwdWithLmOwfPwd(
  342. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  343. IN PLM_OWF_PASSWORD KeyLmOwfPassword,
  344. OUT PLM_OWF_PASSWORD DataLmOwfPassword
  345. );
  346. NTSTATUS
  347. RtlEncryptNtOwfPwdWithNtOwfPwd(
  348. IN PNT_OWF_PASSWORD DataNtOwfPassword,
  349. IN PNT_OWF_PASSWORD KeyNtOwfPassword,
  350. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  351. );
  352. NTSTATUS
  353. RtlDecryptNtOwfPwdWithNtOwfPwd(
  354. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  355. IN PNT_OWF_PASSWORD KeyNtOwfPassword,
  356. OUT PNT_OWF_PASSWORD DataNtOwfPassword
  357. );
  358. //
  359. // Encrypt OwfPassword using SessionKey as the key
  360. //
  361. NTSTATUS
  362. RtlEncryptLmOwfPwdWithLmSesKey(
  363. IN PLM_OWF_PASSWORD LmOwfPassword,
  364. IN PLM_SESSION_KEY LmSessionKey,
  365. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  366. );
  367. NTSTATUS
  368. RtlDecryptLmOwfPwdWithLmSesKey(
  369. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  370. IN PLM_SESSION_KEY LmSessionKey,
  371. OUT PLM_OWF_PASSWORD LmOwfPassword
  372. );
  373. NTSTATUS
  374. RtlEncryptNtOwfPwdWithNtSesKey(
  375. IN PNT_OWF_PASSWORD NtOwfPassword,
  376. IN PNT_SESSION_KEY NtSessionKey,
  377. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  378. );
  379. NTSTATUS
  380. RtlDecryptNtOwfPwdWithNtSesKey(
  381. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  382. IN PNT_SESSION_KEY NtSessionKey,
  383. OUT PNT_OWF_PASSWORD NtOwfPassword
  384. );
  385. //
  386. // Encrypt OwfPassword using UserSessionKey as the key
  387. //
  388. NTSTATUS
  389. RtlEncryptLmOwfPwdWithUserKey(
  390. IN PLM_OWF_PASSWORD LmOwfPassword,
  391. IN PUSER_SESSION_KEY UserSessionKey,
  392. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  393. );
  394. NTSTATUS
  395. RtlDecryptLmOwfPwdWithUserKey(
  396. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  397. IN PUSER_SESSION_KEY UserSessionKey,
  398. OUT PLM_OWF_PASSWORD LmOwfPassword
  399. );
  400. NTSTATUS
  401. RtlEncryptNtOwfPwdWithUserKey(
  402. IN PNT_OWF_PASSWORD NtOwfPassword,
  403. IN PUSER_SESSION_KEY UserSessionKey,
  404. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  405. );
  406. NTSTATUS
  407. RtlDecryptNtOwfPwdWithUserKey(
  408. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  409. IN PUSER_SESSION_KEY UserSessionKey,
  410. OUT PNT_OWF_PASSWORD NtOwfPassword
  411. );
  412. //
  413. // Encrypt OwfPassword using an index as the key
  414. //
  415. NTSTATUS
  416. RtlEncryptLmOwfPwdWithIndex(
  417. IN PLM_OWF_PASSWORD LmOwfPassword,
  418. IN PCRYPT_INDEX Index,
  419. OUT PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword
  420. );
  421. NTSTATUS
  422. RtlDecryptLmOwfPwdWithIndex(
  423. IN PENCRYPTED_LM_OWF_PASSWORD EncryptedLmOwfPassword,
  424. IN PCRYPT_INDEX Index,
  425. OUT PLM_OWF_PASSWORD LmOwfPassword
  426. );
  427. NTSTATUS
  428. RtlEncryptNtOwfPwdWithIndex(
  429. IN PNT_OWF_PASSWORD NtOwfPassword,
  430. IN PCRYPT_INDEX Index,
  431. OUT PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword
  432. );
  433. NTSTATUS
  434. RtlDecryptNtOwfPwdWithIndex(
  435. IN PENCRYPTED_NT_OWF_PASSWORD EncryptedNtOwfPassword,
  436. IN PCRYPT_INDEX Index,
  437. OUT PNT_OWF_PASSWORD NtOwfPassword
  438. );
  439. ULONG
  440. RtlCheckSignatureInFile(
  441. IN PWSTR File
  442. );
  443. BOOLEAN
  444. RtlGenRandom(
  445. OUT PVOID RandomBuffer,
  446. IN ULONG RandomBufferLength
  447. );
  448. //
  449. // The buffer passed into RtlEncryptMemory and RtlDecryptMemory
  450. // must be a multiple of this length.
  451. //
  452. #define RTL_ENCRYPT_MEMORY_SIZE 8
  453. //
  454. // Allow Encrypt/Decrypt across process boundaries.
  455. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory.
  456. //
  457. #define RTL_ENCRYPT_OPTION_CROSS_PROCESS 0x01
  458. //
  459. // Allow Encrypt/Decrypt across callers with same LogonId.
  460. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory whilst impersonating.
  461. //
  462. #define RTL_ENCRYPT_OPTION_SAME_LOGON 0x02
  463. NTSTATUS
  464. RtlEncryptMemory(
  465. IN OUT PVOID Memory,
  466. IN ULONG MemoryLength,
  467. IN ULONG OptionFlags
  468. );
  469. NTSTATUS
  470. RtlDecryptMemory(
  471. IN OUT PVOID Memory,
  472. IN ULONG MemoryLength,
  473. IN ULONG OptionFlags
  474. );
  475. //
  476. // Get the user session key for an RPC connection
  477. //
  478. #ifndef MIDL_PASS // Don't confuse MIDL
  479. NTSTATUS
  480. RtlGetUserSessionKeyClient(
  481. IN PVOID RpcContextHandle,
  482. OUT PUSER_SESSION_KEY UserSessionKey
  483. );
  484. NTSTATUS
  485. RtlGetUserSessionKeyClientBinding(
  486. IN PVOID RpcBindingHandle,
  487. OUT HANDLE *RedirHandle,
  488. OUT PUSER_SESSION_KEY UserSessionKey
  489. );
  490. NTSTATUS
  491. RtlGetUserSessionKeyServer(
  492. IN PVOID RpcContextHandle OPTIONAL,
  493. OUT PUSER_SESSION_KEY UserSessionKey
  494. );
  495. #endif // MIDL_PASS
  496. #ifdef __cplusplus
  497. } // extern "C"
  498. #endif
  499. #endif // _NTCRYPT_