Chromium Code Reviews| 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 <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <windows.h> | |
| 8 | 9 |
| 9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/lazy_instance.h" | |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 15 typedef BOOLEAN (WINAPI *RtlGenRandomPtr)(PVOID, ULONG); | |
| 13 | 16 |
| 14 uint32 RandUint32() { | 17 class WinRandom { |
| 15 uint32 number; | 18 public: |
| 16 CHECK_EQ(rand_s(&number), 0); | 19 WinRandom() |
| 17 return number; | 20 : rtl_gen_random_(reinterpret_cast<RtlGenRandomPtr>( |
| 18 } | 21 ::GetProcAddress(::GetModuleHandle(L"advapi32.dll"), |
|
bradn
2014/01/22 22:31:29
Any accomodation need to be made for failure here?
wtc
2014/01/22 22:42:04
RtlGenRandom is already mentioned in some official
jschuh
2014/01/22 23:04:24
One thing I forgot. Contrary to popular belief, th
DaleCurtis
2014/01/22 23:50:51
This compiles and works using the workaround menti
| |
| 22 "SystemFunction036"))) {} | |
| 23 ~WinRandom() {} | |
| 24 | |
| 25 void RtlGenRandom(void* output, uint32 output_length) const { | |
| 26 CHECK_EQ(rtl_gen_random_(output, output_length), TRUE); | |
| 27 } | |
| 28 | |
| 29 private: | |
| 30 const RtlGenRandomPtr rtl_gen_random_; | |
| 31 }; | |
| 32 | |
| 33 base::LazyInstance<WinRandom>::Leaky g_win_random = LAZY_INSTANCE_INITIALIZER; | |
| 19 | 34 |
| 20 } // namespace | 35 } // namespace |
| 21 | 36 |
| 22 namespace base { | 37 namespace base { |
| 23 | 38 |
| 24 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 39 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
| 25 uint64 RandUint64() { | 40 uint64 RandUint64() { |
| 26 uint32 first_half = RandUint32(); | 41 uint64 number; |
|
Ryan Sleevi
2014/01/22 22:12:26
Free fixup while you're here - http://src.chromium
DaleCurtis
2014/01/22 22:27:53
I've left this one alone for now since it requires
| |
| 27 uint32 second_half = RandUint32(); | 42 g_win_random.Pointer()->RtlGenRandom(&number, sizeof(number)); |
| 28 return (static_cast<uint64>(first_half) << 32) + second_half; | 43 return number; |
| 29 } | 44 } |
| 30 | 45 |
| 31 void RandBytes(void* output, size_t output_length) { | 46 void RandBytes(void* output, size_t output_length) { |
| 32 uint64 random_int; | 47 char* output_ptr = static_cast<char*>(output); |
| 33 const size_t random_int_size = sizeof(random_int); | 48 const WinRandom* win_random_ptr = g_win_random.Pointer(); |
| 34 for (size_t i = 0; i < output_length; i += random_int_size) { | 49 while (output_length > 0) { |
| 35 random_int = base::RandUint64(); | 50 const uint32 output_bytes_this_pass = |
|
Ryan Sleevi
2014/01/22 22:12:26
use uint32_t and use stdint.h - https://code.googl
DaleCurtis
2014/01/22 22:27:53
Thanks for the pointer. I didn't realize this was
| |
| 36 size_t copy_count = std::min(output_length - i, random_int_size); | 51 std::min(output_length, static_cast<size_t>(kuint32max)); |
|
Ryan Sleevi
2014/01/22 22:12:26
note: Don't you need a cast here, since you're pro
DaleCurtis
2014/01/22 22:27:53
Done.
DaleCurtis
2014/01/22 23:50:51
Sadly this doesn't seem to work with MSVC, it refu
jschuh
2014/01/23 17:24:13
I think the explicit cast is actually correct, as
| |
| 37 memcpy(((uint8*)output) + i, &random_int, copy_count); | 52 win_random_ptr->RtlGenRandom(output_ptr, output_bytes_this_pass); |
| 53 output_length -= output_bytes_this_pass; | |
| 54 output_ptr += output_bytes_this_pass; | |
| 38 } | 55 } |
| 39 } | 56 } |
| 40 | 57 |
| 41 } // namespace base | 58 } // namespace base |
| OLD | NEW |