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