2018-12-31 20:19:28 +03:00
|
|
|
#include <ent/config.h>
|
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
/* ent.h undefs all constants and config.h is #pragma once. */
|
2018-12-31 20:19:28 +03:00
|
|
|
|
|
|
|
#if defined(ENT_GETRANDOM)
|
2018-12-31 20:21:50 +03:00
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
#define _ENT_IMPL
|
|
|
|
#define _GNU_SOURCE
|
2018-12-27 22:13:51 +03:00
|
|
|
#include <unistd.h>
|
2018-12-31 20:19:28 +03:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
#define ENT_DEFS (void)0
|
|
|
|
#define ENT_INIT() (void)0
|
2018-12-31 21:02:21 +03:00
|
|
|
#define ENT_READ(B, L) syscall(SYS_getrandom, B, L, 0)
|
2018-12-31 20:40:38 +03:00
|
|
|
#define ENT_FINI() (void)0
|
|
|
|
|
|
|
|
#elif defined(ENT_URANDOM)
|
2018-12-31 20:19:28 +03:00
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
#define _ENT_IMPL
|
|
|
|
#include <unistd.h>
|
2018-12-27 22:06:22 +03:00
|
|
|
#include <sys/types.h>
|
2018-12-31 20:19:28 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2018-12-27 09:03:44 +03:00
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
#define ENT_DEFS int fd
|
|
|
|
#define ENT_INIT() do { \
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) < 0) \
|
|
|
|
return -1; \
|
|
|
|
} while (0)
|
|
|
|
#define ENT_READ(B, L) read(fd, B, L)
|
|
|
|
#define ENT_FINI() (void) close(fd)
|
|
|
|
|
2018-12-31 20:19:28 +03:00
|
|
|
#endif
|
2018-12-27 09:03:44 +03:00
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
|
|
|
|
#include <ent/ent.h>
|
|
|
|
|
|
|
|
#if defined(_ENT_IMPL)
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
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-31 20:19:28 +03:00
|
|
|
char *cuf = buf;
|
|
|
|
int ret;
|
2018-12-31 20:40:38 +03:00
|
|
|
ENT_DEFS;
|
2018-12-27 09:03:44 +03:00
|
|
|
|
2018-12-27 22:06:22 +03:00
|
|
|
assert(len <= 256);
|
2018-12-28 05:38:54 +03:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2018-12-31 20:40:38 +03:00
|
|
|
ENT_INIT();
|
|
|
|
while (len && (ret = ENT_READ(cuf, len)) != 0) {
|
2018-12-31 20:19:28 +03:00
|
|
|
if (ret < 0) {
|
2018-12-27 22:06:22 +03:00
|
|
|
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
|
|
|
}
|
2018-12-31 20:40:38 +03:00
|
|
|
ENT_FINI();
|
2018-12-31 20:19:28 +03:00
|
|
|
if (!ret) {
|
2018-12-27 22:06:22 +03:00
|
|
|
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-31 20:21:50 +03:00
|
|
|
|
2018-12-31 20:40:38 +03:00
|
|
|
#endif /* _ENT_IMPL */
|