2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Random.h>
25 #include <glog/logging.h>
26 #include <folly/File.h>
27 #include <folly/FileUtil.h>
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);
41 class BufferedRandomDevice {
43 static constexpr size_t kDefaultBufferSize = 128;
45 explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
47 void get(void* data, size_t size) {
48 if (LIKELY(size <= remaining())) {
49 memcpy(data, ptr_, size);
52 getSlow(static_cast<unsigned char*>(data), size);
57 void getSlow(unsigned char* data, size_t size);
59 inline size_t remaining() const {
60 return buffer_.get() + bufferSize_ - ptr_;
63 const size_t bufferSize_;
64 std::unique_ptr<unsigned char[]> buffer_;
68 BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
69 : bufferSize_(bufferSize),
70 buffer_(new unsigned char[bufferSize]),
71 ptr_(buffer_.get() + bufferSize) { // refill on first use
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);
82 size_t copied = remaining();
83 memcpy(data, ptr_, copied);
88 readRandomDevice(buffer_.get(), bufferSize_);
91 memcpy(data, ptr_, size);
98 void Random::secureRandom(void* data, size_t size) {
99 static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
100 bufferedRandomDevice->get(data, size);
103 ThreadLocalPRNG::ThreadLocalPRNG() {
104 static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
105 local_ = localInstance.get();
108 class ThreadLocalPRNG::LocalInstancePRNG {
110 LocalInstancePRNG() : rng(Random::create()) { }
112 Random::DefaultGenerator rng;
115 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {