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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "native_client/src/untrusted/irt/irt.h" | 10 #include "native_client/src/untrusted/nacl/nacl_random.h" |
|
Mark Seaborn
2014/06/16 23:47:38
I think it should work to #include this with:
#inc
Peng
2014/06/16 23:57:26
It works. Done
| |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 class NaclRandom { | 14 class NaclRandom { |
| 15 public: | 15 public: |
| 16 NaclRandom() { | 16 NaclRandom() { |
| 17 const size_t result = | 17 int rv = nacl_secure_random_init(); |
|
Mark Seaborn
2014/06/16 23:39:25
nacl_secure_random_init() is idempotent so you can
Peng
2014/06/16 23:57:26
Done.
| |
| 18 nacl_interface_query(NACL_IRT_RANDOM_v0_1, &random_, sizeof(random_)); | 18 CHECK_EQ(rv, 0); |
| 19 CHECK_EQ(result, sizeof(random_)); | |
| 20 } | 19 } |
| 21 | 20 |
| 22 ~NaclRandom() {} | 21 ~NaclRandom() {} |
| 23 | 22 |
| 24 void GetRandomBytes(void* output, size_t num_bytes) { | 23 void GetRandomBytes(void* output, size_t num_bytes) { |
| 25 char* output_ptr = static_cast<char*>(output); | 24 char* output_ptr = static_cast<char*>(output); |
| 26 while (num_bytes > 0) { | 25 while (num_bytes > 0) { |
| 27 size_t nread; | 26 size_t nread; |
| 28 const int error = random_.get_random_bytes(output_ptr, num_bytes, &nread); | 27 const int error = nacl_secure_random(output_ptr, num_bytes, &nread); |
| 29 CHECK_EQ(error, 0); | 28 CHECK_EQ(error, 0); |
| 30 CHECK_LE(nread, num_bytes); | 29 CHECK_LE(nread, num_bytes); |
| 31 output_ptr += nread; | 30 output_ptr += nread; |
| 32 num_bytes -= nread; | 31 num_bytes -= nread; |
| 33 } | 32 } |
| 34 } | 33 } |
| 35 | |
| 36 private: | |
| 37 nacl_irt_random random_; | |
| 38 }; | 34 }; |
| 39 | 35 |
| 40 base::LazyInstance<NaclRandom>::Leaky g_nacl_random = LAZY_INSTANCE_INITIALIZER; | 36 base::LazyInstance<NaclRandom>::Leaky g_nacl_random = LAZY_INSTANCE_INITIALIZER; |
| 41 | 37 |
| 42 } // namespace | 38 } // namespace |
| 43 | 39 |
| 44 namespace base { | 40 namespace base { |
| 45 | 41 |
| 46 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 42 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
| 47 uint64 RandUint64() { | 43 uint64 RandUint64() { |
| 48 uint64 result; | 44 uint64 result; |
| 49 g_nacl_random.Pointer()->GetRandomBytes(&result, sizeof(result)); | 45 g_nacl_random.Pointer()->GetRandomBytes(&result, sizeof(result)); |
| 50 return result; | 46 return result; |
| 51 } | 47 } |
| 52 | 48 |
| 53 void RandBytes(void* output, size_t output_length) { | 49 void RandBytes(void* output, size_t output_length) { |
| 54 g_nacl_random.Pointer()->GetRandomBytes(output, output_length); | 50 g_nacl_random.Pointer()->GetRandomBytes(output, output_length); |
| 55 } | 51 } |
| 56 | 52 |
| 57 } // namespace base | 53 } // namespace base |
| OLD | NEW |