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

Implementation of the True Random Number Generator (TRNG) functions. More...

#include "trng.h"
Include dependency graph for trng.c:

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.

Variables

volatile unsigned char buffer [10UL]
volatile unsigned char current_buffer_position
volatile unsigned char current_buffer_bit_position
volatile unsigned char first_bit
volatile unsigned char last_bit
volatile unsigned char primary_run

Detailed Description

Implementation of the True Random Number Generator (TRNG) functions.

This file contains the implementation of the TRNG driver functions used to initialize, reset, and manage a True Random Number Generator. It processes entropy bits using a Von Neumann correction algorithm and stores the resulting random data in an internal buffer for later retrieval.

The library is intended for embedded systems requiring nondeterministic random data, such as cryptographic applications, seeding pseudorandom generators, or generating unique identifiers.

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 and the complete revision history, see the project's GitHub repository.
See also
https://github.com/0x007e/drivers-crypto-trng "TRNG driver library"

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:

Variable Documentation

◆ buffer

volatile unsigned char buffer[10UL]

◆ current_buffer_bit_position

volatile unsigned char current_buffer_bit_position

◆ current_buffer_position

volatile unsigned char current_buffer_position

◆ first_bit

volatile unsigned char first_bit

◆ last_bit

volatile unsigned char last_bit

◆ primary_run

volatile unsigned char primary_run