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