ReactOS 0.4.17-dev-1421-g4623072
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 98 of file random.c.

100{
101 MD4_CTX Md4Context;
102 PTEB Teb;
103 PPEB Peb;
107
108 /* Query some generic values */
110 EntropyData->CurrentThreadId = PsGetCurrentThreadId();
111 KeQueryTickCount(&EntropyData->TickCount);
112 KeQuerySystemTime(&EntropyData->SystemTime);
114 &EntropyData->PerformanceFrequency);
115
116 /* Check if we have a TEB/PEB for the process environment */
117 Teb = PsGetCurrentThread()->Tcb.Teb;
118 if (Teb != NULL)
119 {
121
122 /* Initialize the MD4 context */
123 MD4Init(&Md4Context);
125 {
126 /* Get the end of the environment */
128 while (*String)
129 {
130 String += wcslen(String) + 1;
131 }
132
133 /* Update the MD4 context from the environment data */
134 MD4Update(&Md4Context,
137 }
139 {
140 /* Simply ignore the exception */
141 }
142 _SEH2_END;
143
144 /* Finalize and copy the MD4 hash */
145 MD4Final(&Md4Context);
146 RtlCopyMemory(&EntropyData->EnvironmentHash, Md4Context.digest, 16);
147 }
148
149 /* Read some machine specific hardware counters */
151
152 /* Query processor performance information */
156 &ReturnLength);
157 if (!NT_SUCCESS(Status))
158 {
159 return Status;
160 }
161
162 /* Query system performance information */
164 &EntropyData->SystemPerformanceInformation,
166 &ReturnLength);
167 if (!NT_SUCCESS(Status))
168 {
169 return Status;
170 }
171
172 /* Query exception information */
174 &EntropyData->SystemExceptionInformation,
176 &ReturnLength);
177 if (!NT_SUCCESS(Status))
178 {
179 return Status;
180 }
181
182 /* Query lookaside information */
184 &EntropyData->SystemLookasideInformation,
186 &ReturnLength);
187 if (!NT_SUCCESS(Status))
188 {
189 return Status;
190 }
191
192 /* Query interrupt information */
194 &EntropyData->SystemInterruptInformation,
196 &ReturnLength);
197 if (!NT_SUCCESS(Status))
198 {
199 return Status;
200 }
201
202 /* Query process information */
204 &EntropyData->SystemProcessInformation,
206 &ReturnLength);
207 if (!NT_SUCCESS(Status))
208 {
209 return Status;
210 }
211
212 return STATUS_SUCCESS;
213}
_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
void WINAPI MD4Init(MD4_CTX *ctx)
Definition: md4.c:45
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
PPEB Peb
Definition: dllmain.c:27
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2988
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:24
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define SystemPerformanceInformation
Definition: memtest.h:87
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1163
#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:76
HANDLE CurrentProcessId
Definition: ksecdd.h:63
UCHAR EnvironmentHash[16]
Definition: ksecdd.h:69
KSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters
Definition: ksecdd.h:70
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorPerformanceInformation
Definition: ksecdd.h:71
LARGE_INTEGER TickCount
Definition: ksecdd.h:65
SYSTEM_EXCEPTION_INFORMATION SystemExceptionInformation
Definition: ksecdd.h:73
SYSTEM_PERFORMANCE_INFORMATION SystemPerformanceInformation
Definition: ksecdd.h:72
LARGE_INTEGER PerformanceFrequency
Definition: ksecdd.h:68
SYSTEM_INTERRUPT_INFORMATION SystemInterruptInformation
Definition: ksecdd.h:75
HANDLE CurrentThreadId
Definition: ksecdd.h:64
LARGE_INTEGER SystemTime
Definition: ksecdd.h:66
LARGE_INTEGER PerformanceCounter
Definition: ksecdd.h:67
SYSTEM_LOOKASIDE_INFORMATION SystemLookasideInformation
Definition: ksecdd.h:74
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#elif defined(_M_ARM64)
82 /* Read the virtual count of the generic timer */
83#ifdef _MSC_VER
84 MachineSpecificCounters->Cntvct = _ReadStatusReg(ARM64_CNTVCT);
85#else
86 __asm__ __volatile__("mrs %0, cntvct_el0" : "=r"(MachineSpecificCounters->Cntvct));
87#endif
88#else
89 #error Implement me!
90#endif
91}
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
__asm__(".p2align 4, 0x90\n" ".seh_proc __seh2_global_filter_func\n" "__seh2_global_filter_func:\n" "\tsub %rbp, %rax\n" "\tpush %rbp\n" "\t.seh_pushreg %rbp\n" "\tpush %rbx\n" "\t.seh_pushreg %rbx\n" "\tpush %rdi\n" "\t.seh_pushreg %rdi\n" "\tpush %rsi\n" "\t.seh_pushreg %rsi\n" "\tpush %r12\n" "\t.seh_pushreg %r12\n" "\tpush %r13\n" "\t.seh_pushreg %r13\n" "\tpush %r14\n" "\t.seh_pushreg %r14\n" "\tpush %r15\n" "\t.seh_pushreg %r15\n" "\tsub $40, %rsp\n" "\t.seh_stackalloc 40\n" "\t.seh_endprologue\n" "\tsub %rax, %rdx\n" "\tmov %rdx, %rbp\n" "\tjmp *%r8\n" "__seh2_global_filter_func_exit:\n" "\t.p2align 4\n" "\tadd $40, %rsp\n" "\tpop %r15\n" "\tpop %r14\n" "\tpop %r13\n" "\tpop %r12\n" "\tpop %rsi\n" "\tpop %rdi\n" "\tpop %rbx\n" "\tpop %rbp\n" "\tret\n" "\t.seh_endproc")
#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().