X-Git-Url: http://git.thejh.net/?p=detour.git;a=blobdiff_plain;f=devurandom.c;fp=devurandom.c;h=8a7a7b6ff73bcd46172f259c5e630d894ae78ae7;hp=0000000000000000000000000000000000000000;hb=b6172a42cb1cc9e60c23696f20f5cdbd86e64cec;hpb=b2a2f3d5cc67240d1450c48700a20f07270a42c2 diff --git a/devurandom.c b/devurandom.c new file mode 100644 index 0000000..8a7a7b6 --- /dev/null +++ b/devurandom.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +/* it's really stupid that there isn't a syscall for this */ + +static int fd = -1; + +void randombytes(void *x_,unsigned long long xlen) +{ + unsigned char *x = x_; + int i; + + if (fd == -1) { + for (;;) { + fd = open("/dev/urandom",O_RDONLY); + if (fd != -1) break; + sleep(1); + } + } + + while (xlen > 0) { + if (xlen < 1048576) i = xlen; else i = 1048576; + + i = read(fd,x,i); + if (i < 1) { + sleep(1); + continue; + } + + x += i; + xlen -= i; + } +}