entropy.h
mbedtls/config.h
stddef.h
mbedtls/sha512.h
mbedtls/threading.h
mbedtls/havege.h
mbedtls/check_config.h
mbedtls/config.h
entropy.h
mbedtls/havege.h
mbedtls/sha512.h
mbedtls/threading.h
limits.h
pthread.h
stddef.h
stdint.h
stdlib.h
mbedtls_entropy_source_state
mbedtls_entropy_context
SECTION: Module settings

The configuration options you can set for this module are in this section. Either change them in config.h or define them on the compiler command line.

MBEDTLS_ENTROPY_MAX_SOURCES
20

Maximum number of sources supported

MBEDTLS_ENTROPY_MAX_GATHER
128

Maximum amount requested from entropy sources

MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
-0x003C

Critical entropy source failure.

MBEDTLS_ERR_ENTROPY_MAX_SOURCES
-0x003E

No more sources can be added.

MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED
-0x0040

No sources have been added to poll.

MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE
-0x003D

No strong sources have been added to poll.

MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
-0x003F

Read/write error in file.

MBEDTLS_ENTROPY_BLOCK_SIZE
64

Block size of entropy accumulator (SHA-512)

MBEDTLS_ENTROPY_MAX_SEED_SIZE
1024

Maximum size of seed we read from seed file

MBEDTLS_ENTROPY_SOURCE_MANUAL
MBEDTLS_ENTROPY_MAX_SOURCES
MBEDTLS_ENTROPY_SOURCE_STRONG
1

Entropy source is strong

MBEDTLS_ENTROPY_SOURCE_WEAK
0

Entropy source is weak

int(* typedef int(* mbedtls_entropy_f_source_ptr) (void *data, unsigned char *output, size_t len, size_t *olen) )(void *data, unsigned char *output, size_t len, size_t *olen) mbedtls_entropy_f_source_ptr

Entropy poll callback pointer.

data

Callback-specific data pointer

output

Data to fill

len

Maximum size to provide

olen

The actual amount of bytes put into the buffer (Can be 0)

0 if no critical failures occurred, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise

struct mbedtls_entropy_source_state typedef struct mbedtls_entropy_source_state mbedtls_entropy_source_state mbedtls_entropy_source_state

Entropy source state.

struct mbedtls_entropy_context typedef struct mbedtls_entropy_context mbedtls_entropy_context mbedtls_entropy_context

Entropy context structure.

void void mbedtls_entropy_init (mbedtls_entropy_context *ctx) mbedtls_entropy_init mbedtls_entropy_context * ctx

Initialize the context.

ctx

Entropy context to initialize

void void mbedtls_entropy_free (mbedtls_entropy_context *ctx) mbedtls_entropy_free mbedtls_entropy_context * ctx

Free the data in the context.

ctx

Entropy context to free

int int mbedtls_entropy_add_source (mbedtls_entropy_context *ctx, mbedtls_entropy_f_source_ptr f_source, void *p_source, size_t threshold, int strong) mbedtls_entropy_add_source mbedtls_entropy_context * ctx mbedtls_entropy_f_source_ptr f_source void * p_source size_t threshold int strong

Adds an entropy source to poll (Thread-safe if MBEDTLS_THREADING_C is enabled)

ctx

Entropy context

f_source

Entropy function

p_source

Function data

threshold

Minimum required from source before entropy is released ( with mbedtls_entropy_func() ) (in bytes)

strong

MBEDTLS_ENTROPY_SOURCE_STRONG or MBEDTLS_ENTROPY_SOURCE_WEAK. At least one strong source needs to be added. Weaker sources (such as the cycle counter) can be used as a complement.

0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES

int int mbedtls_entropy_gather (mbedtls_entropy_context *ctx) mbedtls_entropy_gather mbedtls_entropy_context * ctx

Trigger an extra gather poll for the accumulator (Thread-safe if MBEDTLS_THREADING_C is enabled)

ctx

Entropy context

0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED

int int mbedtls_entropy_func (void *data, unsigned char *output, size_t len) mbedtls_entropy_func void * data unsigned char * output size_t len

Retrieve entropy from the accumulator (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) (Thread-safe if MBEDTLS_THREADING_C is enabled)

data

Entropy context

output

Buffer to fill

len

Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE

0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED

int int mbedtls_entropy_update_manual (mbedtls_entropy_context *ctx, const unsigned char *data, size_t len) mbedtls_entropy_update_manual mbedtls_entropy_context * ctx const unsigned char * data size_t len

Add data to the accumulator manually (Thread-safe if MBEDTLS_THREADING_C is enabled)

ctx

Entropy context

data

Data to add

len

Length of data

0 if successful

int int mbedtls_entropy_write_seed_file (mbedtls_entropy_context *ctx, const char *path) mbedtls_entropy_write_seed_file mbedtls_entropy_context * ctx const char * path

Write a seed file.

ctx

Entropy context

path

Name of the file

0 if successful, MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED

int int mbedtls_entropy_update_seed_file (mbedtls_entropy_context *ctx, const char *path) mbedtls_entropy_update_seed_file mbedtls_entropy_context * ctx const char * path

Read and update a seed file. Seed is added to this instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are read from the seed file. The rest is ignored.

ctx

Entropy context

path

Name of the file

0 if successful, MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED

int int mbedtls_entropy_self_test (int verbose) mbedtls_entropy_self_test int verbose

Checkup routine.

This module self-test also calls the entropy self-test, mbedtls_entropy_source_self_test();

0 if successful, or 1 if a test failed

