]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/random-util.h
Merge pull request #16336 from yuwata/ifindex-cleanups
[thirdparty/systemd.git] / src / basic / random-util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3df3e884
RC
2#pragma once
3
f0d09059 4#include <stdbool.h>
11c3a366 5#include <stddef.h>
3df3e884
RC
6#include <stdint.h>
7
94d457e8
LP
8typedef enum RandomFlags {
9 RANDOM_EXTEND_WITH_PSEUDO = 1 << 0, /* If we can't get enough genuine randomness, but some, fill up the rest with pseudo-randomness */
68534345 10 RANDOM_BLOCK = 1 << 1, /* Rather block than return crap randomness (only if the kernel supports that) */
1a0ffa1e 11 RANDOM_MAY_FAIL = 1 << 2, /* If we can't get any randomness at all, return early with -ENODATA */
cc83d519 12 RANDOM_ALLOW_RDRAND = 1 << 3, /* Allow usage of the CPU RNG */
0497c4c2 13 RANDOM_ALLOW_INSECURE = 1 << 4, /* Allow usage of GRND_INSECURE flag to kernel's getrandom() API */
94d457e8
LP
14} RandomFlags;
15
1a0ffa1e 16int genuine_random_bytes(void *p, size_t n, RandomFlags flags); /* returns "genuine" randomness, optionally filled up with pseudo random, if not enough is available */
94d457e8
LP
17void pseudo_random_bytes(void *p, size_t n); /* returns only pseudo-randommess (but possibly seeded from something better) */
18void random_bytes(void *p, size_t n); /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
3335dc2d 19
3df3e884
RC
20void initialize_srand(void);
21
22static inline uint64_t random_u64(void) {
23 uint64_t u;
24 random_bytes(&u, sizeof(u));
25 return u;
26}
27
28static inline uint32_t random_u32(void) {
29 uint32_t u;
30 random_bytes(&u, sizeof(u));
31 return u;
32}
97fa202a 33
33dbab6f 34int rdrand(unsigned long *ret);
3e155eba
LP
35
36/* Some limits on the pool sizes when we deal with the kernel random pool */
37#define RANDOM_POOL_SIZE_MIN 512U
38#define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U)
39
40size_t random_pool_size(void);
4dd055f9
LP
41
42int random_write_entropy(int fd, const void *seed, size_t size, bool credit);