| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/rand_util.h" | 5 #include "base/rand_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 8 | 10 |
| 9 // #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the | 11 // #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the |
| 10 // "Community Additions" comment on MSDN here: | 12 // "Community Additions" comment on MSDN here: |
| 11 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx | 13 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx |
| 12 #define SystemFunction036 NTAPI SystemFunction036 | 14 #define SystemFunction036 NTAPI SystemFunction036 |
| 13 #include <NTSecAPI.h> | 15 #include <NTSecAPI.h> |
| 14 #undef SystemFunction036 | 16 #undef SystemFunction036 |
| 15 | 17 |
| 16 #include <algorithm> | 18 #include <algorithm> |
| 17 #include <limits> | 19 #include <limits> |
| 18 | 20 |
| 19 #include "base/logging.h" | 21 #include "base/logging.h" |
| 20 | 22 |
| 21 namespace base { | 23 namespace base { |
| 22 | 24 |
| 23 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 25 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
| 24 uint64 RandUint64() { | 26 uint64_t RandUint64() { |
| 25 uint64 number; | 27 uint64_t number; |
| 26 RandBytes(&number, sizeof(number)); | 28 RandBytes(&number, sizeof(number)); |
| 27 return number; | 29 return number; |
| 28 } | 30 } |
| 29 | 31 |
| 30 void RandBytes(void* output, size_t output_length) { | 32 void RandBytes(void* output, size_t output_length) { |
| 31 char* output_ptr = static_cast<char*>(output); | 33 char* output_ptr = static_cast<char*>(output); |
| 32 while (output_length > 0) { | 34 while (output_length > 0) { |
| 33 const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min( | 35 const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min( |
| 34 output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max()))); | 36 output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max()))); |
| 35 const bool success = | 37 const bool success = |
| 36 RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE; | 38 RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE; |
| 37 CHECK(success); | 39 CHECK(success); |
| 38 output_length -= output_bytes_this_pass; | 40 output_length -= output_bytes_this_pass; |
| 39 output_ptr += output_bytes_this_pass; | 41 output_ptr += output_bytes_this_pass; |
| 40 } | 42 } |
| 41 } | 43 } |
| 42 | 44 |
| 43 } // namespace base | 45 } // namespace base |
| OLD | NEW |