trng 1.0
trng driver library
Loading...
Searching...
No Matches
trng.h File Reference

Header file with declarations and macros for the True Random Number Generator (TRNG) module. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TRNG_BUFFER_SIZE   10UL
 Size of the internal random data buffer in bytes.

Typedefs

typedef enum TRNG_Buffer_t TRNG_Buffer
 Alias for enum TRNG_Buffer_t representing the TRNG buffer status type.

Enumerations

enum  TRNG_Buffer_t { TRNG_Buffer_Empty = 0 , TRNG_Buffer_Filling , TRNG_Buffer_Full }
 Represents the current fill status of the TRNG data buffer. More...

Functions

void trng_init (void)
 Initializes the True Random Number Generator (TRNG) module.
void trng_next_bit (unsigned char bit)
 Processes a single entropy bit and adds it to the TRNG buffer.
TRNG_Buffer trng_buffer_status (void)
 Returns the current fill status of the TRNG buffer.
volatile unsigned char * trng_buffer (void)
 Returns a pointer to the internal TRNG buffer.
void trng_reset (void)
 Returns a pointer to the internal TRNG buffer.

Detailed Description

Header file with declarations and macros for the True Random Number Generator (TRNG) module.

This file provides function prototypes, type definitions, and constants for managing a True Random Number Generator that collects entropy bits, applies Von Neumann correction, and stores the resulting random data in a configurable buffer.

The TRNG library is designed for use in embedded systems where nondeterministic randomness is required, such as for cryptographic operations, random initialization data, or simulation purposes.

Author
g.raf
Date
2026-01-27
Version
1.0 Release
Note
This file is part of a larger embedded project and is subject to the license specified in the repository. For updates, documentation, and the complete revision history, see the project's GitHub repository.
See also
https://github.com/0x007e/drivers-crypto-trng "TRNG driver library"

Macro Definition Documentation

◆ TRNG_BUFFER_SIZE

#define TRNG_BUFFER_SIZE   10UL

Size of the internal random data buffer in bytes.

This macro defines the number of bytes the TRNG (True Random Number Generator) uses internally to store generated random bits temporarily. The buffer is filled until it reaches the specified size.

Note
The default value is 10 bytes, but it can be redefined before including the header file to adjust the buffer size.
Warning
A larger buffer size increases memory usage but allows for more random data to be collected before processing.
See also
trng_buffer(), trng_buffer_status()

Typedef Documentation

◆ TRNG_Buffer

typedef enum TRNG_Buffer_t TRNG_Buffer

Alias for enum TRNG_Buffer_t representing the TRNG buffer status type.

Enumeration Type Documentation

◆ TRNG_Buffer_t

Represents the current fill status of the TRNG data buffer.

This enumeration indicates the current state of the internal True Random Number Generator (TRNG) buffer used to store random bits. It helps determine whether the buffer is empty, currently being filled with new data, or already full.

Enumerator
TRNG_Buffer_Empty 

The buffer is empty; no random data has been written yet.

TRNG_Buffer_Filling 

The buffer is being filled with random bits.

TRNG_Buffer_Full 

The buffer is full and ready for retrieval or reset.

Function Documentation

◆ trng_buffer()

volatile unsigned char * trng_buffer ( void )

Returns a pointer to the internal TRNG buffer.

This function provides direct access to the internal memory buffer that stores the corrected random bytes generated by the True Random Number Generator (TRNG). The buffer can be read once the buffer status, retrieved by trng_buffer_status(), indicates that it is full.

Returns
volatile unsigned char*: Pointer to the internal TRNG buffer containing random data.
Note
  • The returned pointer references volatile memory, as the buffer content may be modified by ongoing TRNG operations.
  • Ensure that the buffer is not read while still filling to avoid inconsistent data.
See also
trng_buffer_status(), trng_reset()

◆ trng_buffer_status()

TRNG_Buffer trng_buffer_status ( void )

Returns the current fill status of the TRNG buffer.

This function checks the internal TRNG buffer and reports whether it is empty, partially filled, or completely full. It uses internal position counters to determine the current state of the random data storage.

The return value can be one of the following:

Returns
TRNG_Buffer: The current buffer status as a value from the TRNG_Buffer enumeration.
Note
This function allows external code to determine when new random data can be read or when the buffer should be reset.
See also
trng_buffer(), trng_reset(), trng_next_bit()

◆ trng_init()

void trng_init ( void )

Initializes the True Random Number Generator (TRNG) module.

This function prepares the TRNG module for operation by resetting its internal state and buffer. It should be called once during system initialization before feeding entropy bits using trng_next_bit(). Internally, this function calls trng_reset() to clear all buffer data and reset counters.

Note
Must be called before any random data collection begins.
See also
trng_reset(), trng_next_bit(), trng_buffer_status()
Here is the call graph for this function:

◆ trng_next_bit()

void trng_next_bit ( unsigned char bit)

Processes a single entropy bit and adds it to the TRNG buffer.

This function is called repeatedly with raw entropy bits obtained from a hardware or software noise source. It applies the Von Neumann correction algorithm to remove bias from the input bit sequence and produces unbiased random bits. The corrected bits are packed into the internal TRNG buffer until it reaches the configured size defined by TRNG_BUFFER_SIZE. Once the buffer is full, no further bits are stored until it is reset.

Parameters
bitThe next raw entropy bit to process (must be 0 or 1).
Note
  • The function performs no operation if the buffer is already full.
  • Each call expects a single bit input, not a byte or word.
  • This function internally manages bit-packing and buffer indexing.
See also
trng_init(), trng_reset(), trng_buffer(), trng_buffer_status()

◆ trng_reset()

void trng_reset ( void )

Returns a pointer to the internal TRNG buffer.

This function provides direct access to the internal memory buffer that stores the corrected random bytes generated by the True Random Number Generator (TRNG). The buffer can be read once the buffer status, retrieved by trng_buffer_status(), indicates that it is full.

Returns
volatile unsigned char*: Pointer to the internal TRNG buffer containing random data.
Note
  • The returned pointer references volatile memory, as the buffer content may be modified by ongoing TRNG operations.
  • Ensure that the buffer is not read while still filling to avoid inconsistent data.
See also
trng_buffer_status(), trng_reset()
Here is the caller graph for this function: