Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 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 | |||||
[email protected] | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 7 | #include <windows.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 10 | |
[email protected] | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 11 | // #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the |
12 | // "Community Additions" comment on MSDN here: | ||||
13 | // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx | ||||
14 | #define SystemFunction036 NTAPI SystemFunction036 | ||||
15 | #include <NTSecAPI.h> | ||||
16 | #undef SystemFunction036 | ||||
17 | |||||
18 | #include <algorithm> | ||||
19 | #include <limits> | ||||
20 | |||||
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 21 | #include "base/check.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 22 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 23 | namespace base { |
24 | |||||
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 25 | void RandBytes(void* output, size_t output_length) { |
[email protected] | 8c37218a | 2014-01-24 00:01:14 | [diff] [blame] | 26 | char* output_ptr = static_cast<char*>(output); |
27 | while (output_length > 0) { | ||||
28 | const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min( | ||||
29 | output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max()))); | ||||
30 | const bool success = | ||||
31 | RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE; | ||||
32 | CHECK(success); | ||||
33 | output_length -= output_bytes_this_pass; | ||||
34 | output_ptr += output_bytes_this_pass; | ||||
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 35 | } |
36 | } | ||||
37 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 38 | } // namespace base |