about summary refs log tree commit diff
path: root/shared/systemd/src/basic/random-util.h
diff options
context:
space:
mode:
authorSebastien Bacher <seb128@ubuntu.com>2019-01-25 11:26:25 +0100
committerSebastien Bacher <seb128@ubuntu.com>2019-01-25 11:26:25 +0100
commitf54a568880bf47c2ea54c9b30d291156b34d6539 (patch)
treeaab9bb44b306da7651e8caf38bbab5cfeeb81b7d /shared/systemd/src/basic/random-util.h
parente8ac7dacdcf4b38addef4f8ac6ab038f9d29582c (diff)
parentbbae86d3d2997a853ca0365e8eb7a3ca7489ee09 (diff)
Update upstream source from tag 'upstream/1.15.2'
Update to upstream version '1.15.2'
with Debian dir b4be4b7cfce96679a31cb9d423107789761e3b49
Diffstat (limited to 'shared/systemd/src/basic/random-util.h')
-rw-r--r--shared/systemd/src/basic/random-util.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/shared/systemd/src/basic/random-util.h b/shared/systemd/src/basic/random-util.h
new file mode 100644
index 00000000..3e8c288d
--- /dev/null
+++ b/shared/systemd/src/basic/random-util.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+typedef enum RandomFlags {
+        RANDOM_EXTEND_WITH_PSEUDO = 1 << 0, /* If we can't get enough genuine randomness, but some, fill up the rest with pseudo-randomness */
+        RANDOM_BLOCK              = 1 << 1, /* Rather block than return crap randomness (only if the kernel supports that) */
+        RANDOM_DONT_DRAIN         = 1 << 2, /* If we can't get any randomness at all, return early with -EAGAIN */
+        RANDOM_ALLOW_RDRAND       = 1 << 3, /* Allow usage of the CPU RNG */
+} RandomFlags;
+
+int genuine_random_bytes(void *p, size_t n, RandomFlags flags); /* returns "genuine" randomness, optionally filled upwith pseudo random, if not enough is available */
+void pseudo_random_bytes(void *p, size_t n);                    /* returns only pseudo-randommess (but possibly seeded from something better) */
+void random_bytes(void *p, size_t n);                           /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
+
+void initialize_srand(void);
+
+static inline uint64_t random_u64(void) {
+        uint64_t u;
+        random_bytes(&u, sizeof(u));
+        return u;
+}
+
+static inline uint32_t random_u32(void) {
+        uint32_t u;
+        random_bytes(&u, sizeof(u));
+        return u;
+}
+
+int rdrand(unsigned long *ret);