PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
rand.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdbool.h>
4#include <stdint.h>
5
6/**
7 * @brief Random Number Generator
8 * @defgroup kernel_drivers_rand Random Number Generator
9 * @ingroup kernel_drivers
10 *
11 * The random number generator driver provides functions to generate random numbers for use in the kernel.
12 *
13 * @see [RDRAND Instruction](https://www.felixcloutier.com/x86/rdrand)
14 *
15 * @{
16 */
17
18/**
19 * @brief CPU random number generator context.
20 * @struct rand_cpu_ctx_t
21 */
22typedef struct
23{
24 bool rdrandAvail; ///< If set, the `RDRAND` instruction is available and working.
26
27/**
28 * @brief Initializes the random number generator.
29 */
31
32/**
33 * @brief Fills a buffer with random bytes.
34 *
35 * If the RDRAND instruction is available and working, it will be used. Otherwise, a fallback time based RNG will be
36 * used.
37 *
38 * @param buffer A pointer to the buffer to fill.
39 * @param size The number of bytes to fill.
40 * @return On success, `0`. On failure, `ERR` and `errno` is set.
41 */
42uint64_t rand_gen(void* buffer, uint64_t size);
43
44/**
45 * @brief Generates a random 32-bit unsigned integer using the RDRAND instruction.
46 *
47 * @param value A pointer to store the generated random value.
48 * @param retries The number of retries to attempt if RDRAND fails.
49 * @return On success, `0`. On failure, `ERR` and `errno` is set.
50 */
51extern uint64_t rdrand_do(uint32_t* value, uint8_t retries);
52
53/** @} */
uint64_t rdrand_do(uint32_t *value, uint8_t retries)
Generates a random 32-bit unsigned integer using the RDRAND instruction.
void rand_cpu_init(rand_cpu_ctx_t *ctx)
Initializes the random number generator.
Definition rand.c:28
uint64_t rand_gen(void *buffer, uint64_t size)
Fills a buffer with random bytes.
Definition rand.c:61
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
CPU random number generator context.
Definition rand.h:23
bool rdrandAvail
If set, the RDRAND instruction is available and working.
Definition rand.h:24