PatchworkOS  19e446b
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_t
21 */
22typedef struct
23{
24 bool rdrandAvail; ///< If set, the `RDRAND` instruction is available and working.
26
27/**
28 * @brief Fills a buffer with random bytes.
29 *
30 * If the RDRAND instruction is available and working, it will be used. Otherwise, a fallback time based RNG will be
31 * used.
32 *
33 * @param buffer A pointer to the buffer to fill.
34 * @param size The number of bytes to fill.
35 * @return On success, `0`. On failure, `ERR` and `errno` is set.
36 */
37uint64_t rand_gen(void* buffer, uint64_t size);
38
39/**
40 * @brief Generates a random 32-bit unsigned integer using the RDRAND instruction.
41 *
42 * @param value A pointer to store the generated random value.
43 * @param retries The number of retries to attempt if RDRAND fails.
44 * @return On success, `0`. On failure, `ERR` and `errno` is set.
45 */
46extern uint64_t rdrand_do(uint32_t* value, uint8_t retries);
47
48/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
uint64_t rdrand_do(uint32_t *value, uint8_t retries)
Generates a random 32-bit unsigned integer using the RDRAND instruction.
uint64_t rand_gen(void *buffer, uint64_t size)
Fills a buffer with random bytes.
Definition rand.c:64
__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