NewRandom.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Copyright (c) 2003/2004, Dominik Reichl <[email protected]>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. - Neither the name of ReichlSoft nor the names of its contributors may be
  12. used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "StdAfx.h"
  27. #include "NewRandom.h"
  28. #include "MemUtil.h"
  29. static DWORD g_dwNewRandomInstanceCounter = 0;
  30. static unsigned long g_xorW = 0;
  31. static unsigned long g_xorX = 0;
  32. static unsigned long g_xorY = 0;
  33. static unsigned long g_xorZ = 0;
  34. CNewRandom::CNewRandom()
  35. {
  36. Reset();
  37. }
  38. CNewRandom::~CNewRandom()
  39. {
  40. Reset();
  41. }
  42. void CNewRandom::Reset()
  43. {
  44. mem_erase(m_pPseudoRandom, INTRAND_SIZE);
  45. m_dwCounter = 0;
  46. }
  47. void CNewRandom::Initialize()
  48. {
  49. DWORD inx;
  50. WORD ww;
  51. DWORD dw;
  52. LARGE_INTEGER li;
  53. SYSTEMTIME st;
  54. POINT pt;
  55. MEMORYSTATUS ms;
  56. SYSTEM_INFO si;
  57. #ifndef _WIN32_WCE
  58. STARTUPINFO sui;
  59. #endif
  60. g_dwNewRandomInstanceCounter++;
  61. Reset();
  62. inx = 0;
  63. dw = GetTickCount();
  64. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  65. QueryPerformanceCounter(&li);
  66. memcpy(&m_pPseudoRandom[inx], &li, sizeof(LARGE_INTEGER));
  67. inx += sizeof(LARGE_INTEGER);
  68. GetLocalTime(&st);
  69. memcpy(&m_pPseudoRandom[inx], &st, sizeof(SYSTEMTIME));
  70. inx += sizeof(SYSTEMTIME);
  71. GetCursorPos(&pt);
  72. memcpy(&m_pPseudoRandom[inx], &pt, sizeof(POINT));
  73. inx += sizeof(POINT);
  74. ww = (WORD)(rand());
  75. memcpy(&m_pPseudoRandom[inx], &ww, 2); inx += 2;
  76. ww = (WORD)(rand());
  77. memcpy(&m_pPseudoRandom[inx], &ww, 2); inx += 2;
  78. ww = (WORD)(rand());
  79. memcpy(&m_pPseudoRandom[inx], &ww, 2); inx += 2;
  80. GetCaretPos(&pt);
  81. memcpy(&m_pPseudoRandom[inx], &pt, sizeof(POINT));
  82. inx += sizeof(POINT);
  83. GlobalMemoryStatus(&ms);
  84. memcpy(&m_pPseudoRandom[inx], &ms, sizeof(MEMORYSTATUS));
  85. inx += sizeof(MEMORYSTATUS);
  86. dw = (DWORD)GetActiveWindow();
  87. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  88. dw = (DWORD)GetCapture();
  89. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  90. dw = (DWORD)GetClipboardOwner();
  91. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  92. #ifndef _WIN32_WCE
  93. // No support under Windows CE
  94. dw = (DWORD)GetClipboardViewer();
  95. memcpy(&m_pPseudoRandom[inx], &dw, 4);
  96. #else
  97. // Leave the stack data - random :)
  98. #endif
  99. inx += 4;
  100. dw = GetCurrentProcessId();
  101. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  102. dw = (DWORD)GetCurrentProcess();
  103. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  104. dw = (DWORD)GetActiveWindow();
  105. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  106. dw = GetCurrentThreadId();
  107. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  108. dw = (DWORD)GetCurrentThread();
  109. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  110. dw = (DWORD)GetDesktopWindow();
  111. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  112. dw = (DWORD)GetFocus();
  113. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  114. dw = (DWORD)GetForegroundWindow();
  115. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  116. #ifndef _WIN32_WCE
  117. dw = (DWORD)GetInputState();
  118. memcpy(&m_pPseudoRandom[inx], &dw, 4);
  119. #else
  120. // Leave the stack data - random :)
  121. #endif
  122. inx += 4;
  123. dw = GetMessagePos();
  124. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  125. #ifndef _WIN32_WCE
  126. dw = (DWORD)GetMessageTime();
  127. memcpy(&m_pPseudoRandom[inx], &dw, 4);
  128. #else
  129. // Leave the stack data - random :)
  130. #endif
  131. inx += 4;
  132. dw = (DWORD)GetOpenClipboardWindow();
  133. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  134. dw = (DWORD)GetProcessHeap();
  135. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  136. GetSystemInfo(&si);
  137. memcpy(&m_pPseudoRandom[inx], &si, sizeof(SYSTEM_INFO));
  138. inx += sizeof(SYSTEM_INFO);
  139. dw = (DWORD)randXorShift();
  140. memcpy(&m_pPseudoRandom[inx], &dw, 4); inx += 4;
  141. #ifndef _WIN32_WCE
  142. GetStartupInfo(&sui);
  143. memcpy(&m_pPseudoRandom[inx], &sui, sizeof(STARTUPINFO));
  144. #else
  145. // Leave the stack data - random :)
  146. #endif
  147. inx += sizeof(STARTUPINFO);
  148. memcpy(&m_pPseudoRandom[inx], &g_dwNewRandomInstanceCounter, 4);
  149. inx += 4;
  150. ASSERT(inx <= INTRAND_SIZE);
  151. }
  152. void CNewRandom::GetRandomBuffer(BYTE *pBuf, DWORD dwSize)
  153. {
  154. sha256_ctx hashctx;
  155. BYTE aTemp[32];
  156. DWORD dw;
  157. ASSERT(pBuf != NULL);
  158. while(dwSize != 0)
  159. {
  160. m_dwCounter++;
  161. sha256_begin(&hashctx);
  162. sha256_hash(m_pPseudoRandom, INTRAND_SIZE, &hashctx);
  163. sha256_hash((BYTE *)&m_dwCounter, 4, &hashctx);
  164. sha256_end(aTemp, &hashctx);
  165. dw = (dwSize < 32) ? dwSize : 32;
  166. memcpy(pBuf, aTemp, dw);
  167. pBuf += dw;
  168. dwSize -= dw;
  169. }
  170. }
  171. // Seed the xorshift random number generator
  172. void srandXorShift(unsigned long *pSeed128)
  173. {
  174. ASSERT(pSeed128 != NULL); // No NULL parameter allowed
  175. if((g_xorW == 0) && (g_xorX == 0) && (g_xorY == 0) && (g_xorZ == 0))
  176. {
  177. g_xorW = pSeed128[0];
  178. g_xorX = pSeed128[1];
  179. g_xorY = pSeed128[2];
  180. g_xorZ = pSeed128[3];
  181. if((g_xorW + g_xorX + g_xorY + g_xorZ) == 0) g_xorX += 0xB7E15163;
  182. }
  183. }
  184. // Fast XorShift random number generator
  185. unsigned long randXorShift()
  186. {
  187. unsigned long tmp;
  188. tmp = (g_xorX ^ (g_xorX << 15));
  189. g_xorX = g_xorY; g_xorY = g_xorZ; g_xorZ = g_xorW;
  190. g_xorW = (g_xorW ^ (g_xorW >> 21)) ^ (tmp ^ (tmp >> 4));
  191. return g_xorW;
  192. }
  193. void randCreateUUID(BYTE *pUUID16, CNewRandom *pRandomSource)
  194. {
  195. SYSTEMTIME st;
  196. BYTE *p = pUUID16;
  197. DWORD *pdw1 = (DWORD *)pUUID16, *pdw2 = (DWORD *)&pUUID16[4],
  198. *pdw3 = (DWORD *)&pUUID16[8], *pdw4 = (DWORD *)&pUUID16[12];
  199. ASSERT(pRandomSource != NULL);
  200. ASSERT((sizeof(DWORD) == 4) && (sizeof(USHORT) == 2) && (pUUID16 != NULL));
  201. if(pUUID16 == NULL) return;
  202. ZeroMemory(&st, sizeof(SYSTEMTIME));
  203. GetSystemTime(&st);
  204. _PackTimeToStruct(p, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  205. p += 5; // +5 => 5 bytes filled
  206. *p = (BYTE)((st.wMilliseconds >> 2) & 0xFF); // Store milliseconds
  207. p++; // +1 => 6 bytes filled
  208. // Use the xorshift random number generator as pseudo-counter
  209. DWORD dwPseudoCounter = randXorShift();
  210. memcpy(p, &dwPseudoCounter, 2); // Use only 2/4 bytes
  211. p += 2; // +2 => 8 bytes filled
  212. pRandomSource->GetRandomBuffer(p, 8); // +8 => 16 bytes filled
  213. // Mix buffer for better read- and processability using PHTs
  214. *pdw1 += *pdw2; *pdw2 += *pdw1; *pdw3 += *pdw4; *pdw4 += *pdw3;
  215. *pdw2 += *pdw3; *pdw3 += *pdw2; *pdw1 += *pdw4; *pdw4 += *pdw1;
  216. *pdw1 += *pdw3; *pdw3 += *pdw1; *pdw2 += *pdw4; *pdw4 += *pdw2;
  217. *pdw1 += *pdw2; *pdw2 += *pdw1; *pdw3 += *pdw4; *pdw4 += *pdw3;
  218. *pdw2 += *pdw3; *pdw3 += *pdw2; *pdw1 += *pdw4; *pdw4 += *pdw1;
  219. *pdw1 += *pdw3; *pdw3 += *pdw1; *pdw2 += *pdw4; *pdw4 += *pdw2;
  220. }