Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/rand_util.h" |
| 6 | |
dalecurtis@chromium.org | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 7 | #include <windows.h> |
Bruce Dawson | a1e1cfcb | 2022-11-22 20:04:35 | [diff] [blame] | 8 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | |
dalecurtis@chromium.org | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 12 | #include <algorithm> |
Daniel Cheng | b6bbf5a6 | 2022-09-09 18:26:34 | [diff] [blame] | 13 | #include <atomic> |
dalecurtis@chromium.org | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 14 | #include <limits> |
| 15 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 16 | #include "base/check.h" |
Daniel Cheng | b6bbf5a6 | 2022-09-09 18:26:34 | [diff] [blame] | 17 | #include "base/feature_list.h" |
| 18 | #include "third_party/boringssl/src/include/openssl/crypto.h" |
| 19 | #include "third_party/boringssl/src/include/openssl/rand.h" |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 20 | |
Alex Gough | d28e61a | 2023-07-11 18:45:34 | [diff] [blame] | 21 | // Prototype for ProcessPrng. |
| 22 | // See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng |
| 23 | extern "C" { |
| 24 | BOOL WINAPI ProcessPrng(PBYTE pbData, SIZE_T cbData); |
| 25 | } |
| 26 | |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 27 | namespace base { |
| 28 | |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 29 | namespace internal { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // The BoringSSl helpers are duplicated in rand_util_fuchsia.cc and |
| 34 | // rand_util_posix.cc. |
| 35 | std::atomic<bool> g_use_boringssl; |
| 36 | |
| 37 | BASE_FEATURE(kUseBoringSSLForRandBytes, |
| 38 | "UseBoringSSLForRandBytes", |
| 39 | FEATURE_DISABLED_BY_DEFAULT); |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | void ConfigureBoringSSLBackedRandBytesFieldTrial() { |
| 44 | g_use_boringssl.store(FeatureList::IsEnabled(kUseBoringSSLForRandBytes), |
| 45 | std::memory_order_relaxed); |
| 46 | } |
| 47 | |
| 48 | bool UseBoringSSLForRandBytes() { |
| 49 | return g_use_boringssl.load(std::memory_order_relaxed); |
| 50 | } |
| 51 | |
| 52 | } // namespace internal |
| 53 | |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 54 | namespace { |
| 55 | |
Alex Gough | d28e61a | 2023-07-11 18:45:34 | [diff] [blame] | 56 | // Import bcryptprimitives!ProcessPrng rather than cryptbase!RtlGenRandom to |
| 57 | // avoid opening a handle to \\Device\KsecDD in the renderer. |
| 58 | decltype(&ProcessPrng) GetProcessPrng() { |
| 59 | HMODULE hmod = LoadLibraryW(L"bcryptprimitives.dll"); |
| 60 | CHECK(hmod); |
| 61 | decltype(&ProcessPrng) process_prng_fn = |
| 62 | reinterpret_cast<decltype(&ProcessPrng)>( |
| 63 | GetProcAddress(hmod, "ProcessPrng")); |
| 64 | CHECK(process_prng_fn); |
| 65 | return process_prng_fn; |
| 66 | } |
| 67 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 68 | void RandBytes(span<uint8_t> output, bool avoid_allocation) { |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 69 | if (!avoid_allocation && internal::UseBoringSSLForRandBytes()) { |
Daniel Cheng | b6bbf5a6 | 2022-09-09 18:26:34 | [diff] [blame] | 70 | // Ensure BoringSSL is initialized so it can use things like RDRAND. |
| 71 | CRYPTO_library_init(); |
| 72 | // BoringSSL's RAND_bytes always returns 1. Any error aborts the program. |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 73 | (void)RAND_bytes(output.data(), output.size()); |
Daniel Cheng | b6bbf5a6 | 2022-09-09 18:26:34 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Alex Gough | d28e61a | 2023-07-11 18:45:34 | [diff] [blame] | 77 | static decltype(&ProcessPrng) process_prng_fn = GetProcessPrng(); |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 78 | BOOL success = |
| 79 | process_prng_fn(static_cast<BYTE*>(output.data()), output.size()); |
Alex Gough | d28e61a | 2023-07-11 18:45:34 | [diff] [blame] | 80 | // ProcessPrng is documented to always return TRUE. |
| 81 | CHECK(success); |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 82 | } |
| 83 | |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 86 | void RandBytes(span<uint8_t> output) { |
| 87 | RandBytes(output, /*avoid_allocation=*/false); |
| 88 | } |
| 89 | |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 90 | void RandBytes(void* output, size_t output_length) { |
Tom Sepez | 30d6842 | 2024-01-24 19:55:09 | [diff] [blame] | 91 | RandBytes(make_span(static_cast<uint8_t*>(output), output_length), |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 92 | /*avoid_allocation=*/false); |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | namespace internal { |
| 96 | |
| 97 | double RandDoubleAvoidAllocation() { |
| 98 | uint64_t number; |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 99 | RandBytes(as_writable_bytes(make_span(&number, 1u)), |
| 100 | /*avoid_allocation=*/true); |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 101 | // This transformation is explained in rand_util.cc. |
| 102 | return (number >> 11) * 0x1.0p-53; |
| 103 | } |
| 104 | |
| 105 | } // namespace internal |
| 106 | |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 107 | } // namespace base |