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

Unified Diff: base/rand_util_nacl.cc

Issue 10795083: Enable some parts of base that were previously disabled in base_untrusted (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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/os_compat_nacl.cc ('k') | base/threading/platform_thread_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
new file mode 100644
index 0000000000000000000000000000000000000000..383ebcff19f7dedd16c0460a788ef0b8716c3a87
--- /dev/null
+++ b/base/rand_util_nacl.cc
@@ -0,0 +1,53 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/rand_util.h"
+
+#include "base/lazy_instance.h"
+#include "base/logging.h"
+#include "native_client/src/untrusted/irt/irt.h"
+
+namespace {
+
+class NaclRandom {
+ public:
+ NaclRandom() {
+ int success = nacl_interface_query(NACL_IRT_RANDOM_v0_1,
+ &random_, sizeof(random_));
+ if (!success)
+ LOG(FATAL) << "Failed to initialize RNG.";
willchan no longer on Chromium 2012/07/25 17:52:55 Can you split this out into DLOG(ERROR) with the m
Sergey Ulanov 2012/07/25 19:43:23 Replaced with CHECK()
+ }
+
+ ~NaclRandom() {
+ }
+
+ void GetRandomBytes(char* buffer, uint32_t num_bytes) {
+ while (num_bytes > 0) {
+ size_t nread;
+ int error = random_.get_random_bytes(buffer, num_bytes, &nread);
+ CHECK(error != 0) << "Failed to get random bytes.";
willchan no longer on Chromium 2012/07/25 17:52:55 Ditto here on the CHECKs with messages. Ditch the
Sergey Ulanov 2012/07/25 19:43:23 Done.
+ CHECK_LE(nread, num_bytes);
+ buffer += nread;
+ num_bytes -= nread;
+ }
+ }
+
+ private:
+ nacl_irt_random random_;
+};
+
+base::LazyInstance<NaclRandom> g_nacl_random = LAZY_INSTANCE_INITIALIZER;
willchan no longer on Chromium 2012/07/25 17:52:55 Should this be leaky? I don't know anything about
Sergey Ulanov 2012/07/25 19:43:23 Done.
+
+} // namespace
+
+namespace base {
+
+uint64 RandUint64() {
+ uint64 result;
willchan no longer on Chromium 2012/07/25 17:52:55 I think you may need basictypes.h for this type.
Sergey Ulanov 2012/07/25 19:43:23 Done.
+ g_nacl_random.Pointer()->GetRandomBytes(
+ reinterpret_cast<char*>(&result), sizeof(result));
+ return result;
+}
+
+} // namespace base
« no previous file with comments | « base/os_compat_nacl.cc ('k') | base/threading/platform_thread_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698