27 #if defined(ASCON_TRNG_DEV_RANDOM)
29 #if defined(HAVE_CONFIG_H)
32 #include <sys/types.h>
39 #if defined(__linux__) || defined(HAVE_SYS_SYSCALL_H)
40 #include <sys/syscall.h>
42 #if defined(HAVE_SYS_RANDOM_H)
43 #include <sys/random.h>
47 #define RANDOM_DEVICE "/dev/urandom"
50 #if defined(HAVE_GETRANDOM)
51 #define ascon_getrandom(buf, size) getrandom((buf), (size), 0)
52 #elif defined(HAVE_GETENTROPY)
53 #define ascon_getrandom(buf, size) getentropy((buf), (size))
54 #elif defined(SYS_getrandom)
55 #define ascon_getrandom(buf, size) syscall(SYS_getrandom, (buf), (size), 0)
58 #if !defined(ascon_getrandom)
59 static int ascon_dev_random_open(
void)
61 return open(RANDOM_DEVICE, O_RDONLY);
64 #define ascon_dev_random_open() -1
67 static int ascon_dev_random_read(
int fd,
unsigned char *out,
size_t outlen)
69 #if defined(ascon_getrandom)
73 int ret = ascon_getrandom(out, outlen);
79 if (errno != EINTR && errno != EAGAIN) {
81 memset(out, 0, outlen);
91 int ret = read(fd, out, outlen);
92 if (ret == (
int)outlen) {
95 if (errno != EINTR && errno != EAGAIN)
101 memset(out, 0, outlen);
108 #if defined(ascon_getrandom)
109 return ascon_dev_random_read(-1, out, outlen);
111 int fd = ascon_dev_random_open();
112 int ok = ascon_dev_random_read(fd, out, outlen);
int ascon_trng_generate(unsigned char *out, size_t outlen)
Generates a buffer of bytes from the system TRNG source.
Access to the system's random number source.