| 1 | #include "kernelrandombytes.h" |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | static int fd = -1; |
| 8 | |
| 9 | void kernelrandombytes(unsigned char *x,unsigned long long xlen) |
| 10 | { |
| 11 | int i; |
| 12 | |
| 13 | if (fd == -1) { |
| 14 | for (;;) { |
| 15 | fd = open("/dev/urandom",O_RDONLY); |
| 16 | if (fd != -1) break; |
| 17 | sleep(1); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | while (xlen > 0) { |
| 22 | if (xlen < 1048576) i = xlen; else i = 1048576; |
| 23 | |
| 24 | i = read(fd,x,i); |
| 25 | if (i < 1) { |
| 26 | sleep(1); |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | x += i; |
| 31 | xlen -= i; |
| 32 | } |
| 33 | } |