Fix -Wsign-compare
[folly.git] / folly / Random.cpp
1 /*
2  * Copyright 2014 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 it open for the duration of the program
35   static File randomDevice("/dev/urandom");
36   auto bytesRead = readFull(randomDevice.fd(), data, size);
37   PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
38 }
39
40 class BufferedRandomDevice {
41  public:
42   static constexpr size_t kDefaultBufferSize = 128;
43
44   explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
45
46   void get(void* data, size_t size) {
47     if (LIKELY(size <= remaining())) {
48       memcpy(data, ptr_, size);
49       ptr_ += size;
50     } else {
51       getSlow(static_cast<unsigned char*>(data), size);
52     }
53   }
54
55  private:
56   void getSlow(unsigned char* data, size_t size);
57
58   inline size_t remaining() const {
59     return buffer_.get() + bufferSize_ - ptr_;
60   }
61
62   const size_t bufferSize_;
63   std::unique_ptr<unsigned char[]> buffer_;
64   unsigned char* ptr_;
65 };
66
67 BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
68   : bufferSize_(bufferSize),
69     buffer_(new unsigned char[bufferSize]),
70     ptr_(buffer_.get() + bufferSize) {  // refill on first use
71 }
72
73 void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
74   DCHECK_GT(size, remaining());
75   if (size >= bufferSize_) {
76     // Just read directly.
77     readRandomDevice(data, size);
78     return;
79   }
80
81   size_t copied = remaining();
82   memcpy(data, ptr_, copied);
83   data += copied;
84   size -= copied;
85
86   // refill
87   readRandomDevice(buffer_.get(), bufferSize_);
88   ptr_ = buffer_.get();
89
90   memcpy(data, ptr_, size);
91   ptr_ += size;
92 }
93
94
95 }  // namespace
96
97 void Random::secureRandom(void* data, size_t size) {
98   static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
99   bufferedRandomDevice->get(data, size);
100 }
101
102 ThreadLocalPRNG::ThreadLocalPRNG() {
103   static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
104   local_ = localInstance.get();
105 }
106
107 class ThreadLocalPRNG::LocalInstancePRNG {
108  public:
109   LocalInstancePRNG() : rng(Random::create()) { }
110
111   Random::DefaultGenerator rng;
112 };
113
114 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {
115   return local->rng();
116 }
117
118 }