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.

391 lines
10 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1999
  6. //
  7. // File: fipscryp.c
  8. //
  9. // Contents: Base level stuff for the FIPS mode crypto device driver
  10. //
  11. //
  12. // History: 04 JAN 00, Jeffspel Stolen from KSECDD
  13. // FEB 00, kschutz
  14. //
  15. //------------------------------------------------------------------------
  16. #include <ntddk.h>
  17. #include <fipsapi.h>
  18. #include <fipslog.h>
  19. #include <randlib.h>
  20. #pragma hdrstop
  21. NTSTATUS SelfMACCheck(IN LPWSTR pszImage);
  22. NTSTATUS AlgorithmCheck(void);
  23. /*
  24. typedef struct _DEVICE_EXTENSION {
  25. ULONG OpenCount;
  26. PVOID CodeHandle;
  27. PVOID DataHandle;
  28. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  29. */
  30. VOID
  31. FipsLogError(
  32. IN PVOID Object,
  33. IN NTSTATUS ErrorCode,
  34. IN PUNICODE_STRING Insertion,
  35. IN ULONG DumpData
  36. )
  37. /*++
  38. Routine Description:
  39. This routine allocates an error log entry, copies the supplied data
  40. to it, and requests that it be written to the error log file.
  41. Arguments:
  42. DeviceObject - Supplies a pointer to the device object associated
  43. with the device that had the error, early in
  44. initialization, one may not yet exist.
  45. Insertion - An insertion string that can be used to log
  46. additional data. Note that the message file
  47. needs %2 for this insertion, since %1
  48. is the name of the driver
  49. ErrorCode - Supplies the IO status for this particular error.
  50. DumpData - One word to dump
  51. Return Value:
  52. None.
  53. --*/
  54. {
  55. PIO_ERROR_LOG_PACKET errorLogEntry;
  56. errorLogEntry = IoAllocateErrorLogEntry(
  57. Object,
  58. (UCHAR) (
  59. sizeof(IO_ERROR_LOG_PACKET) +
  60. (Insertion ? Insertion->Length + sizeof(WCHAR) : 0)
  61. )
  62. );
  63. ASSERT(errorLogEntry != NULL);
  64. if (errorLogEntry == NULL) {
  65. return;
  66. }
  67. errorLogEntry->ErrorCode = ErrorCode;
  68. errorLogEntry->SequenceNumber = 0;
  69. errorLogEntry->MajorFunctionCode = 0;
  70. errorLogEntry->RetryCount = 0;
  71. errorLogEntry->UniqueErrorValue = 0;
  72. errorLogEntry->FinalStatus = STATUS_SUCCESS;
  73. errorLogEntry->DumpDataSize = (DumpData ? sizeof(ULONG) : 0);
  74. errorLogEntry->DumpData[0] = DumpData;
  75. if (Insertion) {
  76. errorLogEntry->StringOffset =
  77. sizeof(IO_ERROR_LOG_PACKET);
  78. errorLogEntry->NumberOfStrings = 1;
  79. RtlCopyMemory(
  80. ((PCHAR)(errorLogEntry) + errorLogEntry->StringOffset),
  81. Insertion->Buffer,
  82. Insertion->Length
  83. );
  84. }
  85. IoWriteErrorLogEntry(errorLogEntry);
  86. }
  87. NTSTATUS
  88. FipsCreateClose(
  89. IN PDEVICE_OBJECT DeviceObject,
  90. IN PIRP Irp
  91. )
  92. {
  93. //PDEVICE_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  94. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  95. /*
  96. if (irpStack->MajorFunction == IRP_MJ_CREATE) {
  97. if (deviceExtension->OpenCount++ == 0) {
  98. extern DWORD Spbox[8][64];
  99. deviceExtension->CodeHandle = MmLockPagableCodeSection(A_SHAInit);
  100. MmLockPagableSectionByHandle(deviceExtension->CodeHandle);
  101. deviceExtension->DataHandle = MmLockPagableDataSection(Spbox);
  102. MmLockPagableSectionByHandle(deviceExtension->DataHandle);
  103. }
  104. } else {
  105. if (--deviceExtension->OpenCount == 0) {
  106. MmUnlockPagableImageSection(deviceExtension->CodeHandle);
  107. MmUnlockPagableImageSection(deviceExtension->DataHandle);
  108. }
  109. }
  110. */
  111. Irp->IoStatus.Information = 0;
  112. Irp->IoStatus.Status = STATUS_SUCCESS;
  113. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  114. return STATUS_SUCCESS;
  115. }
  116. NTSTATUS
  117. FipsDeviceControl(
  118. IN PDEVICE_OBJECT DeviceObject,
  119. IN PIRP Irp
  120. )
  121. {
  122. PIO_STACK_LOCATION ioStackLocation = IoGetCurrentIrpStackLocation(Irp);
  123. ULONG ioControlCode = ioStackLocation->Parameters.DeviceIoControl.IoControlCode;
  124. NTSTATUS status = STATUS_NOT_SUPPORTED;
  125. Irp->IoStatus.Information = 0;
  126. switch (ioControlCode) {
  127. FIPS_FUNCTION_TABLE FipsFuncs;
  128. FIPS_FUNCTION_TABLE_1 OldFipsFuncsV1;
  129. case IOCTL_FIPS_GET_FUNCTION_TABLE:
  130. if (ioStackLocation->Parameters.DeviceIoControl.OutputBufferLength >=
  131. sizeof(FipsFuncs)) {
  132. FipsFuncs.FipsDesKey = FipsDesKey;
  133. FipsFuncs.FipsDes = FipsDes;
  134. FipsFuncs.Fips3Des3Key = Fips3Des3Key;
  135. FipsFuncs.Fips3Des = Fips3Des;
  136. FipsFuncs.FipsSHAInit = FipsSHAInit;
  137. FipsFuncs.FipsSHAUpdate = FipsSHAUpdate;
  138. FipsFuncs.FipsSHAFinal = FipsSHAFinal;
  139. FipsFuncs.FipsCBC = FipsCBC;
  140. FipsFuncs.FIPSGenRandom = FIPSGenRandom;
  141. FipsFuncs.FipsBlockCBC = FipsBlockCBC;
  142. FipsFuncs.FipsHmacSHAInit = FipsHmacSHAInit;
  143. FipsFuncs.FipsHmacSHAUpdate = FipsHmacSHAUpdate;
  144. FipsFuncs.FipsHmacSHAFinal = FipsHmacSHAFinal;
  145. FipsFuncs.HmacMD5Init = HmacMD5Init;
  146. FipsFuncs.HmacMD5Update = HmacMD5Update;
  147. FipsFuncs.HmacMD5Final = HmacMD5Final;
  148. *((PFIPS_FUNCTION_TABLE) Irp->AssociatedIrp.SystemBuffer) =
  149. FipsFuncs;
  150. Irp->IoStatus.Information = sizeof(FipsFuncs);
  151. status = STATUS_SUCCESS;
  152. } else if (ioStackLocation->Parameters.DeviceIoControl.OutputBufferLength >=
  153. sizeof(OldFipsFuncsV1)) {
  154. OldFipsFuncsV1.FipsDesKey = FipsDesKey;
  155. OldFipsFuncsV1.FipsDes = FipsDes;
  156. OldFipsFuncsV1.Fips3Des3Key = Fips3Des3Key;
  157. OldFipsFuncsV1.Fips3Des = Fips3Des;
  158. OldFipsFuncsV1.FipsSHAInit = FipsSHAInit;
  159. OldFipsFuncsV1.FipsSHAUpdate = FipsSHAUpdate;
  160. OldFipsFuncsV1.FipsSHAFinal = FipsSHAFinal;
  161. OldFipsFuncsV1.FipsCBC = FipsCBC;
  162. OldFipsFuncsV1.FIPSGenRandom = FIPSGenRandom;
  163. OldFipsFuncsV1.FipsBlockCBC = FipsBlockCBC;
  164. *((PFIPS_FUNCTION_TABLE_1) Irp->AssociatedIrp.SystemBuffer) =
  165. OldFipsFuncsV1;
  166. Irp->IoStatus.Information = sizeof(OldFipsFuncsV1);
  167. status = STATUS_SUCCESS;
  168. } else {
  169. status = STATUS_BUFFER_TOO_SMALL;
  170. }
  171. break;
  172. }
  173. Irp->IoStatus.Status = status;
  174. IoCompleteRequest(
  175. Irp,
  176. IO_NO_INCREMENT
  177. );
  178. return status;
  179. }
  180. VOID
  181. FipsUnload(
  182. IN PDRIVER_OBJECT DriverObject
  183. )
  184. {
  185. PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject;
  186. ShutdownRNG(NULL);
  187. // Delete the device from the system
  188. IoDeleteDevice(deviceObject);
  189. FipsDebug(
  190. DEBUG_TRACE,
  191. ("Fips driver unloaded\n")
  192. );
  193. }
  194. NTSTATUS
  195. DriverEntry(
  196. IN PDRIVER_OBJECT DriverObject,
  197. IN PUNICODE_STRING RegistryPath
  198. )
  199. /*++
  200. Routine Description:
  201. This is the initialization routine for the synchronous NULL device driver.
  202. This routine creates the device object for the NullS device and performs
  203. all other driver initialization.
  204. Arguments:
  205. DriverObject - Pointer to driver object created by the system.
  206. Return Value:
  207. The function value is the final status from the initialization operation.
  208. --*/
  209. {
  210. RTL_QUERY_REGISTRY_TABLE paramTable[2];
  211. UNICODE_STRING fileName, deviceName;
  212. PDEVICE_OBJECT deviceObject = NULL;
  213. NTSTATUS status;
  214. BOOL rngInitialized = FALSE;
  215. __try {
  216. //
  217. // Create the device object.
  218. //
  219. RtlInitUnicodeString(
  220. &deviceName,
  221. FIPS_DEVICE_NAME
  222. );
  223. status = IoCreateDevice(
  224. DriverObject,
  225. 0 /*sizeof(DEVICE_EXTENSION)*/,
  226. &deviceName,
  227. FILE_DEVICE_FIPS,
  228. 0,
  229. FALSE,
  230. &deviceObject
  231. );
  232. if (!NT_SUCCESS( status )) {
  233. __leave;
  234. }
  235. deviceObject->Flags |= DO_BUFFERED_IO;
  236. // initialize the device extension
  237. /*
  238. RtlZeroMemory(
  239. deviceObject->DeviceExtension,
  240. sizeof(PDEVICE_EXTENSION)
  241. );
  242. */
  243. // append the name of our driver to the system root path
  244. RtlInitUnicodeString(
  245. &fileName,
  246. L"\\SystemRoot\\System32\\Drivers\\fips.sys"
  247. );
  248. status = SelfMACCheck(fileName.Buffer);
  249. if (!NT_SUCCESS(status)) {
  250. FipsLogError(
  251. DriverObject,
  252. FIPS_MAC_INCORRECT,
  253. NULL,
  254. status
  255. );
  256. __leave;
  257. }
  258. InitializeRNG(NULL);
  259. rngInitialized = TRUE;
  260. status = AlgorithmCheck();
  261. if (!NT_SUCCESS(status)) {
  262. FipsLogError(
  263. DriverObject,
  264. FIPS_SELF_TEST_FAILURE,
  265. NULL,
  266. status
  267. );
  268. __leave;
  269. }
  270. //
  271. // Initialize the driver object with this device driver's entry points.
  272. //
  273. DriverObject->DriverUnload = FipsUnload;
  274. DriverObject->MajorFunction[IRP_MJ_CREATE] = FipsCreateClose;
  275. DriverObject->MajorFunction[IRP_MJ_CLOSE] = FipsCreateClose;
  276. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = FipsDeviceControl ;
  277. }
  278. __finally {
  279. if (status != STATUS_SUCCESS) {
  280. if (rngInitialized) {
  281. ShutdownRNG(NULL);
  282. }
  283. if (deviceObject) {
  284. IoDeleteDevice(deviceObject);
  285. }
  286. }
  287. }
  288. return status;
  289. }