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

Unified Diff: src/nonsfi/irt/irt_random.c

Issue 686723003: Non-SFI mode: Implement nacl_irt_random only for nacl_helper_nonsfi. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Rebase Created 6 years, 1 month 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: src/nonsfi/irt/irt_random.c
diff --git a/src/nonsfi/irt/irt_random.c b/src/nonsfi/irt/irt_random.c
new file mode 100644
index 0000000000000000000000000000000000000000..543d77b12eb8a69bd5dba7fd51c158d8cec607ce
--- /dev/null
+++ b/src/nonsfi/irt/irt_random.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2014 The Native Client 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 "native_client/src/nonsfi/irt/irt_random.h"
+#include "native_client/src/untrusted/nacl/nacl_random.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+static int g_urandom_fd = -1;
+
+void nonsfi_set_urandom_fd(int fd) {
+ g_urandom_fd = fd;
+}
+
+/*
+ * Note: This library provides the nacl_secure_random_init for testing purposes.
+ */
+int nacl_secure_random_init(void) {
+ return 0; /* Success */
+}
+
+int nacl_secure_random(void *buf, size_t count, size_t *nread) {
+ if (g_urandom_fd < 0) {
+ /*
+ * If the fd for /dev/urandom is not initialized, try to open it.
+ * This happens on testing, or in nonsfi_loader. Abort on failure.
+ */
+ g_urandom_fd = open("/dev/urandom", O_RDONLY);
+ if (g_urandom_fd < 0)
+ abort();
+ }
+
+ int result = read(g_urandom_fd, buf, count);
+ if (result < 0)
+ return errno;
+ *nread = result;
+ return 0;
+}

Powered by Google App Engine
This is Rietveld 408576698