ReactOS  0.4.13-dev-544-gede3fdd
random.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Drivers
4  * PURPOSE: Kernel Security Support Provider Interface Driver
5  *
6  * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "ksecdd.h"
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 
17 /* GLOBALS ********************************************************************/
18 
19 static ULONG KsecRandomSeed = 0x62b409a1;
20 
21 
22 /* FUNCTIONS ******************************************************************/
23 
25 NTAPI
27  PVOID Buffer,
28  SIZE_T Length)
29 {
30  LARGE_INTEGER TickCount;
31  ULONG i, RandomValue;
32  PULONG P;
33 
34  /* Try to generate a more random seed */
35  KeQueryTickCount(&TickCount);
36  KsecRandomSeed ^= _rotl(TickCount.LowPart, (KsecRandomSeed % 23));
37 
38  P = Buffer;
39  for (i = 0; i < Length / sizeof(ULONG); i++)
40  {
42  }
43 
44  Length &= (sizeof(ULONG) - 1);
45  if (Length > 0)
46  {
47  RandomValue = RtlRandomEx(&KsecRandomSeed);
48  RtlCopyMemory(&P[i], &RandomValue, Length);
49  }
50 
51  return STATUS_SUCCESS;
52 }
53 
54 VOID
55 NTAPI
57  _Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters)
58 {
59 #if defined(_M_IX86) || defined(_M_AMD64)
60  /* Check if RDTSC is available */
62  {
63  /* Read the TSC value */
64  MachineSpecificCounters->Tsc = __rdtsc();
65  }
66 #if 0 // FIXME: investigate what the requirements are for these
67  /* Read the CPU event counter MSRs */
68  //MachineSpecificCounters->Ctr0 = __readmsr(0x12);
69  //MachineSpecificCounters->Ctr1 = __readmsr(0x13);
70 
71  /* Check if this is an MMX capable CPU */
73  {
74  /* Read the CPU performance counters 0 and 1 */
75  MachineSpecificCounters->Pmc0 = __readpmc(0);
76  MachineSpecificCounters->Pmc1 = __readpmc(1);
77  }
78 #endif
79 #elif defined(_M_ARM)
80  /* Read the Cycle Counter Register */
81  MachineSpecificCounters->Ccr = _MoveFromCoprocessor(CP15_PMCCNTR);
82 #else
83  #error Implement me!
84 #endif
85 }
86 
91 NTAPI
93  PKSEC_ENTROPY_DATA EntropyData)
94 {
95  MD4_CTX Md4Context;
96  PTEB Teb;
97  PPEB Peb;
98  PWSTR String;
101 
102  /* Query some generic values */
103  EntropyData->CurrentProcessId = PsGetCurrentProcessId();
104  EntropyData->CurrentThreadId = PsGetCurrentThreadId();
105  KeQueryTickCount(&EntropyData->TickCount);
106  KeQuerySystemTime(&EntropyData->SystemTime);
108  &EntropyData->PerformanceFrequency);
109 
110  /* Check if we have a TEB/PEB for the process environment */
111  Teb = PsGetCurrentThread()->Tcb.Teb;
112  if (Teb != NULL)
113  {
115 
116  /* Initialize the MD4 context */
117  MD4Init(&Md4Context);
118  _SEH2_TRY
119  {
120  /* Get the end of the environment */
122  while (*String)
123  {
124  String += wcslen(String) + 1;
125  }
126 
127  /* Update the MD4 context from the environment data */
128  MD4Update(&Md4Context,
131  }
133  {
134  /* Simply ignore the exception */
135  }
136  _SEH2_END;
137 
138  /* Finalize and copy the MD4 hash */
139  MD4Final(&Md4Context);
140  RtlCopyMemory(&EntropyData->EnvironmentHash, Md4Context.digest, 16);
141  }
142 
143  /* Read some machine specific hardware counters */
145 
146  /* Query processor performance information */
150  &ReturnLength);
151  if (!NT_SUCCESS(Status))
152  {
153  return Status;
154  }
155 
156  /* Query system performance information */
158  &EntropyData->SystemPerformanceInformation,
160  &ReturnLength);
161  if (!NT_SUCCESS(Status))
162  {
163  return Status;
164  }
165 
166  /* Query exception information */
168  &EntropyData->SystemExceptionInformation,
170  &ReturnLength);
171  if (!NT_SUCCESS(Status))
172  {
173  return Status;
174  }
175 
176  /* Query lookaside information */
178  &EntropyData->SystemLookasideInformation,
180  &ReturnLength);
181  if (!NT_SUCCESS(Status))
182  {
183  return Status;
184  }
185 
186  /* Query interrupt information */
188  &EntropyData->SystemInterruptInformation,
190  &ReturnLength);
191  if (!NT_SUCCESS(Status))
192  {
193  return Status;
194  }
195 
196  /* Query process information */
198  &EntropyData->SystemProcessInformation,
200  &ReturnLength);
201  if (!NT_SUCCESS(Status))
202  {
203  return Status;
204  }
205 
206  return STATUS_SUCCESS;
207 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
unsigned char digest[16]
Definition: util.c:86
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
ULONG NTAPI RtlRandomEx(IN OUT PULONG Seed)
Definition: random.c:150
PPEB Peb
Definition: dllmain.c:27
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
SYSTEM_PROCESS_INFORMATION SystemProcessInformation
Definition: ksecdd.h:71
static ULONG KsecRandomSeed
Definition: random.c:19
SYSTEM_EXCEPTION_INFORMATION SystemExceptionInformation
Definition: ksecdd.h:68
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
BOOLEAN NTAPI ExIsProcessorFeaturePresent(IN ULONG ProcessorFeature)
Definition: sysinfo.c:363
#define CP15_PMCCNTR
Definition: ketypes.h:113
uint16_t * PWSTR
Definition: typedefs.h:54
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
static WCHAR String[]
Definition: stringtable.c:55
_SEH2_TRY
Definition: create.c:4250
SYSTEM_PERFORMANCE_INFORMATION SystemPerformanceInformation
Definition: ksecdd.h:67
VOID WINAPI MD4Init(MD4_CTX *ctx)
Definition: md4.c:45
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
SYSTEM_INTERRUPT_INFORMATION SystemInterruptInformation
Definition: ksecdd.h:70
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
HANDLE CurrentProcessId
Definition: ksecdd.h:58
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
NTSYSAPI NTSTATUS NTAPI ZwQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
smooth NULL
Definition: ftsmooth.c:416
#define _Out_
Definition: no_sal2.h:323
LARGE_INTEGER PerformanceFrequency
Definition: ksecdd.h:63
PsGetCurrentThreadId
Definition: CrNtStubs.h:7
Definition: bufpool.h:45
#define SystemPerformanceInformation
Definition: memtest.h:87
VOID NTAPI KeQueryTickCount(IN PLARGE_INTEGER TickCount)
Definition: clock.c:165
UCHAR EnvironmentHash[16]
Definition: ksecdd.h:64
VOID NTAPI KsecReadMachineSpecificCounters(_Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters)
Definition: random.c:56
LARGE_INTEGER SystemTime
Definition: ksecdd.h:61
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID WINAPI MD4Final(MD4_CTX *ctx)
Definition: md4.c:113
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
LARGE_INTEGER TickCount
Definition: ksecdd.h:60
NTSTATUS NTAPI KsecGatherEntropyData(PKSEC_ENTROPY_DATA EntropyData)
Definition: random.c:92
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1868
ULONG LowPart
Definition: typedefs.h:104
#define P(row, col)
Status
Definition: gdiplustypes.h:24
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorPerformanceInformation
Definition: ksecdd.h:66
__INTRIN_INLINE unsigned long long __readpmc(unsigned long counter)
Definition: intrin_x86.h:1895
ULONG_PTR SIZE_T
Definition: typedefs.h:78
Definition: compat.h:484
KSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters
Definition: ksecdd.h:65
#define PF_RDTSC_INSTRUCTION_AVAILABLE
_SEH2_END
Definition: create.c:4424
Definition: util.c:81
ULONG * PKSEC_MACHINE_SPECIFIC_COUNTERS
Definition: ksecdd.h:53
SYSTEM_LOOKASIDE_INFORMATION SystemLookasideInformation
Definition: ksecdd.h:69
unsigned int * PULONG
Definition: retypes.h:1
NTSTATUS NTAPI KsecGenRandom(PVOID Buffer, SIZE_T Length)
Definition: random.c:26
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
_Check_return_ unsigned int __cdecl _rotl(_In_ unsigned int _Value, _In_ int _Shift)
unsigned int ULONG
Definition: retypes.h:1
VOID WINAPI MD4Update(MD4_CTX *ctx, const unsigned char *buf, unsigned int len)
Definition: md4.c:59
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:2777
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HANDLE CurrentThreadId
Definition: ksecdd.h:59
#define PF_MMX_INSTRUCTIONS_AVAILABLE
LARGE_INTEGER PerformanceCounter
Definition: ksecdd.h:62