OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/helpers.h" | 11 #include "webrtc/base/helpers.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #if defined(FEATURE_ENABLE_SSL) | |
17 #include "webrtc/base/sslconfig.h" | |
18 #if defined(SSL_USE_OPENSSL) | |
19 #include <openssl/rand.h> | 16 #include <openssl/rand.h> |
20 #else | |
21 #if defined(WEBRTC_WIN) | |
22 #define WIN32_LEAN_AND_MEAN | |
23 #include <windows.h> | |
24 #include <ntsecapi.h> | |
25 #endif // WEBRTC_WIN | |
26 #endif // else | |
27 #endif // FEATURE_ENABLED_SSL | |
28 | 17 |
29 #include "webrtc/base/base64.h" | 18 #include "webrtc/base/base64.h" |
30 #include "webrtc/base/basictypes.h" | 19 #include "webrtc/base/basictypes.h" |
31 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
32 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
33 #include "webrtc/base/timeutils.h" | 22 #include "webrtc/base/timeutils.h" |
34 | 23 |
35 // Protect against max macro inclusion. | 24 // Protect against max macro inclusion. |
36 #undef max | 25 #undef max |
37 | 26 |
38 namespace rtc { | 27 namespace rtc { |
39 | 28 |
40 // Base class for RNG implementations. | 29 // Base class for RNG implementations. |
41 class RandomGenerator { | 30 class RandomGenerator { |
42 public: | 31 public: |
43 virtual ~RandomGenerator() {} | 32 virtual ~RandomGenerator() {} |
44 virtual bool Init(const void* seed, size_t len) = 0; | 33 virtual bool Init(const void* seed, size_t len) = 0; |
45 virtual bool Generate(void* buf, size_t len) = 0; | 34 virtual bool Generate(void* buf, size_t len) = 0; |
46 }; | 35 }; |
47 | 36 |
48 #if defined(SSL_USE_OPENSSL) | |
49 // The OpenSSL RNG. | 37 // The OpenSSL RNG. |
50 class SecureRandomGenerator : public RandomGenerator { | 38 class SecureRandomGenerator : public RandomGenerator { |
51 public: | 39 public: |
52 SecureRandomGenerator() {} | 40 SecureRandomGenerator() {} |
53 ~SecureRandomGenerator() override {} | 41 ~SecureRandomGenerator() override {} |
54 bool Init(const void* seed, size_t len) override { return true; } | 42 bool Init(const void* seed, size_t len) override { return true; } |
55 bool Generate(void* buf, size_t len) override { | 43 bool Generate(void* buf, size_t len) override { |
56 return (RAND_bytes(reinterpret_cast<unsigned char*>(buf), len) > 0); | 44 return (RAND_bytes(reinterpret_cast<unsigned char*>(buf), len) > 0); |
57 } | 45 } |
58 }; | 46 }; |
59 | 47 |
60 #else | |
61 #if defined(WEBRTC_WIN) | |
62 class SecureRandomGenerator : public RandomGenerator { | |
63 public: | |
64 SecureRandomGenerator() : advapi32_(NULL), rtl_gen_random_(NULL) {} | |
65 ~SecureRandomGenerator() { | |
66 FreeLibrary(advapi32_); | |
67 } | |
68 | |
69 virtual bool Init(const void* seed, size_t seed_len) { | |
70 // We don't do any additional seeding on Win32, we just use the CryptoAPI | |
71 // RNG (which is exposed as a hidden function off of ADVAPI32 so that we | |
72 // don't need to drag in all of CryptoAPI) | |
73 if (rtl_gen_random_) { | |
74 return true; | |
75 } | |
76 | |
77 advapi32_ = LoadLibrary(L"advapi32.dll"); | |
78 if (!advapi32_) { | |
79 return false; | |
80 } | |
81 | |
82 rtl_gen_random_ = reinterpret_cast<RtlGenRandomProc>( | |
83 GetProcAddress(advapi32_, "SystemFunction036")); | |
84 if (!rtl_gen_random_) { | |
85 FreeLibrary(advapi32_); | |
86 return false; | |
87 } | |
88 | |
89 return true; | |
90 } | |
91 virtual bool Generate(void* buf, size_t len) { | |
92 if (!rtl_gen_random_ && !Init(NULL, 0)) { | |
93 return false; | |
94 } | |
95 return (rtl_gen_random_(buf, static_cast<int>(len)) != FALSE); | |
96 } | |
97 | |
98 private: | |
99 typedef BOOL (WINAPI *RtlGenRandomProc)(PVOID, ULONG); | |
100 HINSTANCE advapi32_; | |
101 RtlGenRandomProc rtl_gen_random_; | |
102 }; | |
103 | |
104 #elif !defined(FEATURE_ENABLE_SSL) | |
105 | |
106 // No SSL implementation -- use rand() | |
107 class SecureRandomGenerator : public RandomGenerator { | |
108 public: | |
109 virtual bool Init(const void* seed, size_t len) { | |
110 if (len >= 4) { | |
111 srand(*reinterpret_cast<const int*>(seed)); | |
112 } else { | |
113 srand(*reinterpret_cast<const char*>(seed)); | |
114 } | |
115 return true; | |
116 } | |
117 virtual bool Generate(void* buf, size_t len) { | |
118 char* bytes = reinterpret_cast<char*>(buf); | |
119 for (size_t i = 0; i < len; ++i) { | |
120 bytes[i] = static_cast<char>(rand()); | |
121 } | |
122 return true; | |
123 } | |
124 }; | |
125 | |
126 #else | |
127 | |
128 #error No SSL implementation has been selected! | |
129 | |
130 #endif // WEBRTC_WIN | |
131 #endif | |
132 | |
133 // A test random generator, for predictable output. | 48 // A test random generator, for predictable output. |
134 class TestRandomGenerator : public RandomGenerator { | 49 class TestRandomGenerator : public RandomGenerator { |
135 public: | 50 public: |
136 TestRandomGenerator() : seed_(7) { | 51 TestRandomGenerator() : seed_(7) { |
137 } | 52 } |
138 ~TestRandomGenerator() override { | 53 ~TestRandomGenerator() override { |
139 } | 54 } |
140 bool Init(const void* seed, size_t len) override { return true; } | 55 bool Init(const void* seed, size_t len) override { return true; } |
141 bool Generate(void* buf, size_t len) override { | 56 bool Generate(void* buf, size_t len) override { |
142 for (size_t i = 0; i < len; ++i) { | 57 for (size_t i = 0; i < len; ++i) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } while (id == 0); | 209 } while (id == 0); |
295 return id; | 210 return id; |
296 } | 211 } |
297 | 212 |
298 double CreateRandomDouble() { | 213 double CreateRandomDouble() { |
299 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 214 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
300 std::numeric_limits<double>::epsilon()); | 215 std::numeric_limits<double>::epsilon()); |
301 } | 216 } |
302 | 217 |
303 } // namespace rtc | 218 } // namespace rtc |
OLD | NEW |