folly/wangle -> wangle cutover
[folly.git] / folly / Random.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/Random.h>
18
19 #include <atomic>
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <random>
23 #include <array>
24
25 #include <glog/logging.h>
26 #include <folly/File.h>
27 #include <folly/FileUtil.h>
28
29 namespace folly {
30
31 namespace {
32
33 void readRandomDevice(void* data, size_t size) {
34   // Keep the random device open for the duration of the program.
35   static int randomFd = ::open("/dev/urandom", O_RDONLY);
36   PCHECK(randomFd >= 0);
37   auto bytesRead = readFull(randomFd, data, size);
38   PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
39 }
40
41 class BufferedRandomDevice {
42  public:
43   static constexpr size_t kDefaultBufferSize = 128;
44
45   explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
46
47   void get(void* data, size_t size) {
48     if (LIKELY(size <= remaining())) {
49       memcpy(data, ptr_, size);
50       ptr_ += size;
51     } else {
52       getSlow(static_cast<unsigned char*>(data), size);
53     }
54   }
55
56  private:
57   void getSlow(unsigned char* data, size_t size);
58
59   inline size_t remaining() const {
60     return buffer_.get() + bufferSize_ - ptr_;
61   }
62
63   const size_t bufferSize_;
64   std::unique_ptr<unsigned char[]> buffer_;
65   unsigned char* ptr_;
66 };
67
68 BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
69   : bufferSize_(bufferSize),
70     buffer_(new unsigned char[bufferSize]),
71     ptr_(buffer_.get() + bufferSize) {  // refill on first use
72 }
73
74 void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
75   DCHECK_GT(size, remaining());
76   if (size >= bufferSize_) {
77     // Just read directly.
78     readRandomDevice(data, size);
79     return;
80   }
81
82   size_t copied = remaining();
83   memcpy(data, ptr_, copied);
84   data += copied;
85   size -= copied;
86
87   // refill
88   readRandomDevice(buffer_.get(), bufferSize_);
89   ptr_ = buffer_.get();
90
91   memcpy(data, ptr_, size);
92   ptr_ += size;
93 }
94
95
96 }  // namespace
97
98 void Random::secureRandom(void* data, size_t size) {
99   static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
100   bufferedRandomDevice->get(data, size);
101 }
102
103 ThreadLocalPRNG::ThreadLocalPRNG() {
104   static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
105   local_ = localInstance.get();
106 }
107
108 class ThreadLocalPRNG::LocalInstancePRNG {
109  public:
110   LocalInstancePRNG() : rng(Random::create()) { }
111
112   Random::DefaultGenerator rng;
113 };
114
115 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {
116   return local->rng();
117 }
118
119 }