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 <fcntl.h> | |
11 #include <errno.h> | |
Mark Seaborn
2014/11/04 01:19:52
Nit: sort #includes
hidehiko
2014/11/04 11:16:59
Done.
| |
12 #include <stdlib.h> | |
13 #include <sys/stat.h> | |
14 #include <sys/types.h> | |
15 #include <unistd.h> | |
16 | |
17 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.
| |
18 | |
19 static int g_urandom_fd = -1; | |
20 | |
21 void nonsfi_set_urandom_fd(int fd) { | |
22 g_urandom_fd = fd; | |
23 } | |
24 | |
25 /* | |
26 * 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
| |
27 * actually unnecessary, and will never be used. | |
28 */ | |
29 | |
30 int nacl_secure_random_init(void) { | |
31 return 0; /* Success */ | |
32 } | |
33 | |
34 int nacl_secure_random(void *buf, size_t count, size_t *nread) { | |
35 if (g_urandom_fd < 0) { | |
36 /* | |
37 * If the fd for /dev/urandom is not initialized, try to open it. | |
38 * This happens on testing, or in nonsfi_loader. Abort on failure. | |
39 */ | |
40 g_urandom_fd = open("/dev/urandom", O_RDONLY); | |
41 if (g_urandom_fd < 0) | |
42 abort(); | |
43 } | |
44 | |
45 int result = read(g_urandom_fd, buf, count); | |
46 if (result < 0) | |
47 return errno; | |
48 *nread = result; | |
49 return 0; | |
50 } | |
51 | |
52 EXTERN_C_END | |
Mark Seaborn
2014/11/04 01:19:52
Ditto
hidehiko
2014/11/04 11:16:59
Done.
| |
OLD | NEW |