1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 
8 typedef enum RandomFlags {
9         RANDOM_BLOCK              = 1 << 0, /* Rather block than return crap randomness (only if the kernel supports that) */
10 } RandomFlags;
11 
12 int genuine_random_bytes(void *p, size_t n, RandomFlags flags); /* returns "genuine" randomness, optionally filled up with pseudo random, if not enough is available */
13 void pseudo_random_bytes(void *p, size_t n);                    /* returns only pseudo-randommess (but possibly seeded from something better) */
14 void random_bytes(void *p, size_t n);                           /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
15 
16 void initialize_srand(void);
17 
random_u64(void)18 static inline uint64_t random_u64(void) {
19         uint64_t u;
20         random_bytes(&u, sizeof(u));
21         return u;
22 }
23 
random_u32(void)24 static inline uint32_t random_u32(void) {
25         uint32_t u;
26         random_bytes(&u, sizeof(u));
27         return u;
28 }
29 
30 /* Some limits on the pool sizes when we deal with the kernel random pool */
31 #define RANDOM_POOL_SIZE_MIN 32U
32 #define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U)
33 
34 size_t random_pool_size(void);
35 
36 int random_write_entropy(int fd, const void *seed, size_t size, bool credit);
37 
38 uint64_t random_u64_range(uint64_t max);
39