| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
/* Returns random bytes suitable for most uses, but may be insecure sometimes. */ |
| 7 |
void random_bytes(void *p, size_t n) _nonnull_if_nonzero_(1, 2); |
| 8 |
int random_bytes_allocate_iovec(size_t n, struct iovec *ret); |
| 9 |
|
| 10 |
/* Returns secure random bytes after waiting for the RNG to initialize. */ |
| 11 |
int crypto_random_bytes(void *p, size_t n) _nonnull_if_nonzero_(1, 2); |
| 12 |
int crypto_random_bytes_allocate_iovec(size_t n, struct iovec *ret); |
| 13 |
|
| 14 |
static inline uint64_t random_u64(void) { |
| 15 |
uint64_t u; |
| 16 |
random_bytes(&u, sizeof(u)); |
| 17 |
return u; |
| 18 |
} |
| 19 |
|
| 20 |
static inline uint32_t random_u32(void) { |
| 21 |
uint32_t u; |
| 22 |
random_bytes(&u, sizeof(u)); |
| 23 |
return u; |
| 24 |
} |
| 25 |
|
| 26 |
/* Some limits on the pool sizes when we deal with the kernel random pool */ |
| 27 |
#define RANDOM_POOL_SIZE_MIN 32U |
| 28 |
#define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U) |
| 29 |
#define RANDOM_EFI_SEED_SIZE 32U |
| 30 |
|
| 31 |
size_t random_pool_size(void); |
| 32 |
|
| 33 |
int random_write_entropy(int fd, const void *seed, size_t size, bool credit) _nonnull_if_nonzero_(2, 3); |
| 34 |
|
| 35 |
uint64_t random_u64_range(uint64_t max); |