26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_ENTROPY_C)
37 #if defined(POLARSSL_FS_IO)
41 #if defined(POLARSSL_HAVEGE_C)
45 #define ENTROPY_MAX_LOOP 256
47 void entropy_init( entropy_context *ctx )
51 #if defined(POLARSSL_THREADING_C)
55 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
60 #if defined(POLARSSL_HAVEGE_C)
64 #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
65 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
69 #if defined(POLARSSL_TIMING_C)
72 #if defined(POLARSSL_HAVEGE_C)
82 #if defined(POLARSSL_THREADING_C)
93 #if defined(POLARSSL_THREADING_C)
112 #if defined(POLARSSL_THREADING_C)
123 static int entropy_update(
entropy_context *ctx,
unsigned char source_id,
124 const unsigned char *data,
size_t len )
126 unsigned char header[2];
128 size_t use_len = len;
129 const unsigned char *p = data;
133 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
134 sha512( data, len, tmp, 0 );
136 sha256( data, len, tmp, 0 );
142 header[0] = source_id;
143 header[1] = use_len & 0xFF;
145 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
157 const unsigned char *data,
size_t len )
161 #if defined(POLARSSL_THREADING_C)
168 #if defined(POLARSSL_THREADING_C)
205 entropy_update( ctx, (
unsigned char) i, buf, olen );
220 #if defined(POLARSSL_THREADING_C)
225 ret = entropy_gather_internal( ctx );
227 #if defined(POLARSSL_THREADING_C)
235 int entropy_func(
void *data,
unsigned char *output,
size_t len )
237 int ret, count = 0, i, reached;
244 #if defined(POLARSSL_THREADING_C)
254 if( count++ > ENTROPY_MAX_LOOP )
260 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
273 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
306 memcpy( output, buf, len );
311 #if defined(POLARSSL_THREADING_C)
319 #if defined(POLARSSL_FS_IO)
326 if( ( f = fopen( path,
"wb" ) ) == NULL )
351 if( ( f = fopen( path,
"rb" ) ) == NULL )
354 fseek( f, 0, SEEK_END );
355 n = (size_t) ftell( f );
356 fseek( f, 0, SEEK_SET );
361 if( fread( buf, 1, n, f ) != n )
int(* polarssl_mutex_lock)(threading_mutex_t *mutex)
int entropy_add_source(entropy_context *ctx, f_source_ptr f_source, void *p_source, size_t threshold)
Adds an entropy source to poll (Thread-safe if POLARSSL_THREADING_C is enabled)
void sha256_update(sha256_context *ctx, const unsigned char *input, size_t ilen)
SHA-256 process buffer.
void sha256(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = SHA-256( input buffer )
#define ENTROPY_MIN_PLATFORM
Minimum for platform source.
int entropy_update_manual(entropy_context *ctx, const unsigned char *data, size_t len)
Add data to the accumulator manually (Thread-safe if POLARSSL_THREADING_C is enabled) ...
#define POLARSSL_ERR_ENTROPY_MAX_SOURCES
No more sources can be added.
sha512_context accumulator
Configuration options (set of defines)
int entropy_gather(entropy_context *ctx)
Trigger an extra gather poll for the accumulator (Thread-safe if POLARSSL_THREADING_C is enabled) ...
#define ENTROPY_MIN_HARDCLOCK
Minimum for hardclock()
Entropy context structure.
#define ENTROPY_MAX_GATHER
Maximum amount requested from entropy sources.
void * p_source
The callback data pointer.
Platform-specific and custom entropy polling functions.
Entropy accumulator implementation.
#define ENTROPY_SOURCE_MANUAL
int(* f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback pointer.
source_state source[ENTROPY_MAX_SOURCES]
#define ENTROPY_BLOCK_SIZE
Block size of entropy accumulator (SHA-512)
int entropy_write_seed_file(entropy_context *ctx, const char *path)
Write a seed file.
#define ENTROPY_MIN_HAVEGE
Minimum for HAVEGE.
size_t size
Amount received.
f_source_ptr f_source
The entropy source callback.
void sha256_starts(sha256_context *ctx, int is224)
SHA-256 context setup.
SHA-512 context structure.
#define ENTROPY_MAX_SEED_SIZE
Maximum size of seed we read from seed file.
void sha512_starts(sha512_context *ctx, int is384)
SHA-512 context setup.
void sha512(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = SHA-512( input buffer )
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
int platform_entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen)
Platform-specific entropy poll callback.
int(* polarssl_mutex_free)(threading_mutex_t *mutex)
#define ENTROPY_MAX_SOURCES
Maximum number of sources supported.
void sha512_finish(sha512_context *ctx, unsigned char output[64])
SHA-512 final digest.
void havege_init(havege_state *hs)
HAVEGE initialization.
size_t threshold
Minimum level required before release.
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED
No sources have been added to poll.
int(* polarssl_mutex_init)(threading_mutex_t *mutex)
#define POLARSSL_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
int entropy_update_seed_file(entropy_context *ctx, const char *path)
Read and update a seed file.
void sha256_finish(sha256_context *ctx, unsigned char output[32])
SHA-256 final digest.
#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
Read/write error in file.
SHA-256 context structure.
#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED
Critical entropy source failure.
int hardclock_poll(void *data, unsigned char *output, size_t len, size_t *olen)
hardclock-based entropy poll callback
void sha512_update(sha512_context *ctx, const unsigned char *input, size_t ilen)
SHA-512 process buffer.
int entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
void entropy_free(entropy_context *ctx)
Free the data in the context.