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) |
16 #include <openssl/rand.h> | 19 #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 |
17 | 28 |
18 #include "webrtc/base/base64.h" | 29 #include "webrtc/base/base64.h" |
19 #include "webrtc/base/basictypes.h" | 30 #include "webrtc/base/basictypes.h" |
20 #include "webrtc/base/checks.h" | 31 #include "webrtc/base/checks.h" |
21 #include "webrtc/base/logging.h" | 32 #include "webrtc/base/logging.h" |
22 #include "webrtc/base/timeutils.h" | 33 #include "webrtc/base/timeutils.h" |
23 | 34 |
24 // Protect against max macro inclusion. | 35 // Protect against max macro inclusion. |
25 #undef max | 36 #undef max |
26 | 37 |
27 namespace rtc { | 38 namespace rtc { |
28 | 39 |
29 // Base class for RNG implementations. | 40 // Base class for RNG implementations. |
30 class RandomGenerator { | 41 class RandomGenerator { |
31 public: | 42 public: |
32 virtual ~RandomGenerator() {} | 43 virtual ~RandomGenerator() {} |
33 virtual bool Init(const void* seed, size_t len) = 0; | 44 virtual bool Init(const void* seed, size_t len) = 0; |
34 virtual bool Generate(void* buf, size_t len) = 0; | 45 virtual bool Generate(void* buf, size_t len) = 0; |
35 }; | 46 }; |
36 | 47 |
| 48 #if defined(SSL_USE_OPENSSL) |
37 // The OpenSSL RNG. | 49 // The OpenSSL RNG. |
38 class SecureRandomGenerator : public RandomGenerator { | 50 class SecureRandomGenerator : public RandomGenerator { |
39 public: | 51 public: |
40 SecureRandomGenerator() {} | 52 SecureRandomGenerator() {} |
41 ~SecureRandomGenerator() override {} | 53 ~SecureRandomGenerator() override {} |
42 bool Init(const void* seed, size_t len) override { return true; } | 54 bool Init(const void* seed, size_t len) override { return true; } |
43 bool Generate(void* buf, size_t len) override { | 55 bool Generate(void* buf, size_t len) override { |
44 return (RAND_bytes(reinterpret_cast<unsigned char*>(buf), len) > 0); | 56 return (RAND_bytes(reinterpret_cast<unsigned char*>(buf), len) > 0); |
45 } | 57 } |
46 }; | 58 }; |
47 | 59 |
| 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 |
48 // A test random generator, for predictable output. | 133 // A test random generator, for predictable output. |
49 class TestRandomGenerator : public RandomGenerator { | 134 class TestRandomGenerator : public RandomGenerator { |
50 public: | 135 public: |
51 TestRandomGenerator() : seed_(7) { | 136 TestRandomGenerator() : seed_(7) { |
52 } | 137 } |
53 ~TestRandomGenerator() override { | 138 ~TestRandomGenerator() override { |
54 } | 139 } |
55 bool Init(const void* seed, size_t len) override { return true; } | 140 bool Init(const void* seed, size_t len) override { return true; } |
56 bool Generate(void* buf, size_t len) override { | 141 bool Generate(void* buf, size_t len) override { |
57 for (size_t i = 0; i < len; ++i) { | 142 for (size_t i = 0; i < len; ++i) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } while (id == 0); | 294 } while (id == 0); |
210 return id; | 295 return id; |
211 } | 296 } |
212 | 297 |
213 double CreateRandomDouble() { | 298 double CreateRandomDouble() { |
214 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 299 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
215 std::numeric_limits<double>::epsilon()); | 300 std::numeric_limits<double>::epsilon()); |
216 } | 301 } |
217 | 302 |
218 } // namespace rtc | 303 } // namespace rtc |
OLD | NEW |