aboutsummaryrefslogtreecommitdiff
path: root/external/unbound/compat/getentropy_linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'external/unbound/compat/getentropy_linux.c')
-rw-r--r--external/unbound/compat/getentropy_linux.c49
1 files changed, 42 insertions, 7 deletions
diff --git a/external/unbound/compat/getentropy_linux.c b/external/unbound/compat/getentropy_linux.c
index 32d58a7cd..76f0f9df5 100644
--- a/external/unbound/compat/getentropy_linux.c
+++ b/external/unbound/compat/getentropy_linux.c
@@ -77,6 +77,9 @@ int getentropy(void *buf, size_t len);
extern int main(int, char *argv[]);
#endif
static int gotdata(char *buf, size_t len);
+#ifdef SYS_getrandom
+static int getentropy_getrandom(void *buf, size_t len);
+#endif
static int getentropy_urandom(void *buf, size_t len);
#ifdef SYS__sysctl
static int getentropy_sysctl(void *buf, size_t len);
@@ -93,6 +96,17 @@ getentropy(void *buf, size_t len)
return -1;
}
+#ifdef SYS_getrandom
+ /*
+ * Try descriptor-less getrandom()
+ */
+ ret = getentropy_getrandom(buf, len);
+ if (ret != -1)
+ return (ret);
+ if (errno != ENOSYS)
+ return (-1);
+#endif
+
/*
* Try to get entropy with /dev/urandom
*
@@ -178,6 +192,25 @@ gotdata(char *buf, size_t len)
return 0;
}
+#ifdef SYS_getrandom
+static int
+getentropy_getrandom(void *buf, size_t len)
+{
+ int pre_errno = errno;
+ int ret;
+ if (len > 256)
+ return (-1);
+ do {
+ ret = syscall(SYS_getrandom, buf, len, 0);
+ } while (ret == -1 && errno == EINTR);
+
+ if (ret != (int)len)
+ return (-1);
+ errno = pre_errno;
+ return (0);
+}
+#endif
+
static int
getentropy_urandom(void *buf, size_t len)
{
@@ -251,7 +284,7 @@ getentropy_sysctl(void *buf, size_t len)
struct __sysctl_args args = {
.name = mib,
.nlen = 3,
- .oldval = buf + i,
+ .oldval = (char *)buf + i,
.oldlenp = &chunk,
};
if (syscall(SYS__sysctl, &args) != 0)
@@ -474,22 +507,24 @@ getentropy_fallback(void *buf, size_t len)
HD(cnt);
}
-#ifdef AT_RANDOM
+#ifdef HAVE_GETAUXVAL
+# ifdef AT_RANDOM
/* Not as random as you think but we take what we are given */
p = (char *) getauxval(AT_RANDOM);
if (p)
HR(p, 16);
-#endif
-#ifdef AT_SYSINFO_EHDR
+# endif
+# ifdef AT_SYSINFO_EHDR
p = (char *) getauxval(AT_SYSINFO_EHDR);
if (p)
HR(p, pgs);
-#endif
-#ifdef AT_BASE
+# endif
+# ifdef AT_BASE
p = (char *) getauxval(AT_BASE);
if (p)
HD(p);
-#endif
+# endif
+#endif /* HAVE_GETAUXVAL */
SHA512_Final(results, &ctx);
memcpy((char*)buf + i, results, min(sizeof(results), len - i));