Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / randombytes / devurandom.c
blobd8b24db50d96fdc6ce39b8f890743e43e9fa77fc
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #ifdef _WIN32
6 # include <windows.h>
7 #endif
9 /* it's really stupid that there isn't a syscall for this */
10 /* -> Y U NO USE OPENBSD? */
12 static int fd = -1;
14 void randombytes(unsigned char *x,unsigned long long xlen)
16 int i;
18 if (fd == -1) {
19 for (;;) {
20 fd = open("/dev/urandom",O_RDONLY);
21 if (fd != -1) break;
22 #ifdef _WIN32
23 Sleep(1000);
24 #else
25 sleep(1);
26 #endif
30 while (xlen > 0) {
31 if (xlen < 1048576) i = xlen; else i = 1048576;
33 i = read(fd,x,i);
34 if (i < 1) {
35 #ifdef _WIN32
36 Sleep(1000);
37 #else
38 sleep(1);
39 #endif
40 continue;
43 x += i;
44 xlen -= i;