Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Scott Graham | 4ffd63b5 | 2017-06-01 18:03:33 | [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 | |
Scott Graham | fe0e9f46 | 2017-09-18 21:25:04 | [diff] [blame] | 7 | #include <zircon/syscalls.h> |
Scott Graham | 4ffd63b5 | 2017-06-01 18:03:33 | [diff] [blame] | 8 | |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 9 | #include <atomic> |
| 10 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 11 | #include "base/containers/span.h" |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 12 | #include "base/feature_list.h" |
Daniel Cheng | b6bbf5a6 | 2022-09-09 18:26:34 | [diff] [blame] | 13 | #include "third_party/boringssl/src/include/openssl/rand.h" |
| 14 | |
Scott Graham | 4ffd63b5 | 2017-06-01 18:03:33 | [diff] [blame] | 15 | namespace base { |
| 16 | |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 17 | namespace internal { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // The BoringSSl helpers are duplicated in rand_util_posix.cc and |
| 22 | // rand_util_win.cc. |
| 23 | std::atomic<bool> g_use_boringssl; |
| 24 | |
| 25 | BASE_FEATURE(kUseBoringSSLForRandBytes, |
| 26 | "UseBoringSSLForRandBytes", |
| 27 | FEATURE_DISABLED_BY_DEFAULT); |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | void ConfigureBoringSSLBackedRandBytesFieldTrial() { |
| 32 | g_use_boringssl.store(FeatureList::IsEnabled(kUseBoringSSLForRandBytes), |
| 33 | std::memory_order_relaxed); |
| 34 | } |
| 35 | |
| 36 | bool UseBoringSSLForRandBytes() { |
| 37 | return g_use_boringssl.load(std::memory_order_relaxed); |
| 38 | } |
| 39 | |
| 40 | } // namespace internal |
| 41 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 42 | void RandBytes(span<uint8_t> output) { |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 43 | if (internal::UseBoringSSLForRandBytes()) { |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 44 | // BoringSSL's RAND_bytes always returns 1. Any error aborts the program. |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 45 | (void)RAND_bytes(output.data(), output.size()); |
Mark Mentovai | 5d6e763 | 2023-08-11 17:21:43 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 49 | zx_cprng_draw(output.data(), output.size()); |
| 50 | } |
| 51 | |
Egor Pasko | 1c7e624 | 2022-09-20 12:45:39 | [diff] [blame] | 52 | namespace internal { |
| 53 | |
| 54 | double RandDoubleAvoidAllocation() { |
| 55 | uint64_t number; |
| 56 | zx_cprng_draw(&number, sizeof(number)); |
| 57 | // This transformation is explained in rand_util.cc. |
| 58 | return (number >> 11) * 0x1.0p-53; |
| 59 | } |
| 60 | |
| 61 | } // namespace internal |
| 62 | |
Scott Graham | 4ffd63b5 | 2017-06-01 18:03:33 | [diff] [blame] | 63 | } // namespace base |