libhcs
Data Structures | Macros | Functions
hcs_random.h File Reference

Provides secure random state for use in functions which require random values. More...

#include <gmp.h>
Include dependency graph for hcs_random.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  hcs_random
 Random state used by a number of cryptographic functions. More...
 

Macros

#define HCS_RAND_SEED_BITS   256
 If this define is set, then a static seed of 0 will always be used in any hcs_random functions. More...
 

Functions

hcs_randomhcs_init_random (void)
 Initialise an hcs_random and return a pointer to the newly created structure. More...
 
int hcs_reseed_random (hcs_random *hr)
 Reseed the given hcs_random. More...
 
void hcs_free_random (hcs_random *hr)
 Frees a hcs_random and all associated memory. More...
 

Detailed Description

Provides secure random state for use in functions which require random values.

This is a wrapper around gmp_randstate_t, and utilizes them internally as a PRNG. Currently, it uses gmp_randinit_default for choice of PRNG.

Seed is gathered from the operating systems provided entropy. For example, /dev/urandom is used under Linux. This may be slightly altered, but for now it satisfies the required randomness.

Macro Definition Documentation

#define HCS_RAND_SEED_BITS   256

If this define is set, then a static seed of 0 will always be used in any hcs_random functions.

ONLY use this for testing. The number of bits of seed which is gathered from our entropy source.

Function Documentation

hcs_random* hcs_init_random ( void  )

Initialise an hcs_random and return a pointer to the newly created structure.

Returns
A pointer to an hcs_random type on success, NULL on allocation failure
int hcs_reseed_random ( hcs_random hr)

Reseed the given hcs_random.

If we fail to gather sufficient entropy, we return with an error.

Todo:
Potentially expand error codes so we can explain exactly what went wrong to the caller. For now though, it is better than aborting.
Parameters
hrA pointer to an initialised hcs_random
Returns
non-zero on successful reseed, zero on failure
void hcs_free_random ( hcs_random hr)

Frees a hcs_random and all associated memory.

Parameters
hrA pointer to an initliased hcs_random