ReactOS 0.4.15-dev-7958-gcd0bb1a
random.c File Reference
#include "ksecdd.h"
#include <debug.h>
Include dependency graph for random.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI KsecGenRandom (PVOID Buffer, SIZE_T Length)
 
VOID NTAPI KsecReadMachineSpecificCounters (_Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters)
 
NTSTATUS NTAPI KsecGatherEntropyData (PKSEC_ENTROPY_DATA EntropyData)
 

Variables

static ULONG KsecRandomSeed = 0x62b409a1
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file random.c.

Function Documentation

◆ KsecGatherEntropyData()

NTSTATUS NTAPI KsecGatherEntropyData ( PKSEC_ENTROPY_DATA  EntropyData)
See also
http://blogs.msdn.com/b/michael_howard/archive/2005/01/14/353379.aspx

Definition at line 92 of file random.c.

94{
95 MD4_CTX Md4Context;
96 PTEB Teb;
97 PPEB Peb;
101
102 /* Query some generic values */
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);
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}
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PPEB Peb
Definition: dllmain.c:27
VOID NTAPI KsecReadMachineSpecificCounters(_Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters)
Definition: random.c:56
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:43
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
PsGetCurrentThreadId
Definition: CrNtStubs.h:8
NTSYSAPI NTSTATUS NTAPI ZwQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
@ SystemExceptionInformation
Definition: ntddk_ex.h:44
@ SystemLookasideInformation
Definition: ntddk_ex.h:60
@ SystemInterruptInformation
Definition: ntddk_ex.h:34
@ SystemProcessInformation
Definition: ntddk_ex.h:16
@ SystemProcessorPerformanceInformation
Definition: ntddk_ex.h:19
Status
Definition: gdiplustypes.h:25
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
VOID WINAPI MD4Final(MD4_CTX *ctx)
Definition: md4.c:113
VOID WINAPI MD4Update(MD4_CTX *ctx, const unsigned char *buf, unsigned int len)
Definition: md4.c:59
VOID WINAPI MD4Init(MD4_CTX *ctx)
Definition: md4.c:45
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define SystemPerformanceInformation
Definition: memtest.h:87
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define KeQueryTickCount(CurrentCount)
Definition: ke.h:43
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: util.c:82
unsigned char digest[16]
Definition: util.c:86
SYSTEM_PROCESS_INFORMATION SystemProcessInformation
Definition: ksecdd.h:71
HANDLE CurrentProcessId
Definition: ksecdd.h:58
UCHAR EnvironmentHash[16]
Definition: ksecdd.h:64
KSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters
Definition: ksecdd.h:65
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorPerformanceInformation
Definition: ksecdd.h:66
LARGE_INTEGER TickCount
Definition: ksecdd.h:60
SYSTEM_EXCEPTION_INFORMATION SystemExceptionInformation
Definition: ksecdd.h:68
SYSTEM_PERFORMANCE_INFORMATION SystemPerformanceInformation
Definition: ksecdd.h:67
LARGE_INTEGER PerformanceFrequency
Definition: ksecdd.h:63
SYSTEM_INTERRUPT_INFORMATION SystemInterruptInformation
Definition: ksecdd.h:70
HANDLE CurrentThreadId
Definition: ksecdd.h:59
LARGE_INTEGER SystemTime
Definition: ksecdd.h:61
LARGE_INTEGER PerformanceCounter
Definition: ksecdd.h:62
SYSTEM_LOOKASIDE_INFORMATION SystemLookasideInformation
Definition: ksecdd.h:69
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1913
Definition: compat.h:836
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
uint16_t * PWSTR
Definition: typedefs.h:56
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433

Referenced by KsecInitializeEncryptionSupport().

◆ KsecGenRandom()

NTSTATUS NTAPI KsecGenRandom ( PVOID  Buffer,
SIZE_T  Length 
)

Definition at line 26 of file random.c.

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}
NTSYSAPI ULONG NTAPI RtlRandomEx(PULONG Seed)
Definition: bufpool.h:45
#define P(row, col)
static ULONG KsecRandomSeed
Definition: random.c:19
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
_Check_return_ unsigned int __cdecl _rotl(_In_ unsigned int _Value, _In_ int _Shift)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
uint32_t * PULONG
Definition: typedefs.h:59
ULONG LowPart
Definition: typedefs.h:106

Referenced by KsecDeviceControl().

◆ KsecReadMachineSpecificCounters()

VOID NTAPI KsecReadMachineSpecificCounters ( _Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS  MachineSpecificCounters)

Definition at line 56 of file random.c.

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}
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
__INTRIN_INLINE unsigned long long __readpmc(unsigned long counter)
Definition: intrin_x86.h:1998
#define CP15_PMCCNTR
Definition: ketypes.h:119
BOOLEAN NTAPI ExIsProcessorFeaturePresent(IN ULONG ProcessorFeature)
Definition: sysinfo.c:363
#define PF_MMX_INSTRUCTIONS_AVAILABLE
Definition: ketypes.h:127
#define PF_RDTSC_INSTRUCTION_AVAILABLE
Definition: ketypes.h:132

Referenced by KsecGatherEntropyData().

Variable Documentation

◆ KsecRandomSeed

ULONG KsecRandomSeed = 0x62b409a1
static

Definition at line 19 of file random.c.

Referenced by KsecGenRandom().