ReactOS  0.4.15-dev-1150-g593bcce
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;
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)
PPEB Peb
Definition: dllmain.c:27
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
SYSTEM_PROCESS_INFORMATION SystemProcessInformation
Definition: ksecdd.h:71
SYSTEM_EXCEPTION_INFORMATION SystemExceptionInformation
Definition: ksecdd.h:68
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
uint16_t * PWSTR
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
static WCHAR String[]
Definition: stringtable.c:55
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1959
_SEH2_TRY
Definition: create.c:4226
SYSTEM_PERFORMANCE_INFORMATION SystemPerformanceInformation
Definition: ksecdd.h:67
VOID WINAPI MD4Init(MD4_CTX *ctx)
Definition: md4.c:45
SYSTEM_INTERRUPT_INFORMATION SystemInterruptInformation
Definition: ksecdd.h:70
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
LARGE_INTEGER PerformanceFrequency
Definition: ksecdd.h:63
PsGetCurrentThreadId
Definition: CrNtStubs.h:7
#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
Status
Definition: gdiplustypes.h:24
LARGE_INTEGER TickCount
Definition: ksecdd.h:60
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorPerformanceInformation
Definition: ksecdd.h:66
Definition: compat.h:583
KSEC_MACHINE_SPECIFIC_COUNTERS MachineSpecificCounters
Definition: ksecdd.h:65
_SEH2_END
Definition: create.c:4400
Definition: util.c:81
SYSTEM_LOOKASIDE_INFORMATION SystemLookasideInformation
Definition: ksecdd.h:69
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
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
return STATUS_SUCCESS
Definition: btrfs.c:3014
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HANDLE CurrentThreadId
Definition: ksecdd.h:59
LARGE_INTEGER PerformanceCounter
Definition: ksecdd.h:62

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 VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
ULONG NTAPI RtlRandomEx(IN OUT PULONG Seed)
Definition: random.c:150
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
VOID NTAPI KeQueryTickCount(IN PLARGE_INTEGER TickCount)
Definition: clock.c:165
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
ULONG LowPart
Definition: typedefs.h:106
#define P(row, col)
unsigned int * PULONG
Definition: retypes.h:1
_Check_return_ unsigned int __cdecl _rotl(_In_ unsigned int _Value, _In_ int _Shift)
unsigned int ULONG
Definition: retypes.h:1
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:3014

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 }
BOOLEAN NTAPI ExIsProcessorFeaturePresent(IN ULONG ProcessorFeature)
Definition: sysinfo.c:363
#define CP15_PMCCNTR
Definition: ketypes.h:122
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:1909
#define PF_RDTSC_INSTRUCTION_AVAILABLE
#define PF_MMX_INSTRUCTIONS_AVAILABLE

Referenced by KsecGatherEntropyData().

Variable Documentation

◆ KsecRandomSeed

ULONG KsecRandomSeed = 0x62b409a1
static

Definition at line 19 of file random.c.

Referenced by KsecGenRandom().