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..4c110ef969a14cec484a27a5b3fe8e84df33cd51 |
--- /dev/null |
+++ b/src/nonsfi/irt/irt_random.c |
@@ -0,0 +1,52 @@ |
+/* |
+ * 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 <fcntl.h> |
+#include <errno.h> |
Mark Seaborn
2014/11/04 01:19:52
Nit: sort #includes
hidehiko
2014/11/04 11:16:59
Done.
|
+#include <stdlib.h> |
+#include <sys/stat.h> |
+#include <sys/types.h> |
+#include <unistd.h> |
+ |
+EXTERN_C_BEGIN |
Mark Seaborn
2014/11/04 01:19:52
Not needed in a .c file.
hidehiko
2014/11/04 11:16:59
Done.
|
+ |
+static int g_urandom_fd = -1; |
+ |
+void nonsfi_set_urandom_fd(int fd) { |
+ g_urandom_fd = fd; |
+} |
+ |
+/* |
+ * Note: This library does not provide nacl_secure_random_init(), as it is |
Mark Seaborn
2014/11/04 01:19:52
Er, this library does provide nacl_secure_random_i
hidehiko
2014/11/04 11:16:59
Oops, sorry. This was wrong comment. Initially I t
|
+ * actually unnecessary, and will never be used. |
+ */ |
+ |
+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; |
+} |
+ |
+EXTERN_C_END |
Mark Seaborn
2014/11/04 01:19:52
Ditto
hidehiko
2014/11/04 11:16:59
Done.
|