summaryrefslogtreecommitdiff
path: root/src/librandom/random_buf_early.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2022-04-09 02:28:05 +0000
committerLaurent Bercot <ska@appnovation.com>2022-04-09 02:28:05 +0000
commit4e75b40d8a96c5d51490ddea43566fe8e6d4168c (patch)
tree06358f285c713a7f05297cb7d2826319d6561683 /src/librandom/random_buf_early.c
parent1fedaae65b56b8548cdf2d8c26e2becece5fec07 (diff)
downloadskalibs-4e75b40d8a96c5d51490ddea43566fe8e6d4168c.tar.xz
Prepare for 2.12.0.0. librandom revamp.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/librandom/random_buf_early.c')
-rw-r--r--src/librandom/random_buf_early.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/librandom/random_buf_early.c b/src/librandom/random_buf_early.c
new file mode 100644
index 0000000..a306239
--- /dev/null
+++ b/src/librandom/random_buf_early.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#if defined(SKALIBS_HASARC4RANDOM)
+
+#include <skalibs/nonposix.h>
+#include <stdlib.h>
+#include <skalibs/random.h>
+
+void random_buf_early (char *s, size_t n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#elif defined(SKALIBS_HASGETRANDOM) && defined(SKALIBS_HASGRNDINSECURE)
+
+#include <sys/random.h>
+
+#include <skalibs/random.h>
+
+void random_buf_early (char *s, size_t n)
+{
+ while (n)
+ {
+ ssize_t r = getrandom(s, n, GRND_INSECURE) ;
+ if (r >= 0)
+ {
+ s += r ;
+ n -= r ;
+ }
+ }
+}
+
+#elif defined(SKALIBS_HASDEVURANDOM)
+
+#include "random-internal.h"
+
+void random_buf_early (char *s, size_t n)
+{
+ random_devurandom(s, n) ;
+}
+
+#else
+
+#include <skalibs/surf.h>
+#include <skalibs/random.h>
+
+void random_buf_early (char *s, size_t n)
+{
+ autosurf(s, n) ;
+}
+
+#endif