Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: base/rand_util_win.cc

Issue 141753009: Use RtlGenRandom() directly instead of going through rand_s(). (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing header. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <windows.h>
8 8
9 #include "base/basictypes.h" 9 // #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the
10 // "Community Additions" comment on MSDN here:
11 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx
12 #define SystemFunction036 NTAPI SystemFunction036
13 #include <NTSecAPI.h>
14 #undef SystemFunction036
15
16 #include <algorithm>
17 #include <limits>
18
10 #include "base/logging.h" 19 #include "base/logging.h"
11 20
12 namespace {
13
14 uint32 RandUint32() {
15 uint32 number;
16 CHECK_EQ(rand_s(&number), 0);
17 return number;
18 }
19
20 } // namespace
21
22 namespace base { 21 namespace base {
23 22
24 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 23 // NOTE: This function must be cryptographically secure. http://crbug.com/140076
25 uint64 RandUint64() { 24 uint64 RandUint64() {
26 uint32 first_half = RandUint32(); 25 uint64 number;
27 uint32 second_half = RandUint32(); 26 RandBytes(&number, sizeof(number));
28 return (static_cast<uint64>(first_half) << 32) + second_half; 27 return number;
29 } 28 }
30 29
31 void RandBytes(void* output, size_t output_length) { 30 void RandBytes(void* output, size_t output_length) {
32 uint64 random_int; 31 char* output_ptr = static_cast<char*>(output);
33 const size_t random_int_size = sizeof(random_int); 32 while (output_length > 0) {
34 for (size_t i = 0; i < output_length; i += random_int_size) { 33 const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min(
35 random_int = base::RandUint64(); 34 output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max())));
36 const size_t copy_count = std::min(output_length - i, random_int_size); 35 const bool success =
37 memcpy(((uint8*)output) + i, &random_int, copy_count); 36 RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE;
37 CHECK(success);
38 output_length -= output_bytes_this_pass;
39 output_ptr += output_bytes_this_pass;
38 } 40 }
39 } 41 }
40 42
41 } // namespace base 43 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698