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

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: Simplify NaCl. 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
Index: base/rand_util_nacl.cc
diff --git a/base/rand_util_nacl.cc b/base/rand_util_nacl.cc
index 47450aaba68be254001da2f5e9d84e182b19ed76..e972be8e9d4e5f1f85499adb3ced1fb8e37406fc 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,10 +43,14 @@ base::LazyInstance<NaclRandom>::Leaky g_nacl_random = LAZY_INSTANCE_INITIALIZER;
namespace base {
+// NOTE: This function must be cryptographically secure. http://crbug.com/140076
wtc 2014/01/22 22:29:09 This comment actually contradicts the comment for
DaleCurtis 2014/01/22 22:36:26 Are you okay with moving this comment into the ran
wtc 2014/01/22 22:47:12 I think this comment doesn't make sense. The purpo
DaleCurtis 2014/01/22 22:54:14 Heh, fair enough. All changes to it reverted.
+void RandBytes(void* output, size_t output_length) {
wtc 2014/01/22 22:29:09 RandBytes should be defined after RandUint64 to ma
DaleCurtis 2014/01/22 22:54:14 Done.
+ g_nacl_random.Pointer()->GetRandomBytes(output, output_length);
+}
+
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;
}
« no previous file with comments | « base/rand_util.cc ('k') | base/rand_util_posix.cc » ('j') | base/rand_util_posix.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698