OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/base/helpers.h" | |
12 | |
13 #include <limits> | |
14 #include <memory> | |
15 | |
16 #include <openssl/rand.h> | |
17 | |
18 #include "webrtc/base/base64.h" | |
19 #include "webrtc/base/basictypes.h" | |
20 #include "webrtc/base/checks.h" | |
21 #include "webrtc/base/logging.h" | |
22 #include "webrtc/base/timeutils.h" | |
23 | |
24 // Protect against max macro inclusion. | |
25 #undef max | |
26 | |
27 namespace rtc { | |
28 | |
29 // Base class for RNG implementations. | |
30 class RandomGenerator { | |
31 public: | |
32 virtual ~RandomGenerator() {} | |
33 virtual bool Init(const void* seed, size_t len) = 0; | |
34 virtual bool Generate(void* buf, size_t len) = 0; | |
35 }; | |
36 | |
37 // The OpenSSL RNG. | |
38 class SecureRandomGenerator : public RandomGenerator { | |
39 public: | |
40 SecureRandomGenerator() {} | |
41 ~SecureRandomGenerator() override {} | |
42 bool Init(const void* seed, size_t len) override { return true; } | |
43 bool Generate(void* buf, size_t len) override { | |
44 return (RAND_bytes(reinterpret_cast<unsigned char*>(buf), len) > 0); | |
45 } | |
46 }; | |
47 | |
48 // A test random generator, for predictable output. | |
49 class TestRandomGenerator : public RandomGenerator { | |
50 public: | |
51 TestRandomGenerator() : seed_(7) { | |
52 } | |
53 ~TestRandomGenerator() override { | |
54 } | |
55 bool Init(const void* seed, size_t len) override { return true; } | |
56 bool Generate(void* buf, size_t len) override { | |
57 for (size_t i = 0; i < len; ++i) { | |
58 static_cast<uint8_t*>(buf)[i] = static_cast<uint8_t>(GetRandom()); | |
59 } | |
60 return true; | |
61 } | |
62 | |
63 private: | |
64 int GetRandom() { | |
65 return ((seed_ = seed_ * 214013L + 2531011L) >> 16) & 0x7fff; | |
66 } | |
67 int seed_; | |
68 }; | |
69 | |
70 namespace { | |
71 | |
72 // TODO: Use Base64::Base64Table instead. | |
73 static const char kBase64[64] = { | |
74 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
75 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
76 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
77 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
78 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | |
79 | |
80 static const char kHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | |
81 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | |
82 | |
83 static const char kUuidDigit17[4] = {'8', '9', 'a', 'b'}; | |
84 | |
85 // This round about way of creating a global RNG is to safe-guard against | |
86 // indeterminant static initialization order. | |
87 std::unique_ptr<RandomGenerator>& GetGlobalRng() { | |
88 RTC_DEFINE_STATIC_LOCAL(std::unique_ptr<RandomGenerator>, global_rng, | |
89 (new SecureRandomGenerator())); | |
90 return global_rng; | |
91 } | |
92 | |
93 RandomGenerator& Rng() { | |
94 return *GetGlobalRng(); | |
95 } | |
96 | |
97 } // namespace | |
98 | |
99 void SetRandomTestMode(bool test) { | |
100 if (!test) { | |
101 GetGlobalRng().reset(new SecureRandomGenerator()); | |
102 } else { | |
103 GetGlobalRng().reset(new TestRandomGenerator()); | |
104 } | |
105 } | |
106 | |
107 bool InitRandom(int seed) { | |
108 return InitRandom(reinterpret_cast<const char*>(&seed), sizeof(seed)); | |
109 } | |
110 | |
111 bool InitRandom(const char* seed, size_t len) { | |
112 if (!Rng().Init(seed, len)) { | |
113 LOG(LS_ERROR) << "Failed to init random generator!"; | |
114 return false; | |
115 } | |
116 return true; | |
117 } | |
118 | |
119 std::string CreateRandomString(size_t len) { | |
120 std::string str; | |
121 RTC_CHECK(CreateRandomString(len, &str)); | |
122 return str; | |
123 } | |
124 | |
125 static bool CreateRandomString(size_t len, | |
126 const char* table, int table_size, | |
127 std::string* str) { | |
128 str->clear(); | |
129 // Avoid biased modulo division below. | |
130 if (256 % table_size) { | |
131 LOG(LS_ERROR) << "Table size must divide 256 evenly!"; | |
132 return false; | |
133 } | |
134 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); | |
135 if (!Rng().Generate(bytes.get(), len)) { | |
136 LOG(LS_ERROR) << "Failed to generate random string!"; | |
137 return false; | |
138 } | |
139 str->reserve(len); | |
140 for (size_t i = 0; i < len; ++i) { | |
141 str->push_back(table[bytes[i] % table_size]); | |
142 } | |
143 return true; | |
144 } | |
145 | |
146 bool CreateRandomString(size_t len, std::string* str) { | |
147 return CreateRandomString(len, kBase64, 64, str); | |
148 } | |
149 | |
150 bool CreateRandomString(size_t len, const std::string& table, | |
151 std::string* str) { | |
152 return CreateRandomString(len, table.c_str(), | |
153 static_cast<int>(table.size()), str); | |
154 } | |
155 | |
156 bool CreateRandomData(size_t length, std::string* data) { | |
157 data->resize(length); | |
158 // std::string is guaranteed to use contiguous memory in c++11 so we can | |
159 // safely write directly to it. | |
160 return Rng().Generate(&data->at(0), length); | |
161 } | |
162 | |
163 // Version 4 UUID is of the form: | |
164 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx | |
165 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. | |
166 std::string CreateRandomUuid() { | |
167 std::string str; | |
168 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); | |
169 RTC_CHECK(Rng().Generate(bytes.get(), 31)); | |
170 str.reserve(36); | |
171 for (size_t i = 0; i < 8; ++i) { | |
172 str.push_back(kHex[bytes[i] % 16]); | |
173 } | |
174 str.push_back('-'); | |
175 for (size_t i = 8; i < 12; ++i) { | |
176 str.push_back(kHex[bytes[i] % 16]); | |
177 } | |
178 str.push_back('-'); | |
179 str.push_back('4'); | |
180 for (size_t i = 12; i < 15; ++i) { | |
181 str.push_back(kHex[bytes[i] % 16]); | |
182 } | |
183 str.push_back('-'); | |
184 str.push_back(kUuidDigit17[bytes[15] % 4]); | |
185 for (size_t i = 16; i < 19; ++i) { | |
186 str.push_back(kHex[bytes[i] % 16]); | |
187 } | |
188 str.push_back('-'); | |
189 for (size_t i = 19; i < 31; ++i) { | |
190 str.push_back(kHex[bytes[i] % 16]); | |
191 } | |
192 return str; | |
193 } | |
194 | |
195 uint32_t CreateRandomId() { | |
196 uint32_t id; | |
197 RTC_CHECK(Rng().Generate(&id, sizeof(id))); | |
198 return id; | |
199 } | |
200 | |
201 uint64_t CreateRandomId64() { | |
202 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); | |
203 } | |
204 | |
205 uint32_t CreateRandomNonZeroId() { | |
206 uint32_t id; | |
207 do { | |
208 id = CreateRandomId(); | |
209 } while (id == 0); | |
210 return id; | |
211 } | |
212 | |
213 double CreateRandomDouble() { | |
214 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | |
215 std::numeric_limits<double>::epsilon()); | |
216 } | |
217 | |
218 } // namespace rtc | |
OLD | NEW |