OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #include "native_client/src/nonsfi/irt/irt_random.h" |
| 8 #include "native_client/src/untrusted/nacl/nacl_random.h" |
| 9 |
| 10 #include <errno.h> |
| 11 #include <fcntl.h> |
| 12 #include <stdlib.h> |
| 13 #include <sys/stat.h> |
| 14 #include <sys/types.h> |
| 15 #include <unistd.h> |
| 16 |
| 17 static int g_urandom_fd = -1; |
| 18 |
| 19 void nonsfi_set_urandom_fd(int fd) { |
| 20 g_urandom_fd = fd; |
| 21 } |
| 22 |
| 23 /* |
| 24 * Note: This library provides the nacl_secure_random_init for testing purposes. |
| 25 */ |
| 26 int nacl_secure_random_init(void) { |
| 27 return 0; /* Success */ |
| 28 } |
| 29 |
| 30 int nacl_secure_random(void *buf, size_t count, size_t *nread) { |
| 31 if (g_urandom_fd < 0) { |
| 32 /* |
| 33 * If the fd for /dev/urandom is not initialized, try to open it. |
| 34 * This happens on testing, or in nonsfi_loader. Abort on failure. |
| 35 */ |
| 36 g_urandom_fd = open("/dev/urandom", O_RDONLY); |
| 37 if (g_urandom_fd < 0) |
| 38 abort(); |
| 39 } |
| 40 |
| 41 int result = read(g_urandom_fd, buf, count); |
| 42 if (result < 0) |
| 43 return errno; |
| 44 *nread = result; |
| 45 return 0; |
| 46 } |
OLD | NEW |