| 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 <nacl/nacl_random.h> | 7 #include <nacl/nacl_random.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 void GetRandomBytes(void* output, size_t num_bytes) { | 14 void GetRandomBytes(void* output, size_t num_bytes) { |
| 15 CHECK_EQ(0, nacl_secure_random_init()); | |
| 16 char* output_ptr = static_cast<char*>(output); | 15 char* output_ptr = static_cast<char*>(output); |
| 17 while (num_bytes > 0) { | 16 while (num_bytes > 0) { |
| 18 size_t nread; | 17 size_t nread; |
| 19 const int error = nacl_secure_random(output_ptr, num_bytes, &nread); | 18 const int error = nacl_secure_random(output_ptr, num_bytes, &nread); |
| 20 CHECK_EQ(error, 0); | 19 CHECK_EQ(error, 0); |
| 21 CHECK_LE(nread, num_bytes); | 20 CHECK_LE(nread, num_bytes); |
| 22 output_ptr += nread; | 21 output_ptr += nread; |
| 23 num_bytes -= nread; | 22 num_bytes -= nread; |
| 24 } | 23 } |
| 25 } | 24 } |
| 26 | 25 |
| 27 } // namespace | 26 } // namespace |
| 28 | 27 |
| 29 namespace base { | 28 namespace base { |
| 30 | 29 |
| 31 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 30 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
| 32 uint64 RandUint64() { | 31 uint64 RandUint64() { |
| 33 uint64 result; | 32 uint64 result; |
| 34 GetRandomBytes(&result, sizeof(result)); | 33 GetRandomBytes(&result, sizeof(result)); |
| 35 return result; | 34 return result; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void RandBytes(void* output, size_t output_length) { | 37 void RandBytes(void* output, size_t output_length) { |
| 39 GetRandomBytes(output, output_length); | 38 GetRandomBytes(output, output_length); |
| 40 } | 39 } |
| 41 | 40 |
| 42 } // namespace base | 41 } // namespace base |
| OLD | NEW |