ASCON Suite
Macros | Functions
ascon-prng.c File Reference
#include <ascon/random.h>
#include <ascon/utility.h>
#include "random/ascon-trng.h"
#include "core/ascon-util-snp.h"

Go to the source code of this file.

Macros

#define ASCON_RANDOM_RESEED_LIMIT   16384
 Automatically re-seed after generating more than this many bytes. More...
 

Functions

int ascon_random_init (ascon_random_state_t *state)
 Initializes a pseudorandom number generator from the system random number source. More...
 
void ascon_random_free (ascon_random_state_t *state)
 Frees a pseudorandom number generator and destroys any sensitive values. More...
 
void ascon_random_fetch (ascon_random_state_t *state, unsigned char *out, size_t outlen)
 Fetches data from a pseudorandom number generator. More...
 
int ascon_random_reseed (ascon_random_state_t *state)
 Explicitly re-seeds a pseudorandom number generator from the system random number source. More...
 
void ascon_random_feed (ascon_random_state_t *state, const unsigned char *entropy, size_t size)
 Feeds entropy into a pseudorandom number generator. More...
 

Macro Definition Documentation

◆ ASCON_RANDOM_RESEED_LIMIT

#define ASCON_RANDOM_RESEED_LIMIT   16384

Automatically re-seed after generating more than this many bytes.

Definition at line 31 of file ascon-prng.c.

Function Documentation

◆ ascon_random_feed()

void ascon_random_feed ( ascon_random_state_t state,
const unsigned char *  entropy,
size_t  size 
)

Feeds entropy into a pseudorandom number generator.

Parameters
stateThe pseudorandom number generator to feed the entropy into.
entropyPoints to a buffer containing the entropy.
sizeNumber of bytes of entropy to add, which can be zero to "stir" the random pool but not introduce any new entropy.

This API does not keep track of how much entropy has been collected. Estimating the amount of entropy contained in noise sources is difficult and would make the API very complex.

The application can keep track of "entropy credits" itself if that is important. And then only call ascon_random_fetch() when it judges that the entropy pool is sufficiently populated.

Definition at line 135 of file ascon-prng.c.

◆ ascon_random_fetch()

void ascon_random_fetch ( ascon_random_state_t state,
unsigned char *  out,
size_t  outlen 
)

Fetches data from a pseudorandom number generator.

Parameters
stateThe pseudorandom number generator state to use.
outPoints to a buffer to receive the random data.
outlenNumber of bytes of random data to fetch.
See also
ascon_random_reseed()

Definition at line 90 of file ascon-prng.c.

◆ ascon_random_free()

void ascon_random_free ( ascon_random_state_t state)

Frees a pseudorandom number generator and destroys any sensitive values.

Parameters
stateThe pseudorandom number generator state to free.
See also
ascon_random_init()

Definition at line 82 of file ascon-prng.c.

◆ ascon_random_init()

int ascon_random_init ( ascon_random_state_t state)

Initializes a pseudorandom number generator from the system random number source.

Parameters
stateThe pseudorandom number generator state to initialize.
Returns
Non-zero if the system random number source is working; zero if there is no system random number source or it has failed.

In the case of a zero return, the returned data may be predictable so the application should avoid using the pseudorandom number generator unless it has some other source of entropy it can use.

Note
If this function returns zero, then the state is still usable to generate data at a lower level of quality. The state must be freed explicitly with ascon_random_free() regardless of the return value from this function.
See also
ascon_random_fetch(), ascon_random_feed()

Definition at line 66 of file ascon-prng.c.

◆ ascon_random_reseed()

int ascon_random_reseed ( ascon_random_state_t state)

Explicitly re-seeds a pseudorandom number generator from the system random number source.

Parameters
stateThe pseudorandom number generator to re-seed.
Returns
Non-zero if the system random number source is working; zero if there is no system random number source or it has failed.

The pseudorandom number generator will be re-seeded periodically by ascon_random_fetch() but the application can choose to re-seed more often if it needs fresh random data.

See also
ascon_random_fetch()

Definition at line 116 of file ascon-prng.c.