ReactOS 0.4.17-dev-117-g313be0c
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 12 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 (DEAD_LINK)

Definition at line 91 of file random.c.

93{
94 MD4_CTX Md4Context;
95 PTEB Teb;
96 PPEB Peb;
100
101 /* Query some generic values */
103 EntropyData->CurrentThreadId = PsGetCurrentThreadId();
104 KeQueryTickCount(&EntropyData->TickCount);
105 KeQuerySystemTime(&EntropyData->SystemTime);
107 &EntropyData->PerformanceFrequency);
108
109 /* Check if we have a TEB/PEB for the process environment */
110 Teb = PsGetCurrentThread()->Tcb.Teb;
111 if (Teb != NULL)
112 {
114
115 /* Initialize the MD4 context */
116 MD4Init(&Md4Context);
118 {
119 /* Get the end of the environment */
121 while (*String)
122 {
123 String += wcslen(String) + 1;
124 }
125
126 /* Update the MD4 context from the environment data */
127 MD4Update(&Md4Context,
130 }
132 {
133 /* Simply ignore the exception */
134 }
135 _SEH2_END;
136
137 /* Finalize and copy the MD4 hash */
138 MD4Final(&Md4Context);
139 RtlCopyMemory(&EntropyData->EnvironmentHash, Md4Context.digest, 16);
140 }
141
142 /* Read some machine specific hardware counters */
144
145 /* Query processor performance information */
149 &ReturnLength);
150 if (!NT_SUCCESS(Status))
151 {
152 return Status;
153 }
154
155 /* Query system performance information */
157 &EntropyData->SystemPerformanceInformation,
159 &ReturnLength);
160 if (!NT_SUCCESS(Status))
161 {
162 return Status;
163 }
164
165 /* Query exception information */
167 &EntropyData->SystemExceptionInformation,
169 &ReturnLength);
170 if (!NT_SUCCESS(Status))
171 {
172 return Status;
173 }
174
175 /* Query lookaside information */
177 &EntropyData->SystemLookasideInformation,
179 &ReturnLength);
180 if (!NT_SUCCESS(Status))
181 {
182 return Status;
183 }
184
185 /* Query interrupt information */
187 &EntropyData->SystemInterruptInformation,
189 &ReturnLength);
190 if (!NT_SUCCESS(Status))
191 {
192 return Status;
193 }
194
195 /* Query process information */
197 &EntropyData->SystemProcessInformation,
199 &ReturnLength);
200 if (!NT_SUCCESS(Status))
201 {
202 return Status;
203 }
204
205 return STATUS_SUCCESS;
206}
_In_ PVOID _In_ ULONG _Out_ PVOID _In_ ULONG _Inout_ PULONG ReturnLength
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
PPEB Peb
Definition: dllmain.c:27
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
VOID NTAPI KsecReadMachineSpecificCounters(_Out_ PKSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters)
Definition: random.c:55
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
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:90
#define SystemPerformanceInformation
Definition: memtest.h:87
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#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:2439

Referenced by KsecInitializeEncryptionSupport().

◆ KsecGenRandom()

NTSTATUS NTAPI KsecGenRandom ( PVOID  Buffer,
SIZE_T  Length 
)

Definition at line 25 of file random.c.

28{
29 LARGE_INTEGER TickCount;
30 ULONG i, RandomValue;
31 PULONG P;
32
33 /* Try to generate a more random seed */
34 KeQueryTickCount(&TickCount);
35 KsecRandomSeed ^= _rotl(TickCount.LowPart, (KsecRandomSeed % 23));
36
37 P = Buffer;
38 for (i = 0; i < Length / sizeof(ULONG); i++)
39 {
41 }
42
43 Length &= (sizeof(ULONG) - 1);
44 if (Length > 0)
45 {
46 RandomValue = RtlRandomEx(&KsecRandomSeed);
47 RtlCopyMemory(&P[i], &RandomValue, Length);
48 }
49
50 return STATUS_SUCCESS;
51}
NTSYSAPI ULONG NTAPI RtlRandomEx(PULONG Seed)
Definition: bufpool.h:45
#define P(row, col)
_ACRTIMP unsigned int __cdecl _rotl(unsigned int, int)
Definition: intrin_arm.h:392
static ULONG KsecRandomSeed
Definition: random.c:18
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
_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 55 of file random.c.

57{
58#if defined(_M_IX86) || defined(_M_AMD64)
59 /* Check if RDTSC is available */
61 {
62 /* Read the TSC value */
63 MachineSpecificCounters->Tsc = __rdtsc();
64 }
65#if 0 // FIXME: investigate what the requirements are for these
66 /* Read the CPU event counter MSRs */
67 //MachineSpecificCounters->Ctr0 = __readmsr(0x12);
68 //MachineSpecificCounters->Ctr1 = __readmsr(0x13);
69
70 /* Check if this is an MMX capable CPU */
72 {
73 /* Read the CPU performance counters 0 and 1 */
74 MachineSpecificCounters->Pmc0 = __readpmc(0);
75 MachineSpecificCounters->Pmc1 = __readpmc(1);
76 }
77#endif
78#elif defined(_M_ARM)
79 /* Read the Cycle Counter Register */
80 MachineSpecificCounters->Ccr = _MoveFromCoprocessor(CP15_PMCCNTR);
81#else
82 #error Implement me!
83#endif
84}
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:2039
#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:179
#define PF_RDTSC_INSTRUCTION_AVAILABLE
Definition: ketypes.h:184

Referenced by KsecGatherEntropyData().

Variable Documentation

◆ KsecRandomSeed

ULONG KsecRandomSeed = 0x62b409a1
static

Definition at line 18 of file random.c.

Referenced by KsecGenRandom().