Entropy accumulator implementation.

    /*
    *CopyrightTheMbedTLSContributors
    *SPDX-License-Identifier:Apache-2.0ORGPL-2.0-or-later
    */
    #ifndefMBEDTLS_ENTROPY_H
    #defineMBEDTLS_ENTROPY_H

    #if!defined(MBEDTLS_CONFIG_FILE)
    #include"mbedtls/config.h"
    #else
    #includeMBEDTLS_CONFIG_FILE
    #endif

    #include<stddef.h>

    #ifdefined(MBEDTLS_SHA512_C)&&!defined(MBEDTLS_ENTROPY_FORCE_SHA256)
    #include"mbedtls/sha512.h"
    #defineMBEDTLS_ENTROPY_SHA512_ACCUMULATOR
    #else
    #ifdefined(MBEDTLS_SHA256_C)
    #defineMBEDTLS_ENTROPY_SHA256_ACCUMULATOR
    #include"mbedtls/sha256.h"
    #endif
    #endif

    #ifdefined(MBEDTLS_THREADING_C)
    #include"mbedtls/threading.h"
    #endif

    #ifdefined(MBEDTLS_HAVEGE_C)
    #include"mbedtls/havege.h"
    #endif

    #defineMBEDTLS_ERR_ENTROPY_SOURCE_FAILED-0x003C
    #defineMBEDTLS_ERR_ENTROPY_MAX_SOURCES-0x003E
    #defineMBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED-0x0040
    #defineMBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE-0x003D
    #defineMBEDTLS_ERR_ENTROPY_FILE_IO_ERROR-0x003F

    #if!defined(MBEDTLS_ENTROPY_MAX_SOURCES)
    #defineMBEDTLS_ENTROPY_MAX_SOURCES20
    #endif

    #if!defined(MBEDTLS_ENTROPY_MAX_GATHER)
    #defineMBEDTLS_ENTROPY_MAX_GATHER128
    #endif

    #ifdefined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
    #defineMBEDTLS_ENTROPY_BLOCK_SIZE64
    #else
    #defineMBEDTLS_ENTROPY_BLOCK_SIZE32
    #endif

    #defineMBEDTLS_ENTROPY_MAX_SEED_SIZE1024
    #defineMBEDTLS_ENTROPY_SOURCE_MANUALMBEDTLS_ENTROPY_MAX_SOURCES

    #defineMBEDTLS_ENTROPY_SOURCE_STRONG1
    #defineMBEDTLS_ENTROPY_SOURCE_WEAK0
    #ifdef__cplusplus
    extern"C"{
    #endif

    typedefint(*mbedtls_entropy_f_source_ptr)(void*data,unsignedchar*output,size_tlen,
    size_t*olen);

    typedefstructmbedtls_entropy_source_state{
    mbedtls_entropy_f_source_ptrf_source;
    void*p_source;
    size_tsize;
    size_tthreshold;
    intstrong;
    }
    mbedtls_entropy_source_state;

    typedefstructmbedtls_entropy_context{
    intaccumulator_started;/*0afterinit.
    *1afterthefirstupdate.
    *-1afterfree.*/
    #ifdefined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
    mbedtls_sha512_contextaccumulator;
    #elifdefined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
    mbedtls_sha256_contextaccumulator;
    #endif
    intsource_count;/*Numberofentriesusedinsource.*/
    mbedtls_entropy_source_statesource[MBEDTLS_ENTROPY_MAX_SOURCES];
    #ifdefined(MBEDTLS_HAVEGE_C)
    mbedtls_havege_statehavege_data;
    #endif
    #ifdefined(MBEDTLS_THREADING_C)
    mbedtls_threading_mutex_tmutex;
    #endif
    #ifdefined(MBEDTLS_ENTROPY_NV_SEED)
    intinitial_entropy_run;
    #endif
    }
    mbedtls_entropy_context;

    voidmbedtls_entropy_init(mbedtls_entropy_context*ctx);

    voidmbedtls_entropy_free(mbedtls_entropy_context*ctx);

    intmbedtls_entropy_add_source(mbedtls_entropy_context*ctx,
    mbedtls_entropy_f_source_ptrf_source,void*p_source,
    size_tthreshold,intstrong);

    intmbedtls_entropy_gather(mbedtls_entropy_context*ctx);

    intmbedtls_entropy_func(void*data,unsignedchar*output,size_tlen);

    intmbedtls_entropy_update_manual(mbedtls_entropy_context*ctx,
    constunsignedchar*data,size_tlen);

    #ifdefined(MBEDTLS_ENTROPY_NV_SEED)
    intmbedtls_entropy_update_nv_seed(mbedtls_entropy_context*ctx);
    #endif/*MBEDTLS_ENTROPY_NV_SEED*/

    #ifdefined(MBEDTLS_FS_IO)
    intmbedtls_entropy_write_seed_file(mbedtls_entropy_context*ctx,constchar*path);

    intmbedtls_entropy_update_seed_file(mbedtls_entropy_context*ctx,constchar*path);
    #endif/*MBEDTLS_FS_IO*/

    #ifdefined(MBEDTLS_SELF_TEST)
    intmbedtls_entropy_self_test(intverbose);

    #ifdefined(MBEDTLS_ENTROPY_HARDWARE_ALT)
    intmbedtls_entropy_source_self_test(intverbose);
    #endif/*MBEDTLS_ENTROPY_HARDWARE_ALT*/
    #endif/*MBEDTLS_SELF_TEST*/

    #ifdef__cplusplus
    }
    #endif

    #endif/*entropy.h*/