randombytes.c 563 B

123456789101112131415161718192021222324252627282930313233
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. /* it's really stupid that there isn't a syscall for this */
  6. static int fd = -1;
  7. void randombytes(unsigned char *x,unsigned long long xlen) {
  8. int i;
  9. if (fd == -1) {
  10. for (;;) {
  11. fd = open("/dev/urandom",O_RDONLY);
  12. if (fd != -1) break;
  13. sleep(1);
  14. }
  15. }
  16. while (xlen > 0) {
  17. if (xlen < 1048576) i = (int)xlen; else i = 1048576;
  18. i = (int)read(fd,x,i);
  19. if (i < 1) {
  20. sleep(1);
  21. continue;
  22. }
  23. x += i;
  24. xlen -= i;
  25. }
  26. }