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

Unified Diff: base/rand_util_nacl.cc

Issue 140773006: Improve base::RandBytes() performance by 1.75x-2.10x on POSIX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/rand_util.cc ('k') | base/rand_util_posix.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/rand_util_nacl.cc
diff --git a/base/rand_util_nacl.cc b/base/rand_util_nacl.cc
index 47450aaba68be254001da2f5e9d84e182b19ed76..f9751217326b4c72730b98fa1018be49f12a4b1a 100644
--- a/base/rand_util_nacl.cc
+++ b/base/rand_util_nacl.cc
@@ -14,21 +14,21 @@ namespace {
class NaclRandom {
public:
NaclRandom() {
- size_t result = nacl_interface_query(NACL_IRT_RANDOM_v0_1,
- &random_, sizeof(random_));
+ const size_t result =
+ nacl_interface_query(NACL_IRT_RANDOM_v0_1, &random_, sizeof(random_));
CHECK_EQ(result, sizeof(random_));
}
- ~NaclRandom() {
- }
+ ~NaclRandom() {}
- void GetRandomBytes(char* buffer, uint32_t num_bytes) {
+ void GetRandomBytes(void* output, size_t num_bytes) {
+ char* output_ptr = static_cast<char*>(output);
while (num_bytes > 0) {
size_t nread;
- int error = random_.get_random_bytes(buffer, num_bytes, &nread);
+ const int error = random_.get_random_bytes(output_ptr, num_bytes, &nread);
CHECK_EQ(error, 0);
CHECK_LE(nread, num_bytes);
- buffer += nread;
+ output_ptr += nread;
num_bytes -= nread;
}
}
@@ -43,11 +43,15 @@ base::LazyInstance<NaclRandom>::Leaky g_nacl_random = LAZY_INSTANCE_INITIALIZER;
namespace base {
+// NOTE: This function must be cryptographically secure. http://crbug.com/140076
uint64 RandUint64() {
uint64 result;
- g_nacl_random.Pointer()->GetRandomBytes(
- reinterpret_cast<char*>(&result), sizeof(result));
+ g_nacl_random.Pointer()->GetRandomBytes(&result, sizeof(result));
return result;
}
+void RandBytes(void* output, size_t output_length) {
+ g_nacl_random.Pointer()->GetRandomBytes(output, output_length);
+}
+
} // namespace base
« no previous file with comments | « base/rand_util.cc ('k') | base/rand_util_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698