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.

425 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. block.c
  5. Abstract:
  6. Block encryption functions implementation :
  7. RtlEncryptBlock
  8. RtlDecryptBlock
  9. RtlEncrypStdBlock
  10. Author:
  11. David Chalmers (Davidc) 10-21-91
  12. Revision History:
  13. Scott Field (sfield) 03-Nov-97
  14. Removed critical section around crypto calls.
  15. --*/
  16. #include <nt.h>
  17. #include <ntrtl.h>
  18. #include <crypt.h>
  19. #include <engine.h>
  20. #include <nturtl.h>
  21. #include <windows.h>
  22. #include <ntddksec.h>
  23. #ifndef KMODE
  24. HANDLE g_hKsecDD = NULL;
  25. VOID EncryptMemoryShutdown( VOID );
  26. #endif
  27. BOOLEAN
  28. Sys003Initialize(
  29. IN PVOID hmod,
  30. IN ULONG Reason,
  31. IN PCONTEXT Context
  32. )
  33. {
  34. #ifndef KMODE
  35. if( Reason == DLL_PROCESS_DETACH )
  36. {
  37. EncryptMemoryShutdown();
  38. }
  39. #endif
  40. return TRUE;
  41. DBG_UNREFERENCED_PARAMETER(hmod);
  42. DBG_UNREFERENCED_PARAMETER(Context);
  43. }
  44. NTSTATUS
  45. RtlEncryptBlock(
  46. IN PCLEAR_BLOCK ClearBlock,
  47. IN PBLOCK_KEY BlockKey,
  48. OUT PCYPHER_BLOCK CypherBlock
  49. )
  50. /*++
  51. Routine Description:
  52. Takes a block of data and encrypts it with a key producing
  53. an encrypted block of data.
  54. Arguments:
  55. ClearBlock - The block of data that is to be encrypted.
  56. BlockKey - The key to use to encrypt data
  57. CypherBlock - Encrypted data is returned here
  58. Return Values:
  59. STATUS_SUCCESS - The data was encrypted successfully. The encrypted
  60. data block is in CypherBlock
  61. STATUS_UNSUCCESSFUL - Something failed. The CypherBlock is undefined.
  62. --*/
  63. {
  64. unsigned Result;
  65. Result = DES_ECB_LM(ENCR_KEY,
  66. (const char *)BlockKey,
  67. (unsigned char *)ClearBlock,
  68. (unsigned char *)CypherBlock
  69. );
  70. if (Result == CRYPT_OK) {
  71. return(STATUS_SUCCESS);
  72. } else {
  73. #if DBG
  74. DbgPrint("EncryptBlock failed\n\r");
  75. #endif
  76. return(STATUS_UNSUCCESSFUL);
  77. }
  78. }
  79. NTSTATUS
  80. RtlDecryptBlock(
  81. IN PCYPHER_BLOCK CypherBlock,
  82. IN PBLOCK_KEY BlockKey,
  83. OUT PCLEAR_BLOCK ClearBlock
  84. )
  85. /*++
  86. Routine Description:
  87. Takes a block of encrypted data and decrypts it with a key producing
  88. the clear block of data.
  89. Arguments:
  90. CypherBlock - The block of data to be decrypted
  91. BlockKey - The key to use to decrypt data
  92. ClearBlock - The decrpted block of data is returned here
  93. Return Values:
  94. STATUS_SUCCESS - The data was decrypted successfully. The decrypted
  95. data block is in ClearBlock
  96. STATUS_UNSUCCESSFUL - Something failed. The ClearBlock is undefined.
  97. --*/
  98. {
  99. unsigned Result;
  100. Result = DES_ECB_LM(DECR_KEY,
  101. (const char *)BlockKey,
  102. (unsigned char *)CypherBlock,
  103. (unsigned char *)ClearBlock
  104. );
  105. if (Result == CRYPT_OK) {
  106. return(STATUS_SUCCESS);
  107. } else {
  108. #if DBG
  109. DbgPrint("DecryptBlock failed\n\r");
  110. #endif
  111. return(STATUS_UNSUCCESSFUL);
  112. }
  113. }
  114. NTSTATUS
  115. RtlEncryptStdBlock(
  116. IN PBLOCK_KEY BlockKey,
  117. OUT PCYPHER_BLOCK CypherBlock
  118. )
  119. /*++
  120. Routine Description:
  121. Takes a block key encrypts the standard text block with it.
  122. The resulting encrypted block is returned.
  123. This is a One-Way-Function - the key cannot be recovered from the
  124. encrypted data block.
  125. Arguments:
  126. BlockKey - The key to use to encrypt the standard text block.
  127. CypherBlock - The encrypted data is returned here
  128. Return Values:
  129. STATUS_SUCCESS - The encryption was successful.
  130. The result is in CypherBlock
  131. STATUS_UNSUCCESSFUL - Something failed. The CypherBlock is undefined.
  132. --*/
  133. {
  134. unsigned Result;
  135. char StdEncrPwd[] = "KGS!@#$%";
  136. Result = DES_ECB_LM(ENCR_KEY,
  137. (const char *)BlockKey,
  138. (unsigned char *)StdEncrPwd,
  139. (unsigned char *)CypherBlock
  140. );
  141. if (Result == CRYPT_OK) {
  142. return(STATUS_SUCCESS);
  143. } else {
  144. #if DBG
  145. DbgPrint("EncryptStd failed\n\r");
  146. #endif
  147. return(STATUS_UNSUCCESSFUL);
  148. }
  149. }
  150. #ifndef KMODE
  151. BOOLEAN
  152. EncryptMemoryInitialize(
  153. VOID
  154. )
  155. {
  156. UNICODE_STRING DriverName;
  157. OBJECT_ATTRIBUTES ObjA;
  158. IO_STATUS_BLOCK IOSB;
  159. HANDLE hFile;
  160. NTSTATUS Status;
  161. RtlInitUnicodeString( &DriverName, DD_KSEC_DEVICE_NAME_U );
  162. InitializeObjectAttributes(
  163. &ObjA,
  164. &DriverName,
  165. 0,
  166. NULL,
  167. NULL
  168. );
  169. //
  170. // needs to be non-alertable, else, the DeviceIoControl may return
  171. // STATUS_USER_APC.
  172. //
  173. Status = NtOpenFile(
  174. &hFile,
  175. SYNCHRONIZE | FILE_READ_DATA,
  176. &ObjA,
  177. &IOSB,
  178. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  179. FILE_SYNCHRONOUS_IO_NONALERT
  180. );
  181. if(!NT_SUCCESS(Status))
  182. {
  183. return FALSE;
  184. }
  185. if(InterlockedCompareExchangePointer(
  186. &g_hKsecDD,
  187. hFile,
  188. NULL
  189. ) != NULL)
  190. {
  191. NtClose( hFile );
  192. }
  193. return TRUE;
  194. }
  195. VOID
  196. EncryptMemoryShutdown(
  197. VOID
  198. )
  199. {
  200. if( g_hKsecDD != NULL )
  201. {
  202. NtClose( g_hKsecDD );
  203. g_hKsecDD = NULL;
  204. }
  205. }
  206. NTSTATUS
  207. RtlEncryptMemory(
  208. IN PVOID Memory,
  209. IN ULONG MemorySize,
  210. IN ULONG OptionFlags
  211. )
  212. {
  213. IO_STATUS_BLOCK IoStatus;
  214. ULONG IoControlCode;
  215. NTSTATUS Status;
  216. if( g_hKsecDD == NULL )
  217. {
  218. if(!EncryptMemoryInitialize())
  219. {
  220. return STATUS_UNSUCCESSFUL;
  221. }
  222. }
  223. switch( OptionFlags )
  224. {
  225. case 0:
  226. {
  227. IoControlCode = IOCTL_KSEC_ENCRYPT_MEMORY;
  228. break;
  229. }
  230. case RTL_ENCRYPT_OPTION_CROSS_PROCESS:
  231. {
  232. IoControlCode = IOCTL_KSEC_ENCRYPT_MEMORY_CROSS_PROC;
  233. break;
  234. }
  235. case RTL_ENCRYPT_OPTION_SAME_LOGON:
  236. {
  237. IoControlCode = IOCTL_KSEC_ENCRYPT_MEMORY_SAME_LOGON;
  238. break;
  239. }
  240. default:
  241. {
  242. return STATUS_INVALID_PARAMETER;
  243. }
  244. }
  245. Status = NtDeviceIoControlFile(
  246. g_hKsecDD,
  247. NULL,
  248. NULL,
  249. NULL,
  250. &IoStatus,
  251. IoControlCode,
  252. Memory,
  253. MemorySize, // output buffer size
  254. Memory,
  255. MemorySize
  256. );
  257. #if DBG
  258. if((Status != 0) && (Status != STATUS_INVALID_PARAMETER))
  259. {
  260. KdPrint(("EncryptMemory IOCTL failed= 0x%lx\n\r", Status));
  261. }
  262. #endif
  263. return Status;
  264. }
  265. NTSTATUS
  266. RtlDecryptMemory(
  267. IN PVOID Memory,
  268. IN ULONG MemorySize,
  269. IN ULONG OptionFlags
  270. )
  271. {
  272. IO_STATUS_BLOCK IoStatus;
  273. ULONG IoControlCode;
  274. NTSTATUS Status;
  275. if( g_hKsecDD == NULL )
  276. {
  277. if(!EncryptMemoryInitialize())
  278. {
  279. return STATUS_UNSUCCESSFUL;
  280. }
  281. }
  282. switch( OptionFlags )
  283. {
  284. case 0:
  285. {
  286. IoControlCode = IOCTL_KSEC_DECRYPT_MEMORY;
  287. break;
  288. }
  289. case RTL_ENCRYPT_OPTION_CROSS_PROCESS:
  290. {
  291. IoControlCode = IOCTL_KSEC_DECRYPT_MEMORY_CROSS_PROC;
  292. break;
  293. }
  294. case RTL_ENCRYPT_OPTION_SAME_LOGON:
  295. {
  296. IoControlCode = IOCTL_KSEC_DECRYPT_MEMORY_SAME_LOGON;
  297. break;
  298. }
  299. default:
  300. {
  301. return STATUS_INVALID_PARAMETER;
  302. }
  303. }
  304. Status = NtDeviceIoControlFile(
  305. g_hKsecDD,
  306. NULL,
  307. NULL,
  308. NULL,
  309. &IoStatus,
  310. IoControlCode,
  311. Memory,
  312. MemorySize, // output buffer size
  313. Memory,
  314. MemorySize
  315. );
  316. #if DBG
  317. if((Status != 0) && (Status != STATUS_INVALID_PARAMETER))
  318. {
  319. KdPrint(("DecryptMemory IOCTL failed= 0x%lx\n\r", Status));
  320. }
  321. #endif
  322. return Status;
  323. }
  324. #endif