xmss-KAT-generator/randombytes.c

39 line
648 B
C

/*
This code was taken from the SPHINCS reference implementation and is public domain.
*/
2015-08-11 11:08:27 +01:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* it's really stupid that there isn't a syscall for this */
static int fd = -1;
void randombytes(unsigned char *x, unsigned long long xlen)
2015-08-11 11:08:27 +01:00
{
int i;
if (fd == -1) {
for (;;) {
fd = open("/dev/urandom", O_RDONLY);
2015-08-11 11:08:27 +01:00
if (fd != -1) break;
sleep(1);
}
}
while (xlen > 0) {
if (xlen < 1048576) i = xlen; else i = 1048576;
i = read(fd, x, i);
2015-08-11 11:08:27 +01:00
if (i < 1) {
sleep(1);
continue;
}
x += i;
xlen -= i;
}
}