1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxcrt/fx_random.h"
8
9 #include "build/build_config.h"
10 #include "core/fxcrt/fx_memory.h"
11 #include "core/fxcrt/fx_string.h"
12 #include "core/fxcrt/fx_system.h"
13
14 #define MT_N 848
15 #define MT_M 456
16 #define MT_Matrix_A 0x9908b0df
17 #define MT_Upper_Mask 0x80000000
18 #define MT_Lower_Mask 0x7fffffff
19
20 #if defined(OS_WIN)
21 #include <wincrypt.h>
22 #else
23 #include <sys/time.h>
24 #include <unistd.h>
25 #endif
26
27 namespace {
28
29 struct MTContext {
30 uint32_t mti;
31 uint32_t mt[MT_N];
32 };
33
34 bool g_bHaveGlobalSeed = false;
35 uint32_t g_nGlobalSeed = 0;
36
37 #if defined(OS_WIN)
GenerateSeedFromCryptoRandom(uint32_t * pSeed)38 bool GenerateSeedFromCryptoRandom(uint32_t* pSeed) {
39 HCRYPTPROV hCP = 0;
40 if (!::CryptAcquireContext(&hCP, nullptr, nullptr, PROV_RSA_FULL, 0) ||
41 !hCP) {
42 return false;
43 }
44 ::CryptGenRandom(hCP, sizeof(uint32_t), reinterpret_cast<uint8_t*>(pSeed));
45 ::CryptReleaseContext(hCP, 0);
46 return true;
47 }
48 #endif
49
GenerateSeedFromEnvironment()50 uint32_t GenerateSeedFromEnvironment() {
51 char c;
52 uintptr_t p = reinterpret_cast<uintptr_t>(&c);
53 uint32_t seed = ~static_cast<uint32_t>(p >> 3);
54 #if defined(OS_WIN)
55 SYSTEMTIME st;
56 GetSystemTime(&st);
57 seed ^= static_cast<uint32_t>(st.wSecond) * 1000000;
58 seed ^= static_cast<uint32_t>(st.wMilliseconds) * 1000;
59 seed ^= GetCurrentProcessId();
60 #else
61 struct timeval tv;
62 gettimeofday(&tv, 0);
63 seed ^= static_cast<uint32_t>(tv.tv_sec) * 1000000;
64 seed ^= static_cast<uint32_t>(tv.tv_usec);
65 seed ^= static_cast<uint32_t>(getpid());
66 #endif
67 return seed;
68 }
69
ContextFromNextGlobalSeed()70 void* ContextFromNextGlobalSeed() {
71 if (!g_bHaveGlobalSeed) {
72 #if defined(OS_WIN)
73 if (!GenerateSeedFromCryptoRandom(&g_nGlobalSeed))
74 g_nGlobalSeed = GenerateSeedFromEnvironment();
75 #else
76 g_nGlobalSeed = GenerateSeedFromEnvironment();
77 #endif
78 g_bHaveGlobalSeed = true;
79 }
80 return FX_Random_MT_Start(++g_nGlobalSeed);
81 }
82
83 } // namespace
84
FX_Random_MT_Start(uint32_t dwSeed)85 void* FX_Random_MT_Start(uint32_t dwSeed) {
86 MTContext* pContext = FX_Alloc(MTContext, 1);
87 uint32_t* pBuf = pContext->mt;
88 pBuf[0] = dwSeed;
89 for (uint32_t i = 1; i < MT_N; i++)
90 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i);
91
92 pContext->mti = MT_N;
93 return pContext;
94 }
95
FX_Random_MT_Generate(void * pContext)96 uint32_t FX_Random_MT_Generate(void* pContext) {
97 MTContext* pMTC = static_cast<MTContext*>(pContext);
98 uint32_t* pBuf = pMTC->mt;
99 uint32_t v;
100 if (pMTC->mti >= MT_N) {
101 static const uint32_t mag[2] = {0, MT_Matrix_A};
102 uint32_t kk;
103 for (kk = 0; kk < MT_N - MT_M; kk++) {
104 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask);
105 pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1];
106 }
107 for (; kk < MT_N - 1; kk++) {
108 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask);
109 pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1];
110 }
111 v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask);
112 pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1];
113 pMTC->mti = 0;
114 }
115 v = pBuf[pMTC->mti++];
116 v ^= (v >> 11);
117 v ^= (v << 7) & 0x9d2c5680UL;
118 v ^= (v << 15) & 0xefc60000UL;
119 v ^= (v >> 18);
120 return v;
121 }
122
FX_Random_MT_Close(void * pContext)123 void FX_Random_MT_Close(void* pContext) {
124 FX_Free(pContext);
125 }
126
FX_Random_GenerateMT(uint32_t * pBuffer,int32_t iCount)127 void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount) {
128 void* pContext = ContextFromNextGlobalSeed();
129 while (iCount-- > 0)
130 *pBuffer++ = FX_Random_MT_Generate(pContext);
131
132 FX_Random_MT_Close(pContext);
133 }
134