2 * Copyright 2014 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 // Keep it open for the duration of the program
34 File randomDevice("/dev/urandom");
36 void readRandomDevice(void* data, size_t size) {
37 PCHECK(readFull(randomDevice.fd(), data, size) == size);
40 class BufferedRandomDevice {
42 static constexpr size_t kDefaultBufferSize = 128;
44 explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
46 void get(void* data, size_t size) {
47 if (LIKELY(size <= remaining())) {
48 memcpy(data, ptr_, size);
51 getSlow(static_cast<unsigned char*>(data), size);
56 void getSlow(unsigned char* data, size_t size);
58 inline size_t remaining() const {
59 return buffer_.get() + bufferSize_ - ptr_;
62 const size_t bufferSize_;
63 std::unique_ptr<unsigned char[]> buffer_;
67 BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
68 : bufferSize_(bufferSize),
69 buffer_(new unsigned char[bufferSize]),
70 ptr_(buffer_.get() + bufferSize) { // refill on first use
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);
81 size_t copied = remaining();
82 memcpy(data, ptr_, copied);
87 readRandomDevice(buffer_.get(), bufferSize_);
90 memcpy(data, ptr_, size);
94 ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
98 void Random::secureRandom(void* data, size_t size) {
99 bufferedRandomDevice->get(data, size);
102 folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
103 ThreadLocalPRNG::localInstance;
105 class ThreadLocalPRNG::LocalInstancePRNG {
107 LocalInstancePRNG() : rng(Random::create()) { }
109 Random::DefaultGenerator rng;
112 ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() {
113 auto ret = new LocalInstancePRNG;
114 localInstance.reset(ret);
118 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {