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 <limits.h> | |
wtc
2014/01/22 22:42:04
I don't think you are using anything from the C he
| |
8 #include <stdint.h> | |
7 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <windows.h> | |
8 | 11 |
9 #include "base/basictypes.h" | 12 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 13 #include "base/logging.h" |
11 | 14 |
12 namespace { | 15 namespace { |
wtc
2014/01/22 22:42:04
Nit: add a blank line after this line.
| |
16 typedef BOOLEAN (WINAPI *RtlGenRandomPtr)(PVOID, ULONG); | |
13 | 17 |
14 uint32 RandUint32() { | 18 class WinRandom { |
15 uint32 number; | 19 public: |
16 CHECK_EQ(rand_s(&number), 0); | 20 WinRandom() |
17 return number; | 21 : rtl_gen_random_(reinterpret_cast<RtlGenRandomPtr>( |
18 } | 22 ::GetProcAddress(::GetModuleHandle(L"advapi32.dll"), |
23 "SystemFunction036"))) {} | |
24 ~WinRandom() {} | |
25 | |
26 void RtlGenRandom(void* output, uint32_t output_length) const { | |
27 CHECK_EQ(rtl_gen_random_(output, output_length), TRUE); | |
28 } | |
29 | |
30 private: | |
31 const RtlGenRandomPtr rtl_gen_random_; | |
32 }; | |
33 | |
34 base::LazyInstance<WinRandom>::Leaky g_win_random = LAZY_INSTANCE_INITIALIZER; | |
19 | 35 |
20 } // namespace | 36 } // namespace |
21 | 37 |
22 namespace base { | 38 namespace base { |
23 | 39 |
24 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 40 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
25 uint64 RandUint64() { | 41 uint64 RandUint64() { |
26 uint32 first_half = RandUint32(); | 42 uint64 number; |
27 uint32 second_half = RandUint32(); | 43 g_win_random.Pointer()->RtlGenRandom(&number, sizeof(number)); |
28 return (static_cast<uint64>(first_half) << 32) + second_half; | 44 return number; |
29 } | 45 } |
30 | 46 |
31 void RandBytes(void* output, size_t output_length) { | 47 void RandBytes(void* output, size_t output_length) { |
32 uint64 random_int; | 48 char* output_ptr = static_cast<char*>(output); |
33 const size_t random_int_size = sizeof(random_int); | 49 const WinRandom* win_random_ptr = g_win_random.Pointer(); |
wtc
2014/01/22 22:42:04
Nit: this should be
const WinRandom* const win_r
| |
34 for (size_t i = 0; i < output_length; i += random_int_size) { | 50 while (output_length > 0) { |
35 random_int = base::RandUint64(); | 51 const uint32_t output_bytes_this_pass = static_cast<uint32_t>( |
36 size_t copy_count = std::min(output_length - i, random_int_size); | 52 std::min(output_length, std::numeric_limits<uint32_t>::max())); |
37 memcpy(((uint8*)output) + i, &random_int, copy_count); | 53 win_random_ptr->RtlGenRandom(output_ptr, output_bytes_this_pass); |
54 output_length -= output_bytes_this_pass; | |
55 output_ptr += output_bytes_this_pass; | |
38 } | 56 } |
39 } | 57 } |
40 | 58 |
41 } // namespace base | 59 } // namespace base |
OLD | NEW |