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)
46 static void polarssl_zeroize(
void *v,
size_t n ) {
47 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
50 #define ENTROPY_MAX_LOOP 256
52 void entropy_init( entropy_context *ctx )
56 #if defined(POLARSSL_THREADING_C)
60 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
65 #if defined(POLARSSL_HAVEGE_C)
69 #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
70 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
74 #if defined(POLARSSL_TIMING_C)
77 #if defined(POLARSSL_HAVEGE_C)
86 #if defined(POLARSSL_HAVEGE_C)
90 #if defined(POLARSSL_THREADING_C)
101 #if defined(POLARSSL_THREADING_C)
120 #if defined(POLARSSL_THREADING_C)
131 static int entropy_update(
entropy_context *ctx,
unsigned char source_id,
132 const unsigned char *data,
size_t len )
134 unsigned char header[2];
136 size_t use_len = len;
137 const unsigned char *p = data;
141 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
142 sha512( data, len, tmp, 0 );
144 sha256( data, len, tmp, 0 );
150 header[0] = source_id;
151 header[1] = use_len & 0xFF;
153 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
165 const unsigned char *data,
size_t len )
169 #if defined(POLARSSL_THREADING_C)
176 #if defined(POLARSSL_THREADING_C)
213 entropy_update( ctx, (
unsigned char) i, buf, olen );
228 #if defined(POLARSSL_THREADING_C)
233 ret = entropy_gather_internal( ctx );
235 #if defined(POLARSSL_THREADING_C)
243 int entropy_func(
void *data,
unsigned char *output,
size_t len )
245 int ret, count = 0, i, reached;
252 #if defined(POLARSSL_THREADING_C)
262 if( count++ > ENTROPY_MAX_LOOP )
268 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
281 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
314 memcpy( output, buf, len );
319 #if defined(POLARSSL_THREADING_C)
327 #if defined(POLARSSL_FS_IO)
334 if( ( f = fopen( path,
"wb" ) ) == NULL )
359 if( ( f = fopen( path,
"rb" ) ) == NULL )
362 fseek( f, 0, SEEK_END );
363 n = (size_t) ftell( f );
364 fseek( f, 0, SEEK_SET );
369 if( fread( buf, 1, n, f ) != n )
383 #if defined(POLARSSL_SELF_TEST)
385 #if defined(POLARSSL_PLATFORM_C)
389 #define polarssl_printf printf
395 static int entropy_dummy_source(
void *data,
unsigned char *output,
396 size_t len,
size_t *olen )
400 memset( output, 0x2a, len );
442 for( i = 0; i < 8; i++ )
444 if( ( ret =
entropy_func( &ctx, buf,
sizeof( buf ) ) ) != 0 )
447 for( j = 0; j <
sizeof( buf ); j++ )
451 for( j = 0; j <
sizeof( buf ); j++ )
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
void havege_free(havege_state *hs)
Clear HAVEGE state.
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)
int entropy_self_test(int verbose)
Checkup routine.
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.
#define ENTROPY_MAX_SOURCES
SHA-256 context structure.
void entropy_init(entropy_context *ctx)
Initialize the context.
#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.