summaryrefslogtreecommitdiff
path: root/nacl/randombytes/devurandom.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/randombytes/devurandom.c')
-rw-r--r--nacl/randombytes/devurandom.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/nacl/randombytes/devurandom.c b/nacl/randombytes/devurandom.c
new file mode 100644
index 00000000..f3b8d418
--- /dev/null
+++ b/nacl/randombytes/devurandom.c
@@ -0,0 +1,34 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <fcntl.h>
4#include <unistd.h>
5
6/* it's really stupid that there isn't a syscall for this */
7
8static int fd = -1;
9
10void randombytes(unsigned char *x,unsigned long long xlen)
11{
12 int i;
13
14 if (fd == -1) {
15 for (;;) {
16 fd = open("/dev/urandom",O_RDONLY);
17 if (fd != -1) break;
18 sleep(1);
19 }
20 }
21
22 while (xlen > 0) {
23 if (xlen < 1048576) i = xlen; else i = 1048576;
24
25 i = read(fd,x,i);
26 if (i < 1) {
27 sleep(1);
28 continue;
29 }
30
31 x += i;
32 xlen -= i;
33 }
34}