PatchworkOS
19e446b
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
*/
22
typedef
struct
23
{
24
bool
rdrandAvail
;
///< If set, the `RDRAND` instruction is available and working.
25
}
rand_cpu_t
;
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
*/
37
uint64_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
*/
46
extern
uint64_t
rdrand_do
(
uint32_t
* value,
uint8_t
retries);
47
48
/** @} */
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
main.c:237
rdrand_do
uint64_t rdrand_do(uint32_t *value, uint8_t retries)
Generates a random 32-bit unsigned integer using the RDRAND instruction.
rand_gen
uint64_t rand_gen(void *buffer, uint64_t size)
Fills a buffer with random bytes.
Definition
rand.c:64
stdbool.h
stdint.h
uint32_t
__UINT32_TYPE__ uint32_t
Definition
stdint.h:15
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint8_t
__UINT8_TYPE__ uint8_t
Definition
stdint.h:11
rand_cpu_t
CPU random number generator context.
Definition
rand.h:23
rand_cpu_t::rdrandAvail
bool rdrandAvail
If set, the RDRAND instruction is available and working.
Definition
rand.h:24
include
kernel
drivers
rand.h
Generated on Sat Jan 24 2026 10:59:24 for PatchworkOS by
1.9.8