2018-12-27 21:56:10 +03:00
|
|
|
#include <ent/ent.h>
|
2018-12-26 06:44:59 +03:00
|
|
|
|
2018-12-27 22:06:22 +03:00
|
|
|
#if defined(ENT_URANDOM)
|
|
|
|
|
2018-12-27 23:05:15 +03:00
|
|
|
#warning "libent: using /dev/urandom..."
|
|
|
|
|
2018-12-27 22:06:22 +03:00
|
|
|
#include <assert.h>
|
2018-12-27 22:13:51 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2018-12-27 22:06:22 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
2018-12-27 09:03:44 +03:00
|
|
|
|
|
|
|
|
2018-12-27 21:56:10 +03:00
|
|
|
int
|
2018-12-27 22:06:22 +03:00
|
|
|
ent_getentropy(void* buf, size_t len)
|
2018-12-27 09:03:44 +03:00
|
|
|
{
|
2018-12-27 22:13:51 +03:00
|
|
|
int fd;
|
2018-12-27 22:06:22 +03:00
|
|
|
ssize_t ret;
|
2018-12-27 22:19:59 +03:00
|
|
|
char* cuf = buf;
|
2018-12-27 09:03:44 +03:00
|
|
|
|
2018-12-27 22:06:22 +03:00
|
|
|
assert(len <= 256);
|
2018-12-27 09:03:44 +03:00
|
|
|
if (-1 == (fd = open("/dev/urandom", O_RDONLY))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-27 22:13:51 +03:00
|
|
|
while (len != 0 && (ret = read(fd, cuf, len)) != 0) {
|
2018-12-27 22:06:22 +03:00
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= ret;
|
2018-12-27 22:13:51 +03:00
|
|
|
cuf += ret;
|
2018-12-27 22:06:22 +03:00
|
|
|
}
|
|
|
|
(void) close(fd);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = -1;
|
|
|
|
errno = EIO;
|
2018-12-27 09:03:44 +03:00
|
|
|
}
|
2018-12-27 22:06:22 +03:00
|
|
|
return ret < 0 ? ret : 0;
|
2018-12-27 09:03:44 +03:00
|
|
|
}
|
|
|
|
|
2018-12-27 21:56:10 +03:00
|
|
|
#endif /* ENT_USE_URANDOM */
